test

作者: hopless | 来源:发表于2019-08-22 11:36 被阅读0次
    def save_data1(df,time_id):
        database_name = 'is_data_test'
        df.open.rename(time_id).to_frame().T.to_hdf(database_name,mode='a',append=True,key='open')
        df.close.rename(time_id).to_frame().T.to_hdf(database_name,mode='a',append=True,key='close')
        df.high.rename(time_id).to_frame().T.to_hdf(database_name,mode='a',append=True,key='high')
        df.low.rename(time_id).to_frame().T.to_hdf(database_name,mode='a',append=True,key='low')
        df.vol.rename(time_id).to_frame().T.to_hdf(database_name,mode='a',append=True,key='vol')
        
    def save_data(df,time_id):
        database_name = 'is_data_test'
        for i in ['open','high','low','close','vol']:
            df[i].rename(time_id).to_frame().T.to_hdf(database_name,mode='a',append=True,key= i )
    
    begin = 1
    data_old = pd.DataFrame()
    while begin:
        sleep(1)
        try:
            login_info = Solution.login()
            session_key = login_info.session_key
            login_info.success
            begin = 0
        except:
            continue
    all_data = []
    pnl_data = []
    old_seq = -1
    print('begin save data')
    while 1:
        try:
            get_data = Solution.question()
        except:
            sleep(0.5)
            continue
        current_sequence = get_data.sequence
        if current_sequence == old_seq:
            sleep(0.5)
            continue
        data = pd.DataFrame([x.values for x in get_data.dailystk],columns=['sequence_id','code','open','high','low','close','vol'])
        save_data(data,current_sequence)
        if data_old.shape[0] ==0:
            data_old = data
        all_data.append(data)
        pnl = get_data.capital
        print('pnl:',pnl,'time:',current_sequence)
        pnl_data.append((current_sequence,pnl))
    #     pd.DataFrame(pnl_data).to_pickle('pnl_int_1.pkl')
        ## alpha
        if current_sequence > 0:
    #         alpha1 = ((data['vol']-data_old['vol'])/data_old['vol']).rank()
            alpha1 = -data['vol']
            alpha2 = (data['close']-data_old['close'])/data_old['close']
            positions = factor_scale(alpha1) + factor_scale(alpha2)
    #         positions = alpha1.pct_change().iloc[current_sequence+1]
            positions = (positions - positions.mean())
            positions = positions/abs(positions).sum()
            positions = np.array([ max(-0.08,min(x,0.08)) for x in positions])
            pos_vol = pnl*positions/(data['close'])*0.9
            pos_vol = [int(x) for x in pos_vol]
            msg = Solution.submit_answer(session_key,current_sequence,pos_vol)
            print(msg)
        data_old = data
        ##
        old_seq = current_sequence
        if current_sequence == 599:
            break
        sleep(0.5)
    

    相关文章

      网友评论

          本文标题:test

          本文链接:https://www.haomeiwen.com/subject/bmfnsctx.html