def trade_long_ex(pair="ETHUSDT"):
    configs = get_configs(pair, "long")
    quantity = compute_quantity(configs)
    trade_ex = transaction.Buy_sell()
    his = trade_ex.trailing_stop_mkt_future(pair=pair,
                                            price=configs['price'],
                                            q=quantity,
                                            side="BUY",
                                            leverage=configs['leverage_rate'])
    #long with mkt price
    configs['price'] = None
    message = pair + " long at " + str(
        configs['price']) + ". Quantity: " + str(quantity)
    mess = send_sms.Send_message()
    mess.send_a_message(message)
    return his
def trade_short_ex(pair="ETHUSDT"):
    configs = get_configs(pair, "short")
    quantity = compute_quantity(configs)
    trade_ex = transaction.Buy_sell()
    #short with mkt price
    configs['price'] = None
    his = trade_ex.trailing_stop_mkt_future(pair=pair,
                                            price=configs['price'],
                                            q=quantity,
                                            side="SELL",
                                            positionSide="SHORT",
                                            leverage=configs['leverage_rate'])

    message = pair + " short at " + str(
        configs['price']) + ". Quantity: " + str(quantity)
    mess = send_sms.Send_message()
    mess.send_a_message(message)
    return his
Esempio n. 3
0
 def __init__(self, q, trade_type='long',trigger_per=1, deviation=0.5, stop_loss_per=2, pair="BTCUSDT",p=None):
     # trigger percentage 
     self.trigger_per = trigger_per/100
     # trailing percentage 
     self.trailing_per = (trigger_per-deviation)/100
     # deviation percetage 
     self.deviation = deviation/100
     
     self.headers = {"Content-Type": "application/json"}
     self.url = "https://api.binance.com/api/v3/ticker/price?symbol="+pair
     self.pair = pair
     self.buy_price = p
     self.config_func = change_config.Change_config()
     if not p:
         p =  float(ast.literal_eval(requests.get(self.url, headers=self.headers).content.decode("UTF-8"))['price'])
         self.buy_price = p 
     if trade_type == 'long':
         # trigger price 
         self.trigger_p = p*(1+self.trigger_per)
         # trailing price 
         self.trailing_p = p*(1+self.trailing_per)
         # stop loss price 
         self.stop_loss = p*(1-(stop_loss_per/100))
     elif trade_type == 'short':
         self.trigger_p = p*(1-self.trigger_per)
         # trailing price 
         self.trailing_p = p*(1-self.trailing_per)
         # stop loss price 
         self.stop_loss = p*(1+(stop_loss_per/100))    
     else:
         return          
     self.quantity = q
     self.transaction_func = transaction.Buy_sell()
     self.messaging = send_sms.Send_message()
     if self.pair=="BTCUSDT":
         self.threhold = [0.015]
         self.sell_per = [0.8]
     else:
         self.threhold = [0.02]
         self.sell_per = [0.6]
     self.quantity_remain = self.quantity
     self.target = 0
     self.decimals = {"BTCUSDT":3,"ETHUSDT":1,"BNBUSDT":1,"XRPUSDT":0}
Esempio n. 4
0
def trade_ex(val):
    db_name= "../pair_db/features.sqlite"
    record_name="XRPUSDT"
    threshold = 701268.8
    column_path = "../XRPUSDT/column_order.json"
    pair ="XRPUSDT"
    trigger_per=2
    deviation=1
    stop_loss_per= 3
    message_func = send_sms.Send_message()
    #configs = change_config.Change_config() 
    if val == 1:
        ss = sync_sql.sync_dol_bar(pair,db_name,record_name,threshold)
    elif val == 2:
        t = trading.Trading(pair=pair,db_name=db_name,record_name=record_name,threshold=threshold,model_path="../XRPUSDT/model.joblib",columns_path=column_path)
        while True:
            result = t.live_trading()
            print("-----XRP---------")
            if result == "long":
                print("-----XRP long------")
                try:
                    p_q = trade_long_short.get_quantity(pair,"long")
                    # pair,quantity,trade_type='long',trigger_per=1, deviation=0.5, stop_loss_per=2
                    message_func.send_a_message("XRP long price: "+ str(p_q['price']) + " quantity: "+ str(p_q['quantity']))
                    Process(target=trailing_mkt.limit_long_trailing, args=(pair,p_q['price'],p_q['quantity'],'long',trigger_per,deviation,stop_loss_per,)).start()
                except:
                    message_func.send_a_message("long buy failed")
            elif result == 'short':
                print("-----XRP short------")
                try:
                    p_q = trade_long_short.get_quantity(pair,"short")
                    # pair,quantity,trade_type='long',trigger_per=1, deviation=0.5, stop_loss_per=2
                    message_func.send_a_message("XRP long price: "+ str(p_q['price']) + " quantity: "+ str(p_q['quantity']))
                    Process(target=trailing_mkt.limit_short_trailing, args=(pair,p_q['price'],p_q['quantity'],'short',trigger_per,deviation,stop_loss_per,)).start()
                except:
                    message_func.send_a_message("short buy failed")
            time.sleep(150)
Esempio n. 5
0
 def __init__(self,
              pair,
              db_name,
              record_name,
              threshold,
              interval='1m',
              model_path=None,
              columns_path=None):
     self.predict = get_predict.Get_predict()
     self.predict.load_model(model_path)
     self.columns = json.load(open(columns_path))['columns']
     self.configs = change_config.Change_config()
     self.pair = pair
     self.interval = interval
     self.db_name = db_name
     self.record_name = record_name
     self.threshold = threshold
     self.load_long_strategy()
     self.load_short_strategy()
     self.message_func = send_sms.Send_message()
     # save operation result to log
     self.log_func = logging_funcs.Logging()
     #connect to sql database
     self.pre_sql = psql.Preprocess_sql(self.db_name, self.record_name)
