-
Notifications
You must be signed in to change notification settings - Fork 6
/
hypothesis.py
215 lines (179 loc) · 8.38 KB
/
hypothesis.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
from dataTypes import *
from decimal import Decimal
from math import sin, cos, sqrt
from scipy.stats import linregress
import numpy as np
def randomChoice(shortTerm, longTerm, cash, botcoins, customParameters, chartingParameters):
assert isinstance(shortTerm, DiscreteData)
assert isinstance(longTerm, DiscreteData)
assert isinstance(cash, Decimal)
assert isinstance(botcoins, Decimal)
assert isinstance(customParameters, dict)
assert isinstance(chartingParameters, dict)
import random
return Decimal(random.randint(0, 100))/100
def bounce(shortTerm, longTerm, cash, botcoins, customParameters, chartingParameters):
assert isinstance(shortTerm, DiscreteData)
assert isinstance(longTerm, DiscreteData)
assert isinstance(cash, Decimal)
assert isinstance(botcoins, Decimal), "{} instead".format(type(botcoins))
assert isinstance(customParameters, dict)
assert isinstance(chartingParameters, dict)
sellAll = customParameters.get("sell", False)
if sellAll:
customParameters["sell"] = False
return Decimal(0)
else:
customParameters["sell"] = True
return Decimal(1)
def hold(*args):
return Decimal(1)
def testing(shortTerm, longTerm, cash, botcoins, customParameters, chartingParameters, hlist, dlist, **kwargs):
chartingParameters["test"] = float(dlist)
return Decimal(.5)
def mse(a, b):
return np.sqrt(np.sum((np.array([x-y for x, y in zip(a, b)]))**2))
def equationMethod(shortTerm, longTerm, cash, botcoins, customParameters, chartingParameters, **kwargs):
assert isinstance(shortTerm, DiscreteData)
assert isinstance(longTerm, DiscreteData)
assert isinstance(cash, Decimal)
assert isinstance(botcoins, Decimal), "{} instead".format(type(botcoins))
assert isinstance(customParameters, dict)
assert isinstance(chartingParameters, dict)
def getDelta(givenList):
assert isinstance(givenList, list)
deltaList = []
if len(givenList) <= 1:
return deltaList
for i in range(1, len(givenList)):
delta = givenList[i] - givenList[i-1]
deltaList.append(delta)
return deltaList
def linearEQ(givenDeltas):
errorDelta = [abs(x-1) for x in givenDeltas]
totalDelta = [abs(x) for x in givenDeltas]
return sum(errorDelta)/sum(totalDelta)
LONG_TERM_HISTORY_TIME_PERIOD = kwargs.get("long_term_history_time_period", 20)
SHORT_TERM_HISTORY_TIME_PERIOD = kwargs.get("short_term_history_time_period", 20*24)
MAP_EQUATIONS = {
linearEQ : .75,
}
if len(customParameters.keys()) == 0:
customParameters["longTermHistory"] = []
customParameters["shortTermHistory"] = []
if len(customParameters["longTermHistory"]) == 0:
customParameters["longTermHistory"].append(longTerm)
else:
if longTerm.date != customParameters["longTermHistory"][-1].date:
customParameters["longTermHistory"].append(longTerm)
if len(customParameters["longTermHistory"]) > LONG_TERM_HISTORY_TIME_PERIOD:
customParameters["longTermHistory"].pop(0)
longTermHistory = customParameters["longTermHistory"]
if len(customParameters["shortTermHistory"]) == 0:
customParameters["shortTermHistory"].append(shortTerm)
else:
if longTerm.date != customParameters["shortTermHistory"][-1].date:
customParameters["shortTermHistory"].append(shortTerm)
if len(customParameters["shortTermHistory"]) > SHORT_TERM_HISTORY_TIME_PERIOD:
customParameters["shortTermHistory"].pop(0)
shortTermHistory = customParameters["shortTermHistory"]
longTermPrices = []
last = None
for x in longTermHistory:
if x.low:
price = (x.low+x.high+x.open+x.close)/4
last = price
longTermPrices.append(price)
elif last:
longTermPrices.append(last)
shortTermPrices = []
last = None
for x in shortTermHistory:
if x.low:
price = (x.low+x.high+x.open+x.close)/4
last = price
shortTermPrices.append(price)
elif last:
shortTermPrices.append(last)
longTermDeltas = getDelta(longTermPrices)
if len(longTermDeltas) == 0:
chartingParameters["test"] = None
chartingParameters["mse"] = None
return Decimal(0)
slope, intercept, r_value, p_value, std_err = linregress(range(0, len(longTermPrices)), longTermPrices)
shortTermDeltas = getDelta(shortTermPrices)
chartingParameters["test"] = r_value**2
differences = np.array([1] * len(longTermPrices)) - np.array(longTermPrices)
squared_array = np.square(differences)
error = mse([1] * len(longTermPrices), longTermPrices)
chartingParameters["mse"] = error/max(x for x in longTermPrices)
return Decimal(r_value**2)
def bollingerBandsSafe(shortTerm, longTerm, cash, botcoins, customParameters, chartingParameters, **kwargs):
assert isinstance(shortTerm, DiscreteData)
assert isinstance(longTerm, DiscreteData)
assert isinstance(cash, Decimal)
assert isinstance(botcoins, Decimal), "{} instead".format(type(botcoins))
assert isinstance(customParameters, dict)
assert isinstance(chartingParameters, dict)
BOLLINGER_BAND_TIME_PERIOD = kwargs.get("bollinger_band_time_period", 20)
BOLLINGER_NUMBER_OF_STDEV = kwargs.get("bollinger_number_of_stdev", .1)
if len(customParameters.keys()) == 0:
customParameters["history"] = []
customParameters["lastLeverage"] = Decimal(0)
customParameters["buySignal"] = False
customParameters["sellSignal"] = False
chartingParameters["lowerBound"] = []
chartingParameters["upperBound"] = []
lastLeverage = customParameters["lastLeverage"]
if len(customParameters["history"]) == 0:
customParameters["history"].append(longTerm)
else:
if longTerm.date != customParameters["history"][-1].date:
customParameters["history"].append(longTerm)
if len(customParameters["history"]) > BOLLINGER_BAND_TIME_PERIOD:
customParameters["history"].pop(0)
history = customParameters["history"]
if len(history) <= BOLLINGER_BAND_TIME_PERIOD / 10:
chartingParameters["lowerBound"] = None
chartingParameters["upperBound"] = None
return Decimal(0)
currentPrice = shortTerm.safeMeanPrice
try:
movingAveragePrice = mean([x.safeMeanPrice for x in history if x.safeMeanPrice])
movingAverageStdev = stdev([x.safeMeanPrice for x in history if x.safeMeanPrice])
except:
chartingParameters["lowerBound"] = None
chartingParameters["upperBound"] = None
return lastLeverage
upperBound = movingAveragePrice + (movingAverageStdev * BOLLINGER_NUMBER_OF_STDEV)
lowerBound = movingAveragePrice - (movingAverageStdev * BOLLINGER_NUMBER_OF_STDEV)
chartingParameters["lowerBound"] = lowerBound
chartingParameters["upperBound"] = upperBound
if not currentPrice:
return lastLeverage
# print("AVG PRICE", movingAveragePrice, "STDEV", movingAverageStdev, "PRICE NOW", shortTerm.safeMeanPrice)
if currentPrice > upperBound:
customParameters["sellSignal"] = True
customParameters["buySignal"] = False
elif currentPrice < lowerBound:
customParameters["sellSignal"] = False
customParameters["buySignal"] = True
else:
if customParameters["buySignal"]:
customParameters["lastLeverage"] = Decimal(1)
return Decimal(1)
elif customParameters["sellSignal"]:
customParameters["lastLeverage"] = Decimal(0)
return Decimal(0)
return lastLeverage
class HypothesisVariation:
def __init__(self, function, **kwargs):
assert callable(function)
self.kwargs = kwargs
def run(self, shortTerm, longTerm, cash, botcoins, customParameters, chartingParameters):
return bollingerBandsSafe(shortTerm, longTerm, cash, botcoins, customParameters, chartingParameters, **self.kwargs)
# def bollingerBandStdevVariation(givenStdev):
# assert isinstance(givenStdev, float) or isinstance(givenStdev, int) or isinstance(givenStdev, Decimal)
# def bollingerBandWrapper(shortTerm, longTerm, cash, botcoins, customParameters, chartingParameters):
# return bollingerBandsSafe(shortTerm, longTerm, cash, botcoins, customParameters, chartingParameters, bollinger_number_of_stdev=givenStdev)
# return bollingerBandWrapper