Example #1
0
def auto_right_side(coin, amount, avg_price, earn_ratio, loss_ratio):
    sell_price = str(float(avg_price) * (1 + float(earn_ratio)))
    buy_price = str(float(avg_price) * (1 - float(earn_ratio)))

    sell_loss_price = str(float(avg_price) * (1 - float(loss_ratio)))
    # buy_loss_price = str(float(buy_price) * (1 + float(loss_ratio)))

    sell_id = trade.trusted_sell(coin, amount, sell_price)
    print "selling~, id = " + str(sell_id)
    while True:
        if float(trade.get_last_price(
                coin.split('USDT')[0])) < float(sell_loss_price):
            if trade.trusted_cancel_order(coin, sell_id):
                sell_id = trade.trusted_sell(coin, amount, sell_loss_price)
                print "stop loss, sell_id = " + str(sell_id)
                return "stop loss"
        elif trade.trusted_get_open_orders(coin, sell_id) == 'closed':
            print "selled~"
            break
    buy_id = trade.trusted_buy(coin, amount, buy_price)
    print "buying~, id = " + str(buy_id)
    while True:
        if trade.trusted_get_open_orders(coin, buy_id) == 'closed':
            print "bought"
            break
    return 'A great deal!'
Example #2
0
def left_side_method(pair, amount, buy_price, sell_price):
    buy_id = trade.trusted_buy(pair, amount, buy_price)
    print "buying~, id = " + buy_id
    if trade.test_order_closed(buy_id, 0.1):
        print "bought~"
        sell_id = trade.trusted_sell(pair, amount, sell_price)
        print "selling~, id = " + sell_id
        if trade.test_order_closed(sell_id, 3):
            return 'A great deal!'
Example #3
0
def right_side_method(inst_id, amount, buy_price, sell_price):
    sell_id = trade.trusted_sell(inst_id, amount, sell_price)
    print "selling~, id = " + sell_id
    if trade.test_order_closed(sell_id, 0.1):
        print "selled~"
        buy_id = trade.trusted_buy(inst_id, amount, buy_price)
        print "buying~, id = " + buy_id
        if trade.test_order_closed(buy_id, 3):
            return 'A great deal!'
Example #4
0
def auto_right_side(pair, amount_ratio, avg_price, earn_ratio, loss_ratio):
    inst_id = trade.trusted_get_inst(pair)
    sell_price = str(float(avg_price) * (1 + float(earn_ratio)))
    buy_price = str(float(avg_price) * (1 - float(earn_ratio)))

    sell_loss_price = str(float(avg_price) * (1 - float(loss_ratio)))
    buy_loss_price = str(float(avg_price) * (1 + float(loss_ratio)))

    amount = str(
        float(trade.trusted_get_account_balance()[pair.split('USDT')[0]]) *
        float(amount_ratio))
    sell_id = trade.trusted_sell(inst_id, amount, sell_price)
    sell_time = time.time() + 3600
    print "selling~, id = " + str(sell_id)
    while True:
        last_price = float(trade.get_last_price(pair.split('USDT')[0]))
        if ((last_price) < float(sell_loss_price)) or time.time() > sell_time:
            if trade.trusted_cancel_order(inst_id, sell_id):
                return "sell, stop loss or timeout"
        elif trade.trusted_get_open_orders(inst_id, sell_id) == 'closed':
            print "selled~"
            break

    amount = str(
        float(trade.trusted_get_account_balance()['USDT']) *
        float(amount_ratio) / float(buy_price))
    buy_id = trade.trusted_buy(inst_id, amount, buy_price)
    buy_time = time.time() + 3600
    print "buying~, id = " + str(buy_id)
    while True:
        last_price = float(trade.get_last_price(pair.split('USDT')[0]))
        if ((last_price) > float(buy_loss_price)) or time.time() > buy_time:
            if trade.trusted_cancel_order(inst_id, buy_id):
                buy_price = str(float(last_price) * (1 - float(earn_ratio)))
                amount = (float(trade.trusted_get_account_balance()['USDT']) *
                          float(amount_ratio) / float(buy_price))
                buy_id = trade.trusted_buy(inst_id, amount, buy_price)
                buy_time = time.time() + 3600
                print "buying~, id = " + str(buy_id)
                print "buy, stop loss or timeout"
        elif trade.trusted_get_open_orders(inst_id, buy_id) == 'closed':
            print "bought"
            break
    return 'A great deal!'
