CycleIdentifier_v1
Download scriptThis script is designed to identify and visualize cyclical patterns in financial market data, specifically focusing on distinguishing between "Minor" and "Major" cycles. It operates by analyzing price movement trends to detect shifts in momentum and identifies specific "sweep" zones where the market might be overextended or reversing.
Core Methodology
The logic functions by creating a smoothed representation of price action and comparing it against historical highs and lows. It looks for specific patterns where price moves significantly away from its recent average and then meets certain criteria to trigger a cycle shift.
The script distinguishes between two primary layers of market behavior:
- Minor Cycles: These represent smaller, more frequent fluctuations in the market. The script monitors these rapid shifts to provide high-frequency visual feedback on immediate momentum changes.
- Major Cycles: These represent broader, long-term trends. The logic for major cycles is scaled differently, looking for larger "sweeps" or movements that signify significant structural changes in the market's direction.
Key Logic Components
1. Price Smoothing and Filtering The script does not look at raw, erratic price data. Instead, it applies a smoothing technique (similar to a moving average) to create a "Cycle Price." This helps filter out minor market noise, allowing the logic to focus on the underlying trend. It also includes optional filters—such as Relative Strength Index (RSI) or specific Smoothing Moving Averages—to ensure that cycle identifications occur only when there is sufficient momentum.
2. The "Sweep" Mechanism A central part of this script's logic is the calculation of "Sweeps." This represents a predefined distance or intensity of movement.
- The script monitors how far the current price has moved from its previous cycle peak or valley.
- If the price moves beyond a specific threshold (the "Sweep"), it considers this a significant event.
- If the price then reverses significantly from that high/low point, the script identifies a completed cycle and triggers a visual signal.
3. Trend Switching Logic The script uses a "Switch" system to track the current state of the market:
- It tracks whether the current trend is bullish (upward) or bearish (downward).
- When the price violates a specific condition—such as moving sufficiently far against the current trend relative to its cycle strength—the script "switches" its tracking focus.
- This prevents the indicator from providing multiple conflicting signals during a single trend; it waits for a confirmed reversal before identifying a new cycle.
4. Visual Representation The logic translates these mathematical calculations into visual data on the chart:
- It uses Histograms to highlight where Minor and Major cycles are identified.
- Different colors are used to distinguish between Buy (Bullish) and Sell (Bearish) movements, allowing the user to see the hierarchy of the market's movement at a glance.
Summary for the User
In essence, this tool acts as a "cycle detector." It ignores minor price flickers and waits for the market to make significant moves. Once a move is large enough to be considered a "sweep," the script looks for a reversal; once that reversal happens, it marks a new cycle on your chart, helping you visualize whether the market is currently in a minor fluctuation or a major trend shift.
//+------------------------------------------------------------------+
//| CycleIdentifier.mq4 |
//| |
//+------------------------------------------------------------------+
#property strict
#property copyright ""
#property link ""
//----
#property indicator_separate_window
#property indicator_buffers 6
//----
#property indicator_color1 clrDarkGray
#property indicator_color2 clrLime
#property indicator_color3 clrRed
#property indicator_color4 clrDarkGreen
#property indicator_color5 clrBrown
//----
#property indicator_minimum -1.2
#property indicator_maximum 1.2
//----
extern int PriceActionFilter=1;
extern int Length=3;
extern int MajorCycleStrength=4;
extern bool UseCycleFilter=false;
extern int UseFilterSMAorRSI=1;
extern int FilterStrengthSMA=12;
extern int FilterStrengthRSI=21;
//----
double LineBuffer[];
double MajorCycleBuy[];
double MajorCycleSell[];
double MinorCycleBuy[];
double MinorCycleSell[];
double ZL1[];
//----
double CyclePrice=0.0,Strength=0.0,SweepA=0.0,SweepB=0.0;
int Switch=0,Switch2=0,SwitchA=0,SwitchB=0,SwitchC=0,SwitchD=0,SwitchE=0,SwitchAA=0,SwitchBB=0;
//---
double Price1BuyA=0.0,Price2BuyA=0.0;
int Price1BuyB=1,Price2BuyB=1;
//---
double Price1SellA=0.0,Price2SellA=0.0;
int Price1SellB=0,Price2SellB=0;
//---
bool ActiveSwitch=true,BuySwitchA=false,BuySwitchB=false,SellSwitchA=false,SellSwitchB=false;
int BuySellFac=1;
bool Condition1,Condition2,Condition3,Condition6;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int init()
{
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
SetIndexBuffer(0,LineBuffer);
//---
SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,3);
SetIndexBuffer(1,MajorCycleBuy);
//---
SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,3);
SetIndexBuffer(2,MajorCycleSell);
//---
SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,1);
SetIndexBuffer(3,MinorCycleBuy);
//---
SetIndexStyle(4,DRAW_HISTOGRAM,STYLE_SOLID,1);
SetIndexBuffer(4,MinorCycleSell);
//---
SetIndexStyle(5,DRAW_NONE);
SetIndexBuffer(5,ZL1);
//---
SetIndexEmptyValue(1,0.0);
SetIndexEmptyValue(2,0.0);
SetIndexEmptyValue(3,0.0);
SetIndexEmptyValue(4,0.0);
SetIndexEmptyValue(5,0.0);
//---
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int deinit() {return(0);}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
int position=Bars-counted_bars;
if(position<0) position=0;
if(counted_bars==0) position-=(FilterStrengthRSI+4);
//----
int rnglength=250;
double range=0.0,srange=0.0;
for(int pos=position; pos>=0; pos--)
{
srange=0.0;
int j=0;
for(int i=0;i<rnglength;i++)
{
j++;
int posr=pos + i;
if(posr>=Bars)
break;
srange=srange+(High[posr]-Low[posr]);
}
range=srange/j*Length;
int BarNumber=Bars-pos; //??????????
if(BarNumber<0)
BarNumber=0;
CyclePrice=iMA(NULL,0,PriceActionFilter,0,MODE_SMMA,PRICE_CLOSE,pos);
if(UseFilterSMAorRSI==1)
ZL1[pos]=ZeroLag(CyclePrice,FilterStrengthSMA,pos);
if(UseFilterSMAorRSI==2)
ZL1[pos]=ZeroLag(iRSI(NULL,0,14,(ENUM_APPLIED_PRICE)CyclePrice,FilterStrengthRSI),FilterStrengthRSI,pos);
if(ZL1[pos]>ZL1[pos+1])
SwitchC=1;
if(ZL1[pos]<ZL1[pos+1])
SwitchC=2;
if(BarNumber<=1)
{
if(Strength==0)
SweepA=range;
else
SweepA=Strength;
Price1BuyA=CyclePrice;
Price1SellA=CyclePrice;
}
/* PrintFormat("Bars = %d | counted_bars = %d | pos = %d | BarNumber = %d",Bars,counted_bars,pos,BarNumber);
PrintFormat("srange = %f | range = %f",srange,range);
PrintFormat("CyclePrice = %f | ZL1[%d] = %f",CyclePrice,pos,ZL1[pos]);
PrintFormat("SwitchC = %d",SwitchC);
PrintFormat("Strength = %f | SweepA = %f | Price1BuyA = %f | Price1SellA = %f", Strength,SweepA,Price1BuyA,Price1SellA);
*/
/* ***************************************************************** */
if(BarNumber>1)
{
/* ***** */
if(Switch>-1)
{
if(CyclePrice<Price1BuyA)
{
if(UseCycleFilter && (SwitchC==2) && BuySwitchA)
{
MinorCycleBuy[pos+BarNumber-Price1BuyB]=0; //MinorBuySell
LineBuffer[pos+BarNumber - Price1BuyB ]=0; //line
}
if(!UseCycleFilter && BuySwitchA)
{
MinorCycleBuy[pos+BarNumber-Price1BuyB]=0;
LineBuffer[pos+BarNumber-Price1BuyB]=0;
}
Price1BuyA=CyclePrice;
Price1BuyB=BarNumber;
BuySwitchA=true;
}
else if(CyclePrice>Price1BuyA)
{
SwitchA=BarNumber-Price1BuyB;
if(!UseCycleFilter)
{
MinorCycleBuy[pos+SwitchA]=-1;//MinorBuySell - DarkGreen
LineBuffer[pos+SwitchA]=-1;//line
}
if(UseCycleFilter && SwitchC==1)
{
MinorCycleBuy[pos+SwitchA]=-1; //MinorBuySell
LineBuffer[pos+SwitchA]=-1; //line
SwitchD=1;
}
else
{
SwitchD=0;
}
BuySwitchA=true;
double cyclePrice1=iMA(NULL,0,PriceActionFilter,0,MODE_SMMA,PRICE_CLOSE,pos+SwitchA);
if(ActiveSwitch)
{
Condition1=CyclePrice-cyclePrice1>=SweepA;
}
else
{
Condition1=CyclePrice>=cyclePrice1 *(1+SweepA/1000);
}
if(Condition1 && SwitchA>=BuySellFac)
{
Switch=-1;
Price1SellA=CyclePrice;
Price1SellB=BarNumber;
SellSwitchA=false;
BuySwitchA=false;
}
}
}
/* ***** */
if(Switch<1)
{
if(CyclePrice>Price1SellA)
{
if(UseCycleFilter && SwitchC==1 && SellSwitchA)
{
MinorCycleSell[pos+BarNumber-Price1SellB]=0; //MinorBuySell
LineBuffer[pos+BarNumber-Price1SellB]=0; //line
}
if(!UseCycleFilter && SellSwitchA)
{
MinorCycleSell[pos+BarNumber-Price1SellB]=0;//MinorBuySell
LineBuffer[pos+BarNumber-Price1SellB]=0;//line
}
Price1SellA=CyclePrice;
Price1SellB=BarNumber;
SellSwitchA=true;
}
else if(CyclePrice<Price1SellA)
{
SwitchA=BarNumber-Price1SellB;
if(!UseCycleFilter)
{
MinorCycleSell[pos+SwitchA]=1; // MinorBuySell darkRed
LineBuffer[pos+SwitchA]=1; //"CycleLine"
}
if(UseCycleFilter && (SwitchC==2))
{
MinorCycleSell[pos+SwitchA]=1;//MinorBuySell darkRed
LineBuffer[pos+SwitchA]=1;//CycleLine
SwitchD=2;
}
else
SwitchD =0;
SellSwitchA=true;
double cyclePrice2=iMA(NULL,0,PriceActionFilter,0,MODE_SMMA,PRICE_CLOSE,pos+SwitchA);
if(ActiveSwitch)
Condition1=(cyclePrice2-CyclePrice)>=SweepA;
else
Condition1=CyclePrice<=(cyclePrice2 *(1-SweepA/1000));
if(Condition1 && SwitchA>=BuySellFac)
{
Switch=1;
Price1BuyA=CyclePrice;
Price1BuyB=BarNumber;
SellSwitchA=false;
BuySwitchA=false;
}
}
}
}
/* ***************************************************************** */
LineBuffer[pos]=0;
MinorCycleBuy[pos]=0;
MinorCycleSell[pos]=0;
/* ***************************************************************** */
if(BarNumber==1)
{
if(Strength==0)
SweepB=range *MajorCycleStrength;
else
SweepB=Strength*MajorCycleStrength;
Price2BuyA=CyclePrice;
Price2SellA=CyclePrice;
}
/* ***************************************************************** */
if(BarNumber>1)
{
/* ***** */
if(Switch2>-1)
{
if(CyclePrice<Price2BuyA)
{
if(UseCycleFilter && SwitchC==2 && BuySwitchB)
{
MajorCycleBuy[pos+BarNumber-Price2BuyB]=0; //MajorBuySell,green
// LineBuffer[pos + BarNumber - Price2BuyB ] = 0; //line -----
}
if(!UseCycleFilter && BuySwitchB)
{
MajorCycleBuy[pos+BarNumber-Price2BuyB]=0;//MajorBuySell,green
// LineBuffer[pos + BarNumber - Price2BuyB ] = 0; //line-----------
}
Price2BuyA=CyclePrice;
Price2BuyB=BarNumber;
BuySwitchB=true;
}
else if(CyclePrice>Price2BuyA)
{
SwitchB=BarNumber-Price2BuyB;
if(!UseCycleFilter)
{
MajorCycleBuy[pos+SwitchB]=-1; //MajorBuySell green
// LineBuffer[pos + SwitchB] = -1; //line--------------
}
if(UseCycleFilter && SwitchC==1)
{
MajorCycleBuy[pos+SwitchB]=-1; //MajorBuySell green
// LineBuffer[pos + SwitchB] = -1; //line-----------------
SwitchE=1;
}
else
SwitchE=0;
BuySwitchB=true;
double cyclePrice3=iMA(NULL,0,PriceActionFilter,0,MODE_SMMA,PRICE_CLOSE,pos+SwitchB);
if(ActiveSwitch)
Condition6=CyclePrice-cyclePrice3>=SweepB;
else
Condition6=CyclePrice>=cyclePrice3 *(1+SweepB/1000);
if(Condition6 && SwitchB>=BuySellFac)
{
Switch2=-1;
Price2SellA=CyclePrice;
Price2SellB=BarNumber;
SellSwitchB=false;
BuySwitchB=false;
}
}
}
/* ***** */
if(Switch2<1)
{
if(CyclePrice>Price2SellA)
{
if(UseCycleFilter && SwitchC==1 && SellSwitchB)
{
MajorCycleSell[pos+BarNumber-Price2SellB]=0; //"MajorBuySell",red
// LineBuffer[pos + BarNumber - Price2SellB ] = 0; //line -----
}
if(!UseCycleFilter && SellSwitchB)
{
MajorCycleSell[pos+BarNumber-Price2SellB]=0;//"MajorBuySell",red
// LineBuffer[pos + BarNumber - Price2SellB ] = 0; //line -----
}
Price2SellA=CyclePrice;
Price2SellB=BarNumber;
SellSwitchB=true;
}
else if(CyclePrice<Price2SellA)
{
SwitchB=BarNumber-Price2SellB;
if(!UseCycleFilter)
{
MajorCycleSell[pos+SwitchB]=1; //"MajorBuySell",red
// LineBuffer[pos + SwitchB ] = 1; //line -----
}
if(UseCycleFilter && SwitchC==2)
{
MajorCycleSell[pos+SwitchB]=1; //"MajorBuySell",red
// LineBuffer[pos + SwitchB ] = 1; //line -----
SwitchE=2;
}
else
SwitchE =0;
SellSwitchB=true;
double cyclePrice4=iMA(NULL,0,PriceActionFilter,0,MODE_SMMA,PRICE_CLOSE,pos+SwitchB);
if(ActiveSwitch)
Condition6=cyclePrice4-CyclePrice>=SweepB;
else
Condition6=CyclePrice<=cyclePrice4 *(1.0-SweepB/1000.0);
if(Condition6 && SwitchB>=BuySellFac)
{
Switch2=1;
Price2BuyA=CyclePrice;
Price2BuyB=BarNumber;
SellSwitchB=false;
BuySwitchB=false;
}
}
}
}
/* ***************************************************************** */
LineBuffer[pos]=0;
MajorCycleSell[pos]=0;
MajorCycleBuy[pos]=0;
}
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double ZeroLag(double price,int length,int pos)
{
if(length<3)
{
return(price);
}
double aa=MathExp(-1.414*3.14159/length);
double bb=2*aa*MathCos(1.414*180/length);
double CB=bb;
double CC=-aa*aa;
double CA=1 - CB - CC;
double CD=CA*price + CB*ZL1[pos+1] + CC*ZL1[pos+2];
return(CD);
}
//+------------------------------------------------------------------+
Comments