-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
105 lines (98 loc) · 3.28 KB
/
run.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
import sys
import shift
import time
from credentials import my_username, my_password
from stockAndPortfolio import Stock, portfolioInfo, infoCollecting, clearAllPortfolioItems, cancelAllPendingOrders
import datetime
from dongxuRun import marketMaker
'''
********************************************************************************
Initial parameter settings
'''
# Setup trader for accumulating information
verbose = 1
trader = shift.Trader(my_username)
# connect to the server
try:
trader.connect("initiator.cfg", my_password)
except shift.IncorrectPassword as e:
print(e)
sys.exit(2)
except shift.ConnectionTimeout as e:
print(e)
sys.exit(2)
# subscribe to all available order books
trader.subAllOrderBook()
# simulation time
simulation_duration = 380
# Time to stop the simulation
timeToStop = datetime.time(15, 30)
# All companies' ticker in Dow Jones
tickers = trader.getStockList()
# DWDP and WBA will not be available for trading
tickers.remove("DWDP")
tickers.remove("WBA")
# info diction for every ticker
stockList = dict()
for ticker in tickers:
stockList[ticker] = Stock(ticker)
'''
********************************************************************************
Simulation Start
'''
for i in range(1, simulation_duration*60):
time.sleep(1)
'''
****************************************************************************
Data collection in every second
'''
if i % 1 == 0:
# information will be collected every second.
infoCollecting(trader, tickers, stockList, length = 400)
'''
****************************************************************************
Portfolio summary every 10 seconds
'''
# this is the test function for information collection
if i % 10 == 0 and verbose:
print("\n")
print(f"Trading Time: {i // 60} min")
print(trader.getLastTradeTime().time())
portfolioInfo(trader)
'''
****************************************************************************
Dongxu's strategy
'''
dongxuStartTime = 360
dongxuTimeInterval = 1
if i > dongxuStartTime and i % dongxuTimeInterval == 0:
marketMaker(2, trader, stockList, tickers, lookBack = 301,
lag = 3,
numNeighbors = 10, decay = 1)
'''
****************************************************************************
Risk Control and time management
'''
# substantial loss happen terminate the program
if i % 30 == 0:
portfolioSummary = trader.getPortfolioSummary()
if portfolioSummary.getTotalRealizedPL() < -1000.00 \
or portfolioSummary.getTotalBP() < 600000:
break
# Time to stop the simulation.
if trader.getLastTradeTime().time() > timeToStop:
break
'''
********************************************************************************
End simulation clear all stocks in the portfolio book, and cancel all pending
orders
'''
# cancel all pending orders
cancelAllPendingOrders(trader)
# clear all the portfolio items with market sell
clearAllPortfolioItems(trader, tickers)
time.sleep(600)
# Print out the portfolio summary
print("portfolio summary-----------------------------------------------------")
portfolioInfo(trader)
trader.disconnect()