-
Notifications
You must be signed in to change notification settings - Fork 1
/
Strategy.py
168 lines (136 loc) · 6.29 KB
/
Strategy.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
import pandas_ta as ta
import pandas as pd
import json
import time
import sqlite3
from datetime import date
import glob
import os
def Create_Strategy(Input,Token):
Buy_symbol = []
Sell_symbol = []
Buy_price = []
Sell_price = []
TA = []
Keys = Input.keys()
for i in Keys:
if i == 'Bollinger_Band' or i == 'RSI' or i == 'Moving_Average':
if i =='Bollinger_Band':
TA.append({'kind':'bbands','length':Input[i]['length'],'std':Input[i]['std']})
elif i == 'Moving_Average':
TA.append({'kind':'sma','length':Input[i]['length']})
else:
TA.append({'kind':'rsi','length':Input[i]['length']})
elif i =='Supertrend':
TA.append({'kind':'supertrend','length':Input[i]['ATR_length'],'multiplier':Input[i]['Factor']})
CustomStrategy = ta.Strategy(
name="Strategy",
description="Custom Strategy",
ta=TA
)
if Input['TimeFrame'] == '1D':
tokens = glob.glob('./YData/*')
else:
d = date.today().strftime("%d%b%Y")
db = sqlite3.connect('./WData/' + d +'.db')
c=db.cursor()
c.execute('SELECT name from sqlite_master where type= "table"')
tokens = c.fetchall()
# print(tokens)
# c.execute('''PRAGMA table_info(TOKEN975873)''')
# c.fetchall()
for token in tokens:
# print(token)
# for m in c.execute(f'''SELECT * FROM {token[0]}'''):
if Input['TimeFrame'] == '1D':
df = pd.read_csv(token,parse_dates=['Date'])
else:
df_ = pd.read_sql(f'''SELECT * FROM {token[0]}''',db,parse_dates={'ts': {"format": "%Y-%m-%d %H:%M:%S"}})[:-900]
# print(df_.tail())
df = pd.DataFrame(columns=['High','Low','Open','Close'])
print(df)
df['High'] = df_.resample(Input['TimeFrame'],on='ts')['price'].max()
df['Low'] = df_.resample(Input['TimeFrame'],on='ts')['price'].min()
df['Open'] = df_.resample(Input['TimeFrame'],on='ts')['price'].first()
df['Close'] = df_.resample(Input['TimeFrame'],on='ts')['price'].last()
if Keys != 'MACD':
df.ta.strategy(CustomStrategy)
buy_flage = False
sell_flage = False
Time = df.index[-1]
for i in Keys:
print(i)
if i == 'RSI':
if list(df[f'RSI_{Input["RSI"]["length"]}'])[-1] < Input["RSI"]["RSI_Oversold"]:
buy_flage = True
elif list(df[f'RSI_{Input["RSI"]["length"]}'])[-1] > Input["RSI"]["RSI_Overbrought"]:
sell_flage = True
elif i == 'MACD':
ewm_short = df.Close.ewm(span=Input["MACD"]["slow"], adjust=False).mean()
ewm_long = df.Close.ewm(span=Input["MACD"]["fast"], adjust=False).mean()
df['MACD']=ewm_short-ewm_long
df['signal_MACD']=df['MACD'].ewm(span=9, adjust=False).mean()
if list(df[f'MACD'])[-2] < list(df[f'signal_MACD'])[-2] and list(df[f'MACD'])[-1] > list(df[f'signal_MACD'])[-1] :
buy_flage = True
elif list(df[f'MACD'])[-2] > list(df[f'signal_MACD'])[-2] and list(df[f'MACD'])[-1] < list(df[f'signal_MACD'])[-1] :
sell_flage = True
elif i == 'Bollinger_Band':
if list(df[f'BBU_{Input["Bollinger_Band"]["length"]}_{Input["Bollinger_Band"]["std"]}.0'])[-1] < list(df['Close'])[-1]:
buy_flage = True
if list(df[f'BBL_{Input["Bollinger_Band"]["length"]}_{Input["Bollinger_Band"]["std"]}.0'])[-1] > list(df['Close'])[-1]:
sell_flage = True
elif i == 'Moving_Average':
if list(df[f'SMA_{Input["Moving_Average"]["length"]}'])[-1] < list(df['Close'])[-1]:
buy_flage = True
if list(df[f'SMA_{Input["Moving_Average"]["length"]}'])[-1] > list(df['Close'])[-1]:
sell_flage = True
elif i == 'Supertrend':
green = df[f'SUPERTl_{Input["Supertrend"]["ATR_length"]}_{int(Input["Supertrend"]["Factor"])}.0']
red = df[f'SUPERTl_{Input["Supertrend"]["ATR_length"]}_{int(Input["Supertrend"]["Factor"])}.0']
green = green.fillna(0)
red = red.fillna(0)
if list(green)[-1] !=0:
buy_flage = True
if list(green)[-1] ==0:
sell_flage = True
if Input['TimeFrame'] == '1D':
if buy_flage == True and os.path.basename(token).split('.')[0] not in Buy_symbol:
Buy_symbol.append(os.path.basename(token).split('.')[0])
Buy_price.append(list(df['Close'])[-1])
elif sell_flage == True and os.path.basename(token).split('.')[0] not in Sell_symbol:
Sell_symbol.append(os.path.basename(token).split('.')[0])
Sell_price.append(list(df['Close'])[-1])
else:
if buy_flage == True and Token[token[0][5:]] not in Buy_symbol:
Buy_symbol.append(Token[token[0][5:]])
Buy_price.append(df['Close'][-1])
elif sell_flage == True and Token[token[0][5:]] not in Sell_symbol:
Sell_symbol.append(Token[token[0][5:]])
Sell_price.append(df['Close'][-1])
print(Buy_symbol,Sell_symbol)
print(Buy_price,Sell_price)
if len(Buy_symbol) !=0:
df_buy = pd.DataFrame({'Symbol':Buy_symbol,'Price':Buy_price})
df_buy['Time'] = Time
# df_buy.to_csv('./Info/buy.csv',index=False)
else:
df_buy = pd.DataFrame(columns=['Symbol','Price'])
df_buy.to_csv('./Info/buy.csv',index=False)
if len(Sell_symbol) !=0:
df_sell = pd.DataFrame({'Symbol':Sell_symbol,'Price':Sell_price})
df_sell['Time'] = Time
else:
df_sell = pd.DataFrame(columns=['Symbol','Price'])
df_sell.to_csv('./Info/sell.csv',index=False)
return "Done"
if __name__ == '__main__':
while True:
try:
with open('./Info/Strategy.json','r') as f:
Input = json.load(f)
with open('./token.json','r') as f:
Token = json.load(f)
Create_Strategy(Input,Token)
time.sleep(20)
except Exception as e:
print("Error!!!",e)