-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathActualTradingBot.py
80 lines (42 loc) · 1.75 KB
/
ActualTradingBot.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import sqlalchemy
import pandas as pd
from binance.client import Client
# In[2]:
client = Client('api-key', 'api-secret')
# In[4]:
engine = sqlalchemy.create_engine('sqlite:///BTCUSDTsream.db')
# In[5]:
df = pd.read_sql('BTCUSDT', engine)
# In[6]:
df
# In[7]:
df.Price.plot()
# In[8]:
def strategy(entry, lookback, qty, open_position=False):
while True:
df = pd.read_sql('BTCUSDT', engine)
lookbackperiod = df.iloc[-lookback:] #vriskei to pio palio timestamp
cumret = (lookbackperiod.Price.pct_change() +1).cumprod()-1 #cummulative return, h teleutaia timh toy coin
if not open_position:
if cumret[cumret.last_valid_index()] > entry: #check an to cummylative return > entry price
order = client.create_order(symbol='BTCUSDT', side = 'BUY', type = 'MARKET' , quantity=qty)
print(order)
open_position = True
break
if open_position:
while True:
df = pd.read_sql('BTCUSDT', engine)
sincebuy = df.loc[df.Time > pd.to_datetime(order['transactTime'],unit='ms')] #filter gia mono ton xrono ap ton opoio agorasame to asset kai meta
if len(sincebuy) > 1:
sincebuyret = (sincebuy.Price.pct_change() +1).cumprod()-1 #o typos gia na dw thn epistrofh poy exw, idios me panw
last_entry = sincebuyret[sincebuyret.last_valid_index()]
if last_entry > 0.0015 or last_entry < -0.0015:
order = client.create_order(symbol='BTCUSDT', side = 'BUY', type = 'MARKET' , quantity=qty)
print(order)
break
# In[9]:
strategy(0.001, 60, 0.001)
# In[ ]: