How to if previous signal was short AND < 15 days ago AND current signal price is below previous signal

I am attempting to give the cover signal a bit more room if a short is re-entered after the following 3 conditions.

//Condition1 = If previous signal was SHORT AND current signal is SHORT

//Condition2 = previous signal was less 15 bars ago

//Condition3 = current signal price is below previous entry price

Cover = IIf(Condition1 AND Condition2 AND Condition3, C > (Ref(H, -1)*1.05), C > (Ref(H, -1)));

Would someone please be kind enough to help as to how to code the conditions correctly?

Not sure exactly what your after. But have you tried valuewhen() function?

Re-posting to clarify question

I believe through a process of trial / error and lots of reading I came up with a solution for conditions 1 and 2. I’ve created plots in order to get visual confirmation the individual condition mentioned above was met.

Finding a solution for condition 3 has been challenging to say the least. My goal is to plot on current bar if most recent FirstShort entry price is above today's FirstShort entry price. I’m currently on my 12th attempt in finding solution. Any help or guidance would be greatly appreciated.

/////////////////////// Condition 1 ///////////////////////

LastWasShort = BarsSinceLastCover < BarsSinceLastSell;
LastWasBuy = BarsSinceLastCover > BarsSinceLastSell;
PlotShapes(IIf(LastWasShort AND FirstShort,shapeCircle,shapeNone),colorOrange,0,H, Offset=85;
PlotShapes(IIf(LastWasBuy AND FirstBuy,shapeCircle,shapeNone),colorYellow,0,L, Offset=-85);

/////////////////////// Condition 2 ///////////////////////

PlotShapes(IIf(BarsSinceLastCover < 15,shapeCircle,shapeNone),colorOrange,0,H, Offset=85);
PlotShapes(IIf(BarsSinceLastSell < 15,shapeCircle,shapeNone),colorYellow,0,L, Offset=-85);

/////////////////////// Condition 3 ///////////////////////

Hi Metamega,

Thank you for your suggestion. I re-posted my solutions to conditions 1 and 2 and hope it clarifies what I am after with regards to my remaining question in condition 3.

I've tried the valuewhen() function various times with no success. This is my most recent attempt:

PlotShapes(IIf((LastValue(ValueWhen(FirstShort,O)) > FirstShort),shapeCircle,shapeNone),colorOrange,0,H, Offset=85);
PlotShapes(IIf( ValueWhen(firstshort,C,2) > ValueWhen(firstshort,Close),shapeCircle,shapeNone),colorOrange,0,H,offset=85);
//SYNTAX  ValueWhen(EXPRESSION, ARRAY, n = 1)  
//FUNCTION  Returns the value of the ARRAY when the EXPRESSION was true on the n -th most recent occurrence. 
//Note: this function allows also 0 and negative values for n - this enables referencing future  

The way that is wrote it will plot a circle on every bar that the previous "Shorts Close" was greater then the last visible "Short Close". Also It uses "Close" to compare if its greater than. If your code has some value for shortprice or entry delay, plug in the corresponding array.

If you only wanted it to plot if condition 1 and 2 are present you could try something like this.

condition3 = condition1 AND condition2 AND "short criteria here" AND ValueWhen(firstshort,C,2) > ValueWhen(firstshort,C);

Tha's more of a pseudo code. If you only want to worry about plotting condition 3 if other criteria are met.

Hoping its somewhat in the direction your looking for. It doesn't come natural to myself so I usually have to tinker with shapes and columns to get AFL to do what I want.

Just to clarify the issue with your "Valuewhen()" usage, your looking for a numerical value to be greater then True(1) or False(0).

"FirstShort" will return True(1) or False(0), while "valuewhen(firstshort,0)" will return the open price of the last "firstshort" signal.



I can not thank you enough for your help. Your post above is exactly what I was looking for.

Again, thank you very much.

Good day to you kind Sir!

Any thoughts on how to assign all 3 conditions into one variable? The Plots below work fine:

PlotShapes(IIf(FirstShort AND (BarsSince(Cover) < BarsSince(Sell)) AND (BarsSince(Cover) < 15) AND (ValueWhen(FirstShort,O,2) > ValueWhen(FirstShort,Open)),shapeCircle,shapeNone),colorOrange,0,H, Offset=85);
PlotShapes(IIf(FirstBuy AND (BarsSince(Cover) > BarsSince(Sell)) AND (BarsSince(Sell) < 15) AND (ValueWhen(FirstBuy,O,2) < ValueWhen(FirstBuy,Open)),shapeCircle,shapeNone),colorYellow,0,L, Offset=-85);

But when I try and assign to variables CombinedS AND Combined the results are NOT the same as the plots above:

CombinedS = FirstShort AND (BarsSince(Cover) < BarsSince(Sell)) AND (BarsSince(Cover) < 15) AND (ValueWhen(FirstShort,O,2) > ValueWhen(FirstShort,Open));
Combined = FirstBuy AND (BarsSince(Cover) > BarsSince(Sell)) AND (BarsSince(Sell) < 15) AND (ValueWhen(FirstBuy,O,2) < ValueWhen(FirstBuy,Open));

PlotShapes(IIf(CombinedS,shapeCircle,shapeNone),colorOrange,0,H, Offset=85);
PlotShapes(IIf(Combined,shapeCircle,shapeNone),colorYellow,0,L, Offset=-85);

Make sure you are editing the same AFL file that is being used by the chart.

Drag-&-Drop or insert creates a copy in the default hidden Folder by the same name.

1 Like

WHOA! Good catch travick.

Thank you very much for your response.