策略框架
为了降低耦合度,策略以如下模板生成动态库,供交易平台动态调用。
#ifndef MA_STRATEGY_DLL_EXPORTS #define MA_STRATEGY_DLL_EXPORTS #endif #ifdef MA_STRATEGY_DLL_EXPORTS #define MA_STRATEGY_DLL_API __declspec(dllexport) #else #define MA_STRATEGY_DLL_API __declspec(dllimport) #endif #include "StdAfx.h" extern "C"{ MA_STRATEGY_DLL_API void global_Init(); MA_STRATEGY_DLL_API void* createStraregy(string strCode); MA_STRATEGY_DLL_API void setPosition(void* pObj, string szKey, PositionDetail* plstPos,list<TradeListSig>* plstTrade); MA_STRATEGY_DLL_API map<string, parasStruct>* Init(void* pObj); MA_STRATEGY_DLL_API void getPara(void* pObj, vector<string> &vecPara); MA_STRATEGY_DLL_API void setTick(void* pObj, CThostFtdcDepthMarketDataField* pTick); MA_STRATEGY_DLL_API void setKline(void* pObj, vector<list<STRUCT_KLINE>*>& vecKLine); MA_STRATEGY_DLL_API map<string,list<double>>* getOtherIndex(void* pObj); MA_STRATEGY_DLL_API RSP_SIGNAL execStrategy(void* pObj, string szCode,string szCodeTd,vector<STRUCT_RSP_SIGNAL> &vecSigSet); };
底层已经有大量的指标函数了,实时写策略只需要实现该函数即可。
bool Strategy::doSinger(string szCode,string szCodeTd){ //指标计算 RSP_SIGNAL ret = none; int size1=MiniKline->size(); int size2=DayKline->size(); if (size1<=1500||size2<=10) return false; list<STRUCT_KLINE>::reverse_iterator iterDay,iterMin; iterDay=DayKline->rbegin(); iterMin=MiniKline->rbegin(); bool isFirstKline=isFirstK(iterMin); if (isFirstKline) { iterDay++; } double todayOpen=iterDay->dOpen; //开盘价 iterDay++; mStruck.lastClose=iterDay->dClose; mStruck.last2=(iterDay->dLow+iterDay->dHigh)/2; int index=0; double avg10=0.f; double d3High=0.f,d3Low=9999999.f,d3HighClose=0.f,d3LowClose=9999999.f; for (;iterDay!=DayKline->rend();iterDay++) { if (index<3) { d3High=iterDay->dHigh>d3High?iterDay->dHigh:d3High; d3HighClose=iterDay->dClose>d3HighClose?iterDay->dClose:d3HighClose; d3Low=iterDay->dLow<d3Low?iterDay->dLow:d3Low; d3LowClose=iterDay->dClose<d3LowClose?iterDay->dClose:d3LowClose; } if (index==4) { mStruck.lastClose5=iterDay->dClose; } if (index<10) { avg10+=iterDay->dClose; } if (index>=10) break; index++; } mStruck.up=todayOpen+max(d3High-d3LowClose,d3HighClose-d3Low)*0.4; mStruck.down=todayOpen-max(d3High-d3LowClose,d3HighClose-d3Low)*0.4; mStruck.c_ref300=util.REFCLOSE(MiniKline,300); mStruck.boll20_ref1=mStruck.boll20; util.BOLL(MiniKline,20,2,mStruck.boll20); mStruck.ma60=util.MA(MiniKline,60); mStruck.ma120=util2.MA(MiniKline,120); mStruck.count15=util.RSCOUNT2(MiniKline,15); mStruck.count50=util2.RSCOUNT2(MiniKline,50); mStruck.c15=util3.RSCOUNT(MiniKline,15); mStruck.boll200_ref1=mStruck.boll200; util.BOLL(MiniKline,200,2,mStruck.boll200); mStruck.ar26=util.AR(MiniKline,26); util.DMI(MiniKline,280,6,mStruck.dmi280); util2.DMI(MiniKline,70,6,mStruck.dmi70); return true; }
该策略模板只适用于cta策略,一些高频或者特别思路的还需要写代码实现。
以后也可以考虑用python实现,不过主要重心还在交易上面,暂时先放着吧。