Article-13277-MQL5-Template.../TestWilliamsFractals.mq5

211 lines
19 KiB
MQL5
Raw Permalink Normal View History

2026-03-20 13:25:09 +07:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| TestWilliamsFractals.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"
//--- includes
#include "Dashboard.mqh"
2026-03-20 13:25:09 +07:00
//--- global variables
int handle=INVALID_HANDLE; // %M=4; 8=48:0B>@0
int ind_digits=0; // >;8G5AB2> 7=0:>2 ?>A;5 70?OB>9 2 7=0G5=88 8=48:0B>@0
string ind_title; // ?8A0=85 8=48:0B>@0
//--- ?5@5<5==K5 4;O ?0=5;8
int mouse_bar_index; // =45:A 10@0, A :>B>@>3> 15@CBAO 40==K5
CDashboard *panel=NULL; // #:070B5;L =0 >1J5:B ?0=5;8
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
EventSetTimer(60);
//--- =48:0B>@
//--- #AB0=02;8205< =08<5=>20=85 8=48:0B>@0 8 :>;8G5AB2> 7=0:>2 ?>A;5 70?OB>9
ind_title="Fractals";
ind_digits=Digits();
//--- !>740Q< EM=4; 8=48:0B>@0
ResetLastError();
handle=iFractals(Symbol(),PERIOD_CURRENT);
if(handle==INVALID_HANDLE)
{
PrintFormat("%s: Failed to create indicator handle %s. Error %ld",__FUNCTION__,ind_title,GetLastError());
return INIT_FAILED;
}
//--- 0=5;L
//--- !>740Q8 ?0=5;L
panel=new CDashboard(1,20,20,199,225);
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,97);
//--- !>740Q< B01;8FC A 845=B8D8:0B>@>< 1 4;O >B>1@065=8O 2 =59 40==KE 8=48:0B>@0
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,3,2,18,97);
//--- K2>48< 2 6C@=0; B01;8G=K5 40==K5
panel.GridPrint(0,2);
panel.GridPrint(1,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);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
//--- A2>1>6405< EM=4; 8=48:0B>@0
ResetLastError();
if(!IndicatorRelease(handle))
PrintFormat("%s: IndicatorRelease failed. Error %ld",__FUNCTION__,GetLastError());
//--- G8I05< 2A5 :><<5=B0@88 =0 3@0D8:5
Comment("");
//--- A;8 >1J5:B ?0=5;8 ACI5AB2C5B - C40;O5< 53>
if(panel!=NULL)
delete panel;
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void 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);
}
}
//+------------------------------------------------------------------+
//| >72@0I05B 40==K5 8=48:0B>@0 =0 C:070==>< 10@5 |
//+------------------------------------------------------------------+
double IndicatorValue(const int ind_handle,const int index,const int buffer_num)
{
double array[1]={0};
ResetLastError();
if(CopyBuffer(ind_handle,buffer_num,index,1,array)!=1)
{
PrintFormat("%s: CopyBuffer failed. Error %ld",__FUNCTION__,GetLastError());
return EMPTY_VALUE;
}
return array[0];
}
//+------------------------------------------------------------------+
//| 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
MqlTick tick={0};
MqlRates rates[1];
//--- A;8 B5:CI85 F5=K ?>;CG8BL =5 C40;>AL - CE>48<
if(!SymbolInfoTick(Symbol(),tick))
return;
//--- 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("Indicator 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< =0 ?0=5;L 40==K5 8=48:0B>@0 A C:070==>3> 10@0 2 B01;8FC 1 (25@E=89 D@0:B0;)
panel.DrawText(ind_title+" Up", panel.CellX(1,0,0)+2, panel.CellY(1,0,0)+2);
double value0=IndicatorValue(handle,index,UPPER_LINE);
string value_str0=(value0!=EMPTY_VALUE ? DoubleToString(value0,ind_digits) : " ");
panel.DrawText(value_str0,panel.CellX(1,0,1)+2,panel.CellY(1,0,1)+2,clrNONE,90);
//--- K2>48< =0 ?0=5;L 40==K5 8=48:0B>@0 A C:070==>3> 10@0 2 B01;8FC 1 (=86=89 D@0:B0;)
panel.DrawText(ind_title+" Down", panel.CellX(1,1,0)+2, panel.CellY(1,1,0)+2);
double value1=IndicatorValue(handle,index,LOWER_LINE);
string value_str1=(value1!=EMPTY_VALUE ? DoubleToString(value1,ind_digits) : " ");
panel.DrawText(value_str1,panel.CellX(1,1,1)+2,panel.CellY(1,1,1)+2,clrNONE,90);
//--- 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());
}
//+------------------------------------------------------------------+