Buy Stop order and exit first profitable open from the second day included

Hi, I would like to place a buy stop order if the price reaches the "lowerband" but the code I have buys me whether or not it reaches that level. On the other hand, I would like to close the position at the first profitable opening from the first trading day. For example, if it was opened on a Monday, it could be closed from the first profitable opening from Wednesday, including Wednesday.

Also, it does not make me well the closings, many times it does not make to the level that it should, I attach images. I have painted in orange the price at which it should close, as you can see in the image, the closing price goes down the band one candle, when it should stay at that level and close as I said above, at the first profitable opening from the second day, this one included.

This is the code I have:

//Bands
Lowerband = XXXXXXX; 
Upperband = XXXXXXX; 


// Bullish Set Up alcista
cond1 = L < Lowerband;
cond2 = xxxxx;
cond3 = xxxxx;

//bearish Set up 
cond4 = H > Lowerband;
cond5 = xxxxx;
cond6 = xxxxx;

//Buy
BuySignal = cond1 AND cond2 AND cond3 ;
BuyLimitPrice = Lowerband;
// now we check if limit was hit
Buy = Ref(BuySignal,-1) AND Open < Lowerband;
BuyPrice = max( BuyLimitPrice, Open);



//Sell = BarsSince(Buy) >=2 AND Open>ValueWhen(BuySignal, Lowerband);

Sell = Open>ValueWhen(Buy, BuyPrice);
SellPrice = Open;



//ELIMINAR SENYALS EXTRA//
Buy = ExRem(Buy, Sell);
//Short = ExRem(Short, Cover OR Buy);
Sell = ExRem(Sell, Buy);
//Cover = ExRem(Cover, Short);

//StopLoss
ApplyStop(stopTypeLoss, stopModePercent, 5, 1, False);



//pantalla
Plot(Upperband, "Upperband", colorGreen, styleLine);
Plot(Lowerband, "Loweband", colorRed, styleLine);
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorGreen, 0, L, -15 ); 
PlotShapes( IIf( Buy , shapeHollowCircle, shapeNone ), colorGreen, 0, BuyPrice, 0 ); 
//PlotShapes( IIf( Short , shapeDownArrow, shapeNone ), colorBrown, 0, H, -15 ); 
//PlotShapes( IIf( Short, shapeHollowCircle, shapeNone ), colorBrown, 0, ShortPrice, 0 ); 
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, -15 ); 
PlotShapes( IIf( Sell, shapeHollowCircle, shapeNone ), colorRed, 0, SellPrice, 0 ); 
//PlotShapes( IIf( Cover AND NOT Buy, shapeUpArrow, shapeNone ), colorDarkGreen, 0, L, -15 ); 
//PlotShapes( IIf( Cover AND NOT Buy, shapeHollowCircle, shapeNone ), colorDarkGreen, 0, CoverPrice, 0 ); 

On the other hand, I have two doubts about stoploss, what exactly is stopModePoint? I would like to see or understand more examples since I don't know how to put them well.

Thank you very much in advance!!!
nocierrabien

  1. Your code is incomplete (so your observations are not reproducible).
  2. What does it mean exactly "buys me whether or not it reaches that level"? Example?

For proper stops (being dependent on entry) you have to use ApplyStop or looping. Been said many times in forum.

Remove Exrem and Remove Sell = Open>ValueWhen(Buy, BuyPrice);
Use Applystop's stopTypeProfit.

Check the examples using ApplyStop (as well as the ones with Plot).
Plenty of them already in KB and in forum.
For exit at trade price see scenarios in manual (hint: exit at stop mode)


stopModePoint means that amount is in points (e.g. Dollar amount).
Amount may be number or array as mentioned in manual.

This is the complet code, sorry.

//Bands
Lowerband =  MA((L*(1-2*((((H-L)/((H+L)/2))*1000)*0.001))),20); 
Upperband = MA((L*(1+2*((((H-L)/((H+L)/2))*1000)*0.001))),20); 


// Bullish Set Up alcista
cond1 = L < Lowerband;
cond2 = Ref(MA(C,30),-1) < MA(C,30);
cond3 = LinearReg(Foreign("$SPX", "Close"),20) > 0;

//bearish Set up 
cond4 = H > Upperband;
cond5 = Ref(MA(C,30),-1) > MA(C,30);
cond6 = LinearReg(Foreign("$SPX", "Close"),20) < 0;

