-
Notifications
You must be signed in to change notification settings - Fork 2
/
traders.py
56 lines (48 loc) · 1.98 KB
/
traders.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import abc
import random
class Trader(object):
name = 'generic'
def simulation_params(self, timesteps,
possible_jump_locations,
single_jump_probability):
pass
def new_information(self, info, time):
pass
def trades_history(self, trades, time):
pass
@abc.abstractmethod
def trading_opportunity(self, cash_callback, shares_callback,
check_callback, execute_callback, mu):
pass
class TradingPopulation(object):
def __init__(self, timesteps, possible_jump_locations,
single_jump_probability, traders,
user_callback=lambda trader, i:None):
self.timesteps = timesteps
self.possible_jump_locations = possible_jump_locations
self.single_jump_probability = single_jump_probability
self.active_traders = []
self.populations = {}
for i, trader in enumerate(traders):
trader.simulation_params(timesteps, possible_jump_locations,
single_jump_probability)
trader_tuple = (trader, user_callback(trader, i))
self.populations.setdefault(
trader.name, []).append(trader_tuple)
self.active_traders.append(trader_tuple)
def new_information(self, get_info_callback, execution_prices,
round_number):
for trader_type, traders in self.populations.iteritems():
for trader in traders:
trader[0].trades_history(
execution_prices, round_number)
trader[0].new_information(
get_info_callback(), round_number)
def get_traders(self):
random.shuffle(self.active_traders)
return self.active_traders
def all_users(self, key):
ret = []
for trader_type, traders in self.populations.iteritems():
ret.extend(key(trader) for trader in traders)
return ret