python">
import warnings
warnings. filterwarnings( "ignore" )
import rqdatac as rq from typing import List
import pandas as pd
import numpy as np
import re
from datetime import datetime, timedelta, time
from dateutil. relativedelta import relativedelta
from vnsw. alpha_research. data_center import DataCenter
from vnpy. trader. constant import Exchange, Interval
from vnpy. trader. setting import SETTINGS
rq. init( username= SETTINGS[ "datafeed.username" ] , password= SETTINGS[ "datafeed.password" ] ) def to_clickhouse ( dc, table_name, df) : SAVE_REFERENCE_QUERY = "INSERT INTO {table} VALUES" query = SAVE_REFERENCE_QUERY. format ( table= table_name) update = datetime. now( ) df[ "update" ] = updatetry : dc. client. insert_dataframe( query, df) print ( '已存储' , set ( df[ 'symbol' ] ) , min ( df[ 'interval' ] ) , min ( df[ 'datetime' ] ) , '至' , max ( df[ 'datetime' ] ) ) except Exception as e: print ( "报错" , e) def load_rq_data ( symbols: List[ str ] , exchange: Exchange, start_date: datetime, end_date: datetime, frequency: str ) - > pd. DataFrame: if frequency== '1d' or frequency== '1m' : df= rq. get_price( symbols, start_date, end_date, frequency, expect_df= True ) . reset_index( ) else : df= pd. DataFrame( ) start_date= start_date. replace( hour= 0 , minute= 0 , second= 0 , microsecond= 0 ) while start_date< end_date: df2= rq. get_price( symbols, start_date, start_date, frequency, expect_df= True ) if df2 is None : start_date+= relativedelta( days= 1 ) continue elif len ( df2) : df= pd. concat( [ df, df2. reset_index( ) ] ) start_date+= relativedelta( days= 1 ) if max ( df[ 'datetime' ] ) . time( ) < time( 15 , 15 ) : df. drop( df[ df[ 'datetime' ] == max ( df[ 'datetime' ] ) ] . index, inplace= True ) df. rename( columns= { 'order_book_id' : 'symbol' , 'date' : 'datetime' , 'close' : 'close_price' , 'open' : 'open_price' , 'high' : 'high_price' , 'low' : 'low_price' , 'volume' : 'volume' , 'open_interest' : 'open_interest' ,