-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbacktest.py
More file actions
215 lines (154 loc) · 8.29 KB
/
backtest.py
File metadata and controls
215 lines (154 loc) · 8.29 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 18 16:21:56 2023
@author: prasannaa.kolkar
"""
# from app import getR_SDetails
import yfinance as yf
from send_message_discord import send_discord_message
from db_config import setKeyValueRedis, getValueRedis, keyExists
def backtestHammerStrategy(getR_SDetails,company_name='', final_result='',name_index=0, start='',end='',interval=''):
df = yf.download(tickers=company_name, start=start,
end=end, interval=interval)
if(getR_SDetails().get(company_name)):
stock_val_support = getR_SDetails().get(company_name).get('support')
stock_val_resistance = getR_SDetails().get(company_name).get('resistance')
supportKeys = (stock_val_support.keys())
resitanceKeys = (stock_val_resistance.keys())
for key in supportKeys:
val_breach = 0
temp_index = 0
for val in df['Close']:
if((val <= key) and (df['Open'][temp_index] >= key)):
val_breach = 1
if(val_breach):
messageIntro = '''
Get Ready...
You are in a trade
'''
if(val <= stock_val_support.get(key).get('final target')):
send_discord_message("@everyone Target Achieved "+"for Stock - "+company_name+"Price - "+str(key))
print("Company Name", company_name)
print("Short price - ", key)
print("Final Support Target Hit", val)
print("Profit", abs(val-key))
print("Datetime" , df.index[temp_index])
final_result = final_result.append({
'Datetime':df.index[temp_index],
'Company Name': company_name,
'Buy Price':key,
'Trade Type': 'short',
'Target Achieved': val,
'Profit': abs(val-key),
'Loss': 'NA',
'SL': 'NA'
}, ignore_index=True)
print(final_result)
break
# return {"SUPPORT HIT": "TARGET "}
elif(val >= stock_val_support.get(key).get('SL')):
send_discord_message("@everyone SL Hit "+"for Stock - "+company_name+"Price - "+str(key))
print("Company Name", company_name)
print("SL Support", val)
print("Loss", abs(val-key))
print("Datetime" , df.index[temp_index])
final_result = final_result.append({
'Datetime':df.index[temp_index],
'Company Name': company_name,
'Buy Price':key,
'Trade Type': 'short',
'Target Achieved': 'NA',
'Profit': 'NA',
'Loss': abs(val-key),
'SL': val
}, ignore_index=True)
break
# return {"SUPPORT HIT": "SL"}
temp_index+=1
for key in resitanceKeys:
val_breach = 0
temp_index = 0
for val in df['Close']:
if((val >= key) and (df['Open'][temp_index] <= key)):
val_breach = 1
if(val_breach):
messageIntro = '''
Get Ready...
You are in a trade
'''
if(val >= stock_val_resistance.get(key).get('final target')):
send_discord_message("@everyone Target Achieved "+"for Stock - "+company_name+"Price - "+str(key))
print("Company Name", company_name)
print("Buy price - ", key)
print("Final Resistance Target Hit", val)
print("Profit", abs(val-key))
print("Datetime" , df.index[temp_index])
final_result = final_result.append({
'Datetime':df.index[temp_index],
'Company Name': company_name,
'Buy Price':key,
'Trade Type': 'long',
'Target Achieved': val,
'Profit': abs(val-key),
'Loss': 'NA',
'SL': 'NA'
}, ignore_index=True)
break
# return {"RESISTANCE HIT": "TARGET "}
elif(val <= stock_val_resistance.get(key).get('SL')):
send_discord_message("@everyone SL hit"+"for Stock - "+company_name+"Price - "+str(key))
print("Company Name", company_name)
print("SL Resistance", val)
print("Loss", abs(val-key))
print("Datetime" , df.index[temp_index])
final_result = final_result.append({
'Datetime':df.index[temp_index],
'Company Name': company_name,
'Buy Price':key,
'Trade Type': 'long',
'Target Achieved': 'NA',
'Profit': 'NA',
'Loss': abs(val-key),
'SL': val
}, ignore_index=True)
break
# return {"RESISTANCE HIT": "SL"}
temp_index+=1
return final_result
def backtestHammerStrategyLive(quote,getR_SDetails,company_name=''):
ltp = float(quote.get("success")[0].get("ltp"))
open_val = float(quote.get("success")[0].get("open_price"))
# ltp = 3069
# open_val = 3065
print("ltp and open" , ltp, open_val)
if(getR_SDetails().get(company_name)):
print("inside this")
stock_val_support = getR_SDetails().get(company_name).get('support')
stock_val_resistance = getR_SDetails().get(company_name).get('resistance')
supportKeys = (stock_val_support.keys())
resitanceKeys = (stock_val_resistance.keys())
for key in supportKeys:
print("jey" , key)
if(keyExists(key)):
continue
if((ltp <= key) and (open_val >= key)):
if(ltp <= stock_val_support.get(key).get('final target')):
setKeyValueRedis(key, "True")
continue
print("place a limit order at key and add target and other params along with SL")
send_discord_message("@everyone limit order trade placed")
setKeyValueRedis(key, "True")
for key in resitanceKeys:
print("key" , key)
if(keyExists(key)):
continue
if((ltp >= key) and (open_val <= key)):
print("here fopr once",stock_val_resistance)
if(ltp >= stock_val_resistance.get(key).get('final target')):
#check target breached on this and 3 previous candles. if yes then skip (or check last 5 min low)
setKeyValueRedis(key, "True")
continue
print("place a limit order at key and add target and other params along with SL")
send_discord_message("@everyone limit order trade placed")
setKeyValueRedis(key, "True")