Skip to content

Instantly share code, notes, and snippets.

@aido
Forked from prof7bit/_stoploss.py
Last active August 16, 2017 19:51
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aido/5623833 to your computer and use it in GitHub Desktop.
Save aido/5623833 to your computer and use it in GitHub Desktop.
A simple stop loss bot. Adjust STOP_PRICE and STOP_VOLUME to your needs. The file can be reloaded after editing without restarting goxtool by simply pressing the l key. An update of prof7bit's original _stoploss.py at https://gist.github.com/prof7bit/5437131 to sell entire BTC balance when STOP_VOLUME = 0
"""
a simple stop loss bot.
adjust STOP_PRICE and STOP_VOLUME to your needs.
The file can be reloaded after editing without
restarting goxtool by simply pressing the l key.
"""
import strategy
import goxapi
# pylint: disable=C0301
# USD (EUR or other Fiat) price for the sell stop
# positive values are an absolute fixed price,
# negative values enable trailing stop mode and the
# value is the trailing distance
STOP_PRICE = 98.90
# how many BTC to sell at or below the stop price
STOP_VOLUME = 0
class Strategy(strategy.Strategy):
"""a simple stoploss bot"""
def __init__(self, gox):
strategy.Strategy.__init__(self, gox)
gox.history.signal_changed.connect(self.slot_changed)
self.got_wallet = False
self.already_executed = False
self.btc_wallet = 0
self.trail_distance = 0
self.user_currency = gox.currency
self.START_VOLUME = 0
def slot_changed(self, history, _no_data):
"""price history has changed (new trades in history)"""
if self.already_executed:
return
if not self.got_wallet:
try:
self.debug("Retrieving wallet data...")
self.btc_wallet = goxapi.int2float(self.gox.wallet["BTC"], "BTC")
except:
self.debug("Could not retrieve wallet data, retrying...")
return
else:
self.got_wallet = True
if not STOP_VOLUME:
self.STOP_VOLUME = self.btc_wallet
else:
self.STOP_VOLUME = STOP_VOLUME
if STOP_PRICE < 0:
self.debug("initializing trailing stop: sell %f BTC @ %f / BTC" % (self.STOP_VOLUME, STOP_PRICE))
self.stop_price = 0
self.trail_distance = goxapi.float2int(-STOP_PRICE, self.user_currency)
history.signal_changed(history, None) # fire the signal once
else:
self.debug("initializing stop loss: sell %f BTC @ %f / BTC" % (self.STOP_VOLUME / 1e8, STOP_PRICE))
self.stop_price = goxapi.float2int(STOP_PRICE, self.user_currency)
self.trail_distance = 0
else:
last_candle = history.last_candle()
if not last_candle:
self.debug("Could not retrieve candle data from goxtool, retrying...")
else:
price_last = last_candle.cls
if self.trail_distance:
price_trail = price_last - self.trail_distance
if price_trail > self.stop_price:
self.stop_price = price_trail
self.debug("*** trailed stop to %f" %
goxapi.int2float(self.stop_price, self.user_currency))
if not self.stop_price:
return
if price_last <= self.stop_price:
self.debug("*** market order: sell %f BTC at %f / BTC" % (self.STOP_VOLUME, STOP_PRICE))
self.gox.sell(0, int(self.STOP_VOLUME * 1e8))
self.already_executed = True
def slot_trade(self, gox, (date, price, volume, typ, own)):
"""a trade message has been receivd"""
# not interested in other people's trades
if not own:
return
text = {"bid": "sold", "ask": "bought"}[typ]
self.debug("*** %s %f at %f" % (
text,
gox.base2float(volume),
gox.quote2float(price)
))
self.check_trades()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment