forked from szferguson/Binance-Trailing-Stop-Loss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trail.py
67 lines (60 loc) · 2.63 KB
/
trail.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
import ccxt
from binance import Binance
import config
import time
# PLEASE CONFIGURE API DETAILS IN config.py
class StopTrail():
def __init__(self, market, type, stopsize, interval):
self.binance = Binance(
api_key=config.API_DETAILS['API_KEY'],
api_secret=config.API_DETAILS['API_SECRET']
)
self.market = market
self.type = type
self.stopsize = stopsize
self.interval = interval
self.running = False
self.stoploss = self.initialize_stop()
def initialize_stop(self):
if self.type == "buy":
return (self.binance.get_price(self.market) + self.stopsize)
else:
return (self.binance.get_price(self.market) - self.stopsize)
def update_stop(self):
price = self.binance.get_price(self.market)
if self.type == "sell":
if (price - self.stopsize) > self.stoploss:
self.stoploss = price - self.stopsize
print("New high observed: Updating stop loss to %.8f" % self.stoploss)
elif price <= self.stoploss:
self.running = False
amount = self.binance.get_balance(self.market.split("/")[0])
price = self.binance.get_price(self.market)
self.binance.sell(self.market, amount, price)
print("Sell triggered | Price: %.8f | Stop loss: %.8f" % (price, self.stoploss))
elif self.type == "buy":
if (price + self.stopsize) < self.stoploss:
self.stoploss = price + self.stopsize
print("New low observed: Updating stop loss to %.8f" % self.stoploss)
elif price >= self.stoploss:
self.running = False
balance = self.binance.get_balance(self.market.split("/")[1])
price = self.binance.get_price(self.market)
amount = (balance / price) * 0.999 # 0.10% maker/taker fee without BNB
self.binance.buy(self.market, amount, price)
print("Buy triggered | Price: %.8f | Stop loss: %.8f" % (price, self.stoploss))
def print_status(self):
last = self.binance.get_price(self.market)
print("---------------------")
print("Trail type: %s" % self.type)
print("Market: %s" % self.market)
print("Stop loss: %.8f" % self.stoploss)
print("Last price: %.8f" % last)
print("Stop size: %.8f" % self.stopsize)
print("---------------------")
def run(self):
self.running = True
while (self.running):
self.print_status()
self.update_stop()
time.sleep(self.interval)