def strategy(self, open, close, high, low, volume): lot = self.exchange.get_lot() length = self.input('length', int, 9) up = last(highest(high, length)) dn = last(lowest(low, length)) self.exchange.plot('up', up, 'b') self.exchange.plot('dn', dn, 'r') self.exchange.entry("Long", True, round(lot / 2), stop=up) self.exchange.entry("Short", False, round(lot / 2), stop=dn)
def strategy(self, open, close, high, low, volume): lot = self.exchange.get_lot() length = self.input("length", int, 9) up = last(highest(high, length)) dn = last(lowest(low, length)) self.exchange.plot("up", up, "b") self.exchange.plot("dn", dn, "r") self.exchange.entry("Long", True, round(lot / 2), stop=up) self.exchange.entry("Short", False, round(lot / 2), stop=dn)
def strategy(self, open, close, high, low, volume): self.start += 1 lot = self.exchange.get_lot() # for test lot lot = int(round(lot / 10)) bitmex = BitMex(threading=False) price = bitmex.get_market_price() sma_base_l = self.input('sma_short_len', int, 200) resolution = self.input( defval=1, title="resolution", type=int ) # defval 변경, 예) 5분 --> 5, 'm' or 1시간 1, 'h', 1Day 1, 'd' source = self.exchange.security(str(resolution) + 'h') # def __init__ 비교 logger.info('source: %s' % source) series_high = source['high'].values series_low = source['low'].values fb100 = last(highest(series_high, 1)) # 1시간 1, 1D의 경우는 resolution도 변경 fb0 = last(lowest(series_low, 1)) logger.info('resolution: %s' % resolution) logger.info('fb100_resol: %s' % fb100) logger.info('fb0_resol: %s' % fb0) # for test # fb100 = price + 15 # fb0 = price - 15 # 최근 1시간을 본봉단위로 획득 # fibo_l = self.input('length', int, 1440) # 1Day = 60min * 24hr # fibo_l = self.input('length', int, 60) # 1Day = 60min * 24hr # fibo100 = last(highest(high, fibo_l)) # fibo0 = last(lowest(low, fibo_l)) fb62 = math.ceil((fb100 - fb0) * 0.618 + fb0) fb38 = math.ceil((fb100 - fb0) * 0.382 + fb0) fb50 = math.ceil((fb100 - fb0) / 2 + fb0) fb200 = math.ceil((fb100 - fb0) * 1.0 + fb100) fb162 = math.ceil((fb100 - fb0) * 0.618 + fb100) fb138 = math.ceil((fb100 - fb0) * 0.382 + fb100) fb038 = math.ceil(fb0 - (fb100 - fb0) * 0.382) fb062 = math.ceil(fb0 - (fb100 - fb0) * 0.618) fb0100 = math.ceil(fb0 - (fb100 - fb0) * 1.00) logger.info('self.start: %s' % self.start) if self.start == 1: # short position self.exchange.order("Short200", False, lot * 3, limit=fb200, post_only=True) self.exchange.order("Short162", False, lot * 2, limit=fb162, post_only=True) self.exchange.order("Short138", False, lot * 1, limit=fb138, post_only=True) self.exchange.order("Short100", False, lot * 1, limit=fb100, post_only=True) # long position self.exchange.order("Long0", True, lot * 1, limit=fb0, post_only=True) self.exchange.order("Long038", True, lot * 1, limit=fb038, post_only=True) self.exchange.order("Long062", True, lot * 2, limit=fb062, post_only=True) self.exchange.order("Long0100", True, lot * 3, limit=fb0100, post_only=True) Long0 = bitmex.get_open_order("Long0") Long038 = bitmex.get_open_order("Long038") Long062 = bitmex.get_open_order("Long062") Long0100 = bitmex.get_open_order("Long0100") Short200 = bitmex.get_open_order("Short200") Short162 = bitmex.get_open_order("Short162") Short138 = bitmex.get_open_order("Short138") Short100 = bitmex.get_open_order("Short100") # # # logger.info('Long0: %s' % Long0) # logger.info('Long038: %s' % Long038) # logger.info('Long062: %s' % Long062) # logger.info('Long0100: %s' % Long0100) # logger.info('Short200: %s' % Short200) # logger.info('Short162: %s' % Short162) # logger.info('Short138: %s' % Short138) # logger.info('Short100: %s' % Short100) # # logger.info('(Long0 is None): %s' % (Long0 is None)) # entry order # long position self.exchange.order("Long0", True, lot * 1, limit=fb0, when=(Long0 is None), post_only=True) self.exchange.order("Long038", True, lot * 1, limit=fb038, when=(Long038 is None), post_only=True) self.exchange.order("Long062", True, lot * 2, limit=fb062, when=(Long0100 is None), post_only=True) self.exchange.order("Long0100", True, lot * 3, limit=fb0100, when=(Long0100 is None), post_only=True) # short position self.exchange.order("Short200", False, lot * 3, limit=fb200, when=(Short200 is None), post_only=True) self.exchange.order("Short162", False, lot * 2, limit=fb162, when=(Short162 is None), post_only=True) self.exchange.order("Short138", False, lot * 1, limit=fb138, when=(Short138 is None), post_only=True) self.exchange.order("Short100", False, lot * 1, limit=fb100, when=(Short100 is None), post_only=True) # win order if price <= fb0: self.exchange.order("Long0_w", False, lot * 1, limit=fb38, stop=fb0) logger.info('rice <= fb0: %s' % fb0) if price <= fb038: self.exchange.order("Long038_w", False, lot * 1, limit=fb0, stop=fb038) if price <= fb062: self.exchange.order("Long062_w", False, lot * 2, limit=fb038, stop=fb062) if price <= fb0100: self.exchange.order("Long0100_w", False, lot * 3, limit=fb062, stop=fb0100) if price >= fb100: logger.info('price >= fb100: %s' % fb100) self.exchange.order("Short100_w", True, lot * 1, limit=fb62, stop=fb100) if price >= fb138: self.exchange.order("Short138_w", True, lot * 1, limit=fb100, stop=fb138) if price >= fb162: self.exchange.order("Short162_w", True, lot * 2, limit=fb138, stop=fb162) if price >= fb200: self.exchange.order("Short200_w", True, lot * 3, limit=fb162, stop=fb200) # logger.info('bitmex.get_margin():%s' % bitmex.get_margin()) # logger.info('bitmex.get_position():%s' % bitmex.get_position()) # for debug # logger.info('fb200: %s' % fb200) # logger.info('fb162: %s' % fb162) # logger.info('fb138: %s' % fb138) # logger.info('fb100: %s' % fb100) # logger.info('fb62: %s' % fb62) # logger.info('fb50: %s' % fb50) # logger.info('fb38: %s' % fb38) # logger.info('fb0: %s' % fb0) # logger.info('fb038: %s' % fb038) # logger.info('fb062: %s' % fb062) # logger.info('fb0100: %s' % fb0100) diff = (abs(bitmex.get_balance() - abs(self.prebalance))) realised_pnl = bitmex.get_margin()['realisedPnl'] logger.info('prebalance():%s' % self.prebalance) logger.info('bitmex.get_balance():%s' % bitmex.get_balance()) logger.info('diff:%s' % diff) logger.info('realised_pnl:%s' % realised_pnl) logger.info('--------------------------------------------------')