Calculating Swing based on a specific event

Questions about MultiCharts and user contributed studies.
bobfxeuro
Posts: 5
Joined: 11 Mar 2015
Has thanked: 2 times

Calculating Swing based on a specific event

Postby bobfxeuro » 25 Jun 2017

Hello all,
Some help please!!

Part of my strategy for entry is that I am wanting to find the 61.8% retracement on a recent swing. And backtest these 61.8% retracement levels as they are my point of entry.
The easy bit is to work out the Retracement level from the swing values.
The difficult part is that I am unable to find the Values of the swing. Ie Highest Bar, Lowest Bar and Bar numbers.
I have looked at previous codes and they all relate to Swing based on a set of Bars or price.
I am wanting to base the swing on a set of conditions being met and to capture this information.
For simplicity I want to know the Swing Values for a specific past event.
In this case the event is
The price crosses above the moving average for a minimum of say 20 bars and later the price crosses below the Moving Average. From this period, I want to work out The highest high, Lowest Low, and number of bars.
My approach to this issue was to
1 Try to Capture the Bar number when Price Crossed above the Moving Average.
2 As long as 20 bars have been achieved where the price has continues to be above the Moving average then record the Barnumber where the price crosses below the Moving Average.
3 From the Above information I can now find the High and low points of the swing as I now know the amount of Bars of the previous swing ( Based on my conditions)

With regret my code ( below) is wrong.
Please can somone help me..

In advance,

Thanks..

My code is:
Inputs: Price(Close);
Inputs: MaRed(200);
Var: RedMa(0);
Var: BarNumberStartofswing(0);
Var: BarNumberEndofswing(0);
Var: Barsback(0);
Var: Highofswing(0);
Var: Lowofswing(0);
Var: FibRetracementL(0);
Var: FibRetracementS(0);

RedMa = average(price, MaRed); {Moving Average Calculation}

{*************Capture the barnumber when price crosses above the Moving Average}********
If the currentbar crosses above RedMa and BarNumber >1 then begin
BarNumberStartofswing = BarNumber ;

{*************Capture the barnumber when price crosses below Moving Average}********
If the currentbar crosses below RedMa and BarNumber >1 then
BarNumberEndofswing = BarNumber ;

{ *******We now know Bar number start of event and Bar number event finished, we now calclate Bars of entire swing)
Barsback = (BarNumberEndofswing - BarNumberStartofswing );}

end;

{ ****Caputre Highest point of Swing}
Highofswing = highest(High, Barsback);

{ ****Caputre Lowest point of Swing}

Lowofswing = Lowest(Low, Barsback);

{Retracement levels**********}

FibRetracementL = Highofswing - 0.618*(Highofswing - Lowofswing);
FibRetracementS = Lowofswing + 0.618*(Highofswing - Lowofswing);

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Calculating Swing based on a specific event

Postby TJ » 25 Jun 2017

Please see post #1 and post #2
viewtopic.php?t=11713

bobfxeuro
Posts: 5
Joined: 11 Mar 2015
Has thanked: 2 times

Re: Calculating Swing based on a specific event

Postby bobfxeuro » 26 Jun 2017

Dear TJ,
I have tagged my code.
Part of my strategy for entry is that I am wanting to find the 61.8% retracement on a recent swing. And backtest these 61.8% retracement levels as they are my point of entry.
The easy bit is to work out the Retracement level from the swing values.
The difficult part is that I am unable to find the Values of the swing. Ie Highest Bar, Lowest Bar and Bar numbers.
I have looked at previous codes and they all relate to Swing based on a set of Bars or price.
I am wanting to base the swing on a set of conditions being met and to capture this information.
For simplicity I want to know the Swing Values for a specific past event.
In this case the event is
The price crosses above the moving average for a minimum of say 20 bars and later the price crosses below the Moving Average. From this period, I want to work out The highest high, Lowest Low, and number of bars.
My approach to this issue was to
1 Try to Capture the Bar number when Price Crossed above the Moving Average.
2 As long as 20 bars have been achieved where the price has continues to be above the Moving average then record the Barnumber where the price crosses below the Moving Average.
3 From the Above information I can now find the High and low points of the swing as I now know the amount of Bars of the previous swing ( Based on my conditions)

With regret my code ( below) is wrong.
Please can somone help me..

In advance,

Thanks..

Code: Select all

Inputs: Price(Close);
Inputs: MaRed(200);

Var: RedMa(0);
Var: BarNumberStartofswing(0);
Var: BarNumberEndofswing(0);
Var: Barsback(0);
Var: Highofswing(0);
Var: Lowofswing(0);
Var: FibRetracementL(0);
Var: FibRetracementS(0);

RedMa = average(price, MaRed); {Moving Average Calculation}

{*************Capture the barnumber when price crosses above the Moving Average********}
If the currentbar crosses above RedMa and BarNumber >1 then begin
BarNumberStartofswing = BarNumber ;

{*************Capture the barnumber when price crosses below Moving Average********}
If the currentbar crosses below RedMa and BarNumber >1 then
BarNumberEndofswing = BarNumber ;

{ *******We now know Bar number start of event and Bar number event finished, we now calclate Bars of entire swing)
Barsback = (BarNumberEndofswing - BarNumberStartofswing );}

end;

{ ****Caputre Highest point of Swing}
Highofswing = highest(High, Barsback);

{ ****Caputre Lowest point of Swing}

Lowofswing = Lowest(Low, Barsback);

{Retracement levels**********}

FibRetracementL = Highofswing - 0.618*(Highofswing - Lowofswing);
FibRetracementS = Lowofswing + 0.618*(Highofswing - Lowofswing);

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Calculating Swing based on a specific event

Postby TJ » 26 Jun 2017

Dear TJ,
I have tagged my code.
Part of my strategy for entry is ...

With regret my code ( below) is wrong.
Please can somone help me..

In advance,

Thanks..

You have a complex logic with multiple conditions.
ie this is an advanced level programming project.
ie. this is not a question that can be answered with 25 words or less.

My suggestion is to start with a flow chart.

You already have a clear picture of what you want.
You are able to articulate the logic flow in words.
(Although you are only talking in conceptual terms to us)

What you need to do is to take one concrete example as a starting point (any example would do)
and use it to draw a flow chart.
With a flow chart, you can visualize this complex concept as a traceable road map of logic flows,
Programming is simply converting your flow chart into simple step-by-step codes.

See post #6
viewtopic.php?t=11713

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Calculating Swing based on a specific event

Postby JoshM » 28 Jun 2017

Part of my strategy for entry is that I am wanting to find the 61.8% retracement on a recent swing. And backtest these 61.8% retracement levels as they are my point of entry.
The easy bit is to work out the Retracement level from the swing values.
The difficult part is that I am unable to find the Values of the swing. Ie Highest Bar, Lowest Bar and Bar numbers.
Do you might have tried the built-in `PivotHighVS()` and `PivotLowVS()` functions? Those return the recent swing high and swing low, which saves you the trouble of having to figure out that yourself.

There are also built-in example indicators and signals that use those functions (like 'Pivot High' and 'Pivot Low').

bobfxeuro
Posts: 5
Joined: 11 Mar 2015
Has thanked: 2 times

Re: Calculating Swing based on a specific event

Postby bobfxeuro » 29 Jun 2017

Hello Josh,
Thanks for your reply.
Greatly appreciated and always remembered!
Bobby..


Return to “MultiCharts”