cmo_v1
Download scriptThis script is a technical analysis tool designed to track market momentum by measuring the balance between "bullish" (buying) and "bearish" (selling) pressure over a specific period of time. Instead of simply looking at the price, it analyzes how much of the price movement in each bar is driven by upward force versus downward force.
Core Logic Overview
The indicator functions by breaking down the change in price from one bar to the next into two distinct components:
- Bullish Strength: The portion of the price change that moves upward.
- Bearish Strength: The portion of the price change that moves downward.
By separating these forces, the tool can determine if a move is being driven by aggressive buying, aggressive selling, or a mixture of both.
How it Works Step-by-Step
- Data Smoothing: To prevent the data from being too "noisy" or reactive to tiny, insignificant price flickers, the script first applies a Moving Average (MA) to the price data. This creates a smoother trend line to analyze.
- Calculating Movement: For every candle, the script calculates the difference between the current smoothed price and the previous one.
- Splitting the Force: It then mathematically splits that difference into two values:
- If the price went up, it assigns a value to the "Bulls."
- If the price went down, it assigns a value to the "Bears."
- Accumulation: The script looks back at a user-defined number of previous bars (the "Length," which defaults to 14). It adds up all the Bullish values and all the Bearish values over this period.
- Final Calculation: Finally, it compares the total Bullish strength against the total Bearish strength to produce a percentage score:
- A high positive percentage indicates that Bullish pressure is dominating the recent trend.
- A low or negative percentage indicates that Bearish pressure is taking control.
Visual Representation
The indicator appears in a separate window below your main price chart. It creates a visual "cloud" or filled area:
- Color Coding: It uses contrasting colors (Light Sea Green and Dark Orange) to visually distinguish between dominant buying and selling momentum.
- Reference Levels: It features horizontal levels at +50, 0, and -50. These act as benchmarks; for instance, if the value is above +50, the upward momentum is significantly stronger than the downward pressure, and vice versa.
Summary for the User
Think of this tool as a "momentum scale." Rather than just telling you what the price is doing now, it tells you how much effort buyers vs. sellers have put into moving the price over the last several bars. It helps traders identify when a trend is gaining strength or when the momentum is starting to shift in the opposite direction.
//+------------------------------------------------------------------+
//| CMO.mq5 |
//| Copyright © 2006, TrendLaboratory Ltd. |
//| http://finance.groups.yahoo.com/group/TrendLaboratory |
//| E-mail: igorad2004@list.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, TrendLaboratory Ltd."
#property link "http://finance.groups.yahoo.com/group/TrendLaboratory"
#property description "CMO"
//---- íîìåð âåðñèè èíäèêàòîðà
#property version "1.00"
//---- îòðèñîâêà èíäèêàòîðà â îòäåëüíîì îêíå
#property indicator_separate_window
//---- êîëè÷åñòâî èíäèêàòîðíûõ áóôåðîâ 2
#property indicator_buffers 2
//---- èñïîëüçîâàíî âñåãî îäíî ãðàôè÷åñêîå ïîñòðîåíèå
#property indicator_plots 1
//+----------------------------------------------+
//| Îáúÿâëåíèå êîíñòàíò |
//+----------------------------------------------+
#define RESET 0 // êîíñòàíòà äëÿ âîçâðàòà òåðìèíàëó êîìàíäû íà ïåðåñ÷åò èíäèêàòîðà
//+----------------------------------------------+
//| Ïàðàìåòðû îòðèñîâêè èíäèêàòîðà CMO |
//+----------------------------------------------+
//---- îòðèñîâêà èíäèêàòîðà 1 â âèäå îáëàêà
#property indicator_type1 DRAW_FILLING
//---- â êà÷åñòâå öâåòîâ èíäèêàòîðà èñïîëüçîâàíû
#property indicator_color1 clrLightSeaGreen,clrDarkOrange
//---- ëèíèÿ èíäèêàòîðà 1 - íåïðåðûâíàÿ êðèâàÿ
#property indicator_style1 STYLE_SOLID
//---- òîëùèíà ëèíèè èíäèêàòîðà 1 ðàâíà 1
#property indicator_width1 1
//---- îòîáðàæåíèå ìåòêè èíäèêàòîðà
#property indicator_label1 "CMO"
//+----------------------------------------------+
//| Ïàðàìåòðû ãîðèçîíòàëüíûõ óðîâíåé èíäèêàòîðà |
//+----------------------------------------------+
#property indicator_level1 +50
#property indicator_level2 0
#property indicator_level3 -50
#property indicator_levelcolor clrSlateGray
#property indicator_levelstyle STYLE_DASHDOTDOT
//+----------------------------------------------+
//| Âõîäíûå ïàðàìåòðû èíäèêàòîðà |
//+----------------------------------------------+
input uint Length=14; // Ïåðèîä èíäèêàòîðà
input ENUM_MA_METHOD Method=MODE_SMA; // Òèï óñðåäíåíèÿ
input ENUM_APPLIED_PRICE Price=PRICE_CLOSE; // Öåíà
input int Shift=0; // Ñäâèã èíäèêàòîðà ïî ãîðèçîíòàëè â áàðàõ
//+----------------------------------------------+
//---- îáúÿâëåíèå äèíàìè÷åñêèõ ìàññèâîâ, êîòîðûå â äàëüíåéøåì
//---- áóäóò èñïîëüçîâàíû â êà÷åñòâå èíäèêàòîðíûõ áóôåðîâ
double Line1Buffer[];
double Line2Buffer[];
//---- îáúÿâëåíèå öåëî÷èñëåííûõ ïåðåìåííûõ íà÷àëà îòñ÷åòà äàííûõ
int min_rates_total;
//---- îáúÿâëåíèå öåëî÷èñëåííûõ ïåðåìåííûõ äëÿ õåíäëîâ èíäèêàòîðîâ
int MA_Handle;
//---- îáúÿâëåíèå äèíàìè÷åñêèõ ìàññèâîâ, êîòîðûå â äàëüíåéøåì
//---- áóäóò èñïîëüçîâàíû â êà÷åñòâå êîëüöåâûõ áóôåðîâ
int Count[];
double Bulls[],Bears[];
//+------------------------------------------------------------------+
//| Ïåðåñ÷åò ïîçèöèè ñàìîãî íîâîãî ýëåìåíòà â ìàññèâå |
//+------------------------------------------------------------------+
void Recount_ArrayZeroPos(int &CountArray[],// âîçâðàò ïî ññûëêå íîìåðà òåêóùåãî çíà÷åíèÿ öåíîâîãî ðÿäà
int Size)
{
//----
int numb,Max1,Max2;
static int count=1;
//---
Max2=Size;
Max1=Max2-1;
//---
count--;
if(count<0) count=Max1;
//---
for(int iii=0; iii<Max2; iii++)
{
numb=iii+count;
if(numb>Max1) numb-=Max2;
CountArray[iii]=numb;
}
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//---- èíèöèàëèçàöèÿ ïåðåìåííûõ íà÷àëà îòñ÷åòà äàííûõ
min_rates_total=int(Length);
//---- ïîëó÷åíèå õåíäëà èíäèêàòîðà iMA
MA_Handle=iMA(NULL,0,Length,0,Method,Price);
if(MA_Handle==INVALID_HANDLE) Print(" Íå óäàëîñü ïîëó÷èòü õåíäë èíäèêàòîðà iMA");
//---- ðàñïðåäåëåíèå ïàìÿòè ïîä ìàññèâû ïåðåìåííûõ
int size=int(Length);
if(ArrayResize(Count,size)<size) Print("Íå óäàëîñü ðàñïðåäåëèòü ïàìÿòü ïîä ìàññèâ Count[]");
if(ArrayResize(Bulls,size)<size) Print("Íå óäàëîñü ðàñïðåäåëèòü ïàìÿòü ïîä ìàññèâ Bulls[]");
if(ArrayResize(Bears,size)<size) Print("Íå óäàëîñü ðàñïðåäåëèòü ïàìÿòü ïîä ìàññèâ Bears[]");
//---
ArrayInitialize(Count,0);
ArrayInitialize(Bulls,0);
ArrayInitialize(Bears,0);
//---- ïðåâðàùåíèå äèíàìè÷åñêîãî ìàññèâà â èíäèêàòîðíûé áóôåð
SetIndexBuffer(0,Line1Buffer,INDICATOR_DATA);
//---- îñóùåñòâëåíèå ñäâèãà èíäèêàòîðà 2 ïî ãîðèçîíòàëè íà Shift
PlotIndexSetInteger(0,PLOT_SHIFT,Shift);
//---- îñóùåñòâëåíèå ñäâèãà íà÷àëà îòñ÷åòà îòðèñîâêè èíäèêàòîðà 1 íà min_rates_total
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);
//---- èíäåêñàöèÿ ýëåìåíòîâ â áóôåðàõ êàê â òàéìñåðèÿõ
ArraySetAsSeries(Line1Buffer,true);
//---- ïðåâðàùåíèå äèíàìè÷åñêîãî ìàññèâà â èíäèêàòîðíûé áóôåð
SetIndexBuffer(1,Line2Buffer,INDICATOR_DATA);
//---- îñóùåñòâëåíèå ñäâèãà èíäèêàòîðà 3 ïî ãîðèçîíòàëè íà Shift
PlotIndexSetInteger(1,PLOT_SHIFT,Shift);
//---- îñóùåñòâëåíèå ñäâèãà íà÷àëà îòñ÷åòà îòðèñîâêè èíäèêàòîðà 2 íà min_rates_total
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total);
//---- èíäåêñàöèÿ ýëåìåíòîâ â áóôåðàõ êàê â òàéìñåðèÿõ
ArraySetAsSeries(Line2Buffer,true);
//---- èíèöèàëèçàöèè ïåðåìåííîé äëÿ êîðîòêîãî èìåíè èíäèêàòîðà
string shortname;
StringConcatenate(shortname,"CMO(",Length,")");
//--- ñîçäàíèå èìåíè äëÿ îòîáðàæåíèÿ â îòäåëüíîì ïîäîêíå è âî âñïëûâàþùåé ïîäñêàçêå
IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//--- îïðåäåëåíèå òî÷íîñòè îòîáðàæåíèÿ çíà÷åíèé èíäèêàòîðà
IndicatorSetInteger(INDICATOR_DIGITS,0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, // êîëè÷åñòâî èñòîðèè â áàðàõ íà òåêóùåì òèêå
const int prev_calculated,// êîëè÷åñòâî èñòîðèè â áàðàõ íà ïðåäûäóùåì òèêå
const datetime &time[],
const double &open[],
const double& high[], // öåíîâîé ìàññèâ ìàêñèìóìîâ öåíû äëÿ ðàñ÷åòà èíäèêàòîðà
const double& low[], // öåíîâîé ìàññèâ ìèíèìóìîâ öåíû äëÿ ðàñ÷åòà èíäèêàòîðà
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//---- ïðîâåðêà êîëè÷åñòâà áàðîâ íà äîñòàòî÷íîñòü äëÿ ðàñ÷åòà
if(BarsCalculated(MA_Handle)<rates_total || rates_total<min_rates_total) return(RESET);
//---- îáúÿâëåíèÿ ëîêàëüíûõ ïåðåìåííûõ
int limit,to_copy,bar;
double MA[],dPrice,Sum;
//---- ðàñ÷åò ñòàðòîâîãî íîìåðà first äëÿ öèêëà ïåðåñ÷åòà áàðîâ
if(prev_calculated>rates_total || prev_calculated<=0) // ïðîâåðêà íà ïåðâûé ñòàðò ðàñ÷åòà èíäèêàòîðà
{
limit=rates_total-2-min_rates_total; // ñòàðòîâûé íîìåð äëÿ ðàñ÷åòà âñåõ áàðîâ
}
else limit=rates_total-prev_calculated; // ñòàðòîâûé íîìåð äëÿ ðàñ÷åòà íîâûõ áàðîâ
to_copy=limit+2;
//---- êîïèðóåì âíîâü ïîÿâèâøèåñÿ äàííûå â ìàññèâû
if(CopyBuffer(MA_Handle,0,0,to_copy,MA)<=0) return(RESET);
//---- èíäåêñàöèÿ ýëåìåíòîâ â ìàññèâàõ êàê â òàéìñåðèÿõ
ArraySetAsSeries(MA,true);
//---- îñíîâíîé öèêë ðàñ÷åòà èíäèêàòîðà
for(bar=limit; bar>=0 && !IsStopped(); bar--)
{
dPrice=MA[bar]-MA[bar+1];
Bulls[Count[0]]=0.5*(MathAbs(dPrice)+dPrice);
Bears[Count[0]]=0.5*(MathAbs(dPrice)-dPrice);
//---
double SumBulls=0,SumBears=0;
for(int iii=0; iii<int(Length); iii++)
{
SumBulls+=Bulls[Count[iii]];
SumBears+=Bears[Count[iii]];
}
//---
Sum=SumBulls+SumBears;
if(Sum) Line1Buffer[bar]=(SumBulls-SumBears)/(SumBulls+SumBears)*100;
else Line1Buffer[bar]=0.0;
Line2Buffer[bar]=0.0;
if(bar) Recount_ArrayZeroPos(Count,Length);
}
//----
return(rates_total);
}
//+------------------------------------------------------------------+
Comments