Article-13824-MQL5-Multi-Sy.../TestMSTFMomentum.mq5

307 lines
29 KiB
MQL5
Raw Permalink Normal View History

2026-03-21 00:11:16 +07:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| TestMSTFMomentum.mq5 |
//| Copyright 2023, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots 2
//--- enums
//--- plot Momentum1
#property indicator_label1 "Momentum1"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrSeaGreen
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot Momentum2
#property indicator_label2 "Momentum2"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrDodgerBlue
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- includes
#include "IndMSTF.mqh"
#include "Dashboard.mqh"
2026-03-21 00:11:16 +07:00
//--- input parameters
input string InpSymbol = NULL; /* Symbol */ // !8<2>;
input ENUM_TIMEFRAMES InpTimeframe = PERIOD_CURRENT; /* Timeframe */ // "09<D@59<
input int InpPeriod = 14; /* Period */ // 5@8>4 @0AGQB0
input ENUM_APPLIED_PRICE InpPrice = PRICE_CLOSE; /* Applied Price */ // &5=0 @0AGQB0
input uchar InpLineWidth1 = 2; /* Senior period Line Width */ // ">;I8=0 ;8=88 4;O AB0@H53> ?5@8>40
input uchar InpLineWidth2 = 1; /* Junior period Line Width */ // ">;I8=0 ;8=88 4;O <;04H53> ?5@8>40
input bool InpAsSeries = true; /* As Series flag */ // $;03 A5@89=>AB8 <0AA82>2 1CD5@>2 8=48:0B>@0
//--- indicator buffers
double BufferMomentum1[];
double BufferMomentum2[];
//--- global variables
int handle_mom1;
int handle_mom2;
CMSTFIndicators indicators; // -:75<?;O@ >1J5:B0 :>;;5:F88 8=48:0B>@>2
//--- ?5@5<5==K5 4;O ?0=5;8
CDashboard *panel=NULL; // #:070B5;L =0 >1J5:B ?0=5;8
int mouse_bar_index; // =45:A 10@0, A :>B>@>3> 15@CBAO 40==K5
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- #AB0=02;8205< B09<5@ A ?5@8>48G=>ABLN 2 1 A5:C=4C
EventSetTimer(1);
//--- 07=0G05< @8AC5<K< 1CD5@0< 0 8 1 <0AA82K BufferMA1 8 BufferMA2 A>>B25BAB25==>
SetIndexBuffer(0,BufferMomentum1,INDICATOR_DATA);
SetIndexBuffer(1,BufferMomentum2,INDICATOR_DATA);
//--- #AB0=02;8205< H8@8=C ;8=89
int w1=0,w2=0;
if(InpTimeframe>Period())
{
w1=InpLineWidth2;
w2=InpLineWidth1;
}
else
{
w1=InpLineWidth1;
w2=InpLineWidth2;
}
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,w1);
PlotIndexSetInteger(1,PLOT_LINE_WIDTH,w2);
//--- sets indicator shift
//--- #AB0=02;8205< D;038 A5@89=>AB8 <0AA820< 1CD5@>2 8=48:0B>@0 (4;O B5AB0, GB>1K 284=> 1K;> >BACBAB285 @07=8FK)
ArraySetAsSeries(BufferMomentum1,InpAsSeries);
ArraySetAsSeries(BufferMomentum2,InpAsSeries);
//--- !>740Q< 420 8=48:0B>@0 >4=>3> B8?0
//--- 5@2K9 @0AAG8BK205BAO =0 B5:CI8E A8<2>;5/?5@8>45 3@0D8:0, 0 2B>@>9 - =0 B5E, GB> 7040=K 2 =0AB@>9:0E
handle_mom1=indicators.AddNewMomentum(NULL,PERIOD_CURRENT,InpPeriod,InpPrice);
handle_mom2=indicators.AddNewMomentum(InpSymbol,InpTimeframe,InpPeriod,InpPrice);
//--- A;8 =5 C40;>AL A>740BL EM=4;K 8=48:0B>@>2 - 2>72@0I05< >H81:C 8=8F80;870F88
if(handle_mom1==INVALID_HANDLE || handle_mom2==INVALID_HANDLE)
return INIT_FAILED;
//--- #AB0=02;8205< >?8A0=8O ;8=89 8=48:0B>@0 87 >?8A0=89 1CD5@>2 @0AGQB=>9 G0AB8 A>740==KE 8=48:0B>@>2
indicators.SetPlotLabelFromBuffer(0,handle_mom1,0);
indicators.SetPlotLabelFromBuffer(1,handle_mom2,0);
//--- 0=5;L
//--- !>740Q< ?0=5;L
int width=317;
panel=new CDashboard(1,20,20,width,264,0);
if(panel==NULL)
{
Print("Error. Failed to create panel object");
return INIT_FAILED;
}
//--- #AB0=02;8205< ?0@0<5B@K H@8DB0
panel.SetFontParams("Calibri",9);
//--- B>1@0605< ?0=5;L A B5:AB>< 2 703>;>2:5 "!8<2>;, ?8A0=85 B09<D@59<0"
panel.View(Symbol()+", "+StringSubstr(EnumToString(Period()),7));
//--- !>740Q< B01;8FC A 845=B8D8:0B>@>< 0 4;O >B>1@065=8O 2 =59 40==KE 10@0
panel.CreateNewTable(0);
//--- 8AC5< B01;8FC A 845=B8D8:0B>@>< 0 =0 D>=5 ?0=5;8
panel.DrawGrid(0,2,20,6,2,18,width/2-2);
//--- !>740Q< B01;8FC A 845=B8D8:0B>@>< 1 4;O >B>1@065=8O 2 =59 40==KE 8=48:0B>@0 1
panel.CreateNewTable(1);
//--- >;CG05< :>>@48=0BC Y2 B01;8FK A 845=B8D8:0B>@>< 0 8
//--- CAB0=02;8205< :>>@48=0BC Y1 4;O B01;8FK A 845=B8D8:0B>@>< 1
int y1=panel.TableY2(0)+22;
//--- 8AC5< B01;8FC A 845=B8D8:0B>@>< 1 =0 D>=5 ?0=5;8
panel.DrawGrid(1,2,y1,2,2,18,width/2-2);
//--- !>740Q< B01;8FC A 845=B8D8:0B>@>< 2 4;O >B>1@065=8O 2 =59 40==KE 8=48:0B>@0 2
panel.CreateNewTable(2);
//--- >;CG05< :>>@48=0BC Y2 B01;8FK A 845=B8D8:0B>@>< 1 8
//--- CAB0=02;8205< :>>@48=0BC Y1 4;O B01;8FK A 845=B8D8:0B>@>< 2
int y2=panel.TableY2(1)+3;
//--- 8AC5< B01;8FC A 845=B8D8:0B>@>< 2 =0 D>=5 ?0=5;8
panel.DrawGrid(2,2,y2,3,2,18,width/2-2);
//--- =8F80;878@C5< ?5@5<5==CN A 8=45:A>< 10@0 C:070B5;O <KH:8
mouse_bar_index=0;
//--- K2>48< =0 ?0=5;L 40==K5 B5:CI53> 10@0
DrawData(mouse_bar_index,TimeCurrent());
//--- #A?5H=0O 8=8F80;870F8O
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- #=8GB>605< B09<5@
EventKillTimer();
//--- A;8 >1J5:B ?0=5;8 ACI5AB2C5B - C40;O5< 53>
if(panel!=NULL)
delete panel;
//--- !B8@05< 2A5 :><<5=B0@88
Comment("");
}
//+------------------------------------------------------------------+
//| 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[])
{
//--- >;8G5AB2> 10@>2 4;O @0AGQB0
int limit=rates_total-prev_calculated;
//--- A;8 limit > 1, 7=0G8B MB> ?5@2K9 @0AGQB, ;81> 87<5=5=85 2 8AB>@88
if(limit>1)
{
//--- C:07K205< 4;O ?@>AGQB0 2AN 4>ABC?=CN 8AB>@8N
limit=rates_total-1;
/*
// A;8 2 8=48:0B>@5 5ABL :0:85-;81> 1CD5@K, 2 :>B>@KE >B>1@065=K 4@C385 @0AGQBK (=5 <C;LB8- 8=48:0B>@K),
// B> 745AL 8E =C6=> 8=8F80;878@>20BL "?CABK<" 7=0G5=85<, CAB0=>2;5==K< 4;O MB8E 1CD5@>2
*/
}
//--- 0AAG8BK205< 2A5 A>740==K5 <C;LB8A8<2>;L=K5 <C;LB8?5@8>4=K5 8=48:0B>@K
if(!indicators.Calculate())
return 0;
//--- K2>48< =0 ?0=5;L 40==K5 10@0 ?>4 :C@A>@>< (;81> B5:CI89 10@, 5A;8 :C@A>@ 70 ?@545;0<8 3@0D8:0)
DrawData(mouse_bar_index,time[mouse_bar_index]);
//--- 7 1CD5@>2 @0AAG8B0==KE 8=48:0B>@>2 2K2>48< 40==K5 2 8=48:0B>@=K5 1CD5@K
if(!indicators.DataToBuffer(NULL,PERIOD_CURRENT,handle_mom1,0,0,limit,BufferMomentum1))
return 0;
if(!indicators.DataToBuffer(NULL,PERIOD_CURRENT,handle_mom2,0,0,limit,BufferMomentum2))
return 0;
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//--- K7K205< B09<5@ :>;;5:F88 8=48:0B>@>2
indicators.OnTimer();
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//--- 01>B0 A ?0=5;LN
//--- K7K205< >1@01>BG8: A>1KB89 ?0=5;8
panel.OnChartEvent(id,lparam,dparam,sparam);
//--- A;8 :C@A>@ ?5@5<5I05BAO 8;8 I5;G>: ?> 3@0D8:C
if(id==CHARTEVENT_MOUSE_MOVE || id==CHARTEVENT_CLICK)
{
//--- 1JO2;O5< ?5@5<5==K5 4;O 70?8A8 2 =8E :>>@48=0B 2@5<5=8 8 F5=K
datetime time=0;
double price=0;
int wnd=0;
//--- A;8 :>>@48=0BK :C@A>@0 ?@5>1@07>20=K 2 40BC 8 2@5<O
if(ChartXYToTimePrice(ChartID(),(int)lparam,(int)dparam,wnd,time,price))
{
//--- 70?8AK205< 8=45:A 10@0, 345 @0A?>;>65= :C@A>@ 2 3;>10;L=CN ?5@5<5==CN
mouse_bar_index=iBarShift(Symbol(),PERIOD_CURRENT,time);
//--- K2>48< 40==K5 10@0 ?>4 :C@A>@>< =0 ?0=5;L
DrawData(mouse_bar_index,time);
}
}
//--- A;8 ?>;CG8;8 ?>;L7>20B5;LA:>5 A>1KB85 - 2K2>48< >1 MB>< A>>1I5=85 2 6C@=0;
if(id>CHARTEVENT_CUSTOM)
{
//--- 45AL <>65B 1KBL >1@01>B:0 I5;G:0 ?> :=>?:5 70:@KB8O =0 ?0=5;8
PrintFormat("%s: Event id=%ld, object id (lparam): %lu, event message (sparam): %s",__FUNCTION__,id,lparam,sparam);
}
}
//+------------------------------------------------------------------+
//| K2>48B 40==K5 A C:070==>3> 8=45:A0 B09<A5@88 =0 ?0=5;L |
//+------------------------------------------------------------------+
void DrawData(const int index,const datetime time)
{
//--- 1JO2;O5< ?5@5<5==K5 4;O ?>;CG5=8O 2 =8E 40==KE
MqlRates rates[1];
//--- A;8 40==K5 10@0 ?> C:070==><C 8=45:AC ?>;CG8BL =5 C40;>AL - CE>48<
if(CopyRates(Symbol(),PERIOD_CURRENT,index,1,rates)!=1)
return;
//--- #AB0=02;8205< ?0@0<5B@K H@8DB0 4;O 703>;>2:>2 40==KE 10@0 8 8=48:0B>@0
int size=0;
uint flags=0;
uint angle=0;
string name=panel.FontParams(size,flags,angle);
panel.SetFontParams(name,9,FW_BOLD);
panel.DrawText("Bar data ["+(string)index+"]",3,panel.TableY1(0)-16,clrMaroon,panel.Width()-6);
panel.DrawText("Indicators data ["+(string)index+"]",3,panel.TableY1(1)-16,clrGreen,panel.Width()-6);
//--- #AB0=02;8205< ?0@0<5B@K H@8DB0 4;O 40==KE 10@0 8 8=48:0B>@0
panel.SetFontParams(name,9);
//--- K2>48< =0 ?0=5;L 40==K5 C:070==>3> 10@0 2 B01;8FC 0
panel.DrawText("Date", panel.CellX(0,0,0)+2, panel.CellY(0,0,0)+2); panel.DrawText(TimeToString( rates[0].time,TIME_DATE), panel.CellX(0,0,1)+2, panel.CellY(0,0,1)+2,clrNONE,90);
panel.DrawText("Time", panel.CellX(0,1,0)+2, panel.CellY(0,1,0)+2); panel.DrawText(TimeToString( rates[0].time,TIME_MINUTES), panel.CellX(0,1,1)+2, panel.CellY(0,1,1)+2,clrNONE,90);
panel.DrawText("Open", panel.CellX(0,2,0)+2, panel.CellY(0,2,0)+2); panel.DrawText(DoubleToString(rates[0].open,Digits()), panel.CellX(0,2,1)+2, panel.CellY(0,2,1)+2,clrNONE,90);
panel.DrawText("High", panel.CellX(0,3,0)+2, panel.CellY(0,3,0)+2); panel.DrawText(DoubleToString(rates[0].high,Digits()), panel.CellX(0,3,1)+2, panel.CellY(0,3,1)+2,clrNONE,90);
panel.DrawText("Low", panel.CellX(0,4,0)+2, panel.CellY(0,4,0)+2); panel.DrawText(DoubleToString(rates[0].low,Digits()), panel.CellX(0,4,1)+2, panel.CellY(0,4,1)+2,clrNONE,90);
panel.DrawText("Close", panel.CellX(0,5,0)+2, panel.CellY(0,5,0)+2); panel.DrawText(DoubleToString(rates[0].close,Digits()), panel.CellX(0,5,1)+2, panel.CellY(0,5,1)+2,clrNONE,90);
//--- K2>48< 2 B01;8FC 1 40==K5 8=48:0B>@0 1 A C:070==>3> 10@0 2 B01;8FC 1
panel.DrawText(indicators.Title(handle_mom1), panel.CellX(1,0,0)+2, panel.CellY(1,0,0)+2);
double value1=indicators.GetData(handle_mom1,0,0,index);
string value_str1=(value1!=EMPTY_VALUE ? DoubleToString(value1,indicators.Digits(handle_mom1)) : " ");
panel.DrawText(value_str1,panel.CellX(1,0,1)+2,panel.CellY(1,0,1)+2,clrNONE,190);
//--- K2>48< >?8A0=85 A>AB>O=8O ;8=88 8=48:0B>@0 1
panel.DrawText("Line state", panel.CellX(1,1,0)+2, panel.CellY(1,1,0)+2);
ENUM_LINE_STATE state1=indicators.BufferLineState(Symbol(),PERIOD_CURRENT,handle_mom1,0,0,index);
panel.DrawText(BufferLineStateDescription(state1),panel.CellX(1,1,1)+2,panel.CellY(1,1,1)+2,clrNONE,190);
//--- K2>48< 2 B01;8FC 2 40==K5 8=48:0B>@0 2 A C:070==>3> 10@0 2 B01;8FC 2
panel.DrawText(indicators.Title(handle_mom2), panel.CellX(2,0,0)+2, panel.CellY(2,0,0)+2);
double value2=indicators.GetDataTo(Symbol(),PERIOD_CURRENT,handle_mom2,0,0,index);
string value_str2=(value2!=EMPTY_VALUE ? DoubleToString(value2,indicators.Digits(handle_mom2)) : " ");
panel.DrawText(value_str2,panel.CellX(2,0,1)+2,panel.CellY(2,0,1)+2,clrNONE,190);
//--- K2>48< >?8A0=85 A>AB>O=8O ;8=88 8=48:0B>@0 2
panel.DrawText("Line state", panel.CellX(2,1,0)+2, panel.CellY(2,1,0)+2);
ENUM_LINE_STATE state2=indicators.BufferLineState(Symbol(),PERIOD_CURRENT,handle_mom2,0,0,index);
panel.DrawText(BufferLineStateDescription(state2),panel.CellX(2,1,1)+2,panel.CellY(2,1,1)+2,clrNONE,190);
//--- K2>48< >?8A0=85 A>>B=>H5=8O ;8=88 8=48:0B>@0 1 >B=>A8B5;L=> ;8=88 8=48:0B>@0 2
double value21=indicators.GetDataTo(Symbol(),PERIOD_CURRENT,handle_mom2,0,0,index+1);
ENUM_LINE_STATE stateR=indicators.BufferLineStateRelative(Symbol(),PERIOD_CURRENT,handle_mom1,0,0,index,value2,value21);
string ma1=indicators.Name(handle_mom1);
string ma2=indicators.Name(handle_mom2);
string state_relative=
(
stateR==LINE_STATE_ABOVE ? StringFormat("%s1 > %s2",ma1,ma2) :
stateR==LINE_STATE_BELOW ? StringFormat("%s1 < %s2",ma1,ma2) :
stateR==LINE_STATE_CROSS_DOWN ? "Top-down crossing" :
stateR==LINE_STATE_CROSS_UP ? "Bottom-up crossing" :
BufferLineStateDescription(stateR)
);
panel.DrawText(StringFormat("%s1 vs %s2",ma1,ma2), panel.CellX(2,2,0)+2, panel.CellY(2,2,0)+2);
panel.DrawText(state_relative, panel.CellX(2,2,1)+2, panel.CellY(2,2,1)+2,clrNONE,190);
//--- 5@5@8A>2K205< 3@0D8: 4;O =5<54;5==>3> >B>1@065=8O 2A5E 87<5=5=89 =0 ?0=5;8
ChartRedraw(ChartID());
}
//+------------------------------------------------------------------+