Esempio n. 6
0
def trade_ex():
    db_name = "../pair_db/features.sqlite"
    record_name = "BTCUSDT"
    threshold = 7127690.2
    column_path = "column_order.json"
    pair = "BTCUSDT"
    trigger_per = 1.5
    deviation = 1
    stop_loss_per = 2
    message_func = send_sms.Send_message()
    #configs = change_config.Change_config()
    while True:
        val = input("1 for sync; 2 for start trading; 3 for quite: ")
        if val == "1":
            ss = sync_sql.sync_dol_bar(pair, db_name, record_name, threshold)
        elif val == "2":
            t = trading.Trading(pair=pair,
                                db_name=db_name,
                                record_name=record_name,
                                threshold=threshold,
                                model_path="model.joblib",
                                columns_path=column_path)
            while True:
                result = t.live_trading()
                if result == "long":
                    try:
                        p_q = trade_long_short.get_quantity(pair, "long")
                        #pair,long_price,quantity,trade_type='long',trigger_per=1, deviation=0.5, stop_loss_per=2
                        message_func.send_a_message("BTC long price: " +
                                                    str(p_q['price']) +
                                                    " quantity: " +
                                                    str(p_q['quantity']))
                        Process(target=trailing_mkt.limit_long_trailing,
                                args=(
                                    pair,
                                    p_q['price'],
                                    p_q['quantity'],
                                    'long',
                                    trigger_per,
                                    deviation,
                                    stop_loss_per,
                                )).start()
                    except Exception as e:
                        message_func.send_a_message("BTC long buy failed " +
                                                    str(e))
                elif result == 'short':
                    result_error = ""
                    try:
                        p_q = trade_long_short.get_quantity(pair, "short")
                        #pair,quantity,trade_type='long',trigger_per=1, deviation=0.5, stop_loss_per=2
                        message_func.send_a_message("BTC short price: " +
                                                    str(p_q['price']) +
                                                    " quantity: " +
                                                    str(p_q['quantity']))
                        result_error = str(p_q['price']) + " " + str(
                            p_q['quantity']) + " "
                        Process(target=trailing_mkt.limit_short_trailing,
                                args=(
                                    pair,
                                    p_q['price'],
                                    p_q['quantity'],
                                    'short',
                                    trigger_per,
                                    deviation,
                                    stop_loss_per,
                                )).start()
                    except Exception as e:
                        message_func.send_a_message("BTC short buy failed " +
                                                    result_error + str(e))
                time.sleep(150)
        elif val == "3":
            break
Esempio n. 7
0
def trade_ex():
    db_name = "../pair_db/features.sqlite"
    record_name = "ETHUSDT"
    threshold = 1913865.49
    column_path = "column_order.json"
    pair = "ETHUSDT"
    trigger_per = 2
    deviation = 1
    stop_loss_per = 3
    message_func = send_sms.Send_message()
    #configs = change_config.Change_config()
    last_long_time = 1545730073
    last_short_time = 1545730073
    while True:
        val = input("1 for sync; 2 for start trading; 3 for quite: ")
        if val == "1":
            ss = sync_sql.sync_dol_bar(pair, db_name, record_name, threshold)
        elif val == "2":
            t = trading.Trading(pair=pair,
                                db_name=db_name,
                                record_name=record_name,
                                threshold=threshold,
                                model_path="model.joblib",
                                columns_path=column_path)
            while True:
                result = t.live_trading()
                """
                check if trade number allowed > 0 for long or for short 
                """
                if result == "long":
                    try:
                        if last_long_time + 3600 < int(
                                datetime.now().timestamp()):
                            p_q = trade_long_short.get_quantity(pair, "long")
                            message_func.send_a_message("ETH long signal " +
                                                        "price" +
                                                        str(p_q['price']) +
                                                        " quantity:" +
                                                        str(p_q['quantity']))
                            # pair,quantity,trade_type='long',trigger_per=1, deviation=0.5, stop_loss_per=2
                            last_long_time = int(datetime.now().timestamp())
                            Process(target=trailing_mkt.limit_long_trailing,
                                    args=(
                                        pair,
                                        p_q['price'],
                                        round(p_q['quantity'], 1),
                                        'long',
                                        trigger_per,
                                        deviation,
                                        stop_loss_per,
                                    )).start()
                    except Exception as e:
                        message_func.send_a_message("ETH long buy failed" +
                                                    " error: " + str(e))
                elif result == 'short':
                    message_func.send_a_message("ETH short signal")
                    result_error = ""
                    try:
                        if last_short_time + 3600 < int(
                                datetime.now().timestamp()):
                            #price nad quantity
                            p_q = trade_long_short.get_quantity(pair, "short")
                            result_error = str(p_q['price']) + " " + str(
                                p_q['quantity'])
                            # pair,quantity,trade_type='long',trigger_per=1, deviation=0.5, stop_loss_per=2
                            last_short_time = int(datetime.now().timestamp())
                            Process(target=trailing_mkt.limit_short_trailing,
                                    args=(
                                        pair,
                                        p_q['price'],
                                        round(p_q['quantity'], 1),
                                        'short',
                                        trigger_per,
                                        deviation,
                                        stop_loss_per,
                                    )).start()
                    except Exception as e:
                        message_func.send_a_message("ETH short buy failed " +
                                                    result_error + " error: " +
                                                    str(e))
                time.sleep(150)
        elif val == "3":
            break