COCO研究院

 找回密碼
 註冊
搜索
查看: 2233|回復: 3

在AB寫多策略遇到問題

[複製鏈接]
發表於 16-5-12 09:32 | 顯示全部樓層 |閱讀模式
本人有兩個策略想寫進同一個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[i]) trade2Count = 0;

        if (BuyCondition1[i] AND positionHold == 0)
        {
                Buy[i] = 1;
                BuyPrice[i] = AH[i];
                ProfitPrice = (BuyPrice[i] + ProfitPoint1[i]);
                StopPrice = (BuyPrice[i] - LossPoint1[i]);
                positionHold ++;
                B1 = 1;
        }
       
                else if (SellCondition1[i] AND positionHold > 0 AND B1 == 1)
        {
                Sell[i] = 1;
                ProfitPrice = 0;
                StopPrice = 0;
                positionHold = 0;
                B1 = 0;
        }

                else if (H[i] >= ProfitPrice AND positionHold > 0 AND B1 == 1)
        {
                Sell[i] = 1;
                SellPrice[i] = ProfitPrice;
                ProfitPrice = 0;
                StopPrice = 0;
                positionHold = 0;
                B1 = 0;
        }
       
                else if (L[i] <= StopPrice AND positionHold > 0 AND B1 == 1)
        {
                Sell[i] = 1;
                SellPrice[i] = StopPrice;
                ProfitPrice = 0;
                StopPrice = 0;
                positionHold = 0;
                B1 = 0;
        }
       
                else if (ShortCondition1[i] AND positionHold == 0)
        {
                Short[i] = 1;
                ShortPrice[i] = AL[i];
                ProfitPrice = (ShortPrice[i] - ProfitPoint1[i]);
                StopPrice = (ShortPrice[i] + LossPoint1[i]);
                positionHold --;
                S1 = 1;
        }

                else if (CoverCondition1[i] AND positionHold < 0 AND S1 ==1)
        {
                Cover[i] = 1;
                ProfitPrice = 0;
                StopPrice = 0;
                positionHold = 0;
                S1 = 0;
        }

                else if (L[i] <= ProfitPrice AND positionHold < 0 AND S1 ==1)
        {
                Cover[i] = 1;
                CoverPrice[i] = ProfitPrice;
                ProfitPrice = 0;
                StopPrice = 0;
                positionHold = 0;
                S1 = 0;
        }

                else if (H[i] >= StopPrice AND positionHold < 0 AND S1 ==1)
        {
                Cover[i] = 1;
                CoverPrice[i] = StopPrice;
                ProfitPrice = 0;
                StopPrice = 0;
                positionHold = 0;
                S1 = 0;
        }

                else if (BuyCondition2[i] AND positionHold == 0 AND trade2Count == 0)
        {
                Buy[i] = 1;
                BuyPrice[i] = LastDayLow[i];
                ProfitPrice = (BuyPrice[i] + ProfitPoint2[i]);
                StopPrice = (BuyPrice[i] - LossPoint2[i]);
                positionHold ++;
                trade2Count ++;
                B2 = 1;
        }
       
                else if (SellCondition2[i] AND positionHold > 0 AND B2 == 1)
        {
                Sell[i] = 1;
                ProfitPrice = 0;
                StopPrice = 0;
                positionHold = 0;
                B2 = 0;
        }

                else if (H[i] >= ProfitPrice AND positionHold > 0 AND B2 == 1)
        {
                Sell[i] = 1;
                SellPrice[i] = ProfitPrice;
                ProfitPrice = 0;
                StopPrice = 0;
                positionHold = 0;
                B2 = 0;
        }
       
                else if (L[i] <= StopPrice AND positionHold > 0 AND B2 == 1)
        {
                Sell[i] = 1;
                SellPrice[i] = StopPrice;
                ProfitPrice = 0;
                StopPrice = 0;
                positionHold = 0;
                B2 = 0;
        }
       
                else if (ShortCondition2[i] AND positionHold == 0 AND trade2Count == 0)
        {
                Short[i] = 1;
                ShortPrice[i] = LastDayHigh[i];
                ProfitPrice = (ShortPrice[i] - ProfitPoint2[i]);
                StopPrice = (ShortPrice[i] + LossPoint2[i]);
                positionHold --;
                trade2Count ++;
                S2 = 1;
        }

                else if (CoverCondition2[i] AND positionHold < 0 AND S2 == 1)
        {
                Cover[i] = 1;
                ProfitPrice = 0;
                StopPrice = 0;
                positionHold = 0;
                S2 = 0;
        }

                else if (L[i] <= ProfitPrice AND positionHold < 0 AND S2 == 1)
        {
                Cover[i] = 1;
                CoverPrice[i] = ProfitPrice;
                ProfitPrice = 0;
                StopPrice = 0;
                positionHold = 0;
                S2 = 0;
        }

                else if (H[i] >= StopPrice AND positionHold < 0 AND S2 == 1)
        {
                Cover[i] = 1;
                CoverPrice[i] = 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,請問這如果解決?謝謝
 樓主| 發表於 16-5-12 09:46 | 顯示全部樓層
如果用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[i] AND PositionHold == 0)
        {
                BuyCondition[i] = 1;
                PositionHold ++;
                ProfitPoint = R1ProfitPoint;
                LossPoint = R1LossPoint;
                R1 = 1;
        }
       
        else if (Short1[i] AND PositionHold == 0)
        {
                ShortCondition[i] = 1;
                PositionHold --;
                R1TradeCount ++;
                ProfitPoint = R1ProfitPoint;
                LossPoint = R1LossPoint;
                R1 = 1;
        }
       
        else if (Sell1[i] && PositionHold > 0 AND R1 == 1)
        {
                SellCondition[i] = 1;
                PositionHold = 0;
                R1 = 0;
        }
       
        else if (Cover1[i] && PositionHold < 0 AND R1 == 1)
        {
                CoverCondition[i] = 1;
                PositionHold = 0;
                R1 = 0;
        }
       
        else if (Buy2[i] AND PositionHold == 0)
        {
                BuyCondition[i] = 1;
                PositionHold ++;
                ProfitPoint = R2ProfitPoint;
                LossPoint = R2LossPoint;
                R2 = 1;
        }
       
        else if (Short2[i] AND PositionHold == 0)
        {
                ShortCondition[i] = 1;
                PositionHold --;
                ProfitPoint = R2ProfitPoint;
                LossPoint = R2LossPoint;
                R2 = 1;
        }
       
        else if (Sell2[i] && PositionHold > 0 AND R2 == 1)
        {
                SellCondition[i] = 1;
                PositionHold = 0;
                R2 = 0;
        }
       
        else if (Cover2[i] && PositionHold < 0 AND R2 == 1)
        {
                CoverCondition[i] = 1;
                PositionHold = 0;
                R2 = 0;
        }
}

Buy = BuyCondition;
Sell = SellCondition;
Short = ShortCondition;
Cover = CoverCondition;

ApplyStop(stopTypeProfit, stopModePoint, ProfitPoint);
ApplyStop(stopTypeLoss, stopModePoint, LossPoint);
發表於 16-5-14 01:38 來自手機 | 顯示全部樓層
Use 'case', 分兩個情況處理
發表於 16-5-17 10:32 | 顯示全部樓層
建議在CODE裏面加COMMENT, 不然別人會看得很吃力。
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|站長信箱|廣告洽詢|COCO研究院

GMT+8, 24-11-22 17:03

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 |