allow ENUM as string as parameters for indicators

minor code optimization
This commit is contained in:
Gunther Schulz
2020-03-03 13:21:24 +01:00
parent 37cb0fe738
commit 4b2297fbfb
3 changed files with 51 additions and 25 deletions
+44 -18
View File
@@ -30,7 +30,7 @@
#include <Trade/Trade.mqh> #include <Trade/Trade.mqh>
#include <Zmq/Zmq.mqh> #include <Zmq/Zmq.mqh>
#include <Json.mqh> #include <Json.mqh>
#include <EnumStringToInt.mqh> #include <StringToEnumInt.mqh>
#include <ControlErrors.mqh> #include <ControlErrors.mqh>
//#include <ChartObjects\ChartObject.mqh> //#include <ChartObjects\ChartObject.mqh>
//#include<Canvas\Canvas.mqh> //#include<Canvas\Canvas.mqh>
@@ -377,7 +377,7 @@ void OnTimer(){
ZmqMsg chartMsg; ZmqMsg chartMsg;
chartDataSocket.recv(chartMsg, true); chartDataSocket.recv(chartMsg, true);
if(chartMsg.size()>0){ if(chartMsg.size()>0){
Print(chartMsg.getData()); if(debug) Print(chartMsg.getData());
chartIndicatorSocket.send(chartMsg,true); chartIndicatorSocket.send(chartMsg,true);
//ResetLastError(); //ResetLastError();
} }
@@ -490,11 +490,26 @@ void IndicatorControl(CJAVal &dataObject){
} }
} }
//+------------------------------------------------------------------+
//| Check if string is a representation of a number |
//+------------------------------------------------------------------+
bool IsNumberAsString(string str) {
// MQL5 seems to return true if the values are the same, no matter the data type, in this case comparing str and dbl/int.
// (str "2.1" == double 2.1) will return true.
double dbl = StringToDouble(str);
int integer = StringToInteger(str);
// Compaing to both int and double to cover both cases
if(str==dbl || str==integer) return true;
else return false;
}
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Start new indicator instance | //| Start new indicator instance |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void StartIndicator(CJAVal &dataObject){ void StartIndicator(CJAVal &dataObject){
// TODO map Indicators Constants https://www.mql5.com/en/docs/constants/indicatorconstants
string symbol=dataObject["symbol"].ToStr(); string symbol=dataObject["symbol"].ToStr();
string chartTF=dataObject["chartTF"].ToStr(); string chartTF=dataObject["chartTF"].ToStr();
string id=dataObject["id"].ToStr(); string id=dataObject["id"].ToStr();
@@ -511,8 +526,14 @@ void StartIndicator(CJAVal &dataObject){
double params[]; double params[];
indicators[idx].indicatorParamCount = dataObject["params"].Size(); indicators[idx].indicatorParamCount = dataObject["params"].Size();
for(int i=0;i<indicators[idx].indicatorParamCount;i++){ for(int i=0;i<indicators[idx].indicatorParamCount;i++){
// TODO test it. Is it ok to pass EnumInts as Doubles for params?
ArrayResize(params, i+1); ArrayResize(params, i+1);
params[i] = dataObject["params"][i].ToDbl(); string paramStr = dataObject["params"][i].ToStr();
if(IsNumberAsString(paramStr)) params[i] = StringToDouble(paramStr);
else {
params[i] = StringToEnumInt(paramStr);
mControl.mResetLastError(); // TODO find where the Error 4003 is craeted in StringToEnumInt
}
} }
ENUM_TIMEFRAMES period = GetTimeframe(chartTF); ENUM_TIMEFRAMES period = GetTimeframe(chartTF);
@@ -559,10 +580,11 @@ void StartIndicator(CJAVal &dataObject){
} }
CJAVal message; CJAVal message;
if(mControl.mGetLastError()) if(mControl.mGetLastError())
{ {
string desc = mControl.mGetDesc();
int lastError = mControl.mGetLastError(); int lastError = mControl.mGetLastError();
string desc = mControl.mGetDesc();
mControl.Check(); mControl.Check();
message["error"]=(bool) true; message["error"]=(bool) true;
@@ -590,8 +612,6 @@ void StartIndicator(CJAVal &dataObject){
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void GetIndicatorResult(CJAVal &dataObject) { void GetIndicatorResult(CJAVal &dataObject) {
// TODO map Indicators Constants https://www.mql5.com/en/docs/constants/indicatorconstants
datetime fromDate=dataObject["fromDate"].ToInt(); datetime fromDate=dataObject["fromDate"].ToInt();
string id=dataObject["id"].ToStr(); string id=dataObject["id"].ToStr();
string indicatorName=dataObject["indicatorName"].ToStr(); string indicatorName=dataObject["indicatorName"].ToStr();
@@ -611,8 +631,8 @@ void GetIndicatorResult(CJAVal &dataObject) {
if(mControl.mGetLastError()) if(mControl.mGetLastError())
{ {
CJAVal message; CJAVal message;
string desc = mControl.mGetDesc();
int lastError = mControl.mGetLastError(); int lastError = mControl.mGetLastError();
string desc = mControl.mGetDesc();
mControl.Check(); mControl.Check();
message["error"]=(bool) true; message["error"]=(bool) true;
@@ -648,7 +668,10 @@ void ChartControl(CJAVal &dataObject){
string actionType=dataObject["actionType"].ToStr(); string actionType=dataObject["actionType"].ToStr();
if(actionType=="ADDINDICATOR") { if(actionType=="ADDINDICATOR") {
AddIndicatorChartToChart(dataObject); AddChartIndicator(dataObject);
}
else if(actionType=="ADDOBJECT") {
AddObjectToChart(dataObject);
} }
else if(actionType=="OPEN") { else if(actionType=="OPEN") {
OpenChart(dataObject); OpenChart(dataObject);
@@ -686,7 +709,7 @@ void OpenChart(CJAVal &dataObject){
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Add JsonAPIIndicator indicator to chart | //| Add JsonAPIIndicator indicator to chart |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void AddIndicatorChartToChart(CJAVal &dataObject){ void AddChartIndicator(CJAVal &dataObject){
string chartIdStr=dataObject["chartId"].ToStr(); string chartIdStr=dataObject["chartId"].ToStr();
string chartIndicatorId=dataObject["indicatorChartId"].ToStr(); string chartIndicatorId=dataObject["indicatorChartId"].ToStr();
@@ -870,9 +893,9 @@ void ChartDrawX(CJAVal &dataObject){
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Draw on chart | //| Draw on chart function |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void ChartDraw(CJAVal &dataObject){ void AddObjectToChart(CJAVal &dataObject){
// DONT DRAW, if chartid noes not exist any more // DONT DRAW, if chartid noes not exist any more
@@ -1567,21 +1590,24 @@ void OrderDoneOrError(bool error, string funcName, CTrade &trade){
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
bool CheckError(string funcName) { bool CheckError(string funcName) {
//string desc = "OK"; //string desc = "OK";
int lastError = ERR_SUCCESS; //int lastError = ERR_SUCCESS;
if(mControl.mGetLastError()) int lastError = mControl.mGetLastError();
if(lastError)
{ {
string desc = mControl.mGetDesc(); string desc = mControl.mGetDesc();
lastError = mControl.mGetLastError(); //lastError = mControl.mGetLastError();
if(debug) Print("Error handling source: ", funcName ," description: ", desc); if(debug) Print("Error handling source: ", funcName ," description: ", desc);
Print("Error handling source: ", funcName ," description: ", desc); Print("Error handling source: ", funcName ," description: ", desc);
mControl.Check(); mControl.Check();
ActionDoneOrError(lastError, funcName, desc); ActionDoneOrError(lastError, funcName, desc);
return true;
//ActionDoneOrError(lastError, __FUNCTION__, socket, desc) //ActionDoneOrError(lastError, __FUNCTION__, socket, desc)
} }
if(lastError==0) else return false;
return false; // if(lastError==0)
else // return false;
return true; // else
// return true;
//Print("lasterror", lastError); //Print("lasterror", lastError);
//return lastError; //return lastError;
+3 -3
View File
@@ -17,7 +17,7 @@ sed -i '1s/^/\xef\xbb\xbf/' ./Indicators/JsonAPIIndicator.clean.mq5
mv ./Indicators/JsonAPIIndicator.clean.mq5 ./Indicators/JsonAPIIndicator.mq5 mv ./Indicators/JsonAPIIndicator.clean.mq5 ./Indicators/JsonAPIIndicator.mq5
# Remove non-printable ASCII characters # Remove non-printable ASCII characters
tr -cd '[:print:]\n\r' < ./Include/EnumStringToInt.mqh > ./Include/EnumStringToInt.clean.mqh tr -cd '[:print:]\n\r' < ./Include/StringToEnumInt.mqh > ./Include/StringToEnumInt.clean.mqh
# Add UTF-8 BOM # Add UTF-8 BOM
sed -i '1s/^/\xef\xbb\xbf/' ./Include/EnumStringToInt.clean.mqh sed -i '1s/^/\xef\xbb\xbf/' ./Include/StringToEnumInt.clean.mqh
mv ./Include/EnumStringToInt.clean.mqh ./Include/EnumStringToInt.mqh mv ./Include/StringToEnumInt.clean.mqh ./Include/StringToEnumInt.mqh