Current Trade Profit Exit based on some condition

I am trying to exit the current trade based on following two conditions.

  1. My Current Trade profit is positive(>0)
  2. Last close price is below the donchian middle level.

I have been trying applystop but I guess thats not serving the purpose. Any Suggestion or directions please.Will greatly appreciate


@rajput.jps, Welcome to the Forum.

Have you read the "How to use this site" thread? (Start at the TOP)
Have you read the "How do I debug my formula" thread? (Also, start at TOP)

If those still don't help you out, then Post your code here (Use Code Blocks </>).

We can't tell if your code has Logic errors or functional errors until we see it.

Also, I would suggest that you use an Exploration to "SEE" what you are getting from your code. Put Each Condition into a column, and then your combination and see if it is giving you the results you expect. Then you can move forward from there.

Thx for the reply.Would do as suggedsted.

I have used the option of Getting Add columns, and on the reccomendation on one of the forum page where it was suggested for the esoteric exits one needs to customebacktest object and tried using the function if (Status("action") == actionPortfolio) {

bo = GetBacktesterObject();


But I could not get any success. I am being forced to use apply stop ,which i think will not happen in my case as I am gauging the profit in the trade " on the fly".

Any suggestions for the improvement or I m missing something here. Please pardon my ignorance as i am only 10 days to amibroker AFL.

////Donchian Channel  BreakOut Strategy
 SetOption("InitialEquity", 1000000 );

 SetOption( "ActivateStopsImmediately", FALSE ); // enables to exit on the entry bar, which can be really serioius flaw in the strategy and would give very wrong results
 SetOption( "AllowSameBarExit", False ); 

 RoundLotSize = 1;
// Monte Carlo Simulations

 PS= Optimize("No of Tests",1,1,1000,1);
 // PositionScore =  100-RSI(3);//// //Random()*PS;  
// Position Sizing  THE GAME CHANGER

SetOption("MaxOpenPositions", 10 );

//*************   Risk Based Position Sizing*******************///

RiskPercent =3; // The amount of total Equity that is to be risked per trade.This is actually 50 Basis Points

Qunatity = ( (RiskPercent/100)*Equity()) /ATR(10);   // Trying to give all equities a fair chance to perform.The one's with the highe volatility are bought less and vice versa


/// Buying conditions
Liquidity = MA( C * V, 25 );

 Index = Foreign("NIFTY","Close");
 IndexMovAvgLarge = MA(Index,200);
 IndexMovAvgSlow = MA(Index,5);
 MarketTiming=(IndexMovAvgSlow >IndexMovAvgLarge) ;
 DonchianUpper =HHV(Ref(H,-1),long_lookback);
 DonchianLower = LLV(Ref(L,-1),short_lookback);
 DonchianMiddle = (DonchianUpper+DonchianLower)/2;
 // Buying on the Upward breakout of the upper channel
  Buy=   ( MarketTiming=1) and  Cross(Close,Ref( DonchianUpper,-1) )AND Liquidity>=2000000 ;
   Position_Profit= C-ValueWhen(Buy,close);
  LossExitPrice = BuyPrice- 2*ATR(20);
  EntryValue =  ValueWhen(Buy, BuyPrice );  

Sell1 =  Position_Profit>0 AND Close < DonchianMiddle;

Sell2 =Position_Profit < AND Close < LossExitPrice;
Sell3=  Position_Profit < AND Close < DonchianMiddle;
 Sell = sell1 OR sell2 OR Sell3;

I didn't check the whole code but one thing to change is MarketTiming=1 to MarketTiming==1

One additional comment in these cases, where you need to know the current status of an open trade (in this case Open Profit), don't forget that you can have several bars with True in Buy array even though you're still in the same trade.

With this type of rules, I prefer to use loops, to make sure the Buy entry price doesn't change during trade.

You can use arrays, but you have to make sure that Position_Profit only changes when a new trade is opened and not when Buy signal is True.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.