yutiansut / QUANTAXIS

QUANTAXIS 支持任务调度 分布式部署的 股票/期货/期权 数据/回测/模拟/交易/可视化/多账户 纯本地量化解决方案
https://yutiansut.github.io/QUANTAXIS/
MIT License
8.25k stars 2.97k forks source link

一次性全市场回测报错问题和portfolio使用方法的咨询 #969

Open Wier123 opened 5 years ago

Wier123 commented 5 years ago

问题反馈

第一个问题 .一次性全市场回测报错问题: 回测时,在backtest_code_list = 里导入全部深市沪市的一千多个股票时必定报错: 03990', '603991', '603993', '603996', '603997', '603998', '603999', '603348', '603596', '603733', '603773', '603876'] , start=2013-01-01, end=2018-10-01 call QA_fetch_stock_day return None Traceback (most recent call last): File "new_backtest_core_biz.py", line 127, in backtest_func(AC, backtest_code_list, backtest_start_date, backtest_end_date) File "new_backtest_core_biz.py", line 19, in backtest_func DATA = QA.QA_fetch_stock_day_adv(code, start, end).to_qfq() AttributeError: 'NoneType' object has no attribute 'to_qfq' PS D:\quant_strategy\QADEMO\QADemo-master\backtest\example>

但只要把同样这些股票分成小部分(每部分几百个股票吧)来回测就完全没问题。请问是我机能有限?还是?QA的这个某个对象空间有限?(后面会贴回测配置信息)

第二个问题: 群里讨论上个问题的时候说到portofolio和portfolioView 两种模式 我要的模式就是最贴近实际小散的实盘模式。回测时也就是一个资金账户,用一个策略backtest_code_list里的所有股票都测试。每一天看哪个股票符合策略要求了就买入(不管有几个),每一天看哪个股票符合策略的退出要求了就退出(不管几个)。最后回测完在看这个账户的情况。和pnl。 我目前的配置应该就是吧?还是说

组合cookie

portfolio_cookie = 'win300' 这里要为none?

以下为 配置信息: import math import csv import QUANTAXIS as QA import time from core_biz import * from QUANTAXIS.QAApplication.QAAnalysis import QA_backtest_analysis_backtest

def backtest_func(AC:QA.QA_Account, code, start:str, end:str): ''' :param AC: QA_Account :param code: 股票代码,字符串或列表 :param start: 回测开始时间字符串 :param end: 回测结束时间字符串 '''

取数并前复权

DATA = QA.QA_fetch_stock_day_adv(code, start, end).to_qfq()

map_strategy = {}
map_action = {}
map_do_sell = {}
map_do_buy = {}

for code in backtest_code_list:
    map_strategy[code] = Strategy()
    map_action[code] = Action(0)
    map_do_buy[code] = False
    map_do_sell[code] = False

