def get_collateral(self): try: result = self.bitflyer_api.getcollateral(product_code='FX_BTC_JPY') data2csv.data2csv(result) predict.print_and_write(result) except Exception: predict.print_and_write(Exception)
def judge_condition(self): if self.order_exist == True: remain_test = self.cancel_order(self.order_id) + 1 print('cancel order, remain %f' % (remain_test - 1)) checkins = self.get_checkin_price() # if not position exist trade none position if checkins[1] == 0.0: predict.print_and_write('No position exist, trade none position') result = self.get_hilo() hi = result[1] lo = result[0] cur_price = self.get_current_price(100) predict.print_and_write('current price is %f' % (cur_price)) self.trade_none_position(cur_price, hi, lo) # else trade in position else: data2csv.data2csv([ time.strftime('%b:%d:%H:%M'), 'position', '%f' % float(checkins[1]), 'check in price', '%f' % float(checkins[0]) ]) predict.print_and_write('Position exist, trade with position') result = self.get_hilo() hi = result[1] lo = result[0] close = result[2] self.trade_with_position(hi, lo, close)
def trade_market(self, type, amount, wprice=10000): self.maintance_time() product = 'FX_BTC_JPY' print('trade bitflyer') expire_time = 575 try_t = 0 while try_t < 20: if type == 'BUY' or type == 'buy': order = self.bitflyer_api.sendchildorder( product_code=product, child_order_type='MARKET', side='BUY', size=str(amount)) data2csv.data2csv([ time.strftime('%b:%d:%H:%M'), 'order', 'BUY_MARKET', 'amount', '%f' % float(amount) ]) predict.print_and_write('Buy market ' + str(amount)) elif type == "SELL" or type == "sell": order = self.bitflyer_api.sendchildorder( product_code=product, child_order_type='MARKET', side='SELL', size=str(amount)) data2csv.data2csv([ time.strftime('%b:%d:%H:%M'), 'order', 'SELL_MARKET', 'amount', '%f' % float(amount) ]) predict.print_and_write('Sell market ' + str(amount)) else: print("error!") if 'child_order_acceptance_id' in order: time.sleep(2) execute_price = self.get_execute_order() if execute_price != 0: if type == "SELL" or type == "sell": slides = float(wprice) - float(execute_price) predict.print_and_write( 'SELL : Wish price: %f, deal price: %f, slide : %f' % (wprice, execute_price, slides)) notify.notify_it('SELL : Price: %f, amount : %f' % (wprice, float(amount))) elif type == "BUY" or type == "buy": slides = float(execute_price) - float(wprice) predict.print_and_write( 'BUY : Wish price: %f, deal price: %f, slide : %f' % (wprice, execute_price, slides)) notify.notify_it('BUY : Price: %f, amount : %f' % (wprice, float(amount))) return order else: try_t += 1 print(order) print('Failed, try again') time.sleep(20)
def get_collateral(self): try: result = self.bitflyer_api.getcollateral(product_code='FX_BTC_JPY') self.total_margin = float(result['collateral']) self.useable_margin = self.total_margin - self.least_collateral print('Useable for trading: %.0f' % self.useable_margin) data2csv.data2csv(result) predict.print_and_write(result) except Exception: predict.print_and_write(Exception)
def trade_stop(self, type, price, amount): self.maintance_time() product = 'FX_BTC_JPY' print('trade bitflyer') expire_time = 575 try_t = 0 while try_t < 20: if type == 'BUY' or type == 'buy': parameters = [{ 'product_code': product, 'condition_type': 'STOP', 'side': 'BUY', 'trigger_price': str(price - 500), 'size': str(amount) }] order = self.bitflyer_api.sendparentorder( order_method='SIMPLE', minute_to_expire=expire_time, parameters=parameters) data2csv.data2csv([ time.strftime('%b:%d:%H:%M'), 'order', 'BUY_STOP', 'amount', '%f' % float(amount) ]) predict.print_and_write('Buy stop market %f at %f' % (float(amount), price - 500)) elif type == "SELL" or type == "sell": parameters = [{ 'product_code': product, 'condition_type': 'STOP', 'side': 'SELL', 'trigger_price': str(price + 500), 'size': str(amount) }] order = self.bitflyer_api.sendparentorder( order_method='SIMPLE', minute_to_expire=expire_time, parameters=parameters) data2csv.data2csv([ time.strftime('%b:%d:%H:%M'), 'order', 'SELL_STOP', 'amount', '%f' % float(amount) ]) predict.print_and_write('Sell stop market %f at %f' % (float(amount), price + 500)) else: print("error!") if 'parent_order_acceptance_id' in order: return (order) else: try_t += 1 print(order) print('Failed, try again') time.sleep(20)
def trade_oco4(self, po, stoploss, switch): self.maintance_time() product = 'FX_BTC_JPY' print('trade bitflyer') expire_time = 575 try_t = 0 catchup_trial = 0.43 while try_t < 20: if po == 'long': catchup_trial_price = math.floor(stoploss * (100 + catchup_trial) / 100) parameters = [{'product_code': product, 'condition_type': 'STOP_LIMIT', 'side': 'SELL', 'size': str(switch), 'trigger_price': str(stoploss-200), 'price': str(stoploss+100)}, {'product_code': product, 'condition_type': 'STOP', 'side': 'BUY', 'size': str(switch), 'trigger_price': str(catchup_trial_price)}] order = self.bitflyer_api.sendparentorder(order_method='IFD', minute_to_expire=expire_time, parameters=parameters) data2csv.data2csv( [time.strftime('%b:%d:%H:%M'), 'order', 'IFD_STOPLOSS(inhour)', 'amount', '%f' % float(switch), 'catch_up', '%f' % float(stoploss+100), 'stoploss', '%f' % float(catchup_trial_price)]) elif po == 'short': catchup_trial_price = math.floor(stoploss * (100 - catchup_trial) / 100) parameters = [ {'product_code': product, 'condition_type': 'STOP_LIMIT', 'side': 'BUY', 'size': str(switch), 'trigger_price': str(stoploss+200), 'price': str(stoploss-100)}, {'product_code': product, 'condition_type': 'STOP', 'side': 'SELL', 'size': str(switch), 'trigger_price': str(catchup_trial_price)}] order = self.bitflyer_api.sendparentorder(order_method='IFD', minute_to_expire=expire_time, parameters=parameters) data2csv.data2csv( [time.strftime('%b:%d:%H:%M'), 'order', 'IFD_STOPLOSS(inhour)', 'amount', '%f' % float(switch), 'catch_up', '%f' % float(stoploss-100), 'stoploss', '%f' % float(catchup_trial_price)]) if 'parent_order_acceptance_id' in order: return (order) else: try_t += 1 print(order) print('Failed, try again') time.sleep(20)
def trade_oco1(self, buy, sell, amount): self.maintance_time() product = 'FX_BTC_JPY' print('trade bitflyer') expire_time = 575 try_t = 0 while try_t < 20: parameters = [ { 'product_code': product, 'condition_type': 'STOP', 'side': 'BUY', 'size': str(amount), 'trigger_price': str(buy) }, { 'product_code': product, 'condition_type': 'STOP', 'side': 'SELL', 'size': str(amount), 'trigger_price': str(sell) }, ] order = self.bitflyer_api.sendparentorder( order_method='OCO', minute_to_expire=expire_time, parameters=parameters) data2csv.data2csv([ time.strftime('%b:%d:%H:%M'), 'order', 'OCO_STOP', 'amount', '%f' % float(amount), 'buy', '%f' % float(buy), 'sell', '%f' % float(sell) ]) predict.print_and_write( 'OCO stopmarket buy: %f, sell %f, amount %f' % (float(buy), float(sell), float(amount))) if 'parent_order_acceptance_id' in order: return (order) else: try_t += 1 print(order) print('Failed, try again') time.sleep(20)
def trade_oco3(self, po, stopprofit, stoploss, amount, switch): self.maintance_time() product = 'FX_BTC_JPY' print('trade bitflyer') expire_time = 575 try_t = 0 while try_t < 20: if po == 'long': parameters = [{'product_code': product, 'condition_type': 'STOP', 'side': 'SELL', 'size': str(switch), 'trigger_price': str(stoploss)}, {'product_code': product, 'condition_type': 'STOP_LIMIT', 'side': 'SELL', 'size': str(switch), 'trigger_price': str(stopprofit-200), 'price': str(stopprofit+200)}, ] order = self.bitflyer_api.sendparentorder(order_method='OCO', minute_to_expire=expire_time, parameters=parameters) data2csv.data2csv( [time.strftime('%b:%d:%H:%M'), 'order', 'OCO_SELL_LIMIT_STOP(inhour)', 'amount', '%f' % float(switch), 'stopprofit', '%f' % float(stopprofit), 'stoploss', '%f' % float(stoploss)]) elif po == 'short': parameters = [{'product_code': product, 'condition_type': 'STOP', 'side': 'BUY', 'size': str(switch), 'trigger_price': str(stoploss)}, {'product_code': product, 'condition_type': 'STOP_LIMIT', 'side': 'BUY', 'size': str(switch), 'trigger_price': str(stopprofit+200), 'price': str(stopprofit-200)}, ] order = self.bitflyer_api.sendparentorder(order_method='OCO', minute_to_expire=expire_time, parameters=parameters) data2csv.data2csv( [time.strftime('%b:%d:%H:%M'), 'order', 'OCO_BUY_LIMIT_STOP(inhour)', 'amount', '%f' % float(switch), 'stopprofit', '%f' % float(stopprofit), 'stoploss', '%f' % float(stoploss)]) if 'parent_order_acceptance_id' in order: return (order) else: try_t += 1 print(order) print('Failed, try again') time.sleep(20)
def trade_market(self, type, amount): self.maintance_time() product = 'FX_BTC_JPY' print('trade bitflyer') expire_time = 575 try_t = 0 while try_t < 20: if type == 'BUY' or type == 'buy': order = self.bitflyer_api.sendchildorder( product_code=product, child_order_type='MARKET', side='BUY', size=str(amount)) data2csv.data2csv([ time.strftime('%b:%d:%H:%M'), 'order', 'BUY_MARKET', 'amount', '%f' % float(amount) ]) predict.print_and_write('Buy market ' + amount) elif type == "SELL" or type == "sell": order = self.bitflyer_api.sendchildorder( product_code=product, child_order_type='MARKET', side='SELL', size=str(amount)) data2csv.data2csv([ time.strftime('%b:%d:%H:%M'), 'order', 'SELL_MARKET', 'amount', '%f' % float(amount) ]) predict.print_and_write('Sell market ' + amount) else: print("error!") if 'child_order_acceptance_id' in order: return (order) else: try_t += 1 print(order) print('Failed, try again') time.sleep(20)
def trade_simple(self, type, limitorstop, trigger, limit, amount): self.maintance_time() product = 'FX_BTC_JPY' print('trade bitflyer') expire_time = 575 try_t = 0 while try_t < 20: if limitorstop == 'stop': if type == 'BUY' or type == 'buy': # order = self.quoinex_api.create_market_buy(product_id=5, quantity=str(amount), price_range=str(buysellprice)) parameters = [{ 'product_code': product, 'condition_type': 'STOP', 'side': 'BUY', 'size': str(amount), 'trigger_price': str(trigger) }] order = self.bitflyer_api.sendparentorder( order_method='SIMPLE', minute_to_expire=expire_time, parameters=parameters) data2csv.data2csv([ time.strftime('%b:%d:%H:%M'), 'order', 'BUY_STOP', 'amount', '%f' % float(amount), 'trigger', '%f' % float(trigger) ]) elif type == "SELL" or type == "sell": parameters = [{ 'product_code': product, 'condition_type': 'STOP', 'side': 'SELL', 'size': str(amount), 'trigger_price': str(trigger) }] order = self.bitflyer_api.sendparentorder( order_method='SIMPLE', minute_to_expire=expire_time, parameters=parameters) data2csv.data2csv([ time.strftime('%b:%d:%H:%M'), 'order', 'SELL_STOP', 'amount', '%f' % float(amount), 'trigger', '%f' % float(trigger) ]) elif limitorstop == 'stop_limit': if type == 'BUY' or type == 'buy': parameters = [{ 'product_code': product, 'condition_type': 'STOP_LIMIT', 'side': 'BUY', 'price': str(limit), 'size': str(amount), 'trigger_price': str(trigger) }] order = self.bitflyer_api.sendparentorder( order_method='SIMPLE', minute_to_expire=expire_time, parameters=parameters) data2csv.data2csv([ time.strftime('%b:%d:%H:%M'), 'order', 'BUY_STOPLIMIT', 'amount', '%f' % float(amount), 'limit', '%f' % float(limit), 'trigger', '%f' % float(trigger) ]) elif type == "SELL" or type == "sell": parameters = [{ 'product_code': product, 'condition_type': 'STOP_LIMIT', 'side': 'SELL', 'price': str(limit), 'size': str(amount), 'trigger_price': str(trigger) }] order = self.bitflyer_api.sendparentorder( order_method='SIMPLE', minute_to_expire=expire_time, parameters=parameters) data2csv.data2csv([ time.strftime('%b:%d:%H:%M'), 'order', 'SELL_STOPLIMIT', 'amount', '%f' % float(amount), 'limit', '%f' % float(limit), 'trigger', '%f' % float(trigger) ]) elif limitorstop == 'limit': if type == 'BUY' or type == 'buy': parameters = [{ 'product_code': product, 'condition_type': 'LIMIT', 'side': 'BUY', 'price': str(limit), 'size': str(amount) }] order = self.bitflyer_api.sendparentorder( order_method='SIMPLE', minute_to_expire=expire_time, parameters=parameters) data2csv.data2csv([ time.strftime('%b:%d:%H:%M'), 'order', 'BUY_LIMIT', 'amount', '%f' % float(amount), 'limit', '%f' % float(limit) ]) elif type == "SELL" or type == "sell": parameters = [{ 'product_code': product, 'condition_type': 'LIMIT', 'side': 'SELL', 'price': str(limit), 'size': str(amount) }] order = self.bitflyer_api.sendparentorder( order_method='SIMPLE', minute_to_expire=expire_time, parameters=parameters) data2csv.data2csv([ time.strftime('%b:%d:%H:%M'), 'order', 'SELL_LIMIT', 'amount', '%f' % float(amount), 'limit', '%f' % float(limit) ]) else: print("error!") if 'parent_order_acceptance_id' in order: return (order) else: try_t += 1 print(order) print('Failed, try again') time.sleep(20)