def run(cls, start_date, market_code, count=200): is_reach_end_date = False while True: time.sleep(0.11) if start_date.shift(days=count) >= arrow.now().to('local'): is_reach_end_date = True querystring = { 'market': market_code, 'count': count, 'to': start_date.shift(days=count).format('YYYY-MM-DD HH:mm:ss') } url = cls.day_endpoint response = requests.request("GET", url, params=querystring) res_json = response.json() start_date = start_date.shift(days=count) operations = [] for item in res_json: candle_date_time = item['candle_date_time_kst'] candle_date = candle_date_time.split('T')[0] candle_time = candle_date_time.split('T')[1] candle_year = candle_date.split('-')[0] candle_month = candle_date.split('-')[1] candle_day = candle_date.split('-')[2] candle_hour = candle_time.split(':')[0] candle_minute = candle_time.split(':')[1] open_price = item['opening_price'] close_price = item['trade_price'] high_price = item['high_price'] low_price = item['low_price'] candle = Candle( code=market_code, date_time= f'{candle_year}{candle_month}{candle_day}{candle_hour}{candle_minute}', minute=f'{1440}', open_price=open_price, close_price=close_price, high_price=high_price, low_price=low_price, created_at=arrow.now().to('local')) operations.append(InsertOne(candle.to_mongo())) try: Candle._get_collection().bulk_write(operations, ordered=False) except BulkWriteError as e: pass if is_reach_end_date: break
def _get_latest_candle_rsi(cls, market_code, target_candle_minute): candles = Candle.objects(code=market_code, minute=f'{target_candle_minute}').order_by('-date_time')[:100] close_prices = [float(candle.close_price) for candle in candles] close_prices = np.asarray(close_prices)[::-1] rsi_df = cls._create_rsi_df(close_prices) last_df_row = rsi_df.tail(1) return last_df_row['rsi'].values[0]
def create(cls, market_code, current_time, minute): ClosePriceUpdater.update(market_code=market_code, minute=minute, close_price=current_price[market_code]) minute_candle_price = candle_price[market_code][minute] if Candle.objects(code=market_code, date_time=f'{current_time.year:04}{current_time.month:02}{current_time.day:02}{current_time.hour:02}{current_time.minute:02}', minute=minute): Candle.objects(code=market_code, date_time=f'{current_time.year:04}{current_time.month:02}{current_time.day:02}{current_time.hour:02}{current_time.minute:02}', minute=minute).update(open_price=minute_candle_price['open_price'], close_price=minute_candle_price['close_price'], high_price=minute_candle_price['high_price'], low_price=minute_candle_price['low_price'], created_at=arrow.now()) else: Candle(code=market_code, date_time=f'{current_time.year:04}{current_time.month:02}{current_time.day:02}{current_time.hour:02}{current_time.minute:02}', minute=minute, open_price=minute_candle_price['open_price'], close_price=minute_candle_price['close_price'], high_price=minute_candle_price['high_price'], low_price=minute_candle_price['low_price'], created_at=arrow.now()).save() OpenPriceUpdater.update(market_code=market_code, minute=minute, open_price=minute_candle_price['close_price']) ClosePriceUpdater.update(market_code=market_code, minute=minute, close_price=minute_candle_price['close_price']) LowPriceUpdater.update(market_code=market_code, minute=minute, low_price=minute_candle_price['close_price']) HighPriceUpdater.update(market_code=market_code, minute=minute, high_price=minute_candle_price['close_price'])
def _collect_recently_minute_candle(cls, code, minute, count): querystring = {"market": code, 'count': count} url = cls.minute_endpoint + f'{minute}' response = requests.request("GET", url, params=querystring) #remain_request_at_minute = int(response.headers['Remaining-Req'].split(';')[2].strip().split('=')[1]) #remain_request_at_second = int(response.headers['Remaining-Req'].split(';')[1].strip().split('=')[1]) res_json = response.json() operations = [] for item in res_json: candle_date_time = item['candle_date_time_kst'] candle_date = candle_date_time.split('T')[0] candle_time = candle_date_time.split('T')[1] candle_year = candle_date.split('-')[0] candle_month = candle_date.split('-')[1] candle_day = candle_date.split('-')[2] candle_hour = candle_time.split(':')[0] candle_minute = candle_time.split(':')[1] open_price = item['opening_price'] close_price = item['trade_price'] high_price = item['high_price'] low_price = item['low_price'] candle = Candle(code=code, date_time=f'{candle_year}{candle_month}{candle_day}{candle_hour}{candle_minute}', minute=f'{minute}', open_price=open_price, close_price=close_price, high_price=high_price, low_price=low_price, created_at=arrow.now().to('local')) operations.append(InsertOne(candle.to_mongo())) try: Candle._get_collection().bulk_write(operations, ordered=False) except BulkWriteError as e: pass latest_candle = Candle.objects(minute=f'{minute}').order_by('-date_time').first() OpenPriceUpdater.update(market_code=code, minute=f'{minute}', open_price=latest_candle.open_price) ClosePriceUpdater.update(market_code=code, minute=f'{minute}', close_price=latest_candle.close_price) HighPriceUpdater.update(market_code=code, minute=f'{minute}', high_price=latest_candle.high_price) LowPriceUpdater.update(market_code=code, minute=f'{minute}', low_price=latest_candle.low_price)
def load(cls, market_code, target_minute, start_date, end_date): candles = Candle.objects(code=market_code, minute=target_minute, date_time__gt=start_date, date_time__lt=end_date).order_by('date_time').all() return candles