Example #5
0
def test_sell(inst_id, qty, sell_price):
    sell_id = trade.trusted_sell(inst_id, qty, sell_price)
    return sell_id
Example #6
0
def ma_strategy(pair, k_line_time, amount_ratio='0.97', earn_ratio='0.0001'):
    inst_id = trade.trusted_get_inst(pair)
    ref_ma7, ref_ma30, timestamp = get_ma(inst_id, k_line_time)
    print 'ma7: ' + str(ref_ma7) + 'ma30: ' + str(ref_ma30)

    while True:
        while True:
            new_ts = trade.get_candle_ticks(inst_id, 2,
                                            k_line_time)['tick'][-1][-1]
            if new_ts != timestamp:
                print new_ts
                break
            else:
                time.sleep(3)

        ma7, ma30, timestamp = get_ma(inst_id, k_line_time)
        print 'ma7: ' + str(ma7) + 'ma30: ' + str(ma30)

        if ma7 > ma30 and ref_ma7 <= ref_ma30:
            last_price = float(trade.get_last_price(pair.split('USDT')[0]))
            buy_price = str(float(last_price) * (1 - float(earn_ratio)))
            amount = str(
                float(trade.trusted_get_account_balance()['USDT']) *
                float(amount_ratio) / float(buy_price))
            buy_id = trade.trusted_buy(inst_id, amount, buy_price)
            print "buying~, id = " + str(buy_id)
            buy_time = time.time() + 5
            while True:
                if time.time() > buy_time:
                    if trade.trusted_cancel_order(inst_id, buy_id):
                        last_price = float(
                            trade.get_last_price(pair.split('USDT')[0]))
                        buy_price = str(
                            float(last_price) * (1 - float(earn_ratio)))
                        amount = (float(
                            trade.trusted_get_account_balance()['USDT']) *
                                  float(amount_ratio) / float(buy_price))
                        buy_id = trade.trusted_buy(inst_id, amount, buy_price)
                        buy_time = time.time() + 5
                        print "buying~, id = " + str(buy_id)
                elif trade.trusted_get_open_orders(inst_id,
                                                   buy_id) == 'closed':
                    print "bought"
                    break
        if ma7 < ma30 and ref_ma7 >= ref_ma30:
            last_price = float(trade.get_last_price(pair.split('USDT')[0]))
            sell_price = str(float(last_price) * (1 + float(earn_ratio)))
            amount = str(
                float(
                    trade.trusted_get_account_balance()[pair.split('USDT')[0]])
                * float(amount_ratio))
            sell_id = trade.trusted_sell(inst_id, amount, sell_price)
            sell_time = time.time() + 5
            print "selling~, id = " + str(sell_id)
            while True:
                if time.time() > sell_time:
                    if trade.trusted_cancel_order(inst_id, sell_id):
                        last_price = float(
                            trade.get_last_price(pair.split('USDT')[0]))
                        sell_price = str(
                            float(last_price) * (1 + float(earn_ratio)))
                        amount = str(
                            float(trade.trusted_get_account_balance()[
                                pair.split('USDT')[0]]) * float(amount_ratio))
                        sell_id = trade.trusted_sell(inst_id, amount,
                                                     sell_price)
                        sell_time = time.time() + 5
                        print "selling~, id = " + str(sell_id)
                elif trade.trusted_get_open_orders(inst_id,
                                                   sell_id) == 'closed':
                    print "selled~"
                    break

        ref_ma7, ref_ma30 = ma7, ma30