Gann SQ9 Price Levels_version-3
Download scriptThis script functions as a visual mapping tool for MetaTrader, designed to automatically project a series of price levels onto your trading chart based on a specific "anchor" price. Instead of manually drawing hundreds of lines, the script handles the mathematical heavy lifting to create a structured grid of price points.
Core Functionality
The logic operates by identifying a primary reference point (an anchor) that you place on your chart. Depending on which specific object you designate as the anchor, the script determines whether it should calculate prices moving "up" or "down" from that point.
Once the anchor is set, the script automatically generates a sequence of horizontal lines and text labels. These represent various price levels calculated using a mathematical formula involving square roots and factors.
Key Features & Logic Flow:
-
Dynamic Anchor Detection: The script looks for specific named objects on your chart (such as "1" through "6" or "-1" through "-6"). Each of these names serves a different purpose:
- They provide the starting price for your calculations.
- They determine the "multiplier," which helps the script adjust the math correctly based on the currency pair you are viewing (specifically handling the difference between standard pairs and JPY pairs).
- They dictate whether the calculated levels should represent "high" or "low" variations.
-
Automated Grid Generation: The tool creates a series of lines at regular intervals defined by your settings (e.g., every 90 degrees up to a maximum of 360). This allows you to see a comprehensive range of projected price levels instantly.
-
Smart Currency Adjustments: The script contains internal logic to detect if you are trading a Japanese Yen (JPY) pair. Because JPY pairs use different decimal pricing, the script automatically modifies its calculations to ensure the lines appear at the correct price positions regardless of the asset.
-
Visual Organization: For every level calculated, the script performs two actions:
- Draws a Horizontal Line: It places a visual line across the chart at the calculated price.
- Adds a Degree Label: It places a text label (e.g., "90°", "180°") next to the line. This helps you identify exactly which part of the calculation sequence each line represents.
-
Cleanup Mechanism: To keep your chart organized, the script includes a "de-initialization" process. If you remove the indicator, it automatically deletes all the lines and labels it created, ensuring your chart remains clean and free of leftover "ghost" objects.
//+------------------------------------------------------------------+
//| SQ9 (Price).mq4 |
//| Copyright © 2006, Matt Trigwell |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Matt Trigwell"
#property link "m.trigwell@gmail.com" // then hacked around by deanz.
#property indicator_chart_window
int line_spacing_multiplier=0;
extern int levels__to=360;
extern int lines_every=90;
extern double factorNumber=0.5;
extern color line_Color=Yellow;
extern int line_Style_choose_from_0_to_4=0;
extern int font_size=9;
bool is_this_the_HIGH_price=true;
int init()
{
return(0);
}
int deinit()
{
double AngleIndex=0;
string AngleName="";
for(AngleIndex=0;AngleIndex<=levels__to;AngleIndex=AngleIndex+lines_every)
{
AngleName = "Angle_" + AngleIndex;
ObjectDelete(AngleName + " Label");
ObjectDelete(AngleName + " Line");
}
return(0);
}
int start()
{
double the_High_or_Low_Price=0;
double AngleIndex=0;
string AngleName="";
double FactorIndex=0;
double AnglePriceLevel=0;
int Index=0;
string strLabel="";
if(ObjectFind("1")!=-1)
{
line_spacing_multiplier=1000000;
the_High_or_Low_Price = ObjectGet("1", OBJPROP_PRICE1);
is_this_the_HIGH_price=false;
}
if(ObjectFind("2")!=-1)
{
line_spacing_multiplier=100000;
the_High_or_Low_Price = ObjectGet("2", OBJPROP_PRICE1);
is_this_the_HIGH_price=false;
}
if(ObjectFind("3")!=-1)
{
line_spacing_multiplier=10000;
the_High_or_Low_Price = ObjectGet("3", OBJPROP_PRICE1);
is_this_the_HIGH_price=false;
}
if(ObjectFind("4")!=-1)
{
line_spacing_multiplier=1000;
the_High_or_Low_Price = ObjectGet("4", OBJPROP_PRICE1);
is_this_the_HIGH_price=false;
}
if(ObjectFind("5")!=-1)
{
line_spacing_multiplier=100;
the_High_or_Low_Price = ObjectGet("5", OBJPROP_PRICE1);
is_this_the_HIGH_price=false;
}
if(ObjectFind("6")!=-1)
{
line_spacing_multiplier=10;
the_High_or_Low_Price = ObjectGet("6", OBJPROP_PRICE1);
is_this_the_HIGH_price=false;
}
////////////////////////////////////////////////////////////////
if(ObjectFind("-1")!=-1)
{
line_spacing_multiplier=1000000;
the_High_or_Low_Price = ObjectGet("-1", OBJPROP_PRICE1);
is_this_the_HIGH_price=true;
}
if(ObjectFind("-2")!=-1)
{
line_spacing_multiplier=100000;
the_High_or_Low_Price = ObjectGet("-2", OBJPROP_PRICE1);
is_this_the_HIGH_price=true;
}
if(ObjectFind("-3")!=-1)
{
line_spacing_multiplier=10000;
the_High_or_Low_Price = ObjectGet("-3", OBJPROP_PRICE1);
is_this_the_HIGH_price=true;
}
if(ObjectFind("-4")!=-1)
{
line_spacing_multiplier=1000;
the_High_or_Low_Price = ObjectGet("-4", OBJPROP_PRICE1);
is_this_the_HIGH_price=true;
}
if(ObjectFind("-5")!=-1)
{
line_spacing_multiplier=100;
the_High_or_Low_Price = ObjectGet("-5", OBJPROP_PRICE1);
is_this_the_HIGH_price=true;
}
if(ObjectFind("-6")!=-1)
{
line_spacing_multiplier=10;
the_High_or_Low_Price = ObjectGet("-6", OBJPROP_PRICE1);
is_this_the_HIGH_price=true;
}
for(AngleIndex=0;AngleIndex<=levels__to;AngleIndex=AngleIndex+lines_every)
{
AnglePriceLevel = CalculateSquare(FactorIndex,the_High_or_Low_Price);
//Trim Zero's
if(Index==1)
{
strLabel = DoubleToStr(AngleIndex,1);
}
else
{
strLabel = DoubleToStr(AngleIndex,0);
}
AngleName = "Angle_" + AngleIndex;
if(ObjectFind(AngleName + " Line") != 0)
{
ObjectCreate(AngleName + " Line", OBJ_HLINE, 0, Time[40], AnglePriceLevel);
ObjectSet(AngleName + " Line", OBJPROP_STYLE, line_Style_choose_from_0_to_4);
if(MathMod(Index,2) == 0)
{
ObjectSet(AngleName + " Line", OBJPROP_COLOR, line_Color);
}
else
{
ObjectSet(AngleName + " Line", OBJPROP_COLOR, line_Color);
}
}
else
{
ObjectMove(AngleName + " Line", 0, Time[40], AnglePriceLevel);
}
if(ObjectFind(AngleName + " Label") != 0)
{
ObjectCreate(AngleName + " Label", OBJ_TEXT, 0, Time[20], AnglePriceLevel);
if(MathMod(Index,2) == 0)
{
ObjectSetText(AngleName + " Label", strLabel + "°", font_size, "Arial", line_Color);
}
else
{
ObjectSetText(AngleName + " Label", strLabel + "°", font_size, "Arial", line_Color);
}
}
else
{
ObjectMove(AngleName + " Label", 0, Time[20], AnglePriceLevel);
}
FactorIndex = FactorIndex + factorNumber;
Index = Index + 1;
}
return(0);
}
double CalculateSquare(double Factor, double Price)
{
double AnglePrice=0;
if(Price > 0)
{
if(StringFind(Symbol(),"JPY",0) == -1)
{
Price = Price * line_spacing_multiplier;
}
else
{
Price = Price * line_spacing_multiplier;
Price = Price / 100;
}
if(is_this_the_HIGH_price==false)
{
AnglePrice = MathPow(MathSqrt(Price) + Factor,2);
}
else
{
AnglePrice = MathPow(MathSqrt(Price) - Factor,2);
}
if(StringFind(Symbol(),"JPY",0) == -1)
{
AnglePrice = AnglePrice / line_spacing_multiplier;
}
else
{
AnglePrice = AnglePrice / line_spacing_multiplier;
AnglePrice = AnglePrice * 100;
}
}
return(AnglePrice);
}
Comments