예제 #1
0
def SimpleHeikinStrategy(accountID, token, instr, window, weight):
    api = API(token, accountID)
    try:
        candles = api.get_candles(instr, window)
        df = heikin_ashi(candles)
        d = len(str(df['Close'][0]).split('.')[1])
        n = len(df) - 1

        sl = df['Open'][n]
        stop_loss = str(round(float(sl), d))
        p = (df['Open'][n] + df['Close'][n]) / 2
        price = round(p, d)

        open_orders = api.openOrders()

        if instr in open_orders:
            pnl = open_orders[instr]['open_pnl']
            tradeIDs = open_orders[instr]['tradeIDs']
            for _id in tradeIDs:
                tsl = api.update_order(_id, price, endpoint="trailingStopLoss")
                print("HEIKIN TRAILING STOP UPDATED")

            # close orders if this candle's color doesn't match the direction
            if open_orders[instr]['Bias'] == 'Long' and df['color'][n] == 'red':
                api.close(instr)
            elif open_orders[instr]['Bias'] == 'Short' and df['color'][
                    n] == 'green':
                api.close(instr)

        else:
            if df['color'][n] == 'green':  # go long
                #tp = price + (price - float(stop_loss) * 20)
                take_profit = str(
                    round(
                        float(
                            (price + (np.abs(float(stop_loss) - price) * 3))),
                        d))
                try:
                    mo = api.order(instr, weight, price, stop_loss,
                                   take_profit)
                    print("HEIKIN LONG ORDER: %s" % instr)
                except Exception as e:
                    print(e)

            elif df['color'][n] == 'red':  # go short
                #tp = price - ((stop_loss - price) * 20)
                take_profit = str(
                    round(
                        float(
                            (price - (np.abs(float(stop_loss) - price) * 3))),
                        d))
                try:
                    mo = api.order(instr, -weight, price, stop_loss,
                                   take_profit)
                    print("HEIKIN SHORT ORDER: %s" % instr)
                except Exception as e:
                    print(e)
    except:
        print("canldes not available right now: %s" % instr)
예제 #2
0
def AmazingCrossoverStrategy(accountID, token, instrument, window, weight):

    api = API(token, accountID)
    history = api.get_candles(instrument, window)

    try:
        n = len(history) - 1
        price = history['Close'][n]
        decimal = len(price.split('.')[1])
        price = float(price)

        history['High'] = history['High'].astype(float)
        history['Low'] = history['Low'].astype(float)
        history['median_price'] = (history['High'] + history['Low']) / 2
        history['rsi'] = rsi(history['median_price'], 10)
        history['fast_sma'] = ema(history['Close'], 5)
        history['slow_sma'] = ema(history['Close'], 10)
        history[-1:].to_sql(instrument + '_' + window,
                            conn,
                            if_exists='append')

        oo = api.openOrders()

        # long logic
        if history['fast_sma'][n] > history['slow_sma'][n]:

            if instrument in oo:
                if oo[instrument]['Bias'] == 'Short':
                    api.close(instrument)
                else:
                    tradeID = oo[instrument]['tradeIDs'][0]
                    price = history['Close'][n]
                    tsl = api.update_order(tradeID,
                                           price,
                                           endpoint='trailingStopLoss')
                    print("AMAZING TRAILING STOP LOSS UPDATED")

            else:
                # if not look for an opportunity
                #if history['rsi'][n] > 50.0:
                stop_loss = str(round(float(price - (price * 0.002)), decimal))
                take_profit = str(round(float(price + (price * 0.02)),
                                        decimal))
                try:
                    mo = api.order(instrument, weight, price, stop_loss,
                                   take_profit)
                    print("AMAZING went long %s" % instrument)
                except Exception as e:
                    print(e)

        if history['fast_sma'][n] < history['slow_sma'][n]:

            if instrument in oo:
                if oo[instrument]['Bias'] == 'Long':
                    api.close(instrument)
                else:
                    tradeID = oo[instrument]['tradeIDs'][0]
                    price = history['Close'][n]
                    tsl = api.update_order(tradeID,
                                           price,
                                           endpoint='trailingStopLoss')
                    print("AMAZING TRAILING STOP LOSS UPDATED")

            else:
                #if history['rsi'][n] < 50.0:
                stop_loss = str(round(float(price + (price * 0.002)), decimal))
                take_profit = str(round(float(price - (price * 0.02)),
                                        decimal))
                try:
                    mo = api.order(instrument, -weight, price, stop_loss,
                                   take_profit)
                    print("AMAZING went short %s" % instrument)
                except Exception as e:
                    print(e)
    except:
        print("candles unavailable right now: %s" % instrument)
예제 #3
0
def IchimokuStrategy(accountID, token, instrument, window, weight):

    api = API(token, accountID)

    try:
        history = api.get_candles(instrument, window)
        ichi = Ichimoku(history)

        n = len(history) - 1
        price = history['Close'][n]
        decimal = len(price.split('.')[1])
        price = float(price)

        # open orders
        oo = api.openOrders()

        # long logic
        if price > ichi['senkou_spanA'][n] and price > ichi['senkou_spanB'][n]:
            # price is above the cloud

            if instrument in oo:
                if oo[instrument]['Bias'] == 'Short':
                    api.close(instrument)
                elif oo[instrument]['Bias'] == 'Long':
                    tradeID = oo[instrument]['tradeIDs'][0]
                    tsl = api.update_order(tradeID,
                                           price,
                                           endpoint='trailingStopLoss')
                    print("ICHIMOKU TRAILING STOP LOSS UPDATED")

            else:
                if ichi['tenkan_sen'][n] > ichi['kijun_sen'][
                        n]:  # omit chikou span signal
                    stop_loss = str(round(float(ichi['kijun_sen'][n]),
                                          decimal))
                    take_profit = str(
                        round(
                            float((price +
                                   (np.abs(float(stop_loss) - price) * 3))),
                            decimal))
                    try:
                        mo = api.order(instrument, weight, price, stop_loss,
                                       take_profit)
                        print("ICHIMOKU went long %s" % instrument)
                    except Exception as e:
                        print(e)

        # short logic
        if price < ichi['senkou_spanA'][n] and price < ichi['senkou_spanB'][n]:

            # check to see if there's already a position
            if instrument in oo:
                if oo[instrument]['Bias'] == 'Long':
                    api.close(instrument)
                elif oo[instrument]['Bias'] == 'Short':
                    tradeID = oo[instrument]['tradeIDs'][0]
                    tsl = api.update_order(tradeID,
                                           price,
                                           endpoint='trailingStopLoss')
                    print("ICHIMOKU TRAILING STOP LOSS UPDATED")

            else:
                # if not, let's look for an opportunity
                if ichi['tenkan_sen'][n] < ichi['kijun_sen'][n]:
                    stop_loss = str(round(float(ichi['kijun_sen'][n]),
                                          decimal))
                    take_profit = str(
                        round(
                            float((price -
                                   (np.abs(float(stop_loss) - price) * 3))),
                            decimal))
                    try:
                        mo = api.order(instrument, -weight, price, stop_loss,
                                       take_profit)
                        print("ICHIMOKU went short %s" % instrument)
                    except Exception as e:
                        print(e)
    except:
        print("candles unavailable right now: %s" % instrument)