請各位先進開示自己語法上的盲點
我自己寫的內容想做一下回測歐元但跑出來結果有點問題
內容如下
SetPositionSize(1 , spsShares);
SetOption("InitialEquity" , 10000 ); //初始金額 10000
SetOption("MaxOpenPositions" , 2 ); //最大口數 2
SetOption("FuturesMode" , 1 );
SetOption("CommissionMode" , 3 );
SetOption("CommissionAmount" , 6.5 ); //單邊手續費 6.5
RoundLotSize = 1;
TickSize = 0.0001; PS = 10000 ; PointValue = 12.5*PS;
MA20 = MA(C, 20 );//20均
MA60 = MA(C, 60 );//60均
Cross60_Buy =Cross (C, MA60); //K棒向上穿過60均
Cross20_Buy =Cross (C, MA20); //K棒向上穿過20均
Cross60_Sell = Cross (MA60, C); //K棒向下穿過60均
Cross20_Sell = Cross (MA20, C); //K棒向下穿過20均
Buy=Sell=Short=Cover=0; //初始化
BuyIndex = 0;//記錄是否已有多單
ShortIndex = 0; //記錄是否已有空單
for(i = 2 ; i < BarCount ; i++)
{
//當前一根K棒向上穿過60均
//或
//前一根K棒向上穿過20均 且 前一根60均線往上
//做多
if( Cross60_Buy OR ( Cross20_Buy AND MA60 > MA60 ) )
{
//倉位為0
if(BuyIndex == 0 AND ShortIndex == 0)
{
//記錄已有多單
BuyIndex = i;
//做多
Buy = 1 ;
}
}
//有多單時
//且
//前一根K棒向下穿過20均
// 平多單
if(BuyIndex != 0 ANDCross20_Sell)
{
//平倉
Sell = 1;
//多單記錄歸0
BuyIndex = 0;
}
//當前一根K棒向下穿過60均
//或
//前一根K棒向下穿過20均 且 前一根60均線往下
//做空
if(Cross60_Sell OR ( Cross20_Sell AND MA60 < MA60 ) )
{
//倉位為0
if(BuyIndex == 0 AND ShortIndex == 0)
{
//記錄已有空單
ShortIndex = i;
//做空
Short = 1 ;
}
}
//有空單時
//且
//前一根K棒向上穿過20均
// 平空單
if(ShortIndex != 0 AND Cross20_Buy)
{
//平空單
Cover = 1;
//空單記錄歸0
ShortIndex = 0;
}
//當買進訊號出現後濾掉重複的買進訊號直至平倉訊號出現為止
Buy = ExRem(Buy, Sell);
//當賣出訊號出現後濾掉重複的賣出訊號直至平倉訊號出現為止
Short = ExRem(Short, Cover);
//K棒開盤價買入或平空單
BuyPrice = CoverPrice = O;
//K棒開盤價放空或平多單
ShortPrice = SellPrice = O;
}
我自己跑出來的結果如下
setting 設定如下
而我用之前的測試範例是正常
因此應該是程式上的問題
SetPositionSize(1 , spsShares);
SetOption("InitialEquity" , 10000 );
SetOption("MaxOpenPositions", 2);
SetOption("FuturesMode" , 1 );
SetOption("CommissionMode" , 3 );
SetOption("CommissionAmount" , 6.5 );
RoundLotSize = 1;
TickSize = 0.0001; PS = 10000 ; PointValue = 12.5*PS;
Buy = Ref( Cross(C, MA(C,60 )), -1);
Short = Ref( Cross(MA (C, 60), C), -1);
Buy = Cover = ExRem(Buy, Short);
Short = Sell = ExRem(Short, Buy);
BuyPrice = CoverPrice = O ;
ShortPrice = SellPrice = O ;
還請各位先進幫忙解答
1.為何回測結果只有多單而沒有空單
2.
測試範例中並沒有迴圈
但在回測時會從頭到尾一根根的K棒去計算
然後以多空平倉的條件去啓動
那在我自己寫的有迴圈狀況下是怎麼運行的?
每次K棒都會進for 迴圈去計算?
如此的話如果有100根K棒
那BarCount = 100
不就要跑 100 X 100 次
這應該不可能
還是會先將迴圈外的變數依每根K棒先計算過
最後進迴圈內跑一次迴圈做計算與判斷多空平倉的條件?
最後~ 再次感謝大家!
在for 迴圈裡,
buy , short , sell , cover 都應該以access array形式放1。
即是 buy [ i ] = 1 , buy = 1 即是全部放 1 Hi,
可以參考這篇
http://www.coco-in.net/thread-15286-1-1.html
頁:
[1]