How to Take profit with exact value wif

I want to buy or short with cross MA5 & MA15 and exit with exact value to cut loss or take profit
and set a criteria with max loss 3 trades and then stop trade until next day.

But the chart showed as below

How can i fix it?




_SECTION_BEGIN("AT Intraday Profit.100/Loss.-30 Level");

// Define the 5-bar and 15-bar moving averages
MA5 = Ref(MA(C,5), -1);
MA15 = Ref(MA(C, 15), -1);

Plot(MA5,"MA5 signal",colorGreen,1); 
Plot(MA15,"MA15 signal",colorred,1); 

upcon= Cross(MA5,MA15);
downcon=Cross(MA15,MA5);

// Initialize variables
Buy = Sell = Short = Cover = Null;
BuyPrice = Null;
ShortPrice = Null;
lossTrades = 0;

// Condition for the start of a new day
tn_cond = TimeNum() >= 40000;
new_day = tn_cond AND !Ref(tn_cond, -1);

// Use loops to handle trades and conditions
for (i = 1; i < BarCount; i++) {
    // Reset lossTrades counter at the start of each day
    if (new_day[i]) {
        lossTrades = 0;
    }

    if (lossTrades >= 3) {
        continue; // Stop trading for the day after 3 loss trades
    }

    // Buy condition: 5-bar MA crosses above 15-bar MA
    if (upcon[i]) {
        Buy[i] = 1;
        BuyPrice = Open[i]; // Buy at this bar open
    }

    // Sell condition: profit reaches 100 units or loss reaches 30 units
    if (BuyPrice != Null) {
        if ((Close[i] >= BuyPrice + 100) OR (Close[i] <= BuyPrice - 30)) {
            Sell[i] = 1;
            if (Close[i] <= BuyPrice - 30) {
                lossTrades++; // Increment loss trades counter if cut loss
            }
            BuyPrice = Null; // Clear the buy price after selling
        }
    }

    // Short condition: 15-bar MA crosses above 5-bar MA
    if (downcon[i]) {
        Short[i] = 1;
        ShortPrice = Open[i]; // Short at this bar open
    }

    // Cover condition: profit reaches 100 units or loss reaches 30 units
    if (ShortPrice != Null) {
        if ((Close[i] <= ShortPrice - 100) OR (Close[i] >= ShortPrice + 30)) {
            Cover[i] = 1;
            if (Close[i] >= ShortPrice + 30) {
                lossTrades++; // Increment loss trades counter if cut loss
            }
            ShortPrice = Null; // Clear the short price after covering
        }
    }
}

// Plot the signals on the chart
PlotShapes(Buy*shapeUpArrow,colorGreen);
PlotShapes(Short*shapeDownArrow,colorRed);
PlotShapes(cover*shapehollowUptriangle,colorGreen);
PlotShapes(sell*shapehollowdowntriangle,colorRed);

_SECTION_END();

Would you mind just amend my code and give answer instead of giving advice, I have tried many times but also can't complete the task referring to the advice as my programming level is not good enough. Thanks!!!

If you want to exit at a particular price, then you need to put that price in the SellPrice or CoverPrice array for the bar where the exit signal is true, for example:

if (<your exit logic>)
{
   Sell[i] = 1;
   SellPrice[i] = ma15[i];
}
1 Like

This would work if price is within H-L range or if you turn off price bound checking via SetOption.

Refer to my case, i want to exit base on "entry price" +100 or -30, but no ma15[i] how shoudl I set?

My programing lanugage is not good enough,
I have difficult to store the entry price
I suppose the following code, the Buyprice[i] stored the Entry price, is it right?

// Buy condition: 5-bar MA crosses above 15-bar MA
    if (upcon[i]) {
        Buy[i] = 1;
        BuyPrice[i] = Open[i]; // Buy at this bar open
    }

    // Sell condition: profit reaches 100 units or loss reaches 30 units
    if (BuyPrice != Null) {
        if ((Close[i] >= BuyPrice[i] + 100) OR (Close[i] <= BuyPrice[i] - 30)) {
            Sell[i] = 1;
            if (Close[i] <= BuyPrice[i] - 30) {
                lossTrades++; // Increment loss trades counter if cut loss
            }
            BuyPrice[i] = Null; // Clear the buy price after selling
        }
    }

Thanks but would you mind answer my post with exact answer.
Actually I think You also remember I have tried many time but also fail to complete the task by only referring to your links.

I am Amibroker's fans and used many years beacuse it's user friendly to my type of finance people who don't have very good programming language. but u everytime answer in this way, just like keep pushing my type of users to use other softwares.

