求助天神,QA_RISK 的 初始金额为什么不对?
发布于 1 个月前 作者 uglylemon 91 次浏览 来自 代码实现
#沪深300 399300
#中证500 000905
#中证1000 000852
#创业板指 399006
#【买入条件】(两个条件全部满足才买入)
#1、近N个交易日涨幅排名第一(不管涨幅的正负)。
#2、当前价大于近M个交易日均线。


#【卖出条件】(两个条件满足一个就卖出)
#1、近N个交易日涨幅排名不是第一。
#2、当前价小于近M个交易日均线

import QUANTAXIS as QA
import pandas as pd
import matplotlib.pyplot as plt


data = QA.QA_fetch_index_day_adv(['399300','000905','000852','399006'],'2005-01-01','2019-10-19')
# data = QA.QA_fetch_stock_day_adv(['000001','000002','002027'],'2018-01-01','2019-10-22')
def strategylundong(data,N=13,M=13):
    CLOSE = data.close
    zhangfu = (CLOSE-QA.REF(CLOSE,N))/QA.REF(CLOSE,N)
    ma = QA.MA(CLOSE,M)
    return pd.DataFrame({'close':CLOSE,'涨幅':zhangfu,'MA':ma})

user = QA.QA_User(username='quantaxis', password='quantaxis')
portfolio = user.new_portfolio('strategylm')
Broker = QA.QA_BacktestBroker()
acc = portfolio.new_account(account_cookie='acc001', init_cash=10000000,
                                        market_type=QA.MARKET_TYPE.STOCK_CN,commission_coeff=0.00015)
ind=data.add_func(strategylundong)
ind= ind.dropna()


for items in data.panel_gen:
    # print(items)
    today_time = items.index[0][0]
#     print(today_time)
    try:
        one_day_data = ind.loc[today_time]  
        one_day_data['date'] = today_time
        one_day_data.reset_index(inplace=True)
        one_day_data.sort_values(by='涨幅', axis=0, ascending=False, inplace=True)
        today_buy_stock = one_day_data.iloc[0]['code'] if one_day_data.iloc[0]['close'] > one_day_data.iloc[0]['MA'] else ''
        
        today_sell_stock_list = list(one_day_data.iloc[1:]['code'])
#         print(today_sell_stock_list)
        if one_day_data.iloc[0]['close'] < one_day_data.iloc[0]['MA']:
            today_sell_stock_list.append(one_day_data.iloc[0]['code'])
#             print(today_sell_stock_list)
        one_day_data.set_index(['date', 'code'], inplace=True)
        one_day_data = QA.QA_DataStruct_Stock_day(one_day_data)
#         print('SELL:')
        for today_sell_stock in today_sell_stock_list:
            sell_code = today_sell_stock
            item = data.selects(sell_code,today_time)
            if acc.sell_available.get(sell_code, 0) > 0:
#                 print('sell order')
                order = acc.send_order(
                    code=sell_code,
                    time=today_time,
                    amount=int(acc.sell_available.get(sell_code, 0)),
                    towards=QA.ORDER_DIRECTION.SELL,
                    price=item.close,
                    order_model=QA.ORDER_MODEL.CLOSE,
                    amount_model=QA.AMOUNT_MODEL.BY_AMOUNT
                )
                if order:
                    Broker.receive_order(QA.QA_Event(
                        order=order, market_data=item))
                    trade_mes = Broker.query_orders(
                        acc.account_cookie, 'filled')
                    res = trade_mes.loc[order.account_cookie,
                                    order.realorder_id]
                    print('卖出order {} {} {} {} 共 {}元'.format(
                        res.trade_id, res.trade_price, res.trade_amount, res.trade_time,res.trade_price * res.trade_amount))
                    order.trade(res.trade_id, res.trade_price,
                                res.trade_amount, res.trade_time)
            
#         print('BUY:')
        if today_buy_stock:
            buy_code = today_buy_stock
            item = data.selects(buy_code,today_time)
#             print('lmdbg')
#             print(item.close[0])
            if acc.sell_available.get(buy_code, 0) > 0:
                print('持有')
            else:
#                 print('buy order')
#                 print(buy_code)
#                 print(price=item.data)
                order = acc.send_order(
                    code=buy_code,
                    time=today_time,
                    money=int(acc.cash_available),
                    towards=QA.ORDER_DIRECTION.BUY,
                    price=item.close[0],
                    order_model=QA.ORDER_MODEL.CLOSE,
                    amount_model=QA.AMOUNT_MODEL.BY_MONEY
                )
                if order:
                    
                    Broker.receive_order(QA.QA_Event(
                        order=order, market_data=item))
#                     print('buy order')
                    trade_mes = Broker.query_orders(
                        acc.account_cookie, 'filled')
                    res = trade_mes.loc[order.account_cookie,
                                    order.realorder_id]
                    print('买入order {} {} {} {} 共 {}元'.format(
                        res.trade_id, res.trade_price, res.trade_amount, res.trade_time,res.trade_price * res.trade_amount))
                    order.trade(res.trade_id, res.trade_price,
                                res.trade_amount, res.trade_time)                   
    except:
        pass


    acc.settle()
    
Risk = QA.QA_Risk(acc, benchmark_code='000300', benchmark_type='index_cn', if_fq=True)

Risk.plot_assets_curve()  # 总资产
plt.show()
Risk.benchmark_assets.plot()  # 基准收益的资产
plt.show()
acc.save()
Risk.save()

1.png 2.png

4 回复

斗胆回帖,应该是同个account多次回测,初始资金是上次回测的结余

感觉是对指数回测不太完善造成的

卧槽? 这是什么…

为什么是image.png这样的?

回到顶部