在AB寫多策略遇到問題
本人有兩個策略想寫進同一個afl裡,除了策略不同,兩個策略的止賺止賺也不同,本人已寫了一個可以回測的afl了:ProfitPoint1 = 100;
LossPoint1 = 90;
BuyCondition1 = BuySignal1;
SellCondition1 = SellSignal1;
ShortCondition1 = ShortSignal1;
CoverCondition1 = CoverSignal1;
ProfitPoint2 = 150;
LossPoint2 = 70;
BuyCondition2 = BuySignal2;
SellCondition2 = SellSignal2;
ShortCondition2 = ShortSignal2;
CoverCondition2 = CoverSignal2;
positionHold = 0;
Buy = 0;
Sell = 0;
Short = 0;
Cover = 0;
ProfitPrice = 0;
StopPrice = 0;
B1 = 0;
S1 = 0;
B2 = 0;
S2 = 0;
trade2Count = 0;
for (i = 0; i < BarCount; i++)
{
if (newDay) trade2Count = 0;
if (BuyCondition1 AND positionHold == 0)
{
Buy = 1;
BuyPrice = AH;
ProfitPrice = (BuyPrice + ProfitPoint1);
StopPrice = (BuyPrice - LossPoint1);
positionHold ++;
B1 = 1;
}
else if (SellCondition1 AND positionHold > 0 AND B1 == 1)
{
Sell = 1;
ProfitPrice = 0;
StopPrice = 0;
positionHold = 0;
B1 = 0;
}
else if (H >= ProfitPrice AND positionHold > 0 AND B1 == 1)
{
Sell = 1;
SellPrice = ProfitPrice;
ProfitPrice = 0;
StopPrice = 0;
positionHold = 0;
B1 = 0;
}
else if (L <= StopPrice AND positionHold > 0 AND B1 == 1)
{
Sell = 1;
SellPrice = StopPrice;
ProfitPrice = 0;
StopPrice = 0;
positionHold = 0;
B1 = 0;
}
else if (ShortCondition1 AND positionHold == 0)
{
Short = 1;
ShortPrice = AL;
ProfitPrice = (ShortPrice - ProfitPoint1);
StopPrice = (ShortPrice + LossPoint1);
positionHold --;
S1 = 1;
}
else if (CoverCondition1 AND positionHold < 0 AND S1 ==1)
{
Cover = 1;
ProfitPrice = 0;
StopPrice = 0;
positionHold = 0;
S1 = 0;
}
else if (L <= ProfitPrice AND positionHold < 0 AND S1 ==1)
{
Cover = 1;
CoverPrice = ProfitPrice;
ProfitPrice = 0;
StopPrice = 0;
positionHold = 0;
S1 = 0;
}
else if (H >= StopPrice AND positionHold < 0 AND S1 ==1)
{
Cover = 1;
CoverPrice = StopPrice;
ProfitPrice = 0;
StopPrice = 0;
positionHold = 0;
S1 = 0;
}
else if (BuyCondition2 AND positionHold == 0 AND trade2Count == 0)
{
Buy = 1;
BuyPrice = LastDayLow;
ProfitPrice = (BuyPrice + ProfitPoint2);
StopPrice = (BuyPrice - LossPoint2);
positionHold ++;
trade2Count ++;
B2 = 1;
}
else if (SellCondition2 AND positionHold > 0 AND B2 == 1)
{
Sell = 1;
ProfitPrice = 0;
StopPrice = 0;
positionHold = 0;
B2 = 0;
}
else if (H >= ProfitPrice AND positionHold > 0 AND B2 == 1)
{
Sell = 1;
SellPrice = ProfitPrice;
ProfitPrice = 0;
StopPrice = 0;
positionHold = 0;
B2 = 0;
}
else if (L <= StopPrice AND positionHold > 0 AND B2 == 1)
{
Sell = 1;
SellPrice = StopPrice;
ProfitPrice = 0;
StopPrice = 0;
positionHold = 0;
B2 = 0;
}
else if (ShortCondition2 AND positionHold == 0 AND trade2Count == 0)
{
Short = 1;
ShortPrice = LastDayHigh;
ProfitPrice = (ShortPrice - ProfitPoint2);
StopPrice = (ShortPrice + LossPoint2);
positionHold --;
trade2Count ++;
S2 = 1;
}
else if (CoverCondition2 AND positionHold < 0 AND S2 == 1)
{
Cover = 1;
ProfitPrice = 0;
StopPrice = 0;
positionHold = 0;
S2 = 0;
}
else if (L <= ProfitPrice AND positionHold < 0 AND S2 == 1)
{
Cover = 1;
CoverPrice = ProfitPrice;
ProfitPrice = 0;
StopPrice = 0;
positionHold = 0;
S2 = 0;
}
else if (H >= StopPrice AND positionHold < 0 AND S2 == 1)
{
Cover = 1;
CoverPrice = StopPrice;
ProfitPrice = 0;
StopPrice = 0;
positionHold = 0;
S2 = 0;
}
}
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);
Short = ExRem(Short, Cover);
Cover = ExRem(Cover, Short);
經過回側後發覺是可行的,但用這個afl在backtest report是顯示不到max loss 及 max profit,所以本人想在applystop function,如:
ApplyStop(stopTypeProfit, stopModePoint, ProfitPoint);
ApplyStop(stopTypeLoss, stopModePoint, LossPoint);
如何可以令系統知道用Signal1的時候止賺止蝕點是ProfitPoint1及LossPoint1,而用Signa2的時候止賺止蝕點是ProfitPoint2及LossPoint2?
本人試過用for loop來寫但當中可能寫錯了所以如果用ApplyStop的話backtest出來系統只會認為止賺止蝕點是ProfitPoint1及LossPoint1,請問這如果解決?謝謝 如果用apply stop的話,我是這樣寫的,但applystop卻默認所有的stop price都是R1ProfitPoint和R1LossPoint
Buy1 = C > O; //It's just a example
Sell1 = C > Ref(C, -1); //It's just a example
Short1 = O > C; //It's just a example
Cover1 = C < Ref(C, -1); //It's just a example
R1ProfitPoint = 150;
R1LossPoint = 70;
Buy2 = H > Ref(H, -1); //It's just a example
Sell2 = C > Ref(C, -1); //It's just a example
Short2 = L > Ref(L, -1); //It's just a example
Cover2 = C < Ref(C, -1); //It's just a example
R2ProfitPoint = 200;
R2LossPoint = 90;
BuyCondition = 0;
SellCondition = 0;
ShortCondition = 0;
CoverCondition = 0;
PositionHold = 0;
ProfitPoint = 0;
LossPoint = 0;
R1 = 0;
R2 = 0;
for (i = 0; i < BarCount; i++)
{
if (Buy1 AND PositionHold == 0)
{
BuyCondition = 1;
PositionHold ++;
ProfitPoint = R1ProfitPoint;
LossPoint = R1LossPoint;
R1 = 1;
}
else if (Short1 AND PositionHold == 0)
{
ShortCondition = 1;
PositionHold --;
R1TradeCount ++;
ProfitPoint = R1ProfitPoint;
LossPoint = R1LossPoint;
R1 = 1;
}
else if (Sell1 && PositionHold > 0 AND R1 == 1)
{
SellCondition = 1;
PositionHold = 0;
R1 = 0;
}
else if (Cover1 && PositionHold < 0 AND R1 == 1)
{
CoverCondition = 1;
PositionHold = 0;
R1 = 0;
}
else if (Buy2 AND PositionHold == 0)
{
BuyCondition = 1;
PositionHold ++;
ProfitPoint = R2ProfitPoint;
LossPoint = R2LossPoint;
R2 = 1;
}
else if (Short2 AND PositionHold == 0)
{
ShortCondition = 1;
PositionHold --;
ProfitPoint = R2ProfitPoint;
LossPoint = R2LossPoint;
R2 = 1;
}
else if (Sell2 && PositionHold > 0 AND R2 == 1)
{
SellCondition = 1;
PositionHold = 0;
R2 = 0;
}
else if (Cover2 && PositionHold < 0 AND R2 == 1)
{
CoverCondition = 1;
PositionHold = 0;
R2 = 0;
}
}
Buy = BuyCondition;
Sell = SellCondition;
Short = ShortCondition;
Cover = CoverCondition;
ApplyStop(stopTypeProfit, stopModePoint, ProfitPoint);
ApplyStop(stopTypeLoss, stopModePoint, LossPoint); Use 'case', 分兩個情況處理 建議在CODE裏面加COMMENT, 不然別人會看得很吃力。
頁:
[1]