-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathorderGenerator.py
executable file
·138 lines (100 loc) · 4.34 KB
/
orderGenerator.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
__author__ = 'kmanda1'
import pandas as pd
import numpy as np
def generateOrders(price_df, symbol, predY):
print "******** Generate Orders based on Normalized Predictions:"
print "******** start"
#ax = plt.plot(price_array)
ax = price_df[symbol].plot(title="Trading Strategies", label=symbol)
orders = pd.DataFrame(index=np.arange(price_df.size),columns=['Date','Symbol','Order','Shares'])
print "*** 1"
'''
long_entries = np.zeros(shape=(price_array.size,1))
short_entries = np.zeros(shape=(price_array.size,1))
long_exits = np.zeros(shape=(price_array.size,1))
short_exits = np.zeros(shape=(price_array.size,1))
'''
long_entries = pd.DataFrame(index=price_df.index, columns=[symbol])
short_entries = pd.DataFrame(index=price_df.index, columns=[symbol])
long_exits = pd.DataFrame(index=price_df.index, columns=[symbol])
short_exits = pd.DataFrame(index=price_df.index, columns=[symbol])
print "*** 2"
last_position = 'NA'
i = 0 # range(0, df.shape[0])
j = -1
s = -1
t = 0
long_entry_band = 5
short_entry_band = 5
normedPredY = predY*100
N = len(normedPredY)
# print "*** 3 -> price_df", price_df.size
price_df = price_df*0.01
# print "*** 4" , normedPredY.size
for date_index , row in price_df.iterrows():
index_date=date_index.date()
if(i<N):
if(isinstance(normedPredY, pd.DataFrame)):
currentDiff = price_df.irow(i)[symbol] - normedPredY.irow(i)[1]
else:
currentDiff = price_df.irow(i)[symbol] - normedPredY[1]
#print currentDiff
if (currentDiff < 1.5 and long_entry_band > 0):
short_entry_band = 5
long_entry_band = long_entry_band - 1
#long_entries[i,0] = price_array[s,0]
long_entries.irow(i)[symbol] = price_df.irow(i)[symbol]
last_position = 'LONG_ENTRY'
j = j + 1
orders.irow(j)['Date']=index_date
orders.irow(j)['Symbol']=symbol
orders.irow(j)['Order']='BUY'
orders.irow(j)['Shares']=100
#plt.vlines(x = index_date, colors = 'g', ymin= 0, ymax= 140)
elif (currentDiff > 0.2 and short_entry_band > 0):
long_entry_band = 5
short_entry_band = short_entry_band - 1
#short_entries[i,0] = price_array[s,0]
short_entries.irow(i)[symbol] = price_df.irow(i)[symbol]
last_position = 'SHORT_ENTRY'
j = j + 1
orders.irow(j)['Date']=index_date
orders.irow(j)['Symbol']=symbol
orders.irow(j)['Order']='SELL'
orders.irow(j)['Shares']=100
#plt.vlines(x = index_date, colors = 'r', ymin= 0, ymax= 140)
elif (last_position == 'LONG_ENTRY' and long_entry_band < 1) :
long_entry_band = 5
#long_exits[i,0] = price_array[s,0]
long_exits.irow(i)[symbol] = price_df.irow(i)[symbol]
last_position = 'LONG_EXIT'
j = j + 1
orders.irow(j)['Date']=index_date
orders.irow(j)['Symbol']=symbol
orders.irow(j)['Order']='SELL'
orders.irow(j)['Shares']=100
#plt.vlines(x = index_date, colors = 'k', ymin= 0, ymax= 140)
elif (last_position == 'SHORT_ENTRY' and short_entry_band < 1) :
#short_exits[i,0] = price_array[s,0]
short_entry_band = 5
short_exits.irow(i)[symbol] = price_df.irow(i)[symbol]
last_position = 'SHORT_EXIT'
j = j + 1
orders.irow(j)['Date']=index_date
orders.irow(j)['Symbol']=symbol
orders.irow(j)['Order']='BUY'
orders.irow(j)['Shares']=100
#plt.vlines(x = index_date, colors = 'k', ymin= 0, ymax= 140)
i = i + 1
t = t + 1
print '--------------------'
orders = orders.dropna(subset=["Symbol"]).sort_index()
print 'save data into orders file.'
orders.to_csv('orders.txt')
print 'Orders' , orders
# Add axis labels and legend
ax.set_xlabel("Date")
ax.set_ylabel("Price")
ax.legend(loc='lower center')
#plt.show()
return orders