Skip to content

Instantly share code, notes, and snippets.

@aido
Created June 16, 2013 17:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aido/5792839 to your computer and use it in GitHub Desktop.
Save aido/5792839 to your computer and use it in GitHub Desktop.
A simple start gain bot. Adjust START_PRICE and START_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 buy entire USD (or EUR etc.) balance when START_VOLUME = 0
"""
a simple start gain bot.
adjust START_PRICE and START_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 buy start
# positive values are an absolute fixed price,
# negative values enable trailing start mode and the
# value is the trailing distance
START_PRICE = -15
# how many USD (EUR or other Fiat) to sell at or below the start price
# Select 0 to trade full USD (or EUR etc.) wallet amount
START_VOLUME = 0
class Strategy(strategy.Strategy):
"""a simple startgain 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.fiat_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.fiat_wallet = goxapi.int2float(self.gox.wallet[self.user_currency], self.user_currency)
except:
self.debug("Could not retrieve wallet data, retrying...")
return
else:
self.got_wallet = True
if not START_VOLUME:
self.START_VOLUME = self.fiat_wallet
else:
self.START_VOLUME = START_VOLUME
if START_PRICE < 0:
self.debug("initializing trailing start: buy %f worth of BTC @ %f / BTC" % (self.START_VOLUME, START_PRICE))
self.start_price = 0
self.trail_distance = goxapi.float2int(-START_PRICE, self.user_currency)
history.signal_changed(history, None) # fire the signal once
else:
self.debug("initializing start gain: buy %f worth of BTC @ %f / BTC" % (self.START_VOLUME / 1e5, START_PRICE))
self.start_price = goxapi.float2int(START_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.start_price:
self.start_price = price_trail
self.debug("*** trailed start to %f" %
goxapi.int2float(self.start_price, self.user_currency))
if not self.start_price:
return
if price_last <= self.start_price:
self.debug("*** market order: buy %f worth of BTC at %f / BTC " % (self.START_VOLUME, START_PRICE))
self.gox.buy(0, int(self.START_VOLUME))
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