for peroid_item in DATA.panel_gen:
    for stock_item in peroid_item.security_gen:
        code = stock_item.code[0]
        date = stock_item.date[0]
        price_close = stock_item.close[0]
        day_vol = stock_item.volume[0]
        price_open = stock_item.open[0]
        sell_available_amount = AC.sell_available.get(code, 0)

        order = None
        if map_do_sell[code]:
            map_do_sell[code] = False
            if sell_available_amount <= 0:
                print("nothing to sell for:", code)
            else:
                print("-------->>>>>>>>>>>>------", date, "--------<<<<<<<<<<<<<<------")
                print('CODE:', code, "SELL", " amount: ", map_action[code].amount)
                order_data = {}
                order_data['bid_model'] = 1

                order = AC.send_order(
                code=code, 
                time=date, 
                towards=map_action[code].towards,
                order_model=QA.ORDER_MODEL.MARKET, 
                amount_model=QA.AMOUNT_MODEL.BY_AMOUNT,
                amount=map_action[code].amount,
                price=0)

        if map_do_buy[code]:
            map_do_buy[code] = False
            print("-------->>>>>>>>>>>>------", date, "--------<<<<<<<<<<<<<<------")
            print('CODE:', code, "BUY", " amount: ", map_action[code].amount)
            order = {}
            order['bid_model'] = 1

            order = AC.send_order(
            code=code, 
            time=date, 
            towards=map_action[code].towards,
            order_model=QA.ORDER_MODEL.MARKET,
            amount_model=QA.AMOUNT_MODEL.BY_AMOUNT,
            amount=map_action[code].amount,
            price=price_open)

        if order is not None and order:
            Broker.receive_order(QA.QA_Event(order=order, market_data=stock_item))
            trade_mes = Broker.query_orders(AC.account_cookie, 'filled')
            res = trade_mes.loc[order.account_cookie, order.realorder_id]
            order.trade(res.trade_id, res.trade_price,
            res.trade_amount, res.trade_time)

        (map_action[code], status) = map_strategy[code].updateByToDayClose(price_close, day_vol)
        print(date, code, price_close)
        if map_action[code].towards != 0:
            if map_action[code].towards == -1:
                map_do_sell[code] = True
            elif map_action[code].towards == 1:
                map_do_buy[code] = True

    # 当天结算
    AC.settle()

if name == 'main':

策略名称

