-
Notifications
You must be signed in to change notification settings - Fork 1
/
MarketSimulator.py
112 lines (96 loc) · 3.81 KB
/
MarketSimulator.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import datetime as dt
import numpy as np
import pandas as pd
import sys
import csv
import math
import pdb
import logging
# types
from ibapi.common import *
from ibapi.order_condition import *
from ibapi.contract import *
from ibapi.order import *
from ibapi.order_state import *
from ibapi.execution import Execution
from ibapi.execution import ExecutionFilter
from ibapi.commission_report import CommissionReport
from ibapi.scanner import ScannerSubscription
from ibapi.ticktype import *
from Orders import Orders
import HistoricalData
logger = logging.getLogger('MarketSimulator')
logger.setLevel(logging.DEBUG)
class OrderWrapper:
def __init__(self, date:dt.datetime, symbol:str, order:Order):
self.date = date
self.symbol = symbol
self.order = order
class MarketSimulator:
def __init__(self, initValue, ordersFile, valuesFile, commissionPrice=0, callback=None):
self.initValue = initValue
self.ordersFile = ordersFile
self.valuesFile = valuesFile
self.symbols_in_order = []
self.data = []
self.df_data = {}
self.commissionPrice = commissionPrice
self.callback = callback
def run(self):
self.orders = []
self.symbols = []
self.shares = {}
with open (self.ordersFile, 'r') as fin:
reader = csv.reader(fin)
for row in reader:
date = dt.datetime(int(row[0]), int(row[1]), int(row[2]), 16)
symbol = row[3]
action = row[4]
quantity = float(row[5])
order = OrderWrapper(date, symbol, Orders.MarketOrder(action, quantity))
self.orders.append(order)
if order.symbol not in self.symbols:
self.symbols.append(order.symbol)
self.shares[order.symbol] = 0
self.orders = sorted(self.orders, key=lambda order: order.date)
startDate = self.orders[0].date
endDate = self.orders[-1].date
logger.debug("Needing %s", self.symbols)
def doSimulate(df):
self.df_data = df
self.simulate()
HistoricalData.requestMultiple(self.symbols, startDate, endDate, "ADJUSTED_LAST", "1 DAY", doSimulate)
def simulate(self):
cash = self.initValue
n_days = len(self.df_data)
n_orders = len(self.orders)
i_order = 0
with open(self.valuesFile, 'w') as fout:
writer = csv.writer(fout)
started = False
for i in range(0, n_days):
date = self.df_data.index[i].to_datetime() + dt.timedelta(hours=16)
while i_order < n_orders and self.orders[i_order].date < date:
i_order += 1
while i_order < n_orders and self.orders[i_order].date == date:
started = True
order = self.orders[i_order]
try:
price = self.df_data[order.symbol][i]
except:
logger.debug("Bad data. ignoring")
i_order += 1
continue
quantity = order.order.totalQuantity if order.order.action.lower() == "buy" else -order.order.totalQuantity
amount = price * quantity
commission = self.commissionPrice * math.ceil(quantity / 100)
cash -= (amount + commission)
self.shares[order.symbol] += quantity
i_order += 1
value = cash
for symbol in self.df_data.columns.values:
value += self.shares[symbol] * self.df_data[symbol][i]
if started:
writer.writerow([date.year, date.month, date.day, value])
if self.callback != None:
self.callback()