-
Notifications
You must be signed in to change notification settings - Fork 168
/
supertrend.py
105 lines (78 loc) · 3.14 KB
/
supertrend.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
import ccxt
import config
import schedule
import pandas as pd
pd.set_option('display.max_rows', None)
import warnings
warnings.filterwarnings('ignore')
import numpy as np
from datetime import datetime
import time
exchange = ccxt.binanceus({
"apiKey": config.BINANCE_API_KEY,
"secret": config.BINANCE_SECRET_KEY
})
def tr(data):
data['previous_close'] = data['close'].shift(1)
data['high-low'] = abs(data['high'] - data['low'])
data['high-pc'] = abs(data['high'] - data['previous_close'])
data['low-pc'] = abs(data['low'] - data['previous_close'])
tr = data[['high-low', 'high-pc', 'low-pc']].max(axis=1)
return tr
def atr(data, period):
data['tr'] = tr(data)
atr = data['tr'].rolling(period).mean()
return atr
def supertrend(df, period=7, atr_multiplier=3):
hl2 = (df['high'] + df['low']) / 2
df['atr'] = atr(df, period)
df['upperband'] = hl2 + (atr_multiplier * df['atr'])
df['lowerband'] = hl2 - (atr_multiplier * df['atr'])
df['in_uptrend'] = True
for current in range(1, len(df.index)):
previous = current - 1
if df['close'][current] > df['upperband'][previous]:
df['in_uptrend'][current] = True
elif df['close'][current] < df['lowerband'][previous]:
df['in_uptrend'][current] = False
else:
df['in_uptrend'][current] = df['in_uptrend'][previous]
if df['in_uptrend'][current] and df['lowerband'][current] < df['lowerband'][previous]:
df['lowerband'][current] = df['lowerband'][previous]
if not df['in_uptrend'][current] and df['upperband'][current] > df['upperband'][previous]:
df['upperband'][current] = df['upperband'][previous]
return df
in_position = False
def check_buy_sell_signals(df):
global in_position
print("checking for buy and sell signals")
print(df.tail(5))
last_row_index = len(df.index) - 1
previous_row_index = last_row_index - 1
if not df['in_uptrend'][previous_row_index] and df['in_uptrend'][last_row_index]:
print("changed to uptrend, buy")
if not in_position:
order = exchange.create_market_buy_order('ETH/USD', 0.05)
print(order)
in_position = True
else:
print("already in position, nothing to do")
if df['in_uptrend'][previous_row_index] and not df['in_uptrend'][last_row_index]:
if in_position:
print("changed to downtrend, sell")
order = exchange.create_market_sell_order('ETH/USD', 0.05)
print(order)
in_position = False
else:
print("You aren't in position, nothing to sell")
def run_bot():
print(f"Fetching new bars for {datetime.now().isoformat()}")
bars = exchange.fetch_ohlcv('ETH/USDT', timeframe='1m', limit=100)
df = pd.DataFrame(bars[:-1], columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
supertrend_data = supertrend(df)
check_buy_sell_signals(supertrend_data)
schedule.every(10).seconds.do(run_bot)
while True:
schedule.run_pending()
time.sleep(1)