diff --git a/Experts/JsonAPI.mq5 b/Experts/JsonAPI.mq5 index 2aae593..2f0b465 100644 --- a/Experts/JsonAPI.mq5 +++ b/Experts/JsonAPI.mq5 @@ -31,6 +31,7 @@ #include #include #include +#include //#include //#include //#include @@ -70,14 +71,10 @@ struct SymbolSubscription { string chartTf; datetime lastBar; }; - SymbolSubscription symbolSubscriptions[]; -//string chartSymbols[]; int symbolSubscriptionCount = 0; -//string chartSymbolSettings[][3]; // Variables for controlling indicators - struct Indicator { long id; string indicatorId; @@ -87,12 +84,6 @@ struct Indicator { }; Indicator indicators[]; int indicatorCount = 0; -/* -double indicators[]; -string indicatorIds[]; -int indicatorParamCount[]; -int indicatorBufferCount[]; -*/ // Variables for controlling chart struct ChartWindow { @@ -108,6 +99,7 @@ int chartWindowCount = 0; int chartWindowTimerInterval = 100; int chartWindowTimerCounter = 0; +ControlErrors mControl; //string chartWindowObjects[]; @@ -120,28 +112,19 @@ int chartWindowTimerCounter = 0; bool BindSockets(){ bool result = false; result = sysSocket.bind(StringFormat("tcp://%s:%d", HOST,SYS_PORT)); - if (result == false) return result; + if (result == false) { return result; } else {Print("Bound 'System' socket on port ", SYS_PORT);} result = dataSocket.bind(StringFormat("tcp://%s:%d", HOST,DATA_PORT)); - if (result == false) return result; + if (result == false) { return result; } else {Print("Bound 'Data' socket on port ", DATA_PORT);} result = liveSocket.bind(StringFormat("tcp://%s:%d", HOST,LIVE_PORT)); - if (result == false) return result; + if (result == false) { return result; } else {Print("Bound 'Live' socket on port ", LIVE_PORT);} result = streamSocket.bind(StringFormat("tcp://%s:%d", HOST,STR_PORT)); - if (result == false) return result; + if (result == false) { return result; } else {Print("Bound 'Streaming' socket on port ", STR_PORT);} result = indicatorDataSocket.bind(StringFormat("tcp://%s:%d", HOST,IND_DATA_PORT)); - if (result == false) return result; - + if (result == false) { return result; } else {Print("Bound 'Indicator Data' socket on port ", IND_DATA_PORT);} result = chartLiveSocket.bind(StringFormat("tcp://%s:%d", HOST,CHART_LIVE_PORT)); if (result == false) { return result; } else {Print("Bound 'Chart Live' socket on port ", CHART_LIVE_PORT);} - result = pubChartLiveSocket.bind(StringFormat("tcp://%s:%d", HOST,PUB_CHART_LIVE_PORT)); if (result == false) { return result; } else {Print("Bound 'PUB Chart Live' socket on port ", PUB_CHART_LIVE_PORT);} - - Print("Bound 'System' socket on port ", SYS_PORT); - Print("Bound 'Data' socket on port ", DATA_PORT); - Print("Bound 'Live' socket on port ", LIVE_PORT); - Print("Bound 'Streaming' socket on port ", STR_PORT); - Print("Bound 'Indicator Data' socket on port ", IND_DATA_PORT); - sysSocket.setLinger(1000); dataSocket.setLinger(1000); @@ -149,7 +132,7 @@ bool BindSockets(){ streamSocket.setLinger(1000); indicatorDataSocket.setLinger(1000); chartLiveSocket.setLinger(1000); - //pubChartLiveSocket.setLinger(1000); + pubChartLiveSocket.setLinger(1000); // Number of messages to buffer in RAM. sysSocket.setSendHighWaterMark(1); @@ -158,7 +141,7 @@ bool BindSockets(){ streamSocket.setSendHighWaterMark(50); indicatorDataSocket.setSendHighWaterMark(5); chartLiveSocket.setReceiveHighWaterMark(1); // TODO confirm settings - //pubChartLiveSocket.setReceiveHighWaterMark(1); + pubChartLiveSocket.setReceiveHighWaterMark(1); return result; } @@ -167,8 +150,15 @@ bool BindSockets(){ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit(){ - /* Bindinig ZMQ ports on init */ + // Settimg up error reporting + mControl.SetAlert(true); + //mControl.SetPrint(true); + mControl.SetSound(false); + mControl.SetWriteFlag(false); + //mControl.SetPlaySoundFile("news.wav"); + + /* Bindinig ZMQ ports on init */ // Skip reloading of the EA script when the reason to reload is a chart timeframe change if (deInitReason != REASON_CHARTCHANGE){ @@ -232,9 +222,6 @@ void OnDeinit(const int reason){ ResetSubscriptionsAndIndicators(); EventKillTimer(); - for(int i=0;i= 0) { - if(CopyBuffer(indicators[idx].indicatorHandle, i, fromDate, 1, values) < 0) {/* Error handling */} + if(CopyBuffer(indicators[idx].indicatorHandle, i, fromDate, 1, values) < 0) { + if(mControl.mGetLastError()) + { + CJAVal message; + string desc = mControl.mGetDesc(); + int lastError = mControl.mGetLastError(); + mControl.Check(); + + message["error"]=(bool) true; + message["lastError"]=(string) lastError; + message["description"]=desc; + message["function"]=(string) __FUNCTION__; + string t=message.Serialize(); + if(debug) Print(t); + InformClientSocket(indicatorDataSocket,t); + } + } results[i] = DoubleToString(values[0]); } - else { - // TODO error handling - } } - CJAVal message; + message["error"]=(bool) false; message["id"] = (string) id; message["data"].Set(results); @@ -621,11 +636,6 @@ void GetIndicatorResult(CJAVal &dataObject) { if(debug) Print(t); InformClientSocket(indicatorDataSocket,t); - // TODO more error handling - //if(SymbolInfoInteger(symbol, SYMBOL_EXIST)){ - // ActionDoneOrError(ERR_SUCCESS, __FUNCTION__); - //} - //else ActionDoneOrError(ERR_MARKET_UNKNOWN_SYMBOL, __FUNCTION__); } //+------------------------------------------------------------------+ @@ -655,9 +665,6 @@ void OpenChart(CJAVal &dataObject){ chartWindowCount++; ArrayResize(chartWindows,chartWindowCount); - //ArrayResize(chartWindowIds,chartWindowCount); - //ArrayResize(chartWindowPeriods,chartWindowCount); - // ArrayResize(chartWindowObjects,chartWindowCount); int idx = chartWindowCount-1; @@ -665,7 +672,6 @@ void OpenChart(CJAVal &dataObject){ ENUM_TIMEFRAMES period = GetTimeframe(chartTF); chartWindows[idx].id = ChartOpen(symbol, period); - //chartWindowPeriods[idx] = period; CJAVal message; message["chartId"] = (string) chartId; @@ -694,17 +700,18 @@ void AddIndicatorChartToChart(CJAVal &dataObject){ double chartIndicatorHandle = iCustom(ChartSymbol(ChartId),ChartPeriod(ChartId),"JsonAPIIndicator",chartIndicatorId,shortname,colorstyle,linetype,linestyle,linewidth); - ChartIndicatorAdd(ChartId, chartIndicatorSubWindow, chartIndicatorHandle); - - chartWindows[idx].indicatorId = chartIndicatorId; - chartWindows[idx].indicatorHandle = chartIndicatorHandle; - - CJAVal message; - message["chartId"] = (string) chartIdStr; - - string t=message.Serialize(); - if(debug) Print(t); - InformClientSocket(dataSocket,t); + if(ChartIndicatorAdd(ChartId, chartIndicatorSubWindow, chartIndicatorHandle)){ + chartWindows[idx].indicatorId = chartIndicatorId; + chartWindows[idx].indicatorHandle = chartIndicatorHandle; + } + if(!CheckError(__FUNCTION__)) { + CJAVal message; + message["chartId"] = (string) chartIdStr; + + string t=message.Serialize(); + if(debug) Print(t); + InformClientSocket(dataSocket,t); + } } //+------------------------------------------------------------------+ @@ -943,19 +950,22 @@ void HistoryInfo(CJAVal &dataObject){ ulong toDateM = StringToTime(toDate); tickCount=CopyTicksRange(symbol,tickArray,COPY_TICKS_ALL,1000*(ulong)fromDateM,1000*(ulong)toDateM); - if(tickCount){ - ActionDoneOrError(ERR_SUCCESS , __FUNCTION__); + if(tickCount < 0) { + mControl.mSetUserError(65541, GetErrorID(65541)); } - else ActionDoneOrError(65541 , __FUNCTION__); + CheckError(__FUNCTION__); Print("Preparing data of ", tickCount, " ticks for ", symbol); int file_handle=FileOpen(outputFile, FILE_WRITE | FILE_CSV); if(file_handle!=INVALID_HANDLE){ - msg["status"] = (string) "CONNECTED"; - msg["type"] = (string) "NORMAL"; - msg["data"] = (string) StringFormat("Writing to: %s\\%s", TerminalInfoString(TERMINAL_DATA_PATH), outputFile); - if(liveStream) InformClientSocket(liveSocket, msg.Serialize()); - ActionDoneOrError(ERR_SUCCESS , __FUNCTION__); + msg["status"] = (string) "CONNECTED"; + msg["type"] = (string) "NORMAL"; + msg["data"] = (string) StringFormat("Writing to: %s\\%s", TerminalInfoString(TERMINAL_DATA_PATH), outputFile); + if(liveStream) InformClientSocket(liveSocket, msg.Serialize()); + ActionDoneOrError(ERR_SUCCESS , __FUNCTION__, "ERR_SUCCESS"); + //ActionDoneOrError(ERR_SUCCESS , __FUNCTION__, dataSocket); + // Inform client that file is avalable for writing + PrintFormat("%s file is available for writing",fileName); PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH)); //--- write the time and values of signals to the file @@ -975,9 +985,12 @@ void HistoryInfo(CJAVal &dataObject){ if(liveStream) InformClientSocket(liveSocket, msg.Serialize()); } - else{ - PrintFormat("Failed to open %s file, Error code = %d",fileName,GetLastError()); - ActionDoneOrError(65542 , __FUNCTION__); + else{ + // File is not available for writing + mControl.mSetUserError(65542, GetErrorID(65542)); + CheckError(__FUNCTION__); + //PrintFormat("Failed to open %s file, Error code = %d",fileName,GetLastError()); + //ActionDoneOrError(65542 , __FUNCTION__); } connectedFlag=false; } @@ -1005,16 +1018,16 @@ void HistoryInfo(CJAVal &dataObject){ if(dataObject["toDate"].ToInt()!=NULL)Print("4) Date to:"+TimeToString(toDate)); barCount=CopyRates(symbol,period,fromDate,toDate,r); - if(CopySpread(symbol,period, fromDate, toDate, spread)!=1) { /*error processing */ } - - if(barCount){ - ActionDoneOrError(ERR_SUCCESS, __FUNCTION__); + if(CopySpread(symbol,period, fromDate, toDate, spread)!=1) { + mControl.mSetUserError(65541, GetErrorID(65541)); + //CheckError(__FUNCTION__); } - else ActionDoneOrError(65541, __FUNCTION__); Print("Preparing tick data of ", barCount, " ticks for ", symbol); int file_handle=FileOpen(outputFile, FILE_WRITE | FILE_CSV); if(file_handle!=INVALID_HANDLE){ + ActionDoneOrError(ERR_SUCCESS , __FUNCTION__, "ERR_SUCCESS"); + //ActionDoneOrError(ERR_SUCCESS , __FUNCTION__, dataSocket); PrintFormat("%s file is available for writing",outputFile); PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH)); //--- write the time and values of signals to the file @@ -1025,8 +1038,10 @@ void HistoryInfo(CJAVal &dataObject){ PrintFormat("Data is written, %s file is closed", outputFile); } else{ - PrintFormat("Failed to open %s file, Error code = %d",outputFile,GetLastError()); - ActionDoneOrError(65542 , __FUNCTION__); + //PrintFormat("Failed to open %s file, Error code = %d",outputFile,GetLastError()); + //ActionDoneOrError(65542 , __FUNCTION__); + mControl.mSetUserError(65542, GetErrorID(65542)); + CheckError(__FUNCTION__); } } @@ -1091,7 +1106,7 @@ void HistoryInfo(CJAVal &dataObject){ } barCount=CopyRates(symbol, period, fromDate, toDate, r); - if(CopySpread(symbol,period, fromDate, toDate, spread)!=1) { /*error processing */ } + if(CopySpread(symbol,period, fromDate, toDate, spread)!=1) { /*mControl.Check();*/ } if(barCount){ for(int i=0;i