strategy_name = 'default'
# 用户cookie
user_cookie = 'user1'
# 组合cookie
portfolio_cookie = 'win300'
# 账户cookie
account_cookie = 'bba'
benchmark_code = '000300'
initial_cash = 200000
initial_hold = {}
commission_coeff = 0.00025 # 交易佣金
tax_coeff = 0.0015# 印花税
backtest_code_list = ['600000','600004','600006','600007','600008','600009','600010','600011','600012','600015','600016','600017','600018','600019','600020','600021','600022','600023','600025','600026','600027','600028','600029','600030','600031','600033','600035','600036','600037','600038','600039','600048','600050','600051','600052','600053','600054','600055','600056','600057','600058','600059','600060','600061','600062','600063','600064','600066','600067','600068','600069','600070','600071','600072','600073','600074','600075','600076','600077','600078','600079','600080','600081','600082','600083','600084','600085','600086','600088','600089','600090','600091','600093','600094','600095','600096','600097','600098','600099','600100','600101','600103','600104','600105','600106','600107','600108','600109','600110','600111','600112','600113','600114','600115','600116','600117','600118','600119','600120','600121','600122','600123','600125','600126','600127','600128','600129','600130','600131','600132','600133','600136','600137','600138','600139','600141','600143','600145','600146','600148','600149','600150','600151','600152','600153','600155','600156','600158','600159','600160','600161','600162','600163','600165','600166','600167','600168','600169','600170','600171','600172','600173','600175','600176','600177','600178','600179','600180','600182','600183','600184','600185','600186','600187','600188','600189','600190','600191','600192','600193','600195','600196','600197','600198','600199','600200','600201','600202','600203','600206','600207','600208','600209','600210','600211','600212','600213','600215','600216','600217','600218','600219','600220','600221','600222','600223','600225','600226','600227','600228','600229','600230','600231','600232','600233','600234','600235','600236','600237','600238','600239','600240','600241','600242','600243','600247','600248','600249','600250','600251','600252','600255','600256','600257','600258','600259','600260','600261','600262','600265','600266','600267','600268','600269','600270','600271','600272','600273','600275','600276','600277','600278','600279','600280','600281','600282','600283','600284','600287','600288','600289','600290','600291','600292','600293','600295','600297','600298','600299','600300','600301','600302','600303','600305','600306','600307','600308','600309','600310','600311','600312','600313','600315','600316','600317','600318','600319','600320','600321','600322','600323','600325','600326','600327','600328','600329','600330','600331','600332','600333','600335','600336','600337','600338','600339','600340','600343','600345','600346','600348','600350','600351','600352','600353','600354','600355','600356','600358','600359','600360','600361','600362','600363','600365','600366','600367','600368','600369','600370','600371','600372','600373','600375','600376','600377','600378','600379','600380','600381','600382','600383','600385','600386','600387','600388','600389','600390','600391','600392','600393','600395','600396','600397','600398','600399','600400','600403','600405','600406','600408','600409','600410','600415','600416','600418','600419','600420','600421','600422','600423','600425','600426','600428','600429','600433','600435','600436','600439','600444','600446','600448','600449','600452','600455','600456','600458','600459','600460','600461','600462','600463','600466','600467','600468','600469','600470','600475','600476','600477','600478','600479','600480','600481','600482','600483','600485','600486','600487','600488','600489','600491','600493','600495','600496','600497','600498','600499','600500','600501','600502','600503','600505','600506','600507','600508','600509','600510','600511','600512','600513','600515','600516','600517','600518','600519','600520','600521','600522','600523','600525','600526','600527','600528','600529','600530','600531','600532','600533','600535','600536','600537','600538','600539','600540','600543','600545','600546','600547','600548','600549','600550','600551','600552','600555','600556','600557','600558','600559','600560','600561','600562','600563','600565','600566','600567','600568','600569','600570','600571','600572','600573','600575','600576','600577','600578','600579','600580','600581','600582','600583','600584','600585','600586','600587','600588','600589','600590','600592','600593','600594','600595','600596','600597','600598','600599','600600','600601','600602','600603','600604','600605','600606','600608','600609','600610','600611','600612','600613','600614','600615','600616','600617','600618','600619','600620','600621','600622','600623','600624','600626','600628','600629','600630','600633','600634','600635','600636','600637','600638','600639','600641','600642','600643','600644','600645','600647','600648','600649','600650','600651','600652','600653','600654','600655','600657','600658','600660','600661','600662','600663','600664','600665','600666','600667','600668','600671','600673','600674','600675','600676','600677','600678','600679','600681','600682','600683','600684','600685','600686','600687','600688','600689','600690','600691','600692','600693','600694','600695','600696','600697','600698','600699','600701','600702','600703','600704','600705','600706','600707','600708','600710','600711','600712','600713','600714','600715','600716','600717','600718','600719','600720','600721','600722','600723','600724','600725','600726','600727','600728','600729','600730','600731','600732','600733','600734','600735','600736','600737','600738','600739','600740','600741','600742','600743','600744','600745','600746','600747','600748','600749','600750','600751','600753','600754','600755','600756','600757','600758','600759','600760','600761','600763','600764','600765','600766','600767','600768','600769','600770','600771','600773','600774','600775','600776','600777','600778','600779','600780','600781','600782','600783','600784','600785','600787','600789','600790','600791','600792','600793','600794','600795','600796','600797','600798','600800','600801','600802','600803','600804','600805','600807','600808','600809','600810','600811','600812','600814','600815','600816','600817','600818','600819','600820','600821','600822','600823','600824','600825','600826','600827','600828','600829','600830','600831','600833','600834','600835','600836','600837','600838','600839','600841','600843','600844','600845','600846','600847','600848','600850','600851','600853','600854','600855','600856','600857','600858','600859','600860','600861','600862','600863','600864','600865','600866','600867','600868','600869','600870','600871','600872','600873','600874','600875','600876','600877','600879','600880','600881','600882','600883','600884','600885','600886','600887','600888','600889','600890','600891','600892','600893','600894','600895','600896','600897','600898','600900','600901','600903','600908','600909','600917','600919','600926','600929','600933','600936','600939','600958','600959','600960','600961','600962','600963','600965','600966','600969','600970','600971','600973','600975','600976','600977','600978','600979','600980','600981','600982','600983','600984','600985','600986','600987','600988','600990','600992','600993','600995','600996','600997','600998','600999','601000','601001','601002','601003','601005','601006','601007','601008','601009','601010','601011','601012','601015','601016','601018','601019','601020','601021','601028','601038','601058','601069','601086','601088','601098','601099','601100','601101','601106','601107','601108','601111','601113','601116','601117','601118','601126','601127','601128','601137','601139','601155','601158','601163','601166','601168','601169','601177','601179','601186','601188','601198','601199','601200','601208','601211','601212','601216','601218','601222','601225','601226','601228','601229','601231','601233','601238','601258','601288','601311','601318','601326','601328','601333','601336','601339','601360','601366','601368','601369','601375','601377','601388','601390','601398','601500','601515','601518','601519','601555','601558','601566','601567','601579','601595','601599','601600','601601','601607','601608','601611','601616','601618','601619','601628','601633','601636','601666','601668','601669','601677','601678','601688','601689','601699','601700','601717','601718','601727','601766','601777','601788','601789','601798','601799','601800','601801','601808','601811','601818','601828','601838','601857','601858','601866','601872','601877','601878','601880','601881','601882','601886','601888','601890','601898','601899','601900','601901','601908','601918','601919','601928','601929','601939','601949','601952','601958','601965','601966','601968','601985','601988','601989','601991','601992','601996','601997','601998','601999','603000','603001','603002','603003','603005','603006','603007','603008','603009','603010','603011','603012','603015','603016','603017','603018','603019','603020','603021','603022','603023','603025','603026','603027','603028','603029','603030','603031','603032','603033','603035','603036','603037','603038','603039','603040','603041','603042','603043','603050','603055','603056','603058','603059','603060','603063','603066','603067','603069','603076','603077','603078','603079','603080','603081','603083','603085','603086','603088','603089','603090','603096','603098','603099','603100','603101','603103','603106','603108','603110','603111','603113','603116','603117','603118','603123','603126','603127','603128','603129','603131','603133','603136','603138','603139','603156','603157','603158','603159','603160','603161','603165','603166','603167','603168','603169','603177','603178','603179','603180','603181','603183','603186','603188','603189','603196','603197','603198','603199','603200','603203','603208','603214','603218','603222','603223','603225','603226','603227','603228','603229','603232','603233','603238','603239','603258','603260','603266','603268','603269','603277','603278','603283','603286','603288','603289','603298','603299','603300','603301','603303','603305','603306','603308','603309','603311','603313','603315','603316','603318','603319','603320','603321','603322','603323','603326','603328','603329','603330','603331','603333','603335','603336','603337','603338','603339','603345','603355','603356','603357','603358','603359','603360','603363','603365','603366','603367','603368','603369','603377','603378','603380','603383','603385','603386','603387','603388','603389','603393','603396','603398','603399','603416','603421','603429','603444','603456','603458','603466','603477','603488','603496','603499','603500','603501','603505','603506','603507','603508','603515','603516','603517','603518','603519','603520','603527','603528','603533','603535','603536','603538','603555','603556','603557','603558','603559','603566','603567','603568','603569','603577','603578','603579','603580','603585','603586','603588','603589','603595','603598','603599','603600','603601','603602','603603','603605','603606','603607','603608','603609','603611','603612','603615','603616','603617','603618','603619','603626','603628','603630','603633','603636','603637','603638','603639','603648','603655','603656','603658','603659','603660','603661','603663','603665','603667','603668','603669','603676','603677','603678','603679','603680','603683','603685','603686','603688','603689','603690','603696','603698','603699','603701','603703','603707','603708','603709','603711','603712','603716','603717','603718','603721','603722','603725','603726','603727','603728','603729','603730','603737','603738','603757','603758','603766','603767','603768','603776','603777','603778','603779','603787','603788','603789','603797','603798','603799','603800','603801','603803','603806','603808','603809','603811','603813','603816','603817','603818','603819','603822','603823','603825','603826','603828','603829','603833','603838','603839','603843','603848','603855','603856','603858','603859','603860','603861','603866','603868','603869','603871','603877','603878','603879','603880','603881','603882','603883','603885','603886','603887','603888','603889','603890','603895','603896','603897','603898','603899','603900','603901','603903','603906','603908','603909','603912','603916','603917','603918','603919','603920','603922','603926','603928','603929','603933','603936','603937','603938','603939','603955','603958','603959','603960','603963','603966','603968','603969','603970','603976','603977','603978','603979','603980','603985','603986','603987','603988','603989','603990','603991','603993','603996','603997','603998','603999','603348','603596','603733','603773','603876']
backtest_start_date = '2013-01-01'
backtest_end_date = '2018-10-01'
Broker = QA.QA_BacktestBroker()
AC = QA.QA_Account(
    strategy_name=strategy_name,
    user_cookie=user_cookie,
    portfolio_cookie=portfolio_cookie,
    account_cookie=account_cookie,
    init_hold=initial_hold,
    init_cash=initial_cash,
    commission_coeff=commission_coeff,
    tax_coeff=tax_coeff,
    market_type=QA.MARKET_TYPE.STOCK_CN,
    frequence=QA.FREQUENCE.DAY
    )
