-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
445 lines (363 loc) · 19.6 KB
/
main.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import pandas as pd
import numpy as np
import json
from datetime import datetime as dt
from datetime import timedelta
import time
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import alpaca_trade_api as alpaca
from indicator import *
from config_params import *
# Files
key = json.loads(open('AUTH/authAlpaca.txt', 'r').read())
api = alpaca.REST(key['APCA-API-KEY-ID'], key['APCA-API-SECRET-KEY'], base_url= key['BASE-URL'], api_version = 'v2')
tickers = open('AUTH/Tickers.txt', 'r').read() # Tickers
tickers = tickers.split()
# Function to fetch data
def get_data(ticker, timeframe= timeframe, start_date = int(start_date), exchanges = exchange):
df = api.get_crypto_bars(ticker, timeframe, (dt.now() - timedelta(days = start_date)).strftime("%Y-%m-%d"), dt.now().strftime("%Y-%m-%d"), exchanges = exchange).df
df.reset_index(inplace = True)
df = df[['timestamp', 'open', 'high', 'low', 'close']]
df.columns = ['Timestamp', 'Open', 'High', 'Low', 'Close']
return df
def check_params(tickers, run):
tickers_check = tickers
for ticker in tickers_check:
print("Fetching Data for:", ticker)
if run == False:
break
df = get_data(ticker)
print(df.tail)
if stoch == 'True' and stoch_rsi == 'False' and ema == 'False': # Stoch
df = stochastic(df, TYPE = 'Stoch')
print("Calculating Signals for Stoch")
signal_list = list(df['Stoch Signal'].iloc[ -lookback_period : ])
signal_count = 0
for signal in signal_list:
signal_count += 1
if signal == 1:
mail_content = buy(ticker) # buy ticker
# tickers_bought.append(ticker)
mail_alert(mail_content, 0)
order_csv = pd.read_csv('ORDERS/Open Orders.csv')
open_positions = len(list(order_csv.index))
run = open_positions < max_trades
break
if signal_count == lookback_period:
print('No Buy Signal Found for Stoch')
elif stoch == 'False' and stoch_rsi == 'True' and ema == 'False': # StochRSI
print("Calculating Signals for StochRSI")
df = rsi(df)
df = stochastic(df, TYPE = 'StochRSI')
signal_list = list(df['StochRSI Signal'].iloc[ -lookback_period : ])
signal_count = 0
for signal in signal_list:
signal_count += 1
# print(signal)
if signal == 1:
mail_content = buy(ticker)
mail_alert(mail_content, 0)
order_csv = pd.read_csv('ORDERS/Open Orders.csv')
open_positions = len(list(order_csv.index))
run = open_positions < max_trades
break
if signal_count == lookback_period:
print('No Buy Signal Found for StochRSI')
elif stoch == 'False' and stoch_rsi == 'False' and ema == 'True': # EMA
print("Calculating Signals for EMA")
df = implement_ema_strategy(df)
signal_list = list(df['EMA Signal'].iloc[ -lookback_period : ])
signal_count = 0
for signal in signal_list:
signal_count += 1
if signal == 1:
mail_content = buy(ticker)
mail_alert(mail_content, 0)
order_csv = pd.read_csv('ORDERS/Open Orders.csv')
open_positions = len(list(order_csv.index))
run = open_positions < max_trades
break
if signal_count == lookback_period:
print('No Buy Signal found for EMA')
elif stoch == 'True' and stoch_rsi == 'True' and ema == 'True': # All 3
print("Calculating Signals for Stoch + StochRSI + EMA")
df = stochastic(df, TYPE = 'Stochastic')
df = rsi(df)
df = stochastic(df, TYPE = 'StochRSI')
df = implement_ema_strategy(df)
stoch_signal_list = list(df['Stoch Signal'].iloc[ -lookback_period : ])
stochRSI_signal_list = list(df['StochRSI Signal'].iloc[ -lookback_period : ])
ema_signal_list = list(df['EMA Signal'].iloc[ -lookback_period : ])
trade_decision_list = []
for signal in stoch_signal_list:
if signal == 1:
trade_decision_list.append(signal)
break
for signal in stochRSI_signal_list:
if signal == 1:
trade_decision_list.append(signal)
break
for signal in ema_signal_list:
if signal == 1:
trade_decision_list.append(signal)
break
if len(trade_decision_list) == 3:
# buy ticker with amount %capital/trade
mail_content = buy(ticker)
# tickers_bought.append(ticker)
mail_alert(mail_content, 0)
order_csv = pd.read_csv('ORDERS/Open Orders.csv')
open_positions = len(list(order_csv.index))
run = open_positions < max_trades
# elif len(trade_decision_list) < 3:
else:
print('No Buy Signal Found for Stoch + StochRSI + EMA')
continue
elif stoch == 'True' and stoch_rsi == 'True' and ema == 'False': # Stoch + StochRSI
print("Calculating Signals for Stoch + StochRSI")
df = stochastic(df, TYPE = 'Stoch')
df = rsi(df)
df = stochastic(df, TYPE = 'StochRSI')
stoch_signal_list = list(df['Stoch Signal'].iloc[ -lookback_period : ])
stochRSI_signal_list = list(df['StochRSI Signal'].iloc[ -lookback_period : ])
trade_decision_list = []
for signal in stoch_signal_list:
if signal == 1:
trade_decision_list.append(signal)
break
for signal in stochRSI_signal_list:
if signal == 1:
trade_decision_list.append(signal)
break
if len(trade_decision_list) == 2:
mail_content = buy(ticker)
# tickers_bought.append(ticker)
mail_alert(mail_content, 0)
order_csv = pd.read_csv('ORDERS/Open Orders.csv')
open_positions = len(list(order_csv.index))
run = open_positions < max_trades
# elif len(trade_decision_list) < 3:
else:
print("No Buy Signal Found for Stoch + StochRSI")
continue
elif stoch_rsi == 'True' and ema == 'True' and stoch == 'False': # StochRSI + EMA
print("Calculating Signals for StochRSI + EMA")
df = rsi(df)
df = stochastic(df, TYPE = 'StochRSI')
df = implement_ema_strategy(df)
stochRSI_signal_list = list(df['StochRSI Signal'].iloc[ -lookback_period : ])
ema_signal_list = list(df['EMA Signal'].iloc[ -lookback_period : ])
trade_decision_list = []
for signal in stochRSI_signal_list:
if signal == 1:
trade_decision_list.append(signal)
break
for signal in ema_signal_list:
if signal == 1:
trade_decision_list.append(signal)
break
if len(trade_decision_list) == 2:
mail_content = buy(ticker)
# tickers_bought.append(ticker)
mail_alert(mail_content, 0)
order_csv = pd.read_csv('ORDERS/Open Orders.csv')
open_positions = len(list(order_csv.index))
run = open_positions < max_trades
else:
print('No Buy Signal Found for StochRSI + EMA')
continue
elif stoch_rsi == 'False' and ema == 'True' and stoch == 'True': # EMA + Stoch
print("Calculating Signals for EMA + Stoch")
df = stochastic(df, TYPE = "Stochastic")
df = implement_ema_strategy(df)
stoch_signal_list = list(df['Stoch Signal'].iloc[ -lookback_period : ])
ema_signal_list = list(df['EMA Signal'].iloc[ -lookback_period : ])
trade_decision_list = []
for signal in stoch_signal_list:
if signal == 1:
trade_decision_list.append(signal)
break
for signal in ema_signal_list:
if signal == 1:
trade_decision_list.append(signal)
break
if len(trade_decision_list) == 2:
mail_content = buy(ticker)
# tickers_bought.append(ticker)
mail_alert(mail_content, 0)
order_csv = pd.read_csv('ORDERS/Open Orders.csv')
open_positions = len(list(order_csv.index))
run = open_positions < max_trades
else:
print('No Buy Signal Found for EMA + Stoch')
continue
else:
print('Please select any 1 indicator by changing indicator setting to "True"')
def order_files(coin_to_buy, price_coin, highest_price, targetPositionSize, target_price, stop_loss_price, ActivateTrailingStopAt):
# filesDone = 0
# files = ['Orders', 'Open Orders', 'Time and Coins']
if os.path.isfile('ORDERS/Orders.csv'):
# print('Orders IFFF')
df = pd.read_csv('ORDERS/Orders.csv')
df.loc[len(df.index)] = [dt.now().strftime("%Y-%m-%d %H:%M:%S"), coin_to_buy, 'buy',
price_coin, '-', highest_price, targetPositionSize, targetPositionSize*price_coin, api.get_account().cash, target_price, stop_loss_price, ActivateTrailingStopAt]
df.to_csv('ORDERS/Orders.csv', index = False)
else:
# print('Orders ELSEEE')
df = pd.DataFrame()
df[['Time', 'Ticker', 'Type', 'Buy Price', 'Sell Price', 'Highest Price', 'Quantity', 'Total', 'Acc Balance', 'Target Price', 'Stop Loss Price', 'ActivateTrailingStopAt']] = ''
df.loc[len(df.index)] = [dt.now().strftime("%Y-%m-%d %H:%M:%S"), coin_to_buy, 'buy',
price_coin, '-', highest_price, targetPositionSize, targetPositionSize*price_coin, api.get_account().cash, target_price, stop_loss_price, ActivateTrailingStopAt]
df.to_csv('ORDERS/Orders.csv', index = False)
if os.path.isfile('ORDERS/Time and Coins.csv'):
df1 = pd.read_csv('ORDERS/Time and Coins.csv')
df1.loc[len(df.index)] = [dt.now().strftime("%Y-%m-%d %H:%M:%S"), coin_to_buy]
df1.to_csv('ORDERS/Time and Coins.csv', index = False)
else:
df1 = pd.DataFrame()
df1[['Time', 'Ticker']] = ''
df1.loc[len(df.index)] = [dt.now().strftime("%Y-%m-%d %H:%M:%S"), coin_to_buy]
df1.to_csv('ORDERS/Time and Coins.csv', index = False)
if os.path.isfile('ORDERS/Open Orders.csv'):
df2 = pd.read_csv('ORDERS/Open Orders.csv')
df2.loc[len(df.index)] = [dt.now().strftime("%Y-%m-%d %H:%M:%S"), coin_to_buy, 'buy',
price_coin, '-', highest_price, targetPositionSize, targetPositionSize*price_coin, api.get_account().cash, target_price, stop_loss_price, ActivateTrailingStopAt]
df2.to_csv('ORDERS/Open Orders.csv', index = False)
else:
df2 = pd.DataFrame()
df2[['Time', 'Ticker', 'Type', 'Buy Price', 'Sell Price', 'Highest Price', 'Quantity', 'Total', 'Acc Balance', 'Target Price', 'Stop Loss Price', 'ActivateTrailingStopAt']] = ''
df2.loc[len(df.index)] = [dt.now().strftime("%Y-%m-%d %H:%M:%S"), coin_to_buy, 'buy',
price_coin, '-', highest_price, targetPositionSize, targetPositionSize*price_coin, api.get_account().cash, target_price, stop_loss_price, ActivateTrailingStopAt]
df2.to_csv('ORDERS/Open Orders.csv', index = False)
def buy(coin_to_buy: str, trade_cap_percent = trade_capital_percent):
# cashBalance = api.get_account().cash
cashToUse = investment_amount
buy_amount = cashToUse * (trade_cap_percent * 0.01)
price_coin = api.get_latest_crypto_trade(coin_to_buy, exchange= 'CBSE').p
targetPositionSize = ((float(buy_amount)) / (price_coin)) # Calculates required position size
print(coin_to_buy, targetPositionSize)
api.submit_order(str(coin_to_buy), targetPositionSize, "buy", "market", "day") # Market order to open position
mail_content = '''TRADE ALERT: BUY Order Placed for {} {} at ${}'''.format(targetPositionSize, coin_to_buy, price_coin)
stop_loss_price = price_coin * (1- (stop_loss*0.01))
ActivateTrailingStopAt = price_coin * (1+ (activate_trailing_stop_loss_at * 0.01))
target_price = price_coin * (1 + (limit_price * 0.01))
highest_price = price_coin
print(mail_content)
order_files(coin_to_buy, price_coin, highest_price, targetPositionSize, target_price, stop_loss_price, ActivateTrailingStopAt)
return mail_content
def sell(current_coin, quantity, buy_price, highest_price):
# sells current_stock
sell_price = api.get_latest_crypto_trade(str(current_coin), 'CBSE').price
api.submit_order(current_coin, quantity, 'sell', 'market', 'day')
mail_content = '''TRADE ALERT: SELL Order Placed for {} {} at ${}'''.format(quantity, current_coin, sell_price)
df = pd.read_csv('ORDERS/Orders.csv')
df.loc[len(df.index)] = [dt.now().strftime("%Y-%m-%d %H:%M:%S"), current_coin, 'sell',
buy_price, sell_price, highest_price, quantity, quantity*sell_price, api.get_account().cash, '-', '-', '-']
df.to_csv('ORDERS/Orders.csv', index = False)
return mail_content
def mail_alert(mail_content, sleep_time):
# The mail addresses and password
sender_address = 'sender_address'
sender_pass = 'sender_pass'
receiver_address = 'receiver_address'
# Setup MIME
message = MIMEMultipart()
message['From'] = 'Trading Bot'
message['To'] = receiver_address
message['Subject'] = 'Technical Trading Bot'
# The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
# Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) # use gmail with port
session.starttls() # enable security
# login with mail_id and password
session.login(sender_address, sender_pass)
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
time.sleep(sleep_time)
# global open_positions
open_positions = len(api.list_positions())
def mainNEW(open_positions):
tickers_bought = []
mail_alert("The Bot Started Running on {} at {}".format(dt.now().strftime("%Y-%m-%d"), dt.now().strftime("%H:%M:%S")), 0)
try:
while True:
# Checking Returns every time loop runs
if os.path.isfile('ORDERS/Open Orders.csv'):
print('Checking Returns')
df = pd.read_csv('ORDERS/Open Orders.csv')
for i in list(df.index):
ticker = df.loc[i, 'Ticker']
quantity = df.loc[i, 'Quantity']
# stop_loss_price_temp = df.loc[i, 'Stop Loss Price']
curr_price = api.get_latest_crypto_trade(ticker, exchange).p
trailingStopActivatePrice = df.loc[i, 'ActivateTrailingStopAt']
target_price = df.loc[i, 'Target Price']
highest_price_since_buy = df.loc[i, 'Highest Price']
buy_price = df.loc[i, 'Buy Price']
if (curr_price >= trailingStopActivatePrice) and (curr_price > highest_price_since_buy):
new_stop_loss = curr_price*(1- (trailing_stop * 0.01))
df.loc[i, 'Stop Loss Price'] = new_stop_loss
df.loc[i, 'Highest Price'] = curr_price
lower_limit_price = df.loc[i, 'Stop Loss Price']
if (curr_price <= lower_limit_price) or (curr_price >= target_price):
mail_content = sell(ticker, quantity, buy_price, highest_price_since_buy)
mail_alert(mail_content, 0)
df.drop(index = i, inplace = True)
else:
continue
df.to_csv('ORDERS/Open Orders.csv', index = False)
print("Returns Checked")
else: print("No Open Positions, Generating Signals")
if len(api.list_positions()) > 0:
if os.path.isfile('ORDERS/Open Orders.csv'):
order_csv = pd.read_csv('ORDERS/Open Orders.csv')
open_positions = len(list(order_csv.index))
else:
open_positions = 0
else: open_positions = 0
print("Open_Positions < Max_Trades", open_positions < max_trades)
print("Open Positions:", open_positions)
print("Max Trades:", max_trades)
if open_positions < max_trades:
run = True
tickers_check = [ticker for ticker in tickers if ticker not in tickers_bought]
# tickers_bought = check_params(tickers_check, run)
print("Checking Params for {}".format(tickers_check))
check_params(tickers_check, run)
# if len(tickers_bought) == 0:
# time.sleep(20)
if os.path.isfile('ORDERS/Time and Coins.csv'):
coin_bought_df = pd.read_csv('ORDERS/Time and Coins.csv')
# checking if time since order placed < wait_time
tickers_bought = list(coin_bought_df['Ticker'])
if len(tickers_bought) != 0:
for index in range(len(tickers_bought)):
# coin = coin_bought_df['Ticker'][index]
prev_time = coin_bought_df['Time'][index]
if (dt.now() - dt.strptime(prev_time, "%Y-%m-%d %H:%M:%S")).seconds < sleep_time:
pass
else:
try:
# print(tickers_bought)
tickers_bought.pop(index)
coin_bought_df.drop(index = index, inplace = True)
except Exception as e:
print(e)
coin_bought_df.to_csv('ORDERS/Time and Coins.csv', index = False)
else:
# Sleep if len(tickers_bought) (inside wait_time_betn_trades) = 0
print("Sleeping bcause No Tickers Bought inside wait_time_betn_trades")
time.sleep(20)
else:
print("Sleeping bcause No Time and Coins File")
time.sleep(20)
except Exception as e:
print(e)
mail_alert("The Bot Stopped Running on {} at {}".format(dt.now().strftime("%Y-%m-%d"), dt.now().strftime("%H:%M:%S")), 0)
if __name__ == "__main__":
mainNEW(open_positions)