//Compramos
BuySignal = cond1 AND cond2 AND cond3;
BuyLimitPrice = Lowerband;
// now we check if limit was hit
Buy = Ref(BuySignal,-1) AND Open < Lowerband AND H > Lowerband;
BuyPrice = max( BuyLimitPrice, Open);

Comprobacion = ValueWhen(Buy, BuyPrice);
Sell = IIf(BarsSince(Buy)>=2, Open>ValueWhen(Buy, BuyPrice), Null);
SellPrice = Open;
Plot(Comprobacion, "Comprobacion", colorOrange, styleThick);


//*********************************************************************************//


//vendemos
ShortSignal = cond4 AND cond5 AND cond6;
ShortLimitPrice = Upperband;
// now we check if limit was hit
Short = Ref(ShortSignal,-1) AND Open > Upperband AND L < Upperband;
ShortPrice = Min(ShortLimitPrice, Open);

ComprobacionB = ValueWhen(Short, ShortPrice);
Cover = IIf(BarsSince(Short)>=2, Open<ValueWhen(Short, ShortPrice), Null);
CoverPrice = Open;
Plot(ComprobacionB, "ComprobacionB", colorPink, styleThick);



//ELIMINAR SENYALS EXTRA//
Buy = ExRem(Buy, Sell);
//Short = ExRem(Short, Cover OR Buy);
Sell = ExRem(Sell, Buy);
//Cover = ExRem(Cover, Short);

//StopLoss
ApplyStop(stopTypeLoss, stopModePercent, 5, 1, False);



//pantalla
Plot(Upperband, "Upperband", colorGreen, styleLine);
Plot(Lowerband, "Loweband", colorRed, styleLine);
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorGreen, 0, L, -15 ); 
PlotShapes( IIf( Buy , shapeHollowCircle, shapeNone ), colorGreen, 0, BuyPrice, 0 ); 
//PlotShapes( IIf( Short , shapeDownArrow, shapeNone ), colorBrown, 0, H, -15 ); 
//PlotShapes( IIf( Short, shapeHollowCircle, shapeNone ), colorBrown, 0, ShortPrice, 0 ); 
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, -15 ); 
PlotShapes( IIf( Sell, shapeHollowCircle, shapeNone ), colorRed, 0, SellPrice, 0 ); 
//PlotShapes( IIf( Cover AND NOT Buy, shapeUpArrow, shapeNone ), colorDarkGreen, 0, L, -15 ); 
//PlotShapes( IIf( Cover AND NOT Buy, shapeHollowCircle, shapeNone ), colorDarkGreen, 0, CoverPrice, 0 ); 


I have already solved the problem with the purchase, the problem was that it was buying even if I had not reached that level, now I have put it like this:

Buy = Ref(BuySignal,-1) AND Open < Lowerband AND H > Lowerband;

The truth is that I don't know how to use stopTypeProfit, since I don't know how much will be the gain in case there is one... the truth is that the topic of the stops is the worst thing for me (together with the loops and the functions, especially at syntactic level since I master the concept thanks to Python).

Thank you very much for your answer, fxshrat. I have read the manual, but unfortunately I have not been able to adapt it to my needs...

Thank you very much for your time.

If you want to activate (trail, etc.) stop after certain numer of bars after entry have passed then you may use ValidFrom argument of Applystop function.

And to exit as soon as trade price is greater than entry price you may set profit amount to minimum price movement TickSize .

Sell = Cover = 0;
SellPrice = CoverPrice = Open;

//StopLoss
ApplyStop(stopTypeLoss, stopModePercent, 5, 1, False);

// Profit stop
TickSize = 0.01;
valid_from = 2;// n-bars after entry
exit_at_stop = 0;// exit at trade price
// Profit target exit for both Sell and Cover
ApplyStop( stopTypeProfit, stopModePoint, TickSize, exit_at_stop, False, 0, valid_from);

And as I said remove ExRem and replace all your Sell and Cover conditions by setting them to zero.

To Plot stops add eq = Equity(1,0); after ApplyStop and before Plot.

1 Like

Thank you again! One question, why delete ExRem? It is not the case and it works fine, but I don't understand how this code removes the extra signals without using exrem.

Thanks for your time!

@EJSM,

Would you please read documentation of Equity().

1 : works as 0 but additionally updates buy/sell/short/cover arrays so all redundant signals are removed exactly as it is done internally by the backtester plus all exits by stops are applied so it is now possible to visualise ApplyStop() stops.

(Besides Sell and Cover are set to zero.)

1 Like

I will do it now, thank you very much for your invaluable help.

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