-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBACKTEST_mudole_Aings_V2.py
161 lines (149 loc) · 6.63 KB
/
BACKTEST_mudole_Aings_V2.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
import pandas as pd
import pandas_ta as ta
from Treding_live_model.strategy import myStratygy as str
import numpy as np
class BACKTESTING():
def __init__(self,data,ST_Asign: bool=False,Asign: float= 0.5,symbol: str='CASH'):
self.df = data
self.symbol =symbol
self.in_postion = False
self.tp1 =False
self.tp_A =True
self.Enter_D ,self.Exit_D, self.Enter ,self.T1,self.T2,self.Sl,self.TRD,self.Enter_A,self.TS_A=[],[],[],[],[],[],[],[],[]
self.num=0
self.tR = 0
self.tp = 0
self.Slose = 0
self.buyprice = 0
self.ST_Asign =ST_Asign
self.PAsign = 0
self.Asign = Asign
self.Result = []
self.Cash ,self.CTraed = [100],[]
def MTread(self,row,SL,T1,TR,Asign : bool = False):# Asign = اعزيز
if not self.in_postion and row.siganl == 1 :
self.ST_Asign = Asign
self.in_postion=True
self.num=0
self.buyprice = row.Close
self.PAsign = self.buyprice -(self.buyprice * (0.01*self.Asign))
self.tr = 2 * row.ATR
self.tp = self.buyprice + ((self.buyprice * T1)+self.tr)
self.Slose = self.buyprice - ((self.buyprice * SL)+self.tr)
self.Enter_D.append(row.Date)
self.Enter_A.append(self.PAsign)
self.Enter.append(self.buyprice)
self.T1.append(self.tp)
self.T2.append(0 if TR else self.tp+((self.tp * T1)))
#T2.append(tp+((buyprice * 0.02)+tr))
self.Sl.append(self.Slose)
elif (row.Close >= self.tp or row.High>= self.tp ) and self.in_postion :
if TR:
self.tp =self.tp + (self.tp * T1)
self.Slose=self.Slose + (self.Slose * SL)
elif not self.tp1:
self.tp =self.T2[-1]
self.Slose=self.Slose + (self.Slose * SL)
self.tp1 =True
if not self.tp_A and self.num ==1 :
self.num +=1
self.TS_A.append(self.T1[-1])
self.tp_A = True
else:
self.TS_A.append(0)
else:
if len(self.Enter_A) != len(self.TS_A):
self.TS_A.append(0)
self.Exit_D.append(row.Date)
self.TRD.append((self.tp + self.T1[-1])/2)
self.in_postion=False
self.tp1 =False
elif (row.Close <= self.Slose or row.Low<= self.Slose) and self.in_postion :
self.tp1 =False
self.in_postion=False
if len(self.TS_A) != len(self.Enter_A) and not self.tp_A:
self.TS_A.append(self.Slose)
self.TRD.append(self.Slose)
self.Exit_D.append(row.Date)
if self.ST_Asign and self.in_postion :
if self.tp_A and self.num==0 and (row.Close <= self.PAsign or row.Low<= self.PAsign) :
self.num +=1
self.tp_A = False
def start(self,T1,SL,TR: bool=False,Asign: bool= False):
for _ , row in self.df.iterrows():
self.MTread(row,SL,T1,TR,Asign)
if len(self.Enter) !=len(self.TS_A):
self.TS_A.append(0)
if len(self.Enter) !=len(self.TRD):
self.TRD.append(0)
self.Exit_D.append(0)
if len(self.Enter)-len(self.TS_A) > 0:
df1= pd.DataFrame(self.Enter_D,columns=['ENTER_D'])
df1['Exit_D'],df1['Enter'],df1['T1'],df1['T2'],df1['SL'],df1['TRD']=self.Exit_D,self.Enter,self.T1,self.T2,self.Sl,self.TRD
print(i)
#print(len(Enter),len(T1),len(Sl),len(TRD))
else:
df1= pd.DataFrame(self.Enter_D,columns=['ENTER_D'])
df1['Exit_D'],df1['Enter'],df1['T1'],df1['T2'],df1['SL'],df1['TRD'],df1['ENTER_A'],df1['TS_A'] =self.Exit_D,self.Enter,self.T1,self.T2,self.Sl,self.TRD,self.Enter_A,self.TS_A
#df['Def'] = df.Enter - df.TRD
df1.to_csv(f'T{self.symbol}.csv')
self.TCalculatoin()
def Calculation(self,Enter,TRD,retail: int = 5):
amount = round(int(self.Cash[-1]) / retail,3)
if amount >2:
qty = amount / Enter
CTrae = TRD * qty
Cass= self.Cash[-1]+(CTrae - amount) if TRD >0 else self.Cash[-1]
self.Cash.append(Cass)
return CTrae , Cass
def TCalculatoin(self):
df= pd.read_csv(f'T{self.symbol}.csv')
if df.TRD.tail(1).values == 0.:
df = df[:-2]
Cas = list(map(self.Calculation,df.Enter,df.TRD))
if len(self.Enter)-len(self.TS_A) > 0:
df['CTraed'],df['Cash']=[i[0] for i in Cas],[i[1] for i in Cas]
else:
self.Cash.pop()
self.Cash.append(100)
Aing = map(self.Calculation,df.ENTER_A,df.TS_A)
df['CTraed'],df['Cash'],df['Cash_A'] =[i[0] for i in Cas],[i[1] for i in Cas],[i[1] for i in list(Aing)]
df.to_csv(f'T{self.symbol}.csv',index=False)
self.Result =df
import os
fi = os.listdir('data/5m')
fi = [i[:-4] for i in fi]
li = []
tiker = []
cast = []
fi = fi[0:]
a=0
if __name__ == '__main__':
for i in fi:
df=pd.read_csv(f"C:\\Users\\dell\\Desktop\\مجلد جديد\\data\\5m\\{i}.csv")
df1 = df
Siganl = str().macd_str(df1)
Siganl.append(0)
df1.dropna(inplace=True)
df1['siganl'] = Siganl
df1['ATR'] = ta.atr(df1.High, df1.Low, df1.Close, length=14)
df1['sma'] = ta.sma(df.Close,100)
import time
start = time.perf_counter()
#df1 = df1[6000:]
print(a,i)
a +=1
back = BACKTESTING(df1,Asign=1)
back.start(0.02,0.01,Asign=True)
tiker.append(i)
cast.append(back.Result.Cash_A.tail(1).values[-1])
li.append(back.Result.Cash.tail(1).values[-1])
#print(li)
x = pd.DataFrame(li,columns=['Cash'])
x.insert(loc=1 ,column='Cash_A', value=cast)
x.insert(loc=2 ,column='symbol', value=tiker)
x.to_csv('result3_1_2_1.6.csv')
end = time.perf_counter()
#print(df)
print(f'finshed in {end -start}')
print(df['Unnamed: 2'][:].replace('[','',))