Find the most recent Swing Low from the Entry

Hi!
in a strategy I am working on, I need to set the Stop Loss below the low of the most recent Swing Low for each trade.
Is there any built-in function or easy way to get the low of the recent swing low and assign the SL below that low?
Thanks.

1 Like

Any solution to find recent Swing High/Low?!

2 Likes

Hi!
I need to Find the recent Swing Low comes after an entry Signal.

SL

Assume I have the following code:

Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );


StopLoss = BuyPrice - "Recent Swing Low?";
  
ApplyStop(stopTypeLoss  , stopModePoint, StopLoss, 1, 0);

To calculate the StopLoss amount, first I need to find the most recent Swing Low before each entry signal. Of course, one can loop over the candles and find the Swing Low level, but it should an easier way to do it.

Thanks.

There are numerous way to define a swing low. How do you define yours ?

1 Like

Hello again!
You are right. The most common definition for a Swin Low is when price makes a low followed by two consecutive higher lows.

Well , the above setence would translate into AFL something like that.

SwingLow = Low < Ref(Low, 1) and Ref(Low, 1) < Ref(Low, 2);

You should be aware that you are looking into the future,
but if you make sure your entry signal is at least 3 bars from
SwingLow you will be safe

2 Likes

To add to awilson, like he mentioned you need to define in a precise way. So his example is perfect if your entry is right after the swing low.

Other option is the llv() function. Can retrieve the lowest value “n” days ago.

1 Like

awilson, thanks for your reply.
Your suggestion is not correct!
If I would be sure that the Swing Low is three bars away from the entry point then Ref(L,3) would do the job!!
The problem is that you don't know the location of the Swing Low.
What do you mean by "you are looking into the future"!! Why should I find a swing low after the entry signal?!!
I think you are completely misunderstood with what I need.

Metamega, same as the previous answer. If I know that entry is right after the swing low, the why I need to find the Swing Low?!!
The question is how to find the recent Swing Low!
LLV value is also not correct. LLV return the lowest low value of the recent n candles (not days!) regardless of if it is Swing low or not!

1 Like

@Oscar: Before telling experienced forum participants that "they completely misunderstood", you should probably try harder to understand their responses to you.

What @awilson was correctly trying to explain to you is that your definition of a Swing Low requires that you look into the future to confirm that a Swing Low has occurred. For example, if Tuesday's close is lower than Monday's close (which probably should be but is not included in the current swing low rules), then you potentially have a Swing Low on Tuesday. But you cannot confirm that it's a Swing Low until you have two higher lows, so the earliest your Swing Low can be confirmed is on Thursday at the close. Because of that, if you enter a trade before Thursday's close then you can't use the Tuesday swing low. If there's a MACD crossover on Wednesday, then you would need to look back to the Swing Low that occurred on or before Monday because the Tuesday Swing Low is still unconfirmed at that point in time.

As for @Metamega's response, he was simply trying to point out that instead of using a Swing Low as your stop price you could use the Lowest Low that has occurred within the past N days. That's different than the low from exactly N days ago. You could look up Donchian Channels to see how people use the Lowest Low and Highest High over the past N days/bars in their trading systems.

All my examples assume you are using daily bars, but if you are running on a shorter interval and still need to find Swing Low or LLV on daily bars, then that can be easily accomplished using AmiBroker's TimeFrameCompress/TimeFrameExpand commands. However, that's more advanced than what you're currently trying to accomplish.

3 Likes

Well in this case I apologize, sorry making you waste your time

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

Buy = Cross( MACD(), Signal() );
PlotShapes(Buy * shapeUpArrow, colorGreen, 0, Low);
Sell = Cross( Signal(), MACD() );
PlotShapes(Sell * shapeDownArrow, colorRed, 0, High);

InLong = Flip(Buy, Sell);

IsSwingLow = Low < Ref(Low, 1) and Ref(Low, 1) < Ref(Low, 2);
IsSwingLow = IsSwingLow AND NOT InLong;

PlotShapes(IsSwingLow * shapeSmallCircle, colorRed, 0, Low);

ValueWhenSwingLow = ValueWhen(IsSwingLow, Low);
ValueWhenSwingLow = Ref(ValueWhenSwingLow, -2);
ValueWhenSwingLow = ValueWhen(Buy, ValueWhenSwingLow);

ValueWhenSwingLow = IIf(InLong, ValueWhenSwingLow, Null);

Plot(ValueWhenSwingLow,"ValueWhenSwingLow",colorBlue);

3 Likes

Hello again!

SwingLow = Low < Ref(Low, 1) and Ref(Low, 1) < Ref(Low, 2);

According to the Ref function, A positive references "n" periods in the future; a negative references "n" periods ago. So Ref(Low, 1) is one candle ahead of the current candle (entry signal in my case). Or at least this is what I understand. Please correct me if I'm wrong!
If I want to place an order on tomorrow's open (daily time frame), then I would look in the past to find the last Swing low. This means that from the current candle, I need to move backwards until a Swing Low confirmed.

@Metamega's response is not correct as well. The lowest low value of the last n candle might not be the Swing Low.
Consider this example:
SwingLow
If you start from the last candle to the right, then LLV(L,3) or LLV(L,4) or whatever number is, of course, the lowest low value BUT it is not the Swing Low. This is what I want to point out.

Thanks anyway for your time and reply.

1 Like

@Oscar there is something in the tone of your responses that is not polite. Members are trying to help you and you write (with exclamation marks!) that there suggestion was incorrect.
I also think your instructions were incomplete.

