def limit_short_trailing(pair, short_price, quantity, trade_type='short', trigger_per=1, deviation=0.5, stop_loss_per=2): configs = change_config.Change_config() transaction_func = transaction.Buy_sell() # buy """ get order_time while curr_price is higher than price sleep 1.5 second get current price if current time > order_time +2h: print time expired return else: buy """ # update trade number allowed configs.update_trading_num("short", -1) configs.update_short_trade_avl(pair, -1) start_time = int(time.time()) curr_price = price.get_price(pair) print("current price:", curr_price) while curr_price < short_price: print("current price: " + str(curr_price) + "| long price:" + str(short_price)) time.sleep(1.5) if int(time.time()) > start_time + 7200: print("time expired") configs.update_trading_num("short", 1) configs.update_short_trade_avl(pair, 1) return curr_price = price.get_price(pair) else: buy = transaction_func.mkt_buy_sell_future(pair=pair, quantity=quantity, positionSide="SHORT", side='SELL') # trailing trailing_func = trailing.Trailing(q=quantity, trade_type=trade_type, trigger_per=trigger_per, deviation=deviation, stop_loss_per=stop_loss_per, pair=pair) trailing_func.trailing_stop_short() configs.update_trading_num("short", 1) configs.update_short_trade_avl(pair, 1) print("successful")
def limit_long_trailing(pair, long_price, quantity, trade_type='long', trigger_per=1, deviation=0.5, stop_loss_per=2): configs = change_config.Change_config() transaction_func = transaction.Buy_sell() # buy """ get order_time while curr_price is higher than price sleep 1.5 second get current price if current time > order_time +2h: print time expired return else: buy """ # trailing configs.update_trading_num("long", -1) configs.update_long_trade_avl(pair, -1) start_time = int(time.time()) curr_price = price.get_price(pair) while curr_price > long_price: print("current price: " + str(curr_price) + "| long price:" + str(long_price)) time.sleep(1.5) if int(time.time()) > start_time + 7200: print("time expired") configs.update_trading_num("long", 1) configs.update_long_trade_avl(pair, 1) return curr_price = price.get_price(pair) else: buy = transaction_func.mkt_buy_sell_future(pair=pair, quantity=quantity, positionSide="LONG", side='BUY') print("bought at " + str(curr_price)) trailing_func = trailing.Trailing( q=quantity, trade_type=trade_type, trigger_per=trigger_per, deviation=deviation, stop_loss_per=stop_loss_per, pair=pair, ) trailing_func.trailing_stop_long() configs.update_trading_num("long", 1) configs.update_long_trade_avl(pair, 1) print("successful")
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
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}
def mkt_short_trailing(pair, quantity, trade_type='short', trigger_per=1, deviation=0.5, stop_loss_per=2): configs = change_config.Change_config() transaction_func = transaction.Buy_sell() trailing_func = trailing.Trailing(q=quantity, trade_type=trade_type, trigger_per=trigger_per, deviation=deviation, stop_loss_per=stop_loss_per, pair=pair) # buy configs.update_trading_num("short", -1) buy = transaction_func.mkt_buy_sell_future(pair=pair, quantity=quantity, positionSide="SHORT", side='SELL') # trailing configs.update_trading_num("long", -1) trailing_func.trailing_stop_short() print("successful")