Sorry to express my feeling, I am not that type of people put no effort then rely on you to answerm, I did put a lot of effort and sincere to ask questions but you answer in this way, i really feel very disappointing.

Using ma15[i] for the SellPrice was intended to be an example, because it was not clear to me from your original post what "exact price" you were trying to exit at. In your latest code, I still don't see any evidence that you're trying to set the SellPrice or CoverPrice.

The forum is intended to be a place where you can improve your AmiBroker and AFL skills. It is not a free coding service. If you prefer to have someone else write the code for you, perhaps you should consider finding a consultant to work with. If you decide to go that route, then it will be very important to define exactly how you wish your strategy to work.

1 Like

Thanks, I am not requiring the free coding service, I just think maybe it takes only few mins for the owner to solve the problem, I tried few weeks but fail, and everytime also refer the link to me , I know the owner don't have responsibility to help me, but I think a stranger see sombody have difficulty, just like you, also will spend a little time to give help.
so really thanks for you help

when cross(ma5,ma15), then buy at entry price,
My exact price to exit, is base on the entry price, earn 100units then take profit, lose 30 units then cut loss

I believe this works, but you should test it to be sure.

_SECTION_BEGIN("AT Intraday Profit.100/Loss.-30 Level");

stopAmount = 30;
profitAmount = 100;

// Define the 5-bar and 15-bar moving averages
MA5 = Ref(MA(C,5), -1);
MA15 = Ref(MA(C, 15), -1);

Plot(MA5,"MA5 signal",colorGreen,1); 
Plot(MA15,"MA15 signal",colorred,1); 

upcon= Cross(MA5,MA15);
downcon=Cross(MA15,MA5);

// Initialize variables
Buy = Sell = Short = Cover = Null;
BuyPrice = ShortPrice = Open;
lossTrades = 0;

// Condition for the start of a new day
tn_cond = TimeNum() >= 40000;
new_day = tn_cond AND !Ref(tn_cond, -1);

// 1 = long, -1=short, 0 = not in a trade
tradeDir = 0;

// Use loops to handle trades and conditions
for (i = 1; i < BarCount; i++) {
    // Reset lossTrades counter at the start of each day
    if (new_day[i]) {
        lossTrades = 0;
    }

    if (lossTrades >= 3) {
        continue; // Stop trading for the day after 3 loss trades
    }

    // Buy condition: 5-bar MA crosses above 15-bar MA
    if (upcon[i] AND tradeDir == 0) {
        Buy[i] = 1;
        stopPrice = BuyPrice[i] - stopAmount;
        profitPrice = BuyPrice[i] + profitAmount;
        tradeDir = 1;
    }

    // Sell condition: profit reaches 100 units or loss reaches 30 units
    if (tradeDir == 1) {
        if (L[i] < stopPrice) {
            Sell[i] = 2;
            SellPrice[i] = Min(Open[i], stopPrice);
            tradeDir = 0;
            lossTrades++;
        }
        else if (H[i] > profitPrice) {
            Sell[i] = 3;
            SellPrice[i] = Max(Open[i], profitPrice);
            tradeDir = 0;    
        }
    }

    // Short condition: 15-bar MA crosses above 5-bar MA
    if (downcon[i] AND tradeDir == 0) {
        Short[i] = 1;
        stopPrice = ShortPrice[i] + stopAmount;
        profitPrice = ShortPrice[i] - profitAmount;
        tradeDir = -1;
    }

    // Cover condition: profit reaches 100 units or loss reaches 30 units
    if (tradeDir == -1) {
        if (H[i] > stopPrice) {
            Cover[i] = 2;
            CoverPrice[i] = Max(Open[i], stopPrice);
            tradeDir = 0;
            lossTrades++;
        }
        else if (L[i] < profitPrice) {
            Cover[i] = 3;
            CoverPrice[i] = Min(Open[i], profitPrice);
            tradeDir = 0;    
        }
    }
}

// Plot the signals on the chart
PlotShapes(Buy*shapeUpArrow,colorGreen);
PlotShapes(Short*shapeDownArrow,colorRed);
PlotShapes(cover*shapehollowUptriangle,colorGreen);
PlotShapes(sell*shapehollowdowntriangle,colorRed);

_SECTION_END();
1 Like

Thanks a lot x 1000 :pray: :pray:t2: :pray:t2:
I will test it, really thanks a lot!!!

Need time to digest, it give me a lot of hints!!! Thanks!

Just click on the error to see the description / explanation.

1 Like

Thanks for confirm not logic fault.
thanks