Generating a Sell or Cover Signal from ApplyStop

Hi all,

I've been trying to work this out days to no avail

I'm using both "applystop" and "cover" signal (based on the prior ~20 day high) to stop myself out of a short position, whichever results in a lower % loss. The problem is, when I am getting stopped out of a position via "Applystop" as opposed to my "Cover" signal, I am failing to generate another short position due to using the ExRem function to stop excessive signals..

Short = ExRem(Short, Cover);
Cover = ExRem(Cover, Short)

I have read on other threads that ApplyStop does not produce sell/cover signals by itself and does not return any value... Therefore it would make sense why the ExRem function isn't triggering and another short signal is not being generated.

My question: Can "Sell" / "Cover" and "ApplyStop" be used in the same code? Or will it cause problems as the one described above? Is there anyway to generate a "Cover" (or "sell") signal with the "applystop" function, or any alternative?

Here is 1) where I am actually getting stopped out of the position (confirmed by the equity curve) and 2) where I should be re-entering the short position.


Many many thanks


Are you sure the trade was actually stopped out soon after your entry (where you have labeled "Stopped")?

You really should post your full code when seeking help with your code.

1 Like

Thanks for the reply TrendSurfer. Yes, It was stopped out a few days later. Happy to post the code but it is quite long

SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "Close", IIf(Close > Open, colorblue,colorRed), styleBar);

//Initialise functions
Account_Equity = 100000;

SetOption("InitialEquity", Account_Equity);
//SetOption("MaxOpenPositions", Max_Open_Positions);     
SetPositionSize(100, spsPercentOfEquity);   
SetOption("AccountMargin", 10);

//55 day high & low, 20 day high & low for trade signals
Highest_55_Days = HHV(Ref(H,0),Upper);
Lowest_55_Days = LLV(Ref(L,0),Upper);
Highest_20_Days = HHV(Ref(H,0),Lower);
Lowest_20_Days = LLV(Ref(L,0),Lower);

//Trend filters

Stoplength = ATR(20) ;
RiskPerShare =  2 * Stoplength;
ApplyStop( stopTypeLoss, stopModePoint, RiskPerShare, True );

//Trade Signals
Buy = Cross(High, Ref( Highest_55_Days, -1 )) AND MA1 >= MA2;
Sell = Cross(Ref( Lowest_20_Days, -1 ), Low);
Short = Cross(Ref( Lowest_55_Days, -1), Low) AND MA2 >= MA1;
Cover = Cross(High, Ref(Highest_20_Days, -1)); //OR MA1 >= MA2;

//Stop Excessive signals
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell, Buy);
Short = ExRem(Short, Cover);
Cover = ExRem(Cover, Short);

//Plot Stopline
//Determine entry points
Entry = Buy OR Short;
EntryPriceBuy = ValueWhen(Entry, BuyPrice);
EntryPriceShort = ValueWhen(Entry, SellPrice);
AtrAtEntry = ValueWhen(Entry, RiskPerShare);

//Now plot the stoplines
inTrade = Flip(Buy,Sell);
inTrade2 = Flip(Short,Cover);
Stopline=IIf(intrade, Max(EntryPriceBuy - AtrAtEntry, Lowest_20_Days), Null);
Stopline2=IIf(intrade2, Min(EntryPriceShort + AtrAtEntry, Highest_20_Days), Null);
Plot(Stopline,"Stop Loss",colorRed,styleLine,0,0,0,0);
Plot(Stopline2,"Stop Loss",colorRed,styleLine,0,0,0,0);

//Plotting entry and exit prices

SellorCoverTrigger = "Stop,Stop,Profit,Trail,N-Bars,Ruin";

for(i=fvb; i<=lvb; i++)
		PlotText("Buy\n" + BuyPrice[i],i, BuyPrice[i], colorGreen,-1,-40);
		PlotText(StrExtract(sellorcovertrigger, Sell[i]-1) +"\n" + SellPrice[i], i, SellPrice[i], colorRed, -1,-30);
		PlotText("Short\n" + ShortPrice[i], i, ShortPrice[i], colorRed, -1,0);
		PlotText(StrExtract(sellorcovertrigger, Cover[i]-1)+ "\n" + CoverPrice[i], i, CoverPrice[i], colorRed, -1, 0);

//Plot buy and sell shapes
buysignalshape = sellsignalshape = shortsignalshape = 0;
PlotShapes( buy*shapeUpArrow, colorGreen, 0, Low);
PlotShapes( sell*shapeSmallCircle, colorRed, 0,  SellPrice);
PlotShapes( short*shapeDownArrow, colorRed, 0, High);
PlotShapes( cover*shapeSmallCircle, colorGreen, 0, CoverPrice );

You could use one main For Loop to control everything. Therefore you won't need Apply Stop or Exrem. See here for a similar example different-exits.

Ended up completely rewriting my code with a for loop so I no longer need the applystop function. Thanks for your help Trendsurfer

1 Like