def __init__(self, api_key, api_secret, backtest=True, product_code="BTC_JPY", duration="h", stoplimitpercent=0.9): self.api_key = api_key self.api_secret = api_secret self.backtest = backtest self.product_code = product_code self.duration = duration self.candles = get_data.Candle( self.api_key, self.api_secret, code=self.product_code).GetAllCandle(duration=self.duration) super().__init__(candles=self.candles, product_code=self.product_code) self.b = get_data.Balance(self.api_key, self.api_secret, code=self.product_code) self.order = order.BitFlayer_Order(self.api_key, self.api_secret, product_code=self.product_code) self.availavlecurrency = self.order.AvailableBalance()["JPY"] self.availavlesize = self.order.AvailableBalance()[self.product_code] self.stoplimitpercent = stoplimitpercent self.stoplimit = 0.0 self.b.GetExecutions() self.bestparams = None self.latest_close = 0 self.before_close = 0
def run(self): while True: durations = ["s", "m", "h", "d"] cdl = get_data.Candle(api_key=api_key, api_secret=api_secret) for duration in durations: cdl.CreateCandleWithDuration(duration=duration) model = eval("Candle_1" + duration) ticker = model.objects.last() print(f"CreateCandleWithDuration:{ticker}") time.sleep(5)
def test_Save(self): createdf.connectandsave() api_key = key.api_key api_secret = key.api_secret c = get_data.Candle(api_key, api_secret) candle = Candle_1h.objects.order_by("time").last() durations = ["s", "m", "h"] for duration in durations: c.Save(candle, duration) self.assertEqual(eval("Candle_1" + duration).objects.exists(), True)
def test_GetCandle_candlenotexists(self): """[summary] createdf.connectandsave() generate only Candle_1h.SoCandle_1s has no objects. GetCsndle function create new candle of Candle_1s""" createdf.connectandsave() duration = "s" api_key = key.api_key api_secret = key.api_secret c = get_data.Candle(api_key, api_secret) self.assertEqual(Candle_1s.objects.exists(), False) candle = c.GetCandle(duration=duration) self.assertEqual(Candle_1s.objects.exists(), True) self.assertEqual(candle, Candle_1s.objects.first())
def GetClose(self): """[summary]Get Close data. If this function called, get new candle and signalevents by super().__init__(). """ self.candles = get_data.Candle( self.api_key, self.api_secret, code=self.product_code).GetAllCandle(duration=self.duration) super().__init__(candles=self.candles, product_code=self.product_code) self.b.GetExecutions() signalevent = SignalEvents.objects.filter( product_code=self.product_code).last() signaltime = signalevent.time self.now_position = signalevent.side self.price = signalevent.price candles = self.candle_dict if len(candles["close"]) > 2: self.latest_close = candles["close"][-1] self.before_close = candles["close"][-2] print( f"now_position={signaltime, self.now_position, self.price},latest_close ={self.latest_close}, before_close={self.before_close} " )
def test_GetCandle_candleisexists(self): """[summary] create new_candle in Candle_1h, then candle=GetCandle('h') is equal to new_candle """ createdf.connectandsave() duration = "h" api_key = key.api_key api_secret = key.api_secret c = get_data.Candle(api_key, api_secret) now_time = c.TruncateDateTime(duration=duration, time=datetime.datetime.now()) new_candle = Candle_1h( time=now_time, open=0, close=0, high=0, low=0, volume=0) new_candle.save() candle = c.GetCandle(duration=duration, time=now_time) # print(f"Get Candle returns last object of Candle_1h") # print(f"candle.time={candle.time}, new_candle.time ={new_candle.time}") self.assertEqual(candle, new_candle)
def test_CreateCandleWithDuration(self): """[summary] create new candle data of datetime.datetime.now(new_candle). CreateCandleWithDuration(duration,now) makes new_candles to another candle(after_candle). They have same primaly key, so they are equal but not is. """ duration = "h" api_key = key.api_key api_secret = key.api_secret c = get_data.Candle(api_key, api_secret) now_time = c.TruncateDateTime(duration=duration, time=datetime.datetime.now()) new_candle = Candle_1h( time=now_time, open=4000000, close=3000000, high=4100000, low=3000000, volume=10000) new_candle.save() c.CreateCandleWithDuration(duration=duration, time=now_time) after_candle = Candle_1h.objects.last() self.assertEqual(new_candle, after_candle) self.assertIsNot(new_candle, after_candle)