Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 91 additions & 42 deletions supertrend.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,71 @@
import os
import ccxt
import config
import schedule
import numpy as np
import pandas as pd
pd.set_option('display.max_rows', None)

import warnings
import pprint
import json
import time
from datetime import datetime, date

pd.set_option('display.max_rows', None)
warnings.filterwarnings('ignore')
start_st = datetime.fromtimestamp(time.time()).strftime('%Y%m%d-%H%M%S')

import numpy as np
from datetime import datetime
import time

exchange = ccxt.binanceus({
"apiKey": config.BINANCE_API_KEY,
"secret": config.BINANCE_SECRET_KEY
# Set your global variables
symbol = 'ETH/USDT'
timeframe = '30m'
limit = 100
in_position = False
quantity_buy_sell = 0.1


exchange_id = 'binance'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
'apiKey': config.BINANCE_API_KEY,
'secret': config.BINANCE_SECRET_KEY,
'timeout': 50000,
'enableRateLimit': True,
})

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)
def log_write(msg, start_st=start_st, df=False, json=False, print_it=True):
st = datetime.fromtimestamp(time.time()).strftime('%Y%m%d-%H%M%S')
path = os.path.dirname(os.path.realpath(__file__))
log = open('{}/supertrend-{}.log'.format(path, start_st), 'a')

if not df and not json:
entry = f'{st}: {msg}\n'
elif df:
entry = f'{st}\n{msg}\n'
elif json:
entry = '{}\n{}\n'.format(st, pprint.pprint(json.loads(msg)))

log.write(entry)

if print_it:
print(entry.strip())


def tr(df):
df['previous_close'] = df['close'].shift(1)
df['high_minus_low'] = abs(df['high'] - df['low'])
df['high_minus_pc'] = abs(df['high'] - df['previous_close'])
df['low_minus_pc'] = abs(df['low'] - df['previous_close'])
tr = df[['high_minus_low', 'high_minus_pc', 'low_minus_pc']].max(axis=1)
return tr

def atr(data, period):
data['tr'] = tr(data)
atr = data['tr'].rolling(period).mean()

def atr(df, period):
df['tr'] = tr(df)
atr = df['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)
Expand All @@ -41,65 +75,80 @@ def supertrend(df, period=7, atr_multiplier=3):

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
global in_position, symbol, quantity_buy_sell, log

log_write('Checking for buy and sell signals')

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)
log_write('Changed to uptrend, buy')
order = exchange.create_market_buy_order(symbol, quantity_buy_sell)
log_write(order)
in_position = True

print(df.tail(5))
log_write(df.tail(5), df=True)

else:
print("already in position, nothing to do")
log_write('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)
log_write('Changed to downtrend, sell')
order = exchange.create_market_sell_order(symbol, quantity_buy_sell)
print(order)
log_write(order)
in_position = False

print(df.tail(5))
log_write(df.tail(5), df=True)

else:
print("You aren't in position, nothing to sell")
log_write('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')
global symbol, timeframe, limit, log

supertrend_data = supertrend(df)

check_buy_sell_signals(supertrend_data)
msg = f'symbol: {symbol}, timeframe: {timeframe}, limit: {limit}, in_position: {in_position}, quantity_buy_sell: {quantity_buy_sell}'
log_write(msg)

log_write('Fetching new bars')

schedule.every(10).seconds.do(run_bot)
df = None
try:
bars = exchange.fetch_ohlcv(symbol, timeframe=timeframe, limit=limit)
df = pd.DataFrame(bars[:-1], columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
except:
pass

if df is None:
log_write('Error fetching ohlcv-data from exchange')
else:
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)