add support for indicator short name

apply styler 
shortened indicator refresh. indicators draw much faster now
This commit is contained in:
Gunther Schulz
2020-11-08 13:31:20 +01:00
parent b17e82d99d
commit 3b85a93e97
2 changed files with 2175 additions and 1675 deletions
+676 -270
View File
File diff suppressed because it is too large Load Diff
+149 -55
View File
@@ -21,27 +21,21 @@ Context context("MQL5 JSON API");
Socket chartSubscriptionSocket(context,ZMQ_SUB); Socket chartSubscriptionSocket(context,ZMQ_SUB);
//--- input parameters //--- input parameters
#property indicator_buffers 21 #property indicator_buffers 31
#property indicator_plots 20 #property indicator_plots 30
#property indicator_label1 "JsonAPI"
#property indicator_type1 DRAW_NONE
#property indicator_type2 DRAW_NONE
#property indicator_type3 DRAW_NONE
//#property indicator_color3 CLR_NONE
#property indicator_type4 DRAW_NONE
#property indicator_type5 DRAW_NONE
input string IndicatorId=""; input string IndicatorId="";
input string ShortName="JsonAPI"; input string ShortName="JsonAPIIndicator";
//--- indicator settings //--- indicator settings
double B0[], B1[], B2[], B3[], B4[], B5[], B6[], B7[], B8[], B9[], B10[], B11[], B12[], B13[], B14[], B15[], B16[], B17[], B18[], B19[], alive[]; double B0[], B1[], B2[], B3[], B4[], B5[], B6[], B7[], B8[], B9[], B10[];
bool debug = true; double B11[], B12[], B13[], B14[], B15[], B16[], B17[], B18[], B19[], B20[];
double B21[], B22[], B23[], B24[], B25[], B26[], B27[], B28[], B29[];
double alive[];
bool debug = false;
bool first = false; bool first = false;
int activeBufferCount = 0; int activeBufferCount = 0;
int activeBufferCount = 0;
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Custom indicator initialization function | //| Custom indicator initialization function |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
@@ -50,8 +44,13 @@ int OnInit()
{ {
bool result = chartSubscriptionSocket.connect(StringFormat("tcp://%s:%d", HOST, CHART_SUB_PORT)); bool result = chartSubscriptionSocket.connect(StringFormat("tcp://%s:%d", HOST, CHART_SUB_PORT));
if (result == false) {Print("Failed to subscrbe on port ", CHART_SUB_PORT);} if(result == false)
else { {
Print("Failed to subscrbe on port ", CHART_SUB_PORT);
}
else
{
if(debug)
Print("Accepting Chart Indicator data on port ", CHART_SUB_PORT); Print("Accepting Chart Indicator data on port ", CHART_SUB_PORT);
// TODO subscribe only to own IndicatorId topic // TODO subscribe only to own IndicatorId topic
// Subscribe to all topics // Subscribe to all topics
@@ -84,8 +83,19 @@ int OnInit()
ArraySetAsSeries(B17,true); ArraySetAsSeries(B17,true);
ArraySetAsSeries(B18,true); ArraySetAsSeries(B18,true);
ArraySetAsSeries(B19,true); ArraySetAsSeries(B19,true);
ArraySetAsSeries(B20,true);
ArraySetAsSeries(B21,true);
ArraySetAsSeries(B22,true);
ArraySetAsSeries(B23,true);
ArraySetAsSeries(B24,true);
ArraySetAsSeries(B25,true);
ArraySetAsSeries(B26,true);
ArraySetAsSeries(B27,true);
ArraySetAsSeries(B28,true);
ArraySetAsSeries(B29,true);
ArraySetAsSeries(alive,true); ArraySetAsSeries(alive,true);
SetIndexBuffer(0,B0,INDICATOR_DATA); SetIndexBuffer(0,B0,INDICATOR_DATA);
SetIndexBuffer(1,B1,INDICATOR_DATA); SetIndexBuffer(1,B1,INDICATOR_DATA);
SetIndexBuffer(2,B2,INDICATOR_DATA); SetIndexBuffer(2,B2,INDICATOR_DATA);
@@ -106,7 +116,17 @@ int OnInit()
SetIndexBuffer(17,B17,INDICATOR_DATA); SetIndexBuffer(17,B17,INDICATOR_DATA);
SetIndexBuffer(18,B18,INDICATOR_DATA); SetIndexBuffer(18,B18,INDICATOR_DATA);
SetIndexBuffer(19,B19,INDICATOR_DATA); SetIndexBuffer(19,B19,INDICATOR_DATA);
SetIndexBuffer(20,alive,INDICATOR_CALCULATIONS); // If the buffer index changes, the line starting with "CopyBuffer(chartWindowIndicators[i].indicatorHandle," in JsonAPI.mq5 has to be updated SetIndexBuffer(20,B20,INDICATOR_DATA);
SetIndexBuffer(21,B21,INDICATOR_DATA);
SetIndexBuffer(22,B22,INDICATOR_DATA);
SetIndexBuffer(23,B23,INDICATOR_DATA);
SetIndexBuffer(24,B24,INDICATOR_DATA);
SetIndexBuffer(25,B25,INDICATOR_DATA);
SetIndexBuffer(26,B26,INDICATOR_DATA);
SetIndexBuffer(27,B27,INDICATOR_DATA);
SetIndexBuffer(28,B28,INDICATOR_DATA);
SetIndexBuffer(29,B29,INDICATOR_DATA);
SetIndexBuffer(30,alive,INDICATOR_CALCULATIONS); // If the buffer index changes, the line starting with "CopyBuffer(chartWindowIndicators[i].indicatorHandle," in JsonAPI.mq5 has to be updated
//--- //---
@@ -116,7 +136,11 @@ int OnInit()
} }
void SetStyle(int bufferIdx, string linelabel, color colorstyle, int linetype, int linestyle, int linewidth) { //+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void SetStyle(int bufferIdx, string linelabel, color colorstyle, int linetype, int linestyle, int linewidth)
{
PlotIndexSetString(bufferIdx,PLOT_LABEL,linelabel); PlotIndexSetString(bufferIdx,PLOT_LABEL,linelabel);
PlotIndexSetInteger(bufferIdx,PLOT_LINE_COLOR,0,colorstyle); PlotIndexSetInteger(bufferIdx,PLOT_LINE_COLOR,0,colorstyle);
PlotIndexSetInteger(bufferIdx,PLOT_DRAW_TYPE,linetype); PlotIndexSetInteger(bufferIdx,PLOT_DRAW_TYPE,linetype);
@@ -138,9 +162,10 @@ int OnCalculate(const int rates_total,
const long &volume[], const long &volume[],
const int &spread[]) const int &spread[])
{ {
// While a new candle is forming, set the current value to be empty
if(rates_total>prev_calculated){ // While a new candle is forming, set the current value to be empty
if(rates_total>prev_calculated)
{
B0[0] = EMPTY_VALUE; B0[0] = EMPTY_VALUE;
B1[0] = EMPTY_VALUE; B1[0] = EMPTY_VALUE;
B2[0] = EMPTY_VALUE; B2[0] = EMPTY_VALUE;
@@ -161,50 +186,111 @@ int OnCalculate(const int rates_total,
B17[0] = EMPTY_VALUE; B17[0] = EMPTY_VALUE;
B18[0] = EMPTY_VALUE; B18[0] = EMPTY_VALUE;
B19[0] = EMPTY_VALUE; B19[0] = EMPTY_VALUE;
B20[0] = EMPTY_VALUE;
B21[0] = EMPTY_VALUE;
B22[0] = EMPTY_VALUE;
B23[0] = EMPTY_VALUE;
B24[0] = EMPTY_VALUE;
B25[0] = EMPTY_VALUE;
B26[0] = EMPTY_VALUE;
B27[0] = EMPTY_VALUE;
B28[0] = EMPTY_VALUE;
B29[0] = EMPTY_VALUE;
} }
if(first==false) alive[0] = 1; if(first==false)
alive[0] = 1;
// ChartRedraw(0); // ChartRedraw(0);
//--- return value of prev_calculated for next call //--- return value of prev_calculated for next call
return(rates_total); return(rates_total);
} }
void SubscriptionHandler(ZmqMsg &chartMsg){ //+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void SubscriptionHandler(ZmqMsg &chartMsg)
{
CJAVal message; CJAVal message;
// Get data from request // Get data from request
string msg=chartMsg.getData(); string msg=chartMsg.getData();
if(debug) Print("Processing:"+msg); if(debug)
Print("Processing:"+msg);
// Deserialize msg to CJAVal array // Deserialize msg to CJAVal array
if(!message.Deserialize(msg)){ if(!message.Deserialize(msg))
{
Alert("Deserialization Error"); Alert("Deserialization Error");
ExpertRemove(); ExpertRemove();
} }
if(message["indicatorChartId"]==IndicatorId) { if(message["indicatorChartId"]==IndicatorId)
{
if(message["action"]=="PLOT" && message["actionType"]=="DATA") { if(message["action"]=="PLOT" && message["actionType"]=="DATA")
{
int bufferIdx = message["indicatorBufferId"].ToInt(); int bufferIdx = message["indicatorBufferId"].ToInt();
if (bufferIdx == 0) WriteToBuffer(message, B0); if(bufferIdx == 0)
if (bufferIdx == 1) WriteToBuffer(message, B1); WriteToBuffer(message, B0);
if (bufferIdx == 2) WriteToBuffer(message, B2); if(bufferIdx == 1)
if (bufferIdx == 3) WriteToBuffer(message, B3); WriteToBuffer(message, B1);
if (bufferIdx == 4) WriteToBuffer(message, B4); if(bufferIdx == 2)
if (bufferIdx == 5) WriteToBuffer(message, B5); WriteToBuffer(message, B2);
if (bufferIdx == 6) WriteToBuffer(message, B6); if(bufferIdx == 3)
if (bufferIdx == 7) WriteToBuffer(message, B7); WriteToBuffer(message, B3);
if (bufferIdx == 8) WriteToBuffer(message, B8); if(bufferIdx == 4)
if (bufferIdx == 9) WriteToBuffer(message, B9); WriteToBuffer(message, B4);
if (bufferIdx == 10) WriteToBuffer(message, B10); if(bufferIdx == 5)
if (bufferIdx == 11) WriteToBuffer(message, B11); WriteToBuffer(message, B5);
if (bufferIdx == 12) WriteToBuffer(message, B12); if(bufferIdx == 6)
if (bufferIdx == 13) WriteToBuffer(message, B13); WriteToBuffer(message, B6);
if (bufferIdx == 14) WriteToBuffer(message, B14); if(bufferIdx == 7)
if (bufferIdx == 15) WriteToBuffer(message, B15); WriteToBuffer(message, B7);
if (bufferIdx == 16) WriteToBuffer(message, B16); if(bufferIdx == 8)
if (bufferIdx == 17) WriteToBuffer(message, B17); WriteToBuffer(message, B8);
if (bufferIdx == 18) WriteToBuffer(message, B18); if(bufferIdx == 9)
if (bufferIdx == 19) WriteToBuffer(message, B19); WriteToBuffer(message, B9);
if(bufferIdx == 10)
WriteToBuffer(message, B10);
if(bufferIdx == 11)
WriteToBuffer(message, B11);
if(bufferIdx == 12)
WriteToBuffer(message, B12);
if(bufferIdx == 13)
WriteToBuffer(message, B13);
if(bufferIdx == 14)
WriteToBuffer(message, B14);
if(bufferIdx == 15)
WriteToBuffer(message, B15);
if(bufferIdx == 16)
WriteToBuffer(message, B16);
if(bufferIdx == 17)
WriteToBuffer(message, B17);
if(bufferIdx == 18)
WriteToBuffer(message, B18);
if(bufferIdx == 19)
WriteToBuffer(message, B19);
if(bufferIdx == 20)
WriteToBuffer(message, B20);
if(bufferIdx == 21)
WriteToBuffer(message, B21);
if(bufferIdx == 22)
WriteToBuffer(message, B22);
if(bufferIdx == 23)
WriteToBuffer(message, B23);
if(bufferIdx == 24)
WriteToBuffer(message, B24);
if(bufferIdx == 25)
WriteToBuffer(message, B25);
if(bufferIdx == 26)
WriteToBuffer(message, B26);
if(bufferIdx == 27)
WriteToBuffer(message, B27);
if(bufferIdx == 28)
WriteToBuffer(message, B28);
if(bufferIdx == 29)
WriteToBuffer(message, B29);
} }
else if(message["action"]=="PLOT" && message["actionType"]=="ADDBUFFER") { else
if(message["action"]=="PLOT" && message["actionType"]=="ADDBUFFER")
{
string linelabel = message["style"]["linelabel"].ToStr(); string linelabel = message["style"]["linelabel"].ToStr();
string colorstyleStr = message["style"]["color"].ToStr(); string colorstyleStr = message["style"]["color"].ToStr();
string linetypeStr = message["style"]["linetype"].ToStr(); string linetypeStr = message["style"]["linetype"].ToStr();
@@ -237,23 +323,28 @@ void SubscriptionHandler(ZmqMsg &chartMsg){
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Update indicator buffer function | //| Update indicator buffer function |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void WriteToBuffer(CJAVal &message, double &buffer[]) { void WriteToBuffer(CJAVal &message, double &buffer[])
{
int bufferSize = ArraySize(buffer); int bufferSize = ArraySize(buffer);
int messageDataSize = message["data"].Size(); int messageDataSize = message["data"].Size();
// TODO check if this is working as expected. Seems to // TODO check if this is working as expected. Seems to
if(first==false) { if(first==false)
for(int i=0;i<activeBufferCount;i++) { {
for(int i=0; i<activeBufferCount; i++)
{
//Print("BUFF ",bufferSize-messageDataSize, " ",ArraySize(B2)," ", ArraySize(B3), " ",messageDataSize); //Print("BUFF ",bufferSize-messageDataSize, " ",ArraySize(B2)," ", ArraySize(B3), " ",messageDataSize);
PlotIndexSetInteger(i,PLOT_DRAW_BEGIN,bufferSize-messageDataSize); PlotIndexSetInteger(i,PLOT_DRAW_BEGIN,bufferSize-messageDataSize);
} }
first = true; first = true;
} }
for(int i=0;i<messageDataSize;i++){ for(int i=0; i<messageDataSize; i++)
{
// don't add more elements than the automatically sized buffer array can hold // don't add more elements than the automatically sized buffer array can hold
if(i+1<bufferSize){ if(i+1<bufferSize)
{
// the first element is the current unformed candle, so we start at index 1 // the first element is the current unformed candle, so we start at index 1
// we reverse the order of the incoming values, which are expected to be ascending // we reverse the order of the incoming values, which are expected to be ascending
//buffer[i+1] = message["data"][messageDataSize-1-i].ToDbl(); //buffer[i+1] = message["data"][messageDataSize-1-i].ToDbl();
@@ -268,7 +359,8 @@ void WriteToBuffer(CJAVal &message, double &buffer[]) {
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Check for new indicator data function | //| Check for new indicator data function |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void CheckMessages(){ void CheckMessages()
{
// This is a workaround for Timer(). It is needed, because OnTimer() works if the indicator is manually added to a chart, but not with ChartIndicatorAdd() // This is a workaround for Timer(). It is needed, because OnTimer() works if the indicator is manually added to a chart, but not with ChartIndicatorAdd()
ZmqMsg chartMsg; ZmqMsg chartMsg;
@@ -277,7 +369,8 @@ void CheckMessages(){
chartSubscriptionSocket.recv(chartMsg,true); chartSubscriptionSocket.recv(chartMsg,true);
// Request recieved // Request recieved
if(chartMsg.size()>0){ if(chartMsg.size()>0)
{
// Handle subscription SubscriptionHandler() // Handle subscription SubscriptionHandler()
SubscriptionHandler(chartMsg); SubscriptionHandler(chartMsg);
ChartRedraw(ChartID()); ChartRedraw(ChartID());
@@ -293,6 +386,7 @@ void OnChartEvent(const int id,
const double &dparam, const double &dparam,
const string &sparam) const string &sparam)
{ {
if(id==CHARTEVENT_CUSTOM+222) CheckMessages(); if(id==CHARTEVENT_CUSTOM+222)
CheckMessages();
} }
//+---------------------------------------------------- //+----------------------------------------------------