ThreeBarSwingLow = L > Ref( L, -1 ) AND Ref( L, -1 ) > Ref( L, -2 );
PlotShapes( shapesmallCircle * ThreeBarSwingLow, colorYellow, 0, Ref( L, -2 ), -25, -2 );

image

But in your post you show this chart, where your "swing low" is actually four bars before your entry!!! :smile:
image

I don't know the terminology for "swing" high and low but it implies to me a change in direction. If you have 6 higher lows in a row, is the low from 3 bars ago (in the middle of that streak) a "swing low"? I don't think so. I think you need to be more exact in your definition.

Just add a mathematical/formula definition of that part "when price makes a low"

Good luck.

3 Likes

I apologize if the way I formulate my statement is not polite.
Regarding Swing Low, it doesn't matter if it is 3-4 or 5 candles before the entry. As far as the lowest low followed by two consecutive higher lows, it considered as Swing Low.
which means that we should have:

L > Ref(L-1) AND Ref(L-1)  > Ref(L-2)  AND Ref(L-2)  < Ref(L-3)

But this pattern (Swing Low) can occur three or four or ten candles before the current candle.
One way to find this pattern is to for loop over the candles and break when this pattern confirmed. However, I was thinking that it might be other ways to do this without a loop.

Sorry again and thanks to all for your time and support.

2 Likes

@Oscar no problem, sometimes the internet does not convey emotions well and it's easy to be interpreted as angry or aggressive even if that was not the writers intent.

1 Like

@portfoliobuilder @awilson know their way around AFL more then myself but wonder if a zigzag or zig zag ATR that I saw around here recently could work. I know it’s a forward looking/Painting indicator but the recent low at entry should still hold true as swing low value(could change its value later).

here is more or less the same that already has been answered. These pivots they often call "fractal pivots". The swinglow is shown in green. It starts plotting not right at the pivot itself but after "rightStrength" bars, then it is first known.

SetTradeDelays( 0, 0, 0, 0 );
SetOption( "CommissionMode", 3 );
SetOption( "CommissionAmount", 2.32 );
SetOption( "FuturesMode", True );
NumContracts = 1;
PositionSize = NumContracts * MarginDeposit;
SetOption( "MaxOpenPositions", 1 );

rightstrength = Param( "Right Strength", 5, 2, 50, 1 );
leftstrength = Param( "Left Strength", 10, 2, 50, 1 );

bi = BarIndex();

pk = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
pk = pk AND LastValue( bi ) - ValueWhen( pk, bi ) > rightstrength;

tr = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;
tr = tr AND LastValue( bi ) - ValueWhen( tr, bi ) > rightstrength;

swinglow = Ref( ValueWhen( tr, L ), -rightStrength );
swinghigh = Ref( ValueWhen( pk, H ), - rightStrength );

Buy = Cross( MACD(), Signal() ) AND C >= swinglow;
Sell = Cross( Signal(), MACD() ) OR Cross( swinglow, C );
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
BuyPrice = SellPrice = C;

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );
Plot( swinglow, "Swing Low", colorGreen, styleLine, Null, Null, 0, 0, 1 );
Plot( swinghigh, "Swing High", colorRed, styleLine, Null, Null, 0, 0, 1 );
PlotShapes( shapeSmallCircle * tr, ColorRGB( 0, 255, 0 ), 0, L, -10 );
PlotShapes( shapeSmallCircle * pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );

PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L, -15 );
PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), 
			IIf( !Cross( swinglow, C ) AND Cross( Signal(), MACD() ), colorRed,
            IIf( Cross( swinglow, C ) AND !Cross( Signal(), MACD() ), colorOrange,
            IIf( Cross( swinglow, C ) AND Cross( Signal(), MACD() ), colorYellow, colorDefault ) ) ), 0, H, -15 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );
5 Likes

If he does not understand that his definition of swing low, not mine, looks into the future, is because he does not understand how AFL works.

https://www.amibroker.com/guide/h_understandafl.html

1 Like

Hi awilson,
I read that article several times till now. I am new in Amibroker community, but not in trading and programming.
It doesn't matter if my definition seems to look in the future, but in trading, there is no such as looking in the future!
Looking in the future or past is always relative to the reference point. However "the most recent" swing low by its definition means past not future.
In addition, if I got an entry signal today, I cannot look to the future and use the coming Swing Low to make a decision now (here assign a Stop Loss to the low of the future Swing Low), thus looking in the future is meaningless.
You can read the Swing Low definition then as the following:
When two consecutive higher lows come after a lower low.

Thanks again.

@Oscar: I can assure you that there are many active and experienced traders on this forum as well as some superb programmers. I will echo @portfoliobuilder's observation that you seem intent on lecturing the rest of us and discounting the help that people are trying to give you.

Actually, it does matter if your definition looks into the future. Because while you are correct that in live trading you cannot look into the future, in a backtest you can. This is known as a "future leak" and is something that inexperienced developers often do without realizing it. Having a future leak in your backtest is a good way to guarantee that your live trading results will not live up to your expectations.

If you reread the responses to your post, you will discover that @awilson, @portfoliobuilder, and others are all trying to help you avoid a future leak in your code. You will do this by making sure a swing low is confirmed before you use it as a stop price. This can easily be achieved with one or two lines of code, probably utilizing the Ref() and ValueWhen() functions. I think it will help your understanding of AFL and particularly its array-processing functionality if you figure this out on your own rather than one of us just writing the code for you.

2 Likes