backtest_func(AC, backtest_code_list, backtest_start_date, backtest_end_date)

print('-------------------------done---------------------')
print(AC.message)
print(AC.history_table)

performance=QA.QA_Performance(AC)
print(performance.pnl_fifo)

performance.pnl_fifo.to_csv('./result.csv')

__file_name_2 = 'D:\\quant_strategy\QUANTAXIS_RESULT\pnl-' + strategy_name + str(time.time()) + '.csv'
performance.pnl_fifo.to_csv(__file_name_2)

#评价csv

您使用的QUANTAXIS版本号是什么?

您的系统信息(包括系统版本,系统架构(32/64),内存大小等等)

x64   8g

您的系统环境是什么?

- monodb
- nodejs(V9以下版本)
- python3.5/3.6
- docker(无)

您需要反馈的问题

感谢您的反馈 如有意见和建议,请在此说明:

您的联系方式(QQ/EMAIL)

QQ.87532775

THANKS by yutiansut 2017.12

Wier123 commented 5 years ago

关于portofolio和portfolioView 两种模式的文档 https://book.yutiansut.com/Documents/QUANTAXIS%E5%9B%9E%E6%B5%8B%E5%88%86%E6%9E%90%E5%85%A8%E8%BF%87%E7%A8%8B%E8%AE%B2%E8%A7%A3.html 我看了也找了。代码里的account.md readme.me。。。没看到具体说明。。但我是不要组合的方法。应该就是这样吧?

