|
本帖最後由 GnuHomot 於 11-9-19 09:39 PM 編輯
實作完應該是沒問題了,其實還蠻興奮的,因為這個問題困擾我很久了。結果就直接回覆在這篇。菜鳥的程式碼也沒什麼好保留的,直接貼上來了。
我試寫了幾種資金控管的模式,都是參考藍色投機客的文章,請先閱讀
http://tw.myblog.yahoo.com/Blue-Speculator/article?mid=1350&prev=1667&next=1233&l=f&fid=17
以下程式碼如果看不懂,請先把上一篇我貼文的連結PDF看懂,主要是用到mid-level的CustomBacktest
還有有些參數值只是當下測試時隨便設定的,不用太在意。
- Initial=500000;
- SetOption("InitialEquity", Initial);
- ieq=GetOption("InitialEquity");
- InitialShares=1;
- SetPositionSize(InitialShares, spsShares);
- HighLowPeriod=15;
- //HighLowPeriod=Optimize("HighLow Period", 15, 1, 120, 1);
- HighValue=Ref(HHV(H, HighLowPeriod),-1);
- LowValue=Ref(LLV(L, HighLowPeriod),-1);
- Plot(HighValue,"High Value", colorGreen, styleLine);
- Plot(LowValue,"Low Value", colorGreen, styleLine);
- Buy=Sell=Short=Cover=0;
- TrendIndex=0;
- for(i=0; i<BarCount; i++)
- {
- if(TrendIndex<1)
- if(C[i]>HighValue[i])
- {
- Buy[i]=Cover[i]=1;
- TrendIndex=1;
- }
- if(TrendIndex>-1)
- if(C[i]<LowValue[i])
- {
- Short[i]=Sell[i]=1;
- TrendIndex=-1;
- }
- }
- FixDollar=500000;
- Deposit=100000;
- SetCustomBacktestProc("");
- if(Status("action")==actionPortfolio)
- {
-
- bo=GetBacktesterObject();
- bo.Preprocess();
- for(bar=0; bar<BarCount; bar++)
- {
- CurrentEquity=bo.Equity;
- for(sig=bo.GetFirstSignal(bar); sig; sig=bo.GetNextSignal(bar) )
- {
-
- method=0;//change this value to decide which method you want to use
- switch(method)
- {
- case 0: //Position Sizing Method: Fix Size
- shares=1;
- break;
- case 1: //Position Sizing Method: Fix Dollar Amout of Equity
- if( floor(CurrentEquity/FixDollar)>0)
- sig.PosSize=Deposit*floor(CurrentEquity/FixDollar);
- break;
- case 2: //Position Sizing Method: Kelly Formula
- WL=160946/65300;// win average per trade / loss average per trade
- Pw=0.4839;// win numbers/ trade numbers
- FK = ((WL + 1) * Pw - 1 ) / WL;
- sig.PosSize=CurrentEquity*FK;
- break;
- case 3: //Position Sizing Method: Fixed Risk / Fixed Fractional
- FR=0.5;
- Maxdrawdown=400000;//for reference
- if(floor(CurrentEquity*FR/Maxdrawdown)>0)
- sig.PosSize=floor(CurrentEquity*FR/Maxdrawdown)*Deposit;
- break;
- case 4://Show Hand
- sig.PosSize=CurrentEquity;
- break;
- }
-
- }
- bo.ProcessTradeSignals(bar);
- }
- bo.PostProcess();
- }
- SignalColor=Flip(Buy,Short);
- PlotOHLC(O,H,L,C,"Trend",IIf(SignalColor, colorRed, colorGreen), styleCandle);
- PlotShapes(Buy * shapeSmallUpTriangle ,colorRed, 0,Low,-50);
- //PlotShapes(Sell * shapeHollowSmallDownTriangle ,colorDarkRed, 0,High,-45);
- PlotShapes(Short * shapeSmallDownTriangle ,colorGreen, 0,Low,-50);
- //PlotShapes(Cover * shapeHollowSmallUpTriangle ,colorDarkGreen, 0,High,-45);
- dist=1.5*ATR(10);
- for( i = 1; i < BarCount; i++ )
- {
- if( Buy[i-1] ) PlotText( "Buy\n@" + BuyPrice[ i-1 ], i, Low[ i ]-dist[i], colorRed );
- //if(NOT Short[i-1])
- //if( Sell[i-1] ) PlotText( "Sell\n@" + SellPrice[ i-1 ], i, High[ i ]+dist[i], colorDarkRed );
- //if(NOT Buy[i-1])
- //if( Cover[i-1] ) PlotText( "Cover\n@" + CoverPrice[ i-1 ], i, Low[ i ]-dist[i], colorDarkGreen );
- if( Short[i-1] ) PlotText( "Short\n@" + ShortPrice[ i-1 ], i, High[ i ]+dist[i], colorGreen );
- }
- _SECTION_BEGIN("Price");
- SetChartOptions(0,chartShowArrows|chartShowDates);
- _N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
- Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
- _SECTION_END();
複製代碼 |
評分
-
查看全部評分
|