Exemple #1
0
    def end_bot(self):
        if TRADE_TYPE == STOP_LOSS:
            volume_for_fee = goxapi.int2float(self.gox.wallet[self.user_currency], self.user_currency)
        elif TRADE_TYPE == START_GAIN:
            volume_for_fee = goxapi.int2float(self.gox.wallet[BTC], BTC)
        else:
            self.debug(" *** A trade was manually made by user. Bot automatically disabled *** ")
            return

        vol, trade_currency, prc, _no_data, trade_asset = Strategy.set_trade(self, TRADE_TYPE)
        fee = self.total_filled - volume_for_fee

        if fee > 0:

            self.debug("#########################################################@##########################")
            self.debug("                                                                                    ")
            self.debug("  Stop loss/start gain bot is disabled                                              ")
            self.debug("                                                                                    ")
            self.debug("  %s order filled: %.8f %s at %.5f %s/BTC (total filled %.8f %s)"
            % (TRADE_TYPE, vol,  trade_currency, prc, self.user_currency, self.total_filled, trade_asset))
            self.debug("  Mt.Gox fee: %.8f %s"
            % (fee, trade_asset))  
            self.debug("  Press (l) to reload strategy                                                      ")
            self.debug("                                                                                    ")
            self.debug("###############################################################@####################")
Exemple #2
0
    def slot_history_changed(self, history, _dummy):
        """History has changed so recalculate EMAs"""
        candles = []

        # read them all - don't wory about the history parameter
        for c in reversed(self.gox.history.candles):
            candles.append(
                OHLCV(
                    c.tim,
                    # adjust values to be human readable
                    goxapi.int2float(c.opn, self.gox.currency),
                    goxapi.int2float(c.hig, self.gox.currency),
                    goxapi.int2float(c.low, self.gox.currency),
                    goxapi.int2float(c.cls, self.gox.currency),
                    goxapi.int2float(c.vol, "BTC")))

        # self.debug("New EMAs from history with %d candles" % len(candles))

        rng = range(len(candles))
        iterable = (candles[i].opn for i in rng)
        a_opn = np.fromiter(iterable, np.float)

        iterable = (candles[i].hig for i in rng)
        a_hig = np.fromiter(iterable, np.float)

        iterable = (candles[i].low for i in rng)
        a_low = np.fromiter(iterable, np.float)

        iterable = (candles[i].cls for i in rng)
        a_cls = np.fromiter(iterable, np.float)

        iterable = (candles[i].vol for i in rng)
        a_vol = np.fromiter(iterable, np.float)

        a_sema = talib.EMA(a_cls, 40)
        a_lema = talib.EMA(a_cls, 88)
        a_gcr = (a_sema[-1] - a_lema[-1]) / a_lema[-1] * 100
        self.a_gcr = a_gcr
        # Current price in relation to EMA
        # self.debug("Short Exp Moving Average (20 Samples) = %f" % a_sema[-1])
        # self.debug("Long Exp Moving Average (44 Samples) = %f" % a_lema[-1])
        # self.debug("Goomboo's cross = %f pc" % a_gcr)

        target = open('trade', 'w')
        line = "BEGIN"
        if a_gcr < 0:
            line = time.strftime("%H:%M:%S") + ": Sell"
        if a_gcr > 0:
            line = time.strftime("%H:%M:%S") + ": Buy"
        target.write(line)
        target.close()

        if (self.CURRENT_POSITION == "SELL" or self.CURRENT_POSITION
                == "NA") and a_gcr > 0.25 and self.BOT_ACTIVE:
            # self.go_long()
            self.CURRENT_POSITION = "BUY"
        if (self.CURRENT_POSITION == "BUY" or self.CURRENT_POSITION
                == "NA") and a_gcr < -0.25 and self.BOT_ACTIVE:
            # self.go_short()
            self.CURRENT_POSITION = "SELL"