yutiansut commented 5 years ago

欸? 我才看到 你记得at我下 @Wier123 类似这样 我研究下

Wier123 commented 5 years ago

哦哦还可以at的啊。。@yutiansut。ok。你看看是什么问题。谢谢。

zhangshoug commented 5 years ago

@Wier123 我要的模式就是最贴近实际小散的实盘模式。回测时也就是一个资金账户,用一个策略backtest_code_list里的所有股票都测试。每一天看哪个股票符合策略要求了就买入(不管有几个),每一天看哪个股票符合策略的退出要求了就退出(不管几个)。 你这个实现了吗? 我的实盘模式与你这个有点小差别: 1、我每天更新backtest_code_list,每天根据设定的条件往里面加股票和清除股票. 2、每一天看哪个股票符合策略要求了就买入(不管有几个),每一天看哪个股票符合策略的退出要求了就退出(不管几个)。 这个还是要限制买入数量:最多持有几支股。(小散资金有限,不可能无限买) 主要是第一个每天更新backtest_code_list,更新股池。第二个自己打分排序和根据已有持仓实现。 整个过程我已在rqalpha上实现,只用QA还没有实现这样的过程。

yutiansut commented 5 years ago

@zhangshoug 你的backtest_code_list 更新逻辑是要全市场选的吗

选出来以后 account.send_order就可以了