Applystop and scaleout

I want to use applyStop() for scaleout (profit taking) as well as stoploss.


if (StrToLower(strategyType)=="range"){
if (isDifferentBaseInstrument=="Yes"){
SetForeign(BaseInstrumentTicker, True, true);
ApplyStop(stopTypeProfit, stopModePoint, scaleoutTargetInPointsBI, 1);
RestorePriceArrays();
}
ApplyStop(stopTypeProfit, stopModePoint, scaleoutTargetInPointsTI, 1);
}
Equity(1);

LongScaleout=IIf(Sell==3, 1, 0);
Sell=IIf(Sell==3, 0, Sell);
ShortScaleout=IIf(Cover==3, 1, 0);
Cover=IIf(Cover==3, 0, Cover);


if (isDifferentBaseInstrument=="Yes"){
SetForeign(BaseInstrumentTicker, True, true);
ApplyStop(stopTypeLoss, stopModePoint, stopAmountInPointsBI, 1);
if (StrToLower(strategyType)=="range")
ApplyStop(stopTypeLoss, LastValue(IIf(IIf(inLongTrade, IsOverboughtAttained, IsOversoldAttained), 2, 0)), 0, 1);
RestorePriceArrays();
}
ApplyStop(stopTypeLoss, stopModePoint, stopAmountInPointsTI, 1);
Equity(1);

But stoploss does not work if the trade has scaleout. The second equity(1) is writing cover=3 which i can’t figure out why.

Can this ApplyStop when used with stopTypeLoss write cover=3? I think stopTypeLoss writes cover=2? Can anyone help? If see my first post in the code i have supressed cover=3 and changed to cover=0.

ApplyStop only exits positions. If you want to scale out you need to write your own code for stops as described in KB: http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/

I know i have to write code for scaleout and I am doing that. Here 2nd Equity(1) writes cover=3. Why does it do that?

Ok i did some more investigation and I could zero-in on the Equity(1,0) statement. It is supressing the cover signal. Why? see the code below

_TRACE("Buy="+Buy+", Sell="+Sell+", Short="+Short+", cover="+Cover);
Equity(1, 0);
_TRACE("Buy="+Buy+", Sell="+Sell+", Short="+Short+", cover="+Cover);

The first trace reports cover=1 and the second trace reports cover=0. Why does this happen? why is the earlier cover signal supressed by Equity(1,0). This is just making me go nuts

Users’ Guide
http://www.amibroker.com/f?equity

read the comments section, quote

Equity(1) changes buy/sell variables (evaluates stops - and writes them back to buy/sell arrays). If you are using non-zero delays both Equity calls will return different values because in first case exits are generated by stops (not delayed) and in second case STOP signals written back to buy/sell arrays are delayed (opposite to the first case). Equity(1) affects the buy/sell/short/cover variables. It is not a “no-operation” function. If you want a “no-op” you should use Equity( 0 ) to generate equity line.

See also http://www.amibroker.com/guide/h_portfolio.html that has a chart showing how redundant signals are removed. And also as explained here:

Can anyone give me an example working afl of multiple applystop (maxloss, profit, trailing) along with normal exits used in the same afl. I have tried for the last 3 days with all combinations that i could think of but nothing has worked.


Buy=IsOversoldAttained && inLongTrend && ((openBI-BuyStopInitial)<=maxRiskInPointsInTI) 
&& (openBI-BuyStopInitial)>0  && isTradeInitiationTime;
BuyPrice=ValueWhen(Buy, O);
stopLossInPoints=BuyPrice-BuyStopInitial+TickSize;


ApplyStop(stopTypeLoss, stopModePoint, maxRiskInPointsInTI, 1);

ApplyStop(stopTypeProfit, stopModePoint, maxRiskInPointsInTI*2, 1);
Equity(1, 0);
_TRACE("Buy="+Buy+", Sell="+Sell+", Short="+Short+", cover="+Cover);
longScaleout=IIf(Sell==3, 1, 0);
Sell=IIf(Sell==3, 0, Sell);

Sell=sell||isSystemExitTime;



//inLongTrade=BarsSince(buy)<BarsSince(sell);

//Buy=inLongTrade && IIf(HighestSince(Buy, Cross(H, BuyPrice+maxRiskInPointsInTI*2)), sigScaleOut, Buy);


Buy=ExRem(Buy==1, Sell);
Sell=ExRem(Sell, Buy==1);

Buy=IIf(longScaleout&&Ref(HighestSince(Buy, longScaleout), -1)==0, sigScaleOut, Buy);

The problem is:
If the ApplyStop For Profit is executed then the ApplyStop for MaxLoss is not
and
if ApplyStop for MaxLoss is executed then ApplyStop For Profit is not.

The one that is reached first is executed and the other one discarded. I want both to be effective. Because a trade can reach the profit level (where partial positions are sold) then later the max stop loss is also hit in the same trade.

That is how stops work, if one stop exits entire position, the other stop can’t exit it because the position is already gone. I wrote “ApplyStop only exits positions” in my original reply above.

1 Like

Thank you. I have modified my code accordingly and seems to work.

Now I theres one new problem that is: ApplyStop is wrongly exits the position because of the changing BuyPrice.

Buy=IsOversoldAttained && inLongTrend && ((openBI-BuyStopInitial)<=maxRiskInPointsInTI) 
&& (openBI-BuyStopInitial)>0  && isTradeInitiationTime;
BuyPrice=ValueWhen(Buy, O);

ApplyStop(stopTypeLoss, stopModePoint, maxRiskInPointsInTI, 1);


//much later in the code
Buy=ExRem(Buy, Sell);
Sell=ExRem(Sell, Buy);

As my entry condition is price level based and not cross based there is BUY at many bars after the first buy (although they are exremed later) and resultantly buyPrice also changes every bar. As Applystop(stopTypeLoss, stopModePoint, SLPoints, 1) creates stops at BuyPrice-SLPoints, every higher bar (Where buy condition is true) creates a new higher buyPrice and resultantly a higher stop. The Higher stops get hit and the position is exited prematurely. What should i do?

@Jefforey your code is fundamentally incorrect. BuyPrice is INPUT variable, not output and should be equal to given bar price, i.e. OPEN in your case, BuyPrice = Open;. Your use of ValueWhen in that place is totally incorrect.

Correct usage of price-level stops is shown in the Knowledge Base:
http://www.amibroker.com/kb/2014/10/17/using-price-levels-with-applystop-function/