Gann2
Download scriptThis script is designed for the MetaTrader trading platform to visualize specific price levels and structural trend lines based on Gann theory. It serves as a technical analysis tool that automatically plots geometric data onto a daily chart to help traders identify potential support and resistance zones.
Core Logic Overview
The script operates by identifying significant historical price movements and projecting them forward into the current market view. Here is a breakdown of how it processes this information:
1. Time-Based Cycle Identification The logic begins by referencing a specific set of historical dates (labeled as lunar cycles). It scans the chart to find the most recent relevant period from this list. Once identified, it establishes a start and end point in time. This allows the script to "anchor" its calculations to a specific historical window rather than using random data points.
2. High and Low Price Mapping Within that identified historical period, the script automatically searches for the absolute highest and lowest price points reached. It uses these extremes as the foundation for all subsequent drawings. By locating these peaks and valleys, the tool establishes the "boundaries" of a specific market cycle.
3. Dynamic Scaling and Step Calculation Rather than using static numbers, the script calculates a dynamic "step" value. This is based on the price sensitivity of the specific asset being traded (the point value) and a user-defined multiplier. This ensures that the distance between the lines remains proportional to the actual price of the currency or commodity, whether it is moving in small cents or large dollars.
4. Automated Geometry Plotting Once the boundaries and steps are defined, the script performs a series of automated drawing actions:
- Trend Lines: It generates a dense series of trend lines (labeled t1 through t20) that move between the historical start and end points. These represent various trajectories of price movement based on the calculated "top" and "bottom" levels.
- Horizontal Levels: It places a series of horizontal lines across the current price chart. These act as fixed targets or barriers, representing various fractional divisions of the price range.
- Offset Adjustments: To ensure these lines are clearly visible and not overlapping with the immediate candle data, the script applies a mathematical offset to the vertical positioning of every drawn object.
User Experience Summary
When this script runs, it removes the manual labor of drawing complex Gann fans or grid lines. It automatically identifies the significant "range" of a recent cycle, calculates the mathematically correct subdivisions for that range, and paints them onto your chart in real-time. This allows a trader to see where historical price levels correlate with current market geometry instantly.
#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| Gann.mq4 |
//| |
//| |
//| Converted by Mql2Mq4 v0.6 |
//| http://yousky.free.fr |
//| Copyright © 2006, Yousky Soft |
//| |
//| use on daily chart|
//+------------------------------------------------------------------+
#property copyright " Copyright © 2005, MetaQuotes Software Corp."
#property link " http://www.metaquotes.net/"
#property indicator_chart_window
#property indicator_color1 Blue
#property indicator_buffers 2
#property indicator_color2 Red
//+------------------------------------------------------------------+
//| Common External variables |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| External variables |
//+------------------------------------------------------------------+
extern double factor = 1;
extern double back = 0;
//+------------------------------------------------------------------+
//| Special Convertion Functions |
//+------------------------------------------------------------------+
int LastTradeTime;
double ExtHistoBuffer[];
double ExtHistoBuffer2[];
void SetLoopCount(int loops)
{
}
void SetIndexValue(int shift, double value)
{
ExtHistoBuffer[shift] = value;
}
void SetIndexValue2(int shift, double value)
{
ExtHistoBuffer2[shift] = value;
}
bool MoveObject(string name, int type, datetime Atime, double Aprice, datetime Atime2 = 0, double Aprice2 = 0, color Acolor = CLR_NONE, int Aweight = 0, int Astyle = 0)
{
if (ObjectFind(name) != -1)
{
int OType = ObjectType(name);
if ((OType == OBJ_VLINE) ||
(OType == OBJ_HLINE) ||
(OType == OBJ_TRENDBYANGLE) ||
(OType == OBJ_TEXT) ||
(OType == OBJ_ARROW) ||
(OType == OBJ_LABEL))
{
return(ObjectMove(name, 0, Atime, Aprice));
}
if ((OType == OBJ_GANNLINE) ||
(OType == OBJ_GANNFAN) ||
(OType == OBJ_GANNGRID) ||
(OType == OBJ_FIBO) ||
(OType == OBJ_FIBOTIMES) ||
(OType == OBJ_FIBOFAN) ||
(OType == OBJ_FIBOARC) ||
(OType == OBJ_RECTANGLE) ||
(OType == OBJ_ELLIPSE) ||
(OType == OBJ_CYCLES) ||
(OType == OBJ_TREND) ||
(OType == OBJ_STDDEVCHANNEL) ||
(OType == OBJ_REGRESSION))
{
return(ObjectMove(name, 0, Atime, Aprice) && ObjectMove(name, 1, Atime2, Aprice2));
}
/*
OBJ_CHANNEL,
OBJ_EXPANSION,
OBJ_FIBOCHANNEL,
OBJ_TRIANGLE,
OBJ_PITCHFORK
*/
}
else
{
return(ObjectCreate(name, type, 0, Atime, Aprice, Atime2, Aprice2, 0, 0) && ObjectSet(name, OBJPROP_COLOR, Acolor));
}
}
//+------------------------------------------------------------------+
//| End |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Initialization |
//+------------------------------------------------------------------+
int init()
{
SetIndexStyle(0, DRAW_LINE, STYLE_SOLID);
SetIndexBuffer(0, ExtHistoBuffer);
SetIndexStyle(1, DRAW_LINE, STYLE_SOLID);
SetIndexBuffer(1, ExtHistoBuffer2);
return(0);
}
int start()
{
//+------------------------------------------------------------------+
//| Local variables |
//+------------------------------------------------------------------+
int shift = 0;
double sum = 0;
int v1 = 0;
int v2 = 0;
double v3 = 0;
double v4 = 0;
double loop = 0;
int select = 0;
double start = 0;
double endof = 0;
double mult = 0;
double step = 0;
double v45 = 0;
double mml00 = 0;
double mml0 = 0;
double mml1 = 0;
double mml2 = 0;
double mml3 = 0;
double mml4 = 0;
double mml5 = 0;
double mml6 = 0;
double mml7 = 0;
double mml8 = 0;
double mml9 = 0;
double mml98 = 0;
double mml99 = 0;
double range = 0;
double octave = 0;
double mn = 0;
double mx = 0;
double bottomhit = 0;
double bottom = 0;
double top = 0;
double tophit = 0;
int look = 0;
double whereat = 0;
double offset = 0;
double finalH = 0;
double finalL = 0;
double x1 = 0;
double x2 = 0;
double x3 = 0;
double x4 = 0;
double x5 = 0;
double x6 = 0;
double y1 = 0;
double y2 = 0;
double y3 = 0;
double y4 = 0;
double y5 = 0;
double y6 = 0;
double lunarCycles_05[8];
SetLoopCount(0);
// loop from first bar to current bar (with shift=0)
lunarCycles_05[1] = StrToTime("2005.03.25 0:00");
lunarCycles_05[2] = StrToTime("2005.06.22 0:00");
lunarCycles_05[3] = StrToTime("2005.09.18 0:00");
lunarCycles_05[4] = StrToTime("2005.12.15 0:00");
lunarCycles_05[5] = StrToTime("2006.03.15 0:00");
lunarCycles_05[6] = StrToTime("2006.06.12 0:00");
lunarCycles_05[7] = StrToTime("2006.09.07 0:00");
lunarCycles_05[8] = StrToTime("2006.12.05 0:00");
SetLoopCount(0);
if( 10000 * Point == 1 ) mult = 1;
if( 10000 * Point == 100 ) mult = 100;
step = 0.0244140625 * mult*factor;
offset = step*4;
loop = 0;
for(select =1;select <=8 ;select ++){
if( loop == 0 && Time[1] < lunarCycles_05[select] ) {
loop = 1;
start = lunarCycles_05[select-2]*factor;
endof = lunarCycles_05[select-1];
}
}
for(shift =365;shift >=1 ;shift --){
if( Time[shift] == start ) {
v1 = shift;
}
if( Time[shift] == endof ) {
v2 = shift;
}
}
mml00=(Low[Lowest(NULL, 0, MODE_LOW,v1,v1-v2)]);
mml0=(High[Highest(NULL, 0, MODE_HIGH,v1,v1-v2)]);
bottomhit = 0;
tophit = 0;
for(look =1;look <=1000 ;look ++){
whereat = look * (step);
if( bottomhit == 0 && whereat > mml00 ) {
bottomhit = 1;
bottom = (look-1)*(step);
}
if( tophit == 0 && whereat > mml0 ) {
tophit = 1;
top = (look+1)*(step);
}
}
Comment(bottom," ",top," ",mml00," ",mml0);
MoveObject("t1d" ,OBJ_TREND,Time[v1],top+offset,Time[v2],top-step+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t2d" ,OBJ_TREND,Time[v1],top-step+offset,Time[v2],top-(step*2)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t3d" ,OBJ_TREND,Time[v1],top-(step*2)+offset,Time[v2],top-(step*3)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t4d" ,OBJ_TREND,Time[v1],top-(step*3)+offset,Time[v2],top-(step*4)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t5d" ,OBJ_TREND,Time[v1],top-(step*4)+offset,Time[v2],top-(step*5)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t6d" ,OBJ_TREND,Time[v1],top-(step*5)+offset,Time[v2],top-(step*6)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t7d" ,OBJ_TREND,Time[v1],top-(step*6)+offset,Time[v2],top-(step*7)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t8d" ,OBJ_TREND,Time[v1],top-(step*7)+offset,Time[v2],top-(step*8)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t9d" ,OBJ_TREND,Time[v1],top-(step*8)+offset,Time[v2],top-(step*9)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t10d" ,OBJ_TREND,Time[v1],top-(step*9)+offset,Time[v2],top-(step*10)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t11d" ,OBJ_TREND,Time[v1],top-(step*10)+offset,Time[v2],top-(step*11)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t12d" ,OBJ_TREND,Time[v1],top-(step*11)+offset,Time[v2],top-(step*12)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t13d" ,OBJ_TREND,Time[v1],top-(step*12)+offset,Time[v2],top-(step*13)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t14d" ,OBJ_TREND,Time[v1],top-(step*13)+offset,Time[v2],top-(step*14)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t15d" ,OBJ_TREND,Time[v1],top-(step*14)+offset,Time[v2],top-(step*15)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t16d" ,OBJ_TREND,Time[v1],top-(step*15)+offset,Time[v2],top-(step*16)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t17d" ,OBJ_TREND,Time[v1],top-(step*16)+offset,Time[v2],top-(step*17)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t18d" ,OBJ_TREND,Time[v1],top-(step*17)+offset,Time[v2],top-(step*18)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t19d" ,OBJ_TREND,Time[v1],top-(step*18)+offset,Time[v2],top-(step*19)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t20d" ,OBJ_TREND,Time[v1],top-(step*19)+offset,Time[v2],top-(step*20)+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t1u" ,OBJ_TREND,Time[v1],bottom-offset,Time[v2],bottom-offset+step,Red,1,STYLE_SOLID);// -2/8
MoveObject("t2u" ,OBJ_TREND,Time[v1],bottom+(step*2)-offset,Time[v2],bottom+(step*3)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t3u" ,OBJ_TREND,Time[v1],bottom+(step*3)-offset,Time[v2],bottom+(step*4)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t4u" ,OBJ_TREND,Time[v1],bottom+(step*4)-offset,Time[v2],bottom+(step*5)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t5u" ,OBJ_TREND,Time[v1],bottom+(step*5)-offset,Time[v2],bottom+(step*6)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t6u" ,OBJ_TREND,Time[v1],bottom+(step*6)-offset,Time[v2],bottom+(step*7)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t7u" ,OBJ_TREND,Time[v1],bottom+(step*7)-offset,Time[v2],bottom+(step*8)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t8u" ,OBJ_TREND,Time[v1],bottom+(step*8)-offset,Time[v2],bottom+(step*9)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t9u" ,OBJ_TREND,Time[v1],bottom+(step*9)-offset,Time[v2],bottom+(step*10)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t10u" ,OBJ_TREND,Time[v1],bottom+(step*10)-offset,Time[v2],bottom+(step*11)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t11u" ,OBJ_TREND,Time[v1],bottom+(step*11)-offset,Time[v2],bottom+(step*12)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t12u" ,OBJ_TREND,Time[v1],bottom+(step*12)-offset,Time[v2],bottom+(step*13)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t13u" ,OBJ_TREND,Time[v1],bottom+(step*13)-offset,Time[v2],bottom+(step*14)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t14u" ,OBJ_TREND,Time[v1],bottom+(step*14)-offset,Time[v2],bottom+(step*15)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t15u" ,OBJ_TREND,Time[v1],bottom+(step*15)-offset,Time[v2],bottom+(step*16)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t16u" ,OBJ_TREND,Time[v1],bottom+(step*16)-offset,Time[v2],bottom+(step*17)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t17u" ,OBJ_TREND,Time[v1],bottom+(step*17)-offset,Time[v2],bottom+(step*18)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t18u" ,OBJ_TREND,Time[v1],bottom+(step*18)-offset,Time[v2],bottom+(step*19)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t19u" ,OBJ_TREND,Time[v1],bottom+(step*19)-offset,Time[v2],bottom+(step*20)-offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("t20u" ,OBJ_TREND,Time[v1],bottom+(step*1)-offset,Time[v2],bottom+(step*2)-offset,Red,1,STYLE_SOLID);// -2/8
//MoveObject("v11" ,OBJ_VLINE,time[v1],0,time[v1],1000,Red,2,STYLE_SOLID);// -2/8
//MoveObject("v12" ,OBJ_VLINE,time[v2],0,time[v2],1000,Red,2,STYLE_SOLID);// -2/8
MoveObject("mm11" ,OBJ_HLINE,Time[0],top+offset,Time[0],top+offset,Red,1,STYLE_SOLID);// -2/8
MoveObject("mm12" ,OBJ_HLINE,Time[0],top-(step*1)+offset,Time[0],top-(step*2)+offset,Red,1,STYLE_SOLID);// -1/8
MoveObject("mm1" ,OBJ_HLINE,Time[0],top-(step*2)+offset,Time[0],top-(step*3)+offset,Red,1,STYLE_SOLID);// 0/8
MoveObject("mm2" ,OBJ_HLINE,Time[0],top-(step*3)+offset,Time[0],top-(step*4)+offset,Red,1,STYLE_SOLID);// 1/8
MoveObject("mm3" ,OBJ_HLINE,Time[0],top-(step*4)+offset,Time[0],top-(step*5)+offset,Red,1,STYLE_SOLID);// 2/8
MoveObject("mm4" ,OBJ_HLINE,Time[0],top-(step*5)+offset,Time[0],top-(step*6)+offset,Red,1,STYLE_SOLID);// 3/8
MoveObject("mm5" ,OBJ_HLINE,Time[0],top-(step*6)+offset,Time[0],top-(step*7)+offset,Red,1,STYLE_SOLID);// 4/8
MoveObject("mm6" ,OBJ_HLINE,Time[0],top-(step*7)+offset,Time[0],top-(step*8)+offset,Red,1,STYLE_SOLID);// 5/8
MoveObject("mm7" ,OBJ_HLINE,Time[0],top-(step*8)+offset,Time[0],top-(step*9)+offset,Red,1,STYLE_SOLID);// 6/8
MoveObject("mm8" ,OBJ_HLINE,Time[0],top-(step*9)+offset,Time[0],top-(step*10)+offset,Red,1,STYLE_SOLID);// 7/8
MoveObject("mm9" ,OBJ_HLINE,Time[0],top-(step*10)+offset,Time[0],top-(step*11)+offset,Red,1,STYLE_SOLID);// 0/8
MoveObject("mm-1" ,OBJ_HLINE,Time[0],top-(step*11)+offset,Time[0],top-(step*12)+offset,Red,1,STYLE_SOLID);// +2/8
MoveObject("mm-2" ,OBJ_HLINE,Time[0],top-(step*12)+offset,Time[0],top-(step*13)+offset,Red,1,STYLE_SOLID);// +1/8
MoveObject("mm13" ,OBJ_HLINE,Time[0],top-(step*13)+offset,Time[0],top-(step*14)+offset,Red,1,STYLE_SOLID);// 0/8
MoveObject("mm-14" ,OBJ_HLINE,Time[0],top-(step*14)+offset,Time[0],top-(step*15)+offset,Red,1,STYLE_SOLID);// +2/8
MoveObject("mm-25" ,OBJ_HLINE,Time[0],top-(step*15)+offset,Time[0],top-(step*16)+offset,Red,1,STYLE_SOLID);// +1/8
MoveObject("mm12h" ,OBJ_HLINE,Time[0],top-(step*0.5)+offset,Time[0],top-(step*0.5)+offset,Red,1,STYLE_SOLID);// -1/8
MoveObject("mm1h" ,OBJ_HLINE,Time[0],top-(step*1.5)+offset,Time[0],top-(step*1.5)+offset,Red,1,STYLE_SOLID);// 0/8
MoveObject("mm2h" ,OBJ_HLINE,Time[0],top-(step*2.5)+offset,Time[0],top-(step*2.5)+offset,Red,1,STYLE_SOLID);// 1/8
MoveObject("mm3h" ,OBJ_HLINE,Time[0],top-(step*3.5)+offset,Time[0],top-(step*3.5)+offset,Red,1,STYLE_SOLID);// 2/8
MoveObject("mm4h" ,OBJ_HLINE,Time[0],top-(step*4.5)+offset,Time[0],top-(step*4.5)+offset,Red,1,STYLE_SOLID);// 3/8
MoveObject("mm5h" ,OBJ_HLINE,Time[0],top-(step*5.5)+offset,Time[0],top-(step*5.5)+offset,Red,1,STYLE_SOLID);// 4/8
MoveObject("mm6h" ,OBJ_HLINE,Time[0],top-(step*6.5)+offset,Time[0],top-(step*6.5)+offset,Red,1,STYLE_SOLID);// 5/8
MoveObject("mm7h" ,OBJ_HLINE,Time[0],top-(step*7.5)+offset,Time[0],top-(step*7.5)+offset,Red,1,STYLE_SOLID);// 6/8
MoveObject("mm8h" ,OBJ_HLINE,Time[0],top-(step*8.5)+offset,Time[0],top-(step*8.5)+offset,Red,1,STYLE_SOLID);// 7/8
MoveObject("mm9h" ,OBJ_HLINE,Time[0],top-(step*9.5)+offset,Time[0],top-(step*9.5)+offset,Red,1,STYLE_SOLID);// 0/8
MoveObject("mm-1h" ,OBJ_HLINE,Time[0],top-(step*10.5)+offset,Time[0],top-(step*10.5)+offset,Red,1,STYLE_SOLID);// +2/8
MoveObject("mm-2h" ,OBJ_HLINE,Time[0],top-(step*11.5)+offset,Time[0],top-(step*11.5)+offset,Red,1,STYLE_SOLID);// +1/8
MoveObject("mm13h" ,OBJ_HLINE,Time[0],top-(step*12.5)+offset,Time[0],top-(step*12.5)+offset,Red,1,STYLE_SOLID);// 0/8
MoveObject("mm-1h4" ,OBJ_HLINE,Time[0],top-(step*13.5)+offset,Time[0],top-(step*13.5)+offset,Red,1,STYLE_SOLID);// +2/8
MoveObject("mm-25h" ,OBJ_HLINE,Time[0],top-(step*14.5)+offset,Time[0],top-(step*14.5)+offset,Red,1,STYLE_SOLID);// +1/8
SetIndexValue(shift, 0);
SetIndexValue2(shift, 0);
return(0);
}
Comments