Exemple #3
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.already_executed = False
        if STOP_PRICE < 0:
            self.debug("initializing trailing stop: %f @ %f" %
                       (STOP_VOLUME, STOP_PRICE))
            self.stop_price = 0
            self.trail_distance = goxapi.float2int(-STOP_PRICE, gox.currency)
            gox.history.signal_changed(gox.history,
                                       None)  # fire the signal once
        else:
            self.debug("initializing stop loss: %f @ %f" %
                       (STOP_VOLUME, STOP_PRICE))
            self.stop_price = goxapi.float2int(STOP_PRICE, gox.currency)
            self.trail_distance = 0

    def slot_changed(self, history, _no_data):
        """price history has changed (new trades in history)"""
        if self.already_executed:
            return

        last_candle = history.last_candle()
        if not last_candle:
            return

        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.gox.currency))

        if not self.stop_price:
            return

        if price_last <= self.stop_price:
            self.debug("*** market order: sell %f at %f" %
                       (STOP_VOLUME, STOP_PRICE))
            self.gox.sell(0, int(STOP_VOLUME * 1e8))
            self.already_executed = True

    def slot_trade(self, gox, (date, price, volume, typ, own)):
        """a trade message has been received"""
        if not own:
            return

        vol = goxapi.int2float(volume, "BTC")
        prc = goxapi.int2float(price, gox.currency)
        self.debug("*** order filled: %f at %f %s" % (vol, prc, gox.currency))
Exemple #4
0
    def slot_changed(self, history, _no_data):
        """price history has changed (new trades in history)"""
        if self.already_executed:
            return

        last_candle = history.last_candle()
        if not last_candle:
            return

        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.gox.currency))

        if not self.stop_price:
            return

        if price_last <= self.stop_price:
            self.debug("*** market order: sell %f at %f" %
                       (STOP_VOLUME, STOP_PRICE))
            self.gox.sell(0, int(STOP_VOLUME * 1e8))
            self.already_executed = True
Exemple #5
0
    def slot_changed(self, history, _no_data):
        """price history has changed (new trades in history)"""
        global TRADE_TYPE

        if self.already_executed:
            Strategy.end_bot(self)
            return
        
        if not self.init:
            try:
                self.debug("Retrieving wallet data...")
                self.btc_wallet = goxapi.int2float(self.gox.wallet[BTC], BTC)
                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.init = True        
                Strategy.begin_bot(self)
        else:
                        
            # main loop
            last_candle = history.last_candle()
          
            if not last_candle:
                self.debug("Could retrieve candle data from goxtool, retrying...")
            else:
                price_last = goxapi.int2float(last_candle.cls, self.user_currency)

                if self.btc_wallet >= 0.01:
                    TRADE_TYPE = STOP_LOSS
                else:
                    TRADE_TYPE = START_GAIN

                vol, _no_data, prc, _no_data, _no_data = Strategy.active_msg(self, TRADE_TYPE)

                if TRADE_TYPE == STOP_LOSS and price_last <= STOP_PRICE:
                    self.gox.sell(0, int(vol * 1e8))
                    self.debug("  *** Market order fired: SELL %.8f at %f" % (vol, prc))
                    self.already_executed = True

                if TRADE_TYPE == START_GAIN and price_last >= START_PRICE:
                    self.gox.buy(0, int((vol / prc) * 1e8))
                    self.debug("  *** Market order fired: BUY %f at %.8f" % (vol, prc))
                    self.already_executed = True
Exemple #6
0
def gox2float(value, currency):
    return int2float(value, currency)
