Gann Swing - Show Bar

Hello Forum,

 

I feel there is a real lack of scripts from the public on this forum and also on the internet when it comes to trading methods. I can search in every direction for a MetaTrader script, near impossible to find a Optuma Script.

I am in the process of trying to get the script to work correctly, however it is having some small technical errors to what I am trying to achieve. I copied to script prebuilt into OPTUMA and then made a small change, however it still needs tweaking - but I am unsure which section it is!

 

How to Enter a Trade on a Swing Chart

When using Gann Swing Theory to buy a stock buy on the presence of higher swing highs and higher swing lows, when price rises $0.01 above the previous swing high after confirmation of a higher swing low.

//GANN SWING IS UP

GS1 = GANNSWING(USEINSIDE=False, METHOD=Use Next Bar); SWINGUP() and

//GANN SWING 1 is GREATER than Swing Start GS1
(SWINGEND(GS1) > SWINGSTART(GS1,OFFSET=1)) AND

//GANN SWING 2 is GREATER than Swing Low (2)
((SWINGEND(GS1,OFFSET=2) > SWINGSTART(GS1,OFFSET=2)) AND
//SWING HIGH Breaks Above The High of Swing High (1)
(HIGH() >= SWINGSTART(GS1,OFFSET=3)))

 

 

There is defiantly mistakes with this script, However this is what I am trying to achieve - please see the image.

Hi Jonathan,

Thank you for your post.

There are two main issues i can see in the script, the first is with SWINGUP(). It should be SWINGUP(GS1).

Secondly, looking at your screen shot you are using Gann Swing Settings of Use Outside Bar however the script is set to Use Next Bar. I’ve kept the adjusted scripts to Next Bar, but if you are comparing them to your chart you will need to update the Gann Swing Overlays settings.

I believe the following scripts are what you are after…

 

To Buy

GS1 = GANNSWING(METHOD=Use Next Bar);
// current swing is Up swing
SWINGUP(GS1) and
// current high breaks above previous swing high
(High() > SWINGSTART(GS1,OFFSET=1)) and
// current up swing's low is higher than previous up swing's low. preivous up swing is offset by 2
SWINGSTART(GS1) > SWINGSTART(GS1,OFFSET=2)

 

To Sell

GS1 = GANNSWING(METHOD=Use Next Bar);
// current swing is Down swing
SWINGDOWN(GS1) and
// current low breaks below previous swing low
(Low() < SWINGSTART(GS1,OFFSET=1)) and
// current down swing's high is lower than previous down swing's high.
SWINGSTART(GS1) < SWINGSTART(GS1,OFFSET=2)

 

Thank you for providing this script.

 

However there appears to be an error in the code.

 

I changed some code to >= (would > than 0.01c be a way to limit even highs?)

 

Please see both images. The second image, contains changed code & also - Red box for where the script is finding invalid rules, and green where the script highlighted the rule correctly.

The results in the red box appear to be due to the how normal High() data lists join to the Swing list when using High() >= SWINGSTART(), and where the criteria is placed in the script.

The following modified scripts should provide the results you are after:

Bull Script

GS1 = GANNSWING(METHOD=Use Next Bar, USECLUSTERS=False, USEBREAKOUT=False);
// current swing is Up swing
SWINGUP(GS1) and
// current up swing's low is higher than previous up swing's low. preivous up swing is offset by 2
SWINGEND(GS1, OFFSET=1) >= SWINGSTART(GS1,OFFSET=2) and
// current high breaks above previous swing high
(High() >= SWINGSTART(GS1,OFFSET=1)) and
SWINGSTART(GS1) >= SWINGSTART(GS1,OFFSET=2)

Bear script

GS1 = GANNSWING(METHOD=Use Next Bar);
// current swing is Down swing
SWINGDOWN(GS1) and
// current down swing's high is lower than previous down swing's high.
SWINGSTART(GS1) <= SWINGSTART(GS1,OFFSET=2) and
// current low breaks below previous swing low
(Low() <= SWINGSTART(GS1,OFFSET=1)) and
SWINGEND(GS1, OFFSET=1) <= SWINGSTART(GS1,OFFSET=2)

 

With your other question - (would > than 0.01c be a way to limit even highs?)

I think we could replace High() >= SWINGSTART() with (High() - SWINGSTART(GS1,OFFSET=1) > 0.01)

Hey Matthew

 

I think to get the script to be useful to other members who view this forum, we should work at perfecting the script.

Either right now, the program is faulty or the script is faulty.

 

I tried to modify different areas again without success, but here is my findings on a monthly chart.

Did you use the show bar tool and test the script out of curiosity, Am I doing something completely wrong on my end?

 

See image.

Hey,

 

I am still working on perfecting the code.

 

If anyone had input, feel free to add…

Hi Jonathan,

Try these scripts for the Show Bar:

Buy Signal

