I am just starting to build my first EA. I have written an easy indicator, which is working perfect when attaching to a chart in Meta Trader 5. Also when I debug the indicator all is very fine. As a result from my indicator I can see the arrows plotted to the chart. But when I try to start the indicator from my EA nothing happens. I can’t find the error. Please have a look at the OnInit() function. What I intend to do is open “EURUSD” Chart if not already open, bring it top of all other windows (do I need to do this?). And then attach the indicator to that chart. Returncodes are all fine. No error. But indicator is doing nothing.
//+------------------------------------------------------------------+
//| MMM.mq5 |
//| D. Janca |
//| http://www.janca-consulting.de/ |
//+------------------------------------------------------------------+
string cSymbolName="EURUSD";
double Lots = 0.3;
ENUM_TIMEFRAMES GNSTimeFrame = PERIOD_M1;
ENUM_TIMEFRAMES GNS15TimeFrame = PERIOD_M15;
ENUM_TIMEFRAMES GNSD1TimeFrame = PERIOD_D1;
// handle für meine Indikatoren
int hGNS;
int hGNS15;
int hGNSD1;
int logfile; // logfile (debugging)
// es soll immer nur eine Order offen sein, entweder buy oder sell
bool bord=false, sord=false;
datetime zeithD1[500], zeitlD1[500];
int anzhD1, anzlD1;
double highsD1[500], lowsD1[500];
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
long BindToChart(ENUM_TIMEFRAMES setPer) {
// immer Candles anzeigen
ENUM_CHART_MODE cMode = CHART_CANDLES; // CHART_LINE, CHART_BARS
long chartID, nID;
string cSymbol;
int cPer;
int currM;
Sleep(2000);
chartID = ChartFirst();
cSymbol = ChartSymbol(chartID);
cPer = ChartPeriod(chartID);
nID = chartID;
while((cPer != setPer) || (cSymbol != cSymbolName)) {
nID=ChartNext(chartID); // Get the new chart ID by using the previous chart ID
if(nID<0) break;
chartID = nID;
cSymbol = ChartSymbol(chartID);
cPer = ChartPeriod(chartID);
}
if (nID < 0) {
// Chart muß geöffnet werden
chartID = ChartOpen(cSymbolName,setPer);
}
ChartBringToTop(chartID);
// bring chart window to top
ChartForegroundSet(true,chartID);
// show Candles
currM = ChartModeGet(chartID);
if (currM != cMode) {
ChartModeSet(cMode,chartID);
}
return chartID;
}
bool ChartBringToTop(const long chart_ID=0)
{
//--- Setzen den Wert des Fehlers zurück
ResetLastError();
//--- Zeigt wir den Chart über alle anderen
if(!ChartSetInteger(chart_ID,CHART_BRING_TO_TOP,0,true))
{
//--- Schreiben die Fehlermeldung in den Log "Experten"
Print(__FUNCTION__+", Error Code = ",GetLastError());
return(false);
}
//--- erfolgreiche Ausführung
return(true);
}
bool ChartForegroundSet(const bool value,const long chart_ID=0)
{
//--- reset the error value
ResetLastError();
//--- set property value
if(!ChartSetInteger(chart_ID,CHART_FOREGROUND,value))
{
//--- display the error message in Experts journal
Print(__FUNCTION__+", Error Code = ",GetLastError());
return(false);
}
//--- successful execution
return(true);
}
ENUM_CHART_MODE ChartModeGet(const long chart_ID=0)
{
//--- prepare the variable to get the property value
long result=WRONG_VALUE;
//--- reset the error value
ResetLastError();
//--- receive the property value
result = ChartGetInteger(chart_ID,CHART_MODE);
if(!result)
{
//--- display the error message in Experts journal
Print(__FUNCTION__+", Error Code = ",GetLastError());
}
//--- return the value of the chart property
return((ENUM_CHART_MODE)result);
}
//+------------------------------------------------------------------+
//| Sets chart display type (candlesticks, bars or line) |
//+------------------------------------------------------------------+
bool ChartModeSet(const long value,const long chart_ID=0)
{
//--- reset the error value
ResetLastError();
//--- set property value
if(!ChartSetInteger(chart_ID,CHART_MODE,value))
{
//--- display the error message in Experts journal
Print(__FUNCTION__+", Error Code = ",GetLastError());
return(false);
}
//--- successful execution
return(true);
}
int OnInit()
{
MqlDateTime stm;
long chartID;
int timeframe;
datetime itime[];
string s;
hGNS = 0;
hGNS15 = 0;
hGNSD1 = 0;
TimeToStruct((datetime)SymbolInfoInteger(cSymbolName,SYMBOL_TIME),stm);
ResetLastError();
logfile=FileOpen("log.txt",FILE_WRITE|FILE_TXT|FILE_ANSI);
timeframe = 300;
chartID = BindToChart(PERIOD_D1);
s = Symbol();
hGNSD1=iCustom(cSymbolName,PERIOD_D1,"GetNextStopDay",timeframe);
if (!ChartIndicatorAdd(chartID,0,hGNSD1)) {
PrintFormat("Failed to add M1 indicator on main chart window. Error code %d",GetLastError());
return -1;
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if (hGNS != 0) IndicatorRelease(hGNS);
if (hGNS15 != 0) IndicatorRelease(hGNS15);
if (hGNSD1 != 0) IndicatorRelease(hGNSD1);
ResetLastError();
FileClose(logfile);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
}
//+------------------------------------------------------------------+
//| Trade function |
//+------------------------------------------------------------------+
void OnTrade()
{
//---
}
//+------------------------------------------------------------------+