Exemple #7
0
             self.gox.quote2float(next_sell),
             self.gox.quote2float(
                 self.gox.quote2int(
                     self.gox.quote2float(next_sell) *
                     self.gox.base2float(sell_amount))), self.gox.curr_quote))
        if SIMULATE == False and self.bid != 0:
            self.gox.sell(next_sell, sell_amount)
        elif SIMULATE and self.wallet and self.bid != 0:
            self.simulate.update({
                "next_sell": next_sell,
                "sell_amount": sell_amount
            })

    def slot_tick(self, gox, (bid, ask)):
        # Set last bid/ask price
        self.bid = goxapi.int2float(bid, self.gox.orderbook.gox.currency)
        self.ask = goxapi.int2float(ask, self.gox.orderbook.gox.currency)

        # Simulation wallet
        if SIMULATE and self.wallet:
            if ask >= self.simulate['next_sell']:
                gox.wallet[gox.curr_quote] += gox.base2float(
                    self.simulate['sell_amount']) * self.simulate['next_sell']
                gox.wallet[gox.curr_base] -= self.simulate['sell_amount']
                # Trigger slot_trade for simulation.log
                self.slot_trade(gox,
                                (time, self.simulate['next_sell'],
                                 self.simulate['sell_amount'], 'bid', True))
                self.place_orders()

            if bid <= self.simulate['next_buy']:
Exemple #8
0
class Strategy(strategy.Strategy):
    """stop loss/start gain bot"""
    def __init__(self, gox):
        strategy.Strategy.__init__(self, gox)
        gox.history.signal_changed.connect(self.slot_changed)

        self.init = False
        self.got_wallet = False
        self.already_executed = False

        self.user_currency = gox.currency
        self.total_filled = 0 
        self.btc_wallet = 0
        self.fiat_wallet = 0

    def slot_changed(self, history, _no_data):
        """price history has changed (new trades in history)"""
        global TRADE_TYPE

        if self.already_executed:
            Strategy.end_bot(self)
            return
        
        if not self.init:
            try:
                self.debug("Retrieving wallet data...")
                self.btc_wallet = goxapi.int2float(self.gox.wallet[BTC], BTC)
                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.init = True        
                Strategy.begin_bot(self)
        else:
                        
            # main loop
            last_candle = history.last_candle()
          
            if not last_candle:
                self.debug("Could retrieve candle data from goxtool, retrying...")
            else:
                price_last = goxapi.int2float(last_candle.cls, self.user_currency)

                if self.btc_wallet >= 0.01:
                    TRADE_TYPE = STOP_LOSS
                else:
                    TRADE_TYPE = START_GAIN

                vol, _no_data, prc, _no_data, _no_data = Strategy.active_msg(self, TRADE_TYPE)

                if TRADE_TYPE == STOP_LOSS and price_last <= STOP_PRICE:
                    self.gox.sell(0, int(vol * 1e8))
                    self.debug("  *** Market order fired: SELL %.8f at %f" % (vol, prc))
                    self.already_executed = True

                if TRADE_TYPE == START_GAIN and price_last >= START_PRICE:
                    self.gox.buy(0, int((vol / prc) * 1e8))
                    self.debug("  *** Market order fired: BUY %f at %.8f" % (vol, prc))
                    self.already_executed = True
 

    def slot_trade(self, gox, (date, price, volume, typ, own)):
        """a trade message has been received"""
        global TRADE_TYPE

        if not own:
            return

        if own and self.already_executed == False:
            self.already_executed = True
            TRADE_TYPE = None
            return
           
        vol, trade_currency, prc, _no_data, trade_asset = Strategy.set_trade(self, TRADE_TYPE)
       
        vol = goxapi.int2float(volume, BTC) 
        prc = goxapi.int2float(price, self.gox.currency)
        
        if TRADE_TYPE == STOP_LOSS:
            filled = prc * vol
        elif TRADE_TYPE == START_GAIN:
            filled = vol
            vol = prc * filled            

        self.total_filled += filled
        
        self.debug("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
        self.debug("                                                                                 ")
        self.debug("  %s filled: VOL %.8f %s at %.5f %s/BTC (filled %.8f %s)"
        % (TRADE_TYPE, vol, trade_currency, prc, self.user_currency, filled, trade_asset))      
        self.debug("                                                                                 ")
        self.debug("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")