У меня есть немного опыта для разработки на MT4 / MQL4.
Сейчас я пытаюсь перейти на MQL5, но я столкнулся с проблемой на первом этапе.
Я просто хочу показать последний MacD и сигнал для каждого таймфрейма.
Однако он не показывает значения.
Где это неправильно ??
//+------------------------------------------------------------------+
//| test.mq5 |
//| Copyright 2020, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int hMacd;
double bufMacdMain[];
double bufMacdSignal[];
int OnInit()
{
//---
SetIndexBuffer(0, bufMacdMain, INDICATOR_DATA);
//ArraySetAsSeries(bufMacdMain, true);
SetIndexBuffer(1, bufMacdSignal, INDICATOR_DATA);
//ArraySetAsSeries(bufMacdSignal, true);
hMacd = iMACD(NULL,0,13,26,9,PRICE_CLOSE);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
IndicatorRelease(hMacd);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(isNewBar(_Symbol,0)) {
Print("newbar1");
CopyBuffer(hMacd, 0, 0, 100, bufMacdMain);
CopyBuffer(hMacd, 1, 0, 100, bufMacdSignal);
printf(__FUNCTION__+": MACD = ", bufMacdMain[0], " / ", bufMacdSignal[0]);
}
}
//+------------------------------------------------------------------+
bool isNewBar(string symbol, ENUM_TIMEFRAMES tf)
{
static datetime time = 0;
if(iTime(symbol, tf, 0) != time)
{
time = iTime(symbol, tf, 0);
return true;
}
return false;
}
код обновлен благодаря @Daniel Kniaz
Работает.
//+------------------------------------------------------------------+
//| test.mq5 |
//| Copyright 2020, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int hMacd;
double bufMacdMain[];
double bufMacdSignal[];
int OnInit()
{
//---
SetIndexBuffer(0, bufMacdMain, INDICATOR_DATA);
ArraySetAsSeries(bufMacdMain, true);
SetIndexBuffer(1, bufMacdSignal, INDICATOR_DATA);
ArraySetAsSeries(bufMacdSignal, true);
hMacd = iMACD(NULL,0,13,26,9,PRICE_CLOSE);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
IndicatorRelease(hMacd);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(isNewBar(_Symbol)) {
Print("newbar1");
int macCopied = CopyBuffer(hMacd, 0, 0, 1, bufMacdMain);
int sigCopied = CopyBuffer(hMacd, 1, 0, 1, bufMacdSignal);
print("macCopied Num:",macCopied);
print("sigCopied Num:",sigCopied);
Print(__FUNCTION__+": MACD = ", bufMacdMain[0], " / ", bufMacdSignal[0]);
}
}
//+------------------------------------------------------------------+
bool isNewBar(string symbol, ENUM_TIMEFRAMES tf = PERIOD_CURRENT)
{
static datetime time = 0;
if(iTime(symbol, tf, 0) != time)
{
time = iTime(symbol, tf, 0);
return true;
}
return false;
}