-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweipingRun.py
144 lines (138 loc) · 5.89 KB
/
weipingRun.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import numpy as np
import shift
from sklearn.neighbors import KernelDensity
# def rank(sample_list):
# diff = 1 / len(sample_list)
# copy1 = sample_list.copy()
# copy2 = sample_list.copy()
# while (len(copy1) > 0):
# max_index = copy1.index(max(copy1))
# copy2[max_index] = diff * len(copy1)
# copy1.remove(copy1[max_index])
# return copy2
def rank(sample_list):
index = 1
for i in np.argsort(sample_list):
sample_list[i] = index / len(sample_list)
index = index + 1
return sample_list
# stockList['AAPL'].tail(500) => pandas dataFrame, shape: 500*5
# pandas
def Weiping_Algorithm(trader, stockList, tickers):
delta_bid = []
delta_ask = []
for stock in tickers:
bp = trader.getBestPrice(stock)
step = 0.01
check_bidsize = bp.getBidSize()
sim_bid = np.arange(0, check_bidsize, step)
check_asksize = bp.getAskSize()
sim_ask = np.arange(0, check_asksize, step)
lookback = 200
# get the last 500 history size data to fit the distribution of the moving window
history_bidsize = stockList[stock].historicalData(lookback).bidSize
history_asksize = stockList[stock].historicalData(lookback).askSize
history_bidsize = np.array(history_bidsize)
history_asksize = np.array(history_asksize)
kde_bid = KernelDensity(bandwidth=0.8, kernel="gaussian").fit(history_bidsize[:, None])
kde_ask = KernelDensity(bandwidth=0.8, kernel="gaussian").fit(history_asksize[:, None])
# check_bidsize = np.array([check_bidsize])
# check_asksize = np.array([check_asksize])
# log_den_bid = kde_bid.score_sample(check_bidsize[:,None])
# log_den_ask = kde_ask.score_sample(check_asksize[:,None])
# d_prob_bid = np.exp(log_den_bid)
# d_prob_ask = np.exp(log_den_ask)
prob_bid = 0.0
prob_ask = 0.0
for k in sim_bid:
temp_bid = np.array([k])
prob_bid = prob_bid + (np.exp(kde_bid.score_samples(temp_bid[:, None]))) * step
for k in sim_ask:
temp_ask = np.array([k])
prob_ask = prob_ask + (np.exp(kde_ask.score_samples(temp_ask[:, None]))) * step
pvalue_bid = 1 - prob_bid
pvalue_ask = 1 - prob_ask
thresold_bid = 0.2
thresold_ask = 0.2
new_bid = 0.0
new_ask = 0.0
# check the unusual size
if pvalue_bid < thresold_bid:
new_bid = 1 / pvalue_bid
if prob_ask < thresold_ask:
new_ask = 1 / pvalue_ask
if new_bid:
delta_bid.append(new_bid)
else:
delta_bid.append(0)
if new_ask:
delta_ask.append(new_ask)
else:
delta_ask.append(0)
# weight adjusting
last_price = [0] * len(tickers)
for k in range(len(tickers)):
last_price[k] = trader.getLastPrice(tickers[k])
last_price = rank(last_price).copy()
total_bid = sum(delta_bid)
total_ask = sum(delta_ask)
if total_bid != 0:
weight_bid = [delta / total_bid for delta in delta_bid]
elif total_bid==0:
weight_bid = [0]*len(delta_bid)
if total_ask != 0:
weight_ask = [delta / total_ask for delta in delta_ask]
elif total_ask==0:
weight_ask = [0]*len(delta_ask)
for stock in range(len(tickers)):
weight_bid[stock] = weight_bid[stock] * last_price[stock]
weight_ask[stock] = weight_ask[stock] * last_price[stock]
#order submit
limit = 50000
money_long = [0] * len(tickers)
money_short = [0] * len(tickers)
for k in range(len(money_long)):
buying_power = trader.getPortfolioSummary().getTotalBP()
if buying_power < limit:
limit = buying_power
money_long[k] = limit * weight_bid[k]
money_short[k] = limit * weight_ask[k]
bp = trader.getBestPrice(tickers[k])
buy_orders_price = bp.getBidPrice()
sell_orders_price = bp.getAskPrice()
# submit buy orders
if money_long[k] != 0:
buy_orders = np.floor(money_long[k] / buy_orders_price)
if (buy_orders / 100) >= 1:
Makrketbuy = shift.Order(shift.Order.MARKET_BUY, tickers[k], int(buy_orders // 100))
trader.submitOrder(Makrketbuy)
else:
if (buy_orders / 100) > 0.5:
Makrketbuy = shift.Order(shift.Order.MARKET_SELL, tickers[k], 1)
trader.submitOrder(Makrketbuy)
# submit sell orders
if money_short[k] != 0:
item = trader.getPortfolioItem(tickers[k])
current_postion = item.getShares()
if current_postion > 0:
sell_orders = current_postion * weight_ask[k]
if (sell_orders / 100) >= 1:
Marketsell = shift.Order(shift.Order.MARKET_SELL, tickers[k], int(sell_orders // 100))
trader.submitOrder(Marketsell)
else:
if (sell_orders / 100) > 0.5:
Marketsell = shift.Order(shift.Order.MARKET_SELL, tickers[k], 1)
trader.submitOrder(Marketsell)
if current_postion < 0:
buying_power = trader.getPortfolioSummary().getTotalBP()
if money_short[k] > buying_power:
sell_orders = np.floor(buying_power / sell_orders_price)
if money_short[k] <= buying_power:
sell_orders = np.floor(money_short[k] / sell_orders_price)
if (sell_orders / 100) >= 1:
Marketsell = shift.Order(shift.Order.MARKET_SELL, tickers[k], int(sell_orders // 100))
trader.submitOrder(Marketsell)
else:
if (sell_orders / 100) > 0.5:
Marketsell = shift.Order(shift.Order.MARKET_SELL, tickers[k], 1)
trader.submitOrder(Marketsell)