/
tradingmeasure.py
280 lines (219 loc) · 8.2 KB
/
tradingmeasure.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import similarity
import util
""" REGION: MAIN API - START """
# Compute the amount of return when policyFun is applied to targetData.
# sourceData : The data (prices) used to decide the strategy (generally, the predicted data)
# targetData : The actual data (prices) (unknown)
# policyFun : The trading policy to be used.
def computeWithFunOn(sourceData, targetData, policyFun):
buySellPoints = policyFun(sourceData)
return computeWithPoints(targetData, buySellPoints)
# Compute the amount of return by the trading policy policyFun when the future data is fully known.
# i.e. computeWithFunOn, but where sourceData = targetData.
def computeReturnForFullyKnownData(data, policyFun):
return computeWithFunOn(data, data, policyFun)
# policyFun must be a function that returns a strategy(index, futureData)
def computeWithStrategy(sourceData, targetData, policyFun):
strategy = policyFun(sourceData)
return computeWithPointsUsingStrategy(targetData, strategy)
""" REGION: MAIN API - END """
# buySellPoints is a list/tuple that alternates between a buy and a sell..
def computeWithPoints(futureData, buySellPoints):
futureData = similarity.byFirst(futureData)
money = 1
stock = 0
holdingStocks = False
for i in range(0,len(buySellPoints)):
if holdingStocks:
#sell
money += stock*futureData[buySellPoints[i]]
stock = 0
holdingStocks = False
else:
#buy
stock = money / futureData[buySellPoints[i]]
money = 0
holdingStocks = True
# Sell remaining stock at end of period.
if holdingStocks:
money += stock*futureData[len(futureData)-1]
return money
# strategy(index, futureData). 1 means buy, -1 means sell, 0 means do nothing.
def computeWithPointsUsingStrategy(futureData, strategy):
futureData = similarity.byFirst(futureData)
money = 1
stock = 0
holdingStocks = False
for i in range(0,len(futureData)):
action = strategy(i, futureData[:i+1])
if action == 0:
continue
elif action == -1:
if not holdingStocks: continue
#sell
print('Sell ' + str(i))
money += stock*futureData[i]
stock = 0
holdingStocks = False
else: # action == 1
if holdingStocks: continue
#buy
print('Buy ' + str(i))
stock = money / futureData[i]
money = 0
holdingStocks = True
# Sell remaining stock at end of period.
if holdingStocks:
money += stock*futureData[len(futureData)-1]
return money
""" REGION: UTILITY - START """
# Note: higher value is lower confidence
# Values are usually between 0 and 1. However it is occasionally more than 1.
def computeConfidence(meanList, sdList):
return util.mean([sd/val for val,sd in zip(meanList,sdList)])
def computeMeanAndSD(dataLists):
import statistics
dataLists = list(map(similarity.byFirst, dataLists))
datapoints = util.transposeLists(dataLists)
meanList = list(map(statistics.mean, datapoints))
sdList = list(map(statistics.stdev, datapoints))
return meanList, sdList
def averageData(dataLists):
import statistics
dataLists = list(map(similarity.byFirst, dataLists))
datapoints = util.transposeLists(dataLists)
return list(map(statistics.mean, datapoints))
""" REGION: UTILITY - END """
""" REGION: TRADING POLICIES : STRATEGY - START """
# These trading measures take in data as input, and return a strategy.
# Strategy(index, futureData)
# The input futureData is only the future data up till the day being analysed i. (i.e. futureData[0:i+1])
# 1 means buy, -1 means sell, 0 means do nothing.
# A trading measure that returns a strategy that does nothing.
def doNothing(data):
return doNothingStrategy
# A strategy that does nothing.
def doNothingStrategy(index, futureData):
return 0
def buyingThreshold(fraction):
def fun(data):
meanList, sdList = computeMeanAndSD(data)
last = len(meanList)-1
if meanList[last] < meanList[0]:
return doNothingStrategy
class Strategy:
def __init__(self, limit):
self.bought = False
self.limit = limit
self.meanList = meanList
def decide(self, i, futureData):
if self.bought:
return 0
if futureData[i] <= limit:
self.bought = True
return 1
return 0
limit = min(meanList)
limit = 1 - fraction*(1-limit)
strategy = Strategy(limit)
return strategy.decide
return fun
""" REGION: TRADING POLICIES : STRATEGY - END """
""" REGION: TRADING POLICIES : USING MULTIPLE DATALISTS - START """
# these algorithms are run using a set of dataLists for sourceData.
# Not exactly a Trading Measure, but a function that takes in a trading measure from "Using AverageData Only"
# And returns a Trading Measure that uses multiple datalists to compute a "confidence value" using the standard deviation.
# This new Trading Measure bails and does nothing when it is not confident in the predicted data.
# If it is confident, it will trade normally using the trading measure given.
def confidenceFilter(threshold, policy):
def fun(data):
meanList, sdList = computeMeanAndSD(data)
confidence = computeConfidence(meanList, sdList)
if confidence > threshold: return (0,0)
else: return policy(meanList)
return fun
""" REGION: TRADING POLICIES : USING MULTIPLE DATALISTS - END """
""" REGION: TRADING POLICIES : USING AVERAGEDATA ONLY - START """
# these algorithms are run using a single dataList for sourceData.
# If you have a set of dataLists instead, preprocess them with the averageData function first
# to compute a single dataList as the average of the multiple dataLists.
def maxValueSell(data):
sellPoint = max(enumerate(data), key=lambda t:t[1])[0]
return (0,sellPoint)
def tenPercentSell(data):
sellPoint = len(data)-1
for i in range(0,len(data)-1):
ratio = data[i+1]/data[i]
if ratio <= 0.9:
sellPoint = i
if ratio >= 1.1:
sellPoint = i+1
return (0,sellPoint)
# used as a control. does not use the data at all.
def dontSell(data):
return (0,len(data)-1)
def sellOrKeep(data):
last = len(data)-1
if data[last] < data[0]:
return (0,0)
else:
return (0,last)
# only keep when the graph rises by a significant amount.
def riskAverseSellOrKeep(data):
last = len(data)-1
if data[last] >= 1.1*data[0]:
return (0,last)
else:
return (0,0)
# tries to lose as much money as possible
def reversedSellOrKeep(data):
last = len(data)-1
if data[last] < data[0]:
return (0,last)
else:
return (0,0)
# tries to lose as much money as possible
# only sells when the graph falls by a significant amount.
def reversedRiskAverseSellOrKeep(data):
last = len(data)-1
if data[last] < 0.9*data[0]:
return (0,last)
else:
return (0,0)
def largestReturn(data):
data = similarity.byFirst(data)
runningMin = data[0]
minIndex = 0
maxReturn = -1
buyPoint = -1
sellPoint = -1
for i in range(0,len(data)):
v = data[i]
if (v < runningMin):
runningMin = v
minIndex = i
if (v - runningMin > maxReturn):
maxReturn = v - runningMin
buyPoint = minIndex
sellPoint = i
return(buyPoint, sellPoint)
""" REGION: TRADING POLICIES : USING AVERAGEDATA ONLY - END """
if __name__ == '__main__':
import random
test = []
test2 = []
for i in range(0,20):
test.append(50+random.randrange(100))
for i in range(0,20):
test2.append(50+random.randrange(100))
print(test)
print(test2)
print('Length = ' + str(len(test)))
lr = largestReturn(test)
print(lr)
print(str(test[lr[1]]) + ' ' + str(test[lr[0]]))
print(test[lr[1]]-test[lr[0]])
print(computeWithPoints(test, lr))
print(computeReturnForFullyKnownData(test, largestReturn))
print(computeWithStrategy([test,test2], test, buyingThreshold(0.5)))
print(test[-1]/test[0])