ßçûê ïðîãðàììèðîâàíèÿ MQL5: Ïðîäâèíóòîå èñïîëüçîâàíèå òîðãîâîé ïëàòôîðìû MetaTrader 5 Ìàøíèí Òèìóð
Ôóíêöèÿ ResourceCreate ñîçäàåò èç ïèêñåëüíîãî ìàññèâà ðåñóðñ ñ èìåíåì::IMG.
 èòîãå íà áåëîì ôîíå îòîáðàæàåòñÿ íàäïèñü «Text».
Òàêæå ìîæíî âûâåñòè òåêñò íà ãîòîâîå èçîáðàæåíèå:
#resource "\\Images\\i.bmp»
uint ExtImg [10000];
//+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +
//| Custom indicator initialization function |
//+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +
int OnInit ()
{
ObjectCreate (0,«Image», OBJ_BITMAP_LABEL,0,0,0);
ObjectSetString (0,«Image», OBJPROP_BMPFILE,»:: IMG»);
uint width=100;
uint height=100;
ResourceReadImage("::Images\\i.bmp», ExtImg, width, height);
TextOut («Text», 10,10,TA_LEFT|TA_TOP, ExtImg,100,100,0xffffff, COLOR_FORMAT_XRGB_NOALPHA);
ResourceCreate (»:: IMG», ExtImg,100,100,0,0,0,COLOR_FORMAT_XRGB_NOALPHA);
ChartRedraw ();
// – —
return (INIT_SUCCEEDED);
}
Çäåñü ôóíêöèÿ ResourceReadImage ñ÷èòûâàåò ñóùåñòâóþùåå èçîáðàæåíèå èç ïàïêè Images îêíà Navigator ðåäàêòîðà MQL5 â ïèêñåëüíûé ìàññèâ::IMG, ñâÿçàííûé ñ îáúåêòîì «Ãðàôè÷åñêàÿ ìåòêà», à ôóíêöèÿ TextOut âûâîäèò â ïèêñåëüíûé ìàññèâ ñëîâî «Text».
Òî æå ñàìîå ìîæíî ïðîäåëàòü è ñ îáúåêòîì «Ðèñóíîê»:
#resource "\\Images\\i.bmp»
uint ExtImg [10000];
//+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +
//| Custom indicator initialization function |
//+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +
int OnInit ()
{
// – —
return (INIT_SUCCEEDED);
}
//+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +
//| 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 [])
{
// – —
ArraySetAsSeries (time, true);
ArraySetAsSeries (high, true);
ArraySetAsSeries (low, true);
ArraySetAsSeries (close, true);
ObjectDelete (0,«Image»);
ObjectCreate (0,«Image», OBJ_BITMAP,0,time [1],close [1]);
ObjectSetString (0,«Image», OBJPROP_BMPFILE,»:: IMG»);
uint width=100;
uint height=100;
ResourceReadImage("::Images\\i.bmp», ExtImg, width, height);
TextOut («Text», 10,10,TA_LEFT|TA_TOP, ExtImg,100,100,0xffffff, COLOR_FORMAT_XRGB_NOALPHA);
ResourceCreate (»:: IMG», ExtImg,100,100,0,0,0,COLOR_FORMAT_XRGB_NOALPHA);
ChartRedraw ();
// – - return value of prev_calculated for next call
return (rates_total);
}
//+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +
void OnDeinit (const int reason) {
ObjectsDeleteAll (0, -1, -1);
}
 êà÷åñòâå ïðèìåðà èñïîëüçîâàíèÿ ãðàôè÷åñêèõ îáúåêòîâ, ðàññìîòðèì ñîçäàíèå èíäèêàòîðà, êîòîðûé âûâîäèò â íåáîëüøîå îêíî íà ãðàôèêå ñèìâîëà òîò æå ãðàôèê, íî ñ äðóãèì âðåìåííûì ïåðèîäîì.
Äëÿ ýòîãî èñïîëüçóåì ãðàôè÷åñêèé îáúåêò OBJ_CHART.
 êà÷åñòâå âõîäíûõ ïàðàìåòðîâ èíäèêàòîðà èñïîëüçóåì ñèìâîë ãðàôèêà è åãî ïåðèîä:
#property indicator_chart_window
input string InpSymbol=«EURUSD»; // Ñèìâîë
input ENUM_TIMEFRAMES InpPeriod=PERIOD_CURRENT; // Ïåðèîä
 ôóíêöèè OnInit () ñîçäàäèì ãðàôè÷åñêèé îáúåêò Ãðàôèê:
int OnInit ()
{
if (!ObjectCreate (0,«Chart», OBJ_CHART,0,0,0))
{
return (false);
}
Ïî óìîë÷àíèþ òî÷êà ïðèâÿçêè ýòîãî îáúåêòà – ëåâûé âåðõíèé óãîë ãðàôèêà.
Îïðåäåëèì îòñòóï òî÷êè ïðèâÿçêè îáúåêòà, åãî ðàçìåðû, ñèìâîë è ïåðèîä ãðàôèêà, îòîáðàæåíèå øêàëû âðåìåíè, ðàçìåð òî÷êè ïðèâÿçêè, ñ ïîìîùüþ êîòîðîé ìîæíî ïåðåìåùàòü îáúåêò, îòîáðàæåíèå öåíîâîé øêàëû, ðåæèì ïåðåìåùåíèÿ ìûøêîé, öâåò ðàìêè ãðàôèêà:
ObjectSetInteger (0,«Chart», OBJPROP_XDISTANCE,10);
ObjectSetInteger (0,«Chart», OBJPROP_YDISTANCE,20);
ObjectSetInteger (0,«Chart», OBJPROP_XSIZE,300);
ObjectSetInteger (0,«Chart», OBJPROP_YSIZE,200);
ObjectSetString (0,«Chart», OBJPROP_SYMBOL, InpSymbol);
ObjectSetInteger (0,«Chart», OBJPROP_PERIOD, InpPeriod);
ObjectSetInteger (0,«Chart», OBJPROP_DATE_SCALE, true);
ObjectSetInteger (0,«Chart», OBJPROP_WIDTH,1);
ObjectSetInteger (0,«Chart», OBJPROP_PRICE_SCALE, true);
ObjectSetInteger (0,«Chart», OBJPROP_SELECTABLE, true);
ObjectSetInteger (0,«Chart», OBJPROP_SELECTED, true);
ObjectSetInteger (0,«Chart», OBJPROP_COLOR, clrBlue);
Ñ ïîìîùüþ ñâîéñòâà îáúåêòîâ OBJPROP_CHART_ID ôóíêöèè ObjectGetInteger ïîëó÷èì èäåíòèôèêàòîð ãðàôèêà, èñïîëüçóÿ êîòîðûé ìû òåïåðü ìîæåì ïðèìåíÿòü ôóíêöèè ðàáîòû ñ ãðàôèêàìè (https://www.mql5.com/ru/docs/chart_operations) è ñâîéñòâà ãðàôèêîâ (https://www.mql5.com/ru/docs/constants/chartconstants/enum_chart_property):
long chartId=ObjectGetInteger (0,«Chart», OBJPROP_CHART_ID);
Îòêðîåì íàø ãðàôèê ñèìâîëà, ê êîòîðîìó ìû õîòèì ïðèñîåäèíèòü èíäèêàòîð, è íàæàâ ïðàâîé êíîïêîé ìûøêè, âûáåðåì ïóíêò â êîíòåêñòíîì ìåíþ Øàáëîíû è Ñîõðàíèòü øàáëîí.
Òåïåðü ìû ìîæåì ïåðåíåñòè íà íàø ãðàôè÷åñêèé îáúåêò âñå íàñòðîéêè è èíäèêàòîðû ãðàôèêà ñèìâîëà:
ChartApplyTemplate(chartId,"my.tpl»);
ChartRedraw (chartId);
// – —
return (INIT_SUCCEEDED);
}
Ïðèñîåäèíèâ èíäèêàòîð ê ãðàôèêó ñèìâîëà, ìû ìîæåì íàæàòü íà íåì ïðàâîé êíîïêîé ìûøêè è èçìåíèòü åãî ñâîéñòâà, âêëþ÷àÿ åãî ïåðèîä, ðàçìåðû è äð.
Ôóíêöèÿ PlaySound
Ôóíêöèÿ PlaySound âîñïðîèçâîäèò çâóêîâîé ôàéë. Íàïðèìåð, ýòî ìîæíî äåëàòü ïðè ïîÿâëåíèè ñèãíàëà èíäèêàòîðà äëÿ íàïîìèíàíèÿ:
bool PlaySound (
string filename // èìÿ WAV-ôàéëà
);
 êà÷åñòâå ïðèìåðà äîáàâèì çâóêîâîé ñèãíàë â íàø èíäèêàòîð Impulse keeper ïðè ïîÿâëåíèè ïåðâîãî ñèãíàëà íà ïîêóïêó èëè ïðîäàæó.
Ñêà÷àåì êàêîé-íèáóäü WAV-ñèãíàë èç Èíòåðíåòà è ïîìåñòèì åãî ôàéë â ïàïêó Sounds òåðìèíàëà.
Äîáàâèì êîä â èíäèêàòîð Impulse keeper:
#property indicator_chart_window
#property indicator_buffers 4
double EMA34HBuffer [];
double EMA34LBuffer [];
double EMA125Buffer [];
double PSARBuffer [];
int EMA34HHandle;
int EMA34LHandle;
int EMA125Handle;
int PSARHandle;
int bars_calculated=0;
int countBuy=0
int countSell=0;
//+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +
//| Custom indicator initialization function |
//+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +
int OnInit ()
{
// – - indicator buffers mapping
SetIndexBuffer (0,EMA34HBuffer, INDICATOR_CALCULATIONS);
SetIndexBuffer (1,EMA34LBuffer, INDICATOR_CALCULATIONS);
SetIndexBuffer (2,EMA125Buffer, INDICATOR_CALCULATIONS);
SetIndexBuffer (3,PSARBuffer, INDICATOR_CALCULATIONS);
EMA34HHandle=iMA (NULL,0,34,0,MODE_EMA, PRICE_HIGH);
EMA34LHandle=iMA (NULL,0,34,0,MODE_EMA, PRICE_LOW);
EMA125Handle=iMA (NULL,0,125,0,MODE_EMA, PRICE_CLOSE);
PSARHandle=iSAR (NULL,0,0.02, 0.2);
// – —
return (INIT_SUCCEEDED);
}
//+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +
//| 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 [])
{
// – —
int values_to_copy;
int start;
int calculated=BarsCalculated (EMA34HHandle);
if (calculated <=0)
{
return (0);
}
if (prev_calculated==0 || calculated!=bars_calculated)
{
start=1;
if (calculated> rates_total) values_to_copy=rates_total;
else values_to_copy=calculated;
}
else
{
start=rates_total-1;
values_to_copy=1;
}
if (!FillArrayFromMABuffer (EMA34HBuffer,0,EMA34HHandle, values_to_copy)) return (0); if (!FillArrayFromMABuffer (EMA34LBuffer,0,EMA34LHandle, values_to_copy)) return (0); if (!FillArrayFromMABuffer (EMA125Buffer,0,EMA125Handle, values_to_copy)) return (0);
if (!FillArrayFromPSARBuffer (PSARBuffer, PSARHandle, values_to_copy)) return (0);
for (int i=start; i <rates_total &&!IsStopped ();i++)
{
if (close [i-1]> open [i-1] &&close [i-1]> EMA34HBuffer [i-1] &&close [i-1]> EMA34LBuffer [i-1] &&low [i-1]> EMA125Buffer [i-1] &&low [i-1]> PSARBuffer [i-1] &&EMA125Buffer [i-1] <EMA34LBuffer [i-1] &&EMA125Buffer [i-1] <EMA34HBuffer [i-1]) {
if (!ObjectCreate (0,«Buy»+i, OBJ_ARROW,0,time [i-1],high [i-1]))
{
return (false);
}
ObjectSetInteger (0,«Buy»+i, OBJPROP_COLOR, clrGreen);
ObjectSetInteger (0,«Buy»+i, OBJPROP_ARROWCODE,233);
ObjectSetInteger (0,«Buy»+i, OBJPROP_WIDTH,2);
ObjectSetInteger (0,«Buy»+i, OBJPROP_ANCHOR, ANCHOR_UPPER);
ObjectSetInteger (0,«Buy»+i, OBJPROP_HIDDEN, true);
ObjectSetString (0,«Buy»+i, OBJPROP_TOOLTIP, close [i-1]);
}
if (start!=1) {
if (close [i-1]> open [i-1] &&close [i-1]> EMA34HBuffer [i-1] &&close [i-1]> EMA34LBuffer [i-1] &&low [i-1]> EMA125Buffer [i-1] &&low [i-1]> PSARBuffer [i-1] &&EMA125Buffer [i-1] <EMA34LBuffer [i-1] &&EMA125Buffer [i-1] <EMA34HBuffer [i-1]) {
countBuy++;
if (countBuy==1) PlaySound («chime. wav»)
} else {
countBuy=0;
}
if (close [i-1] <open [i-1] &&close [i-1] <EMA34HBuffer [i-1] &&close [i-1] <EMA34LBuffer [i-1] &&high [i-1] <EMA125Buffer [i-1] &&high [i-1] <PSARBuffer [i-1] &&EMA125Buffer [i-1]> EMA34LBuffer [i-1] &&EMA125Buffer [i-1]> EMA34HBuffer [i-1]) {