GS1 = GANNSWING(METHOD=Use Next Bar);
// Look for the downswing and its swing low is higher than the previous swing slow
(SWINGEND(GS1) < SWINGSTART(GS1)) and (SWINGEND(GS1) > SWINGSTART(GS1, OFFSET=1)) and
// And the current bar high is greater than the previous swing high
(High() > SWINGEND(GS1, OFFSET=1)) and (Low() > SWINGEND(GS1))
Sell Signal
GS1 = GANNSWING(METHOD=Use Next Bar); // Look for the upswing and its swing high is lower than the previous swing high (SWINGEND(GS1) > SWINGSTART(GS1)) and (SWINGEND(GS1) < SWINGSTART(GS1, OFFSET=1)) and // And the current bar low is lower than the previous swing low (Low() < SWINGSTART(GS1, OFFSET=1)) and (High() < SWINGSTART(GS1))
Note that the arrows will be applied to the bar where the swing is confirmed, as per the attached weekly chart of CBA.

Hey,
Thanks Darren.

 

Purely based on the rules, Optuma64bit/Market Analyst is not signalling all the possible buy signals that are valid.

Green is missed buy signals & red is missed sell signals.

I am obviously using a small sample of data here but I need to use this on a large scale scan of the top 100 stocks on a weekly basis.

Also I have not had any luck using the NoRepeat function on this script. Essentially I just want it to ‘ShowBar’ the latest signal once activated.

That is, show bar only once for each buy / sell signal. This way If pyramiding is activated the stock is not being bought 4 times in a month due to the show bar being displayed multiple times and the script being activated.

 

Furthermore, I know that we are using the GannSwing function. However I would like to get the script to work for both gann rules and also DOW theory rules. Is the gannswing script function suitable?

DG2-10-02-11.jpg

DG5-10-3-lge.jpg

Hi Jonathan,

Looking at the last screen image i think we need to split the script up further to fill in the missing signals. I’ve tested the following and it seems to do the trick:

Bull Signal

GS1 = GANNSWING(METHOD=Use Next Bar, USEBREAKOUT=False, USEINSIDE=False);
(
// Look for the downswing and its swing low is higher than the previous swing low
(SWINGEND(GS1) < SWINGSTART(GS1)) and (SWINGEND(GS1) > SWINGSTART(GS1, OFFSET=1)) and
// And the current bar high is greater than the previous swing high
(High() > SWINGEND(GS1, OFFSET=1)) and (Low() > SWINGEND(GS1))
) or
(
// Look for the upswing
(SWINGEND(GS1) > SWINGSTART(GS1)) and
// its swing low is higher than the previous up swing low
(SWINGSTART(GS1) > SWINGSTART(GS1, OFFSET=2)) and
// And the current swing high is greater than the previous swing high and current bar high must be greater than the swing high to force to include the first bar of a down swing
(SWINGEND(GS1) > SWINGSTART(GS1, OFFSET=1)) and (High() >= SWINGEND(GS1)))

 

Bear Signal

GS1 = GANNSWING(METHOD=Use Next Bar, USEBREAKOUT=False, USEINSIDE=False);
(
// Look for the upswing and its swing high is lower than the previous swing high
(SWINGEND(GS1) > SWINGSTART(GS1)) and (SWINGEND(GS1) < SWINGSTART(GS1, OFFSET=1)) and
// And the current bar low is lower than the previous swing low
(Low() < SWINGEND(GS1, OFFSET=1)) and (High() < SWINGEND(GS1))
) or 
(
// Look for the downswing 
(SWINGEND(GS1) < SWINGSTART(GS1))   and 
// its swing high is lower than the previous down swing high
(SWINGSTART(GS1) < SWINGSTART(GS1, OFFSET=2)) and
// And the current swing low is lower than the previous swing low
(SWINGEND(GS1)  < SWINGSTART(GS1, OFFSET=1)) and (Low() <= SWINGEND(GS1))
)

Attached is how the above scripts display on my chart of CBA now.

Once we have the core aspect of the script working, we can then look at adding No Repeats, etc.

 

Hey,

 

Thanks for this script. I am sure its much closer to the final product, however I am having a hard time implementing the script due to (&lt;=) placed into the script instead of < = > … Is there a way to have the forum not display (&lt;=) and instead display the script correctly?

 

I tried to modify it, however it is showing no results. I am under the impression that all the (&lt;=) actually equal < or >=

 

Hi Jonathan

Thanks for your last post regarding html coding of the < and > symbols. I’ve made a change to the forum that should prevent the code viewer from translating < and > to &lt; and &gt;

For your reference if you want your code to display like Matthew’s simply wrap your code in <pre>your_code_here</pre> tags. This can be done by swapping to the “Text” view tab and then select your code segment (highlight it) then click the “pre” button.

Hope this helps.

Regards

Mark

Hi all,

These are very useful scripts, thank you. I’m using them successfully in the Back Tester on a “1 week” data timeframe.

Matthew, is the following a valid addition to your scripts? I’m trying to confirm that the monthly Gann Swing is also up/down before a weekly buy/sell is triggered.

(Matthew’s_bull_script) And GANNSWING(MONTH(), METHOD=Use Next Bar) IsUp

(Matthew’s_bear_script) And GANNSWING(MONTH(), METHOD=Use Next Bar) IsDown

Hi Panu,

Yes that should work.

Hi All,

Just wondering if anyone has had any success building on the above script to return an entry/exit price based on the previous peak (or trough for a bear market)?

In theory, all I’m trying to do at this stage is have the script return the price of the previous peak. Running the below script returns what appears to be some form of null value (breaks in the plot). Breaking down the script shows that the individual components of the code execute as expected which is why I’m thinking that it may have something to do with the assembly.

GS1 = GANNSWING(METHOD=Use Next Bar, USEBREAKOUT=False, USEINSIDE=False);

TEST1 = (
// Look for the downswing and its swing low is higher than the previous swing low
(SWINGEND(GS1) < SWINGSTART(GS1)) and (SWINGEND(GS1) > SWINGSTART(GS1, OFFSET=1)) and
// And the current bar high is greater than the previous swing high
(High() > SWINGEND(GS1, OFFSET=1)) and (Low() > SWINGEND(GS1)));

TEST2 = (
// Look for the upswing
(SWINGEND(GS1) > SWINGSTART(GS1)) and
// its swing low is higher than the previous up swing low
(SWINGSTART(GS1) > SWINGSTART(GS1, OFFSET=2)) and
// And the current swing high is greater than the previous swing high and current bar high must be greater than the swing high to force to include the first bar of a down swing
(SWINGEND(GS1) > SWINGSTART(GS1, OFFSET=1)) and (High() >= SWINGEND(GS1)));

//Determine Entry Price
IF( TEST1 > 0, SWINGEND(GS1, OFFSET=1), IF(TEST2 > 0, SWINGSTART(GS1, OFFSET=1), 0))

Hi Ryan,

If you just want the value of the swings displayed on the chart then you can enable the labels option:

Capture

To display the values in a watchlist for example will require a script using offsets to look at the previous swing high and swing low. The value of the offset count will depend if the current swing is up or down. Use the following to get the previous swing high for a 2 bar count:

// get the swings

gs1 = GANNSWING(SWINGCOUNT=2, METHOD=Use Next Bar);

// get the starting values of the swings

s1 = SWINGSTART(gs1);

// get the direction of the swing

d1 = SWINGUP(gs1);

//if the current swing is up get the previous swingstart for the peak or current swingstart if down

IF(d1==1,s1[1],s1)

In this example AMZN is in a downswing so the previous high was the swingstart S1 at 910.50, whereas CAKE is in an upswing so we need to look at the previous swingstart S1[1] - where the number in the is the number of offsets - to show 65.09:

Capture

Expanding on this topic, how can we incorporate the no repeat function?

GS1 = GANNSWING(METHOD=Use Next Bar, USEBREAKOUT=False, USEINSIDE=False); ( // Look for the downswing and its swing low is higher than the previous swing low (SWINGEND(GS1) < SWINGSTART(GS1)) and (SWINGEND(GS1) > SWINGSTART(GS1, OFFSET=1)) and // And the current bar high is greater than the previous swing high (High() > SWINGEND(GS1, OFFSET=1)) and (Low() > SWINGEND(GS1)) ) or ( // Look for the upswing (SWINGEND(GS1) > SWINGSTART(GS1)) and // its swing low is higher than the previous up swing low (SWINGSTART(GS1) > SWINGSTART(GS1, OFFSET=2)) and // And the current swing high is greater than the previous swing high and current bar high must be greater than the swing high to force to include the first bar of a down swing (SWINGEND(GS1) > SWINGSTART(GS1, OFFSET=1)) and (High() >= SWINGEND(GS1)) and NOREPEAT(BARS=1))

Gann No Repeat

 

is it possible to have the script only signal once, once it has been signalled it does not repeat until it is signalled again - freshly?

Hi guys, new to scripting, and have watched a bunch of the tutorials which are great, just getting stuck with one of the settings, I tried to copy one of the above scripts, but I don’t get the green or red arrows, instead just shaded lines from top to bottom, refer to image, am I missing something, as I cannot for the life of me find, how I change this in settings, is there a tutorial that I need to watch, as I feel I have missed something, thanks.

This is what I’m trying to get
CBA

This is what I get when I use the following code: GANNSWING(SWINGCOUNT=1,USEINSIDE=FALSE) TURNSUP
My Gann Swing Chart

My Settings as follows:

Script Settings

Appreciate any help with this, cheers

Hi,

The script needs to be used with the Show Bar tool. For more information please refer to the following article:

https://help.optuma.com/kb/faq.php?id=719