From f27bc3d1d965f7f3d24b262186879a9c246a8910 Mon Sep 17 00:00:00 2001 From: Gunther Schulz Date: Sun, 16 Feb 2020 12:23:28 +0100 Subject: [PATCH] add indicator control any mt5 indicator --- Experts/JsonAPI.mq5 | 182 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 164 insertions(+), 18 deletions(-) diff --git a/Experts/JsonAPI.mq5 b/Experts/JsonAPI.mq5 index 24a5592..74342dc 100644 --- a/Experts/JsonAPI.mq5 +++ b/Experts/JsonAPI.mq5 @@ -36,6 +36,7 @@ int SYS_PORT=15555; int DATA_PORT=15556; int LIVE_PORT=15557; int STR_PORT=15558; +int IND_DATA_PORT=15559; // ZeroMQ Cnnections Context context("MQL5 JSON API"); @@ -43,6 +44,7 @@ Socket sysSocket(context,ZMQ_REP); Socket dataSocket(context,ZMQ_PUSH); Socket liveSocket(context,ZMQ_PUSH); Socket streamSocket(context,ZMQ_PUSH); +Socket indicatorDataSocket(context,ZMQ_PUSH); // Global variables bool debug = false; @@ -53,6 +55,10 @@ string chartSymbols[]; int chartSymbolCount = 0; string chartSymbolSettings[][3]; +int indicatorCount = 0; +double indicators[]; +string indicatorIds[]; + //+------------------------------------------------------------------+ //| Bind ZMQ sockets to ports | //+------------------------------------------------------------------+ @@ -66,22 +72,27 @@ bool BindSockets(){ if (result == false) return result; result = streamSocket.bind(StringFormat("tcp://%s:%d", HOST,STR_PORT)); if (result == false) return result; + result = indicatorDataSocket.bind(StringFormat("tcp://%s:%d", HOST,IND_DATA_PORT)); + if (result == false) return result; 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); liveSocket.setLinger(1000); streamSocket.setLinger(1000); + indicatorDataSocket.setLinger(1000); // Number of messages to buffer in RAM. sysSocket.setSendHighWaterMark(1); dataSocket.setSendHighWaterMark(5); liveSocket.setSendHighWaterMark(1); streamSocket.setSendHighWaterMark(50); + indicatorDataSocket.setSendHighWaterMark(5); return result; } @@ -159,17 +170,31 @@ void OnTick(){ //+------------------------------------------------------------------+ //| Check if subscribed to symbol and timeframe combination | //+------------------------------------------------------------------+ -bool hasChartSymbol(string symbol, string chartTF) +bool HasChartSymbol(string symbol, string chartTF) { for(int i=0;i= 0) { + if(CopyBuffer(indicators[idx], i, fromDate, 1, values) < 0) values[0] = 0.0; + results[i] = DoubleToString(values[0]); + } + else { + // TODO error handling + } + } + + if (liveStream) { + CJAVal data; + data["id"] = (string) id; + data["data"].Set(results); + + string t=data.Serialize(); + if(debug) Print(t); + InformClientSocket(indicatorDataSocket,t); + } +} + //+------------------------------------------------------------------+ //| Account information | //+------------------------------------------------------------------+ @@ -709,18 +847,26 @@ void GetOrders(CJAVal &dataObject){ } //+------------------------------------------------------------------+ -//| Clear symbol subscriptions | +//| Clear symbol subscriptions and indicators | //+------------------------------------------------------------------+ -void ResetSubscriptions(CJAVal &dataObject){ +void ResetSubscriptionsAndIndicators(){ ArrayFree(chartSymbols); - chartSymbolCount=0; ArrayFree(chartSymbolSettings); + chartSymbolCount=0; - if(ArraySize(chartSymbols)!=0 ||ArraySize(chartSymbolSettings)!=0){ + bool error = false; + for(int i=0;i