-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_v2_tests_class.py
259 lines (205 loc) · 9.7 KB
/
lib_v2_tests_class.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
# from lib_v2_cvars import Cvars
import lib_v2_globals as g
import lib_v2_ohlc as o
import math
class Tests:
def __init__(self, cvars, dfl, df, **kwargs):
# + self.cargs = cargs
idx = kwargs['idx']
self.df = df
self.dfl = dfl
self.FLAG = True
self.cvars = cvars
# + - RUNTIME VARS (required)
self.AVG_PRICE = g.avg_price
self.CLOSE = dfl['Close']
self.LOWERCLOSE = dfl['lowerClose']
self.DSTOT = dfl['Dstot']
self.DSTOT_LOW = dfl['Dstot_lo']
self.DATE = dfl['Date']
def xunder(self,**kwargs):
rs = False
df = kwargs['df']
dfl = kwargs['dfl']
varval = kwargs['trigger']
refval = kwargs['against']
current_varval = df[varval].iloc[len(df.index)-1]
prev_varval =df[varval].iloc[len(df.index)-2]
if prev_varval > refval and current_varval < refval:
rs = True
return rs
def xover(self,**kwargs):
rs = False
df = kwargs['df']
dfl = kwargs['dfl']
varval = kwargs['trigger']
refval = kwargs['against']
current_varval = df[varval].iloc[len(df.index)-1]
prev_varval =df[varval].iloc[len(df.index)-2]
if prev_varval < refval and current_varval >= refval:
rs = True
return rs
def buytest(self, test):
g.buyfiltername = test
call = f"self.{test}()"
return eval(call)
def selltest(self, test):
g.sellfiltername = test
call = f"self.{test}()"
return eval(call)
def BUY_never(self): return False
def SELL_never(self): return False
def BUY_always(self): return True
def SELL_always(self): return True
# ! ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
def BUY_tvb3(self):
FLAG = True
g.next_buy_price = o.state_r('last_buy_price')* (1 - g.cvars[g.datatype]['next_buy_increments'] * (o.state_r('curr_run_ct')*2))
PASSED_DSTOT = self.DSTOT < self.DSTOT_LOW
PASSED_NEXTBUY = self.CLOSE < g.next_buy_price
PASSED_BELOWLOW = self.CLOSE < self.LOWERCLOSE
# print("PASSED_DSTOT",PASSED_DSTOT)
# print("PASSED_NEXTBUY",PASSED_NEXTBUY)
# print("PASSED_BELOWLOW",PASSED_BELOWLOW)
PASSED_CXONEXTBUY = self.xover(df=self.df, dfl=self.dfl, trigger="Close", against=g.next_buy_price)
PASSED_LONGRUN = o.state_r('curr_run_ct') > 0
PASSED_SHORTRUN = o.state_r('curr_run_ct') == 0
# PASSED_INBUDGET = g.subtot_qty < g.cvars['maxbuys'] # ! g.subtot_qty is total BEFORE this purchase
PASSED_BASE = (PASSED_DSTOT and PASSED_NEXTBUY and PASSED_BELOWLOW) or PASSED_CXONEXTBUY
# if PASSED_CXONEXTBUY:
# print (">>>>>>>>>>>>>>>>>>>>>>xover here!!!")
if g.market == "bear":
FLAG = FLAG and PASSED_BASE and PASSED_LONGRUN
if FLAG:
g.buymode = "L"
g.df_buysell['mclr'].iloc[0] = 0
if PASSED_CXONEXTBUY:
g.buymode = "X"
g.df_buysell['mclr'].iloc[0] = 2
g.since_short_buy = 0
return FLAG
if g.market == "bull":
FLAG = FLAG and PASSED_BASE and PASSED_SHORTRUN
if FLAG:
g.buymode = "S"
g.short_buys += 1
g.since_short_buy = 0
g.df_buysell['mclr'].iloc[0] = 1
if PASSED_CXONEXTBUY:
g.buymode = "X"
g.df_buysell['mclr'].iloc[0] = 2
if g.short_buys == 1:
g.last_purch_qty = g.purch_qty
g.since_short_buy = 0
return FLAG
# ! ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
def BUY_perf_tvb3(self):
FLAG = True
PASSED_PERF = False
try:
if g.rootperf[g.bsig[:-1]] >= g.cvars['perf_filter']:
PASSED_PERF = True
except:
pass
g.next_buy_price = o.state_r('last_buy_price')* (1 - g.cvars[g.datatype]['next_buy_increments'] * (o.state_r('curr_run_ct')*2))
PASSED_DSTOT = self.DSTOT < self.DSTOT_LOW
PASSED_NEXTBUY = self.CLOSE < g.next_buy_price
PASSED_BELOWLOW = self.CLOSE < self.LOWERCLOSE
# print("PASSED_DSTOT",PASSED_DSTOT)
# print("PASSED_NEXTBUY",PASSED_NEXTBUY)
# print("PASSED_BELOWLOW",PASSED_BELOWLOW)
PASSED_CXONEXTBUY = self.xover(df=self.df, dfl=self.dfl, trigger="Close", against=g.next_buy_price)
PASSED_LONGRUN = o.state_r('curr_run_ct') > 0
PASSED_SHORTRUN = o.state_r('curr_run_ct') == 0
# PASSED_INBUDGET = g.subtot_qty < g.cvars['maxbuys'] # ! g.subtot_qty is total BEFORE this purchase
PASSED_BASE = (PASSED_PERF and PASSED_DSTOT and PASSED_NEXTBUY and PASSED_BELOWLOW) or PASSED_CXONEXTBUY
# if PASSED_CXONEXTBUY:
# print (">>>>>>>>>>>>>>>>>>>>>>xover here!!!")
if g.market == "bear":
FLAG = FLAG and PASSED_BASE and PASSED_LONGRUN
if FLAG:
g.buymode = "L"
g.df_buysell['mclr'].iloc[0] = 0
if PASSED_CXONEXTBUY:
g.buymode = "X"
g.df_buysell['mclr'].iloc[0] = 2
g.since_short_buy = 0
return FLAG
if g.market == "bull":
FLAG = FLAG and PASSED_BASE and PASSED_SHORTRUN
if FLAG:
g.buymode = "S"
g.short_buys += 1
g.since_short_buy = 0
g.df_buysell['mclr'].iloc[0] = 1
if PASSED_CXONEXTBUY:
g.buymode = "X"
g.df_buysell['mclr'].iloc[0] = 2
if g.short_buys == 1:
g.last_purch_qty = g.purch_qty
g.since_short_buy = 0
return FLAG
# ! ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
def BUY_perf(self):
FLAG = True
g.next_buy_price = o.state_r('last_buy_price')* (1 - g.cvars[g.datatype]['next_buy_increments'] * (o.state_r('curr_run_ct')*2))
PASSED_NEXTBUY = self.CLOSE < g.next_buy_price
PASSED_DATE = g.last_date != self.DATE # * prevenst duped that appear in time-filtered data
FLAG = FLAG and PASSED_DATE and PASSED_NEXTBUY
try:
# print(g.rootperf[g.bsig[:-1]])
if g.rootperf[g.bsig[:-1]] >= g.cvars['perf_filter']:
FLAG = FLAG and True
else:
FLAG = FLAG and False
# print(g.bsig, g.rootperf[g.bsig[:-1]])
except:
FLAG = FLAG and False
pass
if FLAG:
g.buymode = "L"
g.df_buysell['mclr'].iloc[0] = 0
g.since_short_buy = 0
g.last_date = self.DATE
return FLAG
# ! ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
def BUY_tvb3_stream(self):
FLAG = True
g.next_buy_price = o.state_r('last_buy_price')* (1 - g.cvars[g.datatype]['next_buy_increments'] * (o.state_r('curr_run_ct')*2))
if g.market == "bear":
FLAG = FLAG and (
(
self.DSTOT < self.DSTOT_LOW # ! g.cvars['dstot_Dadj'][g.long_buys]
and self.CLOSE < g.next_buy_price
and self.CLOSE < self.LOWERCLOSE
and o.state_r('curr_run_ct') > 0
and g.subtot_qty < g.cvars['maxbuys']) # ! g.subtot_qty is total BEFORE this purchase
) or self.xover(df=self.df, dfl=self.dfl, trigger='Close', against=g.next_buy_price
)
if FLAG:
g.buymode = "L"
g.df_buysell['mclr'].iloc[0] = 0
g.since_short_buy = 0
if g.market == "bull":
FLAG = FLAG and (
self.CLOSE < self.LOWERCLOSE
and self.CLOSE < g.next_buy_price
and g.long_buys == 0
) or self.xover(df=self.df, dfl=self.dfl, trigger="Close", against=g.next_buy_price)
if FLAG:
g.buymode = "S"
g.short_buys += 1
g.since_short_buy = 0
g.df_buysell['mclr'].iloc[0] = 1
if g.short_buys == 1:
g.last_purch_qty = g.purch_qty
# g.purch_qty = self.cvars['first_short_buy_amt']
g.since_short_buy = 0
# print(g.buymode,g.market, o.state_r('curr_run_ct'))
# o.waitfor()
return FLAG
# * ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
def SELL_tvb3(self):
FLAG = True
FLAG = FLAG and self.CLOSE > g.coverprice
return FLAG