darwinex / dwx-zeromq-connector

Wrapper library for algorithmic trading in Python 3, providing DMA/STP access to Darwinex liquidity via a ZeroMQ-enabled MetaTrader Bridge EA.
https://blog.darwinex.com/zeromq-interface-python-r-metatrader4/
BSD 3-Clause "New" or "Revised" License
344 stars 228 forks source link

DWX_SetInstrumentList overwrite old calls #20

Closed edge7 closed 3 years ago

edge7 commented 5 years ago

Hello, I noticed that DWX_SetInstrumentList(string& compArray[], string& zmq_ret) { } overwrites the content of PublishInstruments. This is my solution: `bool check_if_in_instrument_list(ENUM_TIMEFRAMES timeframe, string symbol){

  for( int i = 0; i < ArraySize(Publish_Instruments); i ++){
        if ( StringCompare(Publish_Instruments[i].symbol(), symbol) == 0 && Publish_Instruments[i].timeframe() == timeframe)
           return True;
  }
  return False;

}

//+------------------------------------------------------------------+ // Set list of instruments to get OHLC rates void DWX_SetInstrumentList(string& compArray[], string& zmq_ret) {

zmq_ret = zmq_ret + "'_action': 'TRACK_RATES'";

// Format: TRACK_RATES|SYMBOL_1|TIMEFRAME_1|SYMBOL_2|TIMEFRAME_2|...|SYMBOL_N|TIMEFRAME_N
string result = "Tracking RATES from";      
int _num_instruments = (ArraySize(compArray) - 1)/2;
if(_num_instruments > 0){

    for(int s=0; s<_num_instruments; s++){ 
        ENUM_TIMEFRAMES timeframe = (ENUM_TIMEFRAMES)StrToInteger(compArray[(2*s)+2]);
        string symbol = compArray[(2*s)+1]; 
        bool res = check_if_in_instrument_list(timeframe, symbol);
        if( res )
        {
           Print("Timeframe and Symbol are already there");
           continue;
        }
        int oldSize =  ArraySize(Publish_Instruments);
        ArrayResize(Publish_Instruments, ArraySize(Publish_Instruments) + 1);
        Print("Added timeFrame and Symbol");            
        Publish_Instruments[s + oldSize].setup(compArray[(2*s)+1], (ENUM_TIMEFRAMES)StrToInteger(compArray[(2*s)+2]));
        result += " " + Publish_Instruments[s + oldSize].name();
     }
    zmq_ret = zmq_ret + ", '_data': {'instrument_count':" + IntegerToString(ArraySize(Publish_Instruments)) + "}";
    Publish_MarketRates = true;
}
else {
    Publish_MarketRates = false;    
    ArrayResize(Publish_Instruments, 1);           
    zmq_ret = zmq_ret + ", '_data': {'instrument_count': 0}";
    result += " NONE";

}
Print(result); }`

elvinex commented 3 years ago

Hi, Thank you for your suggestion. The reason why we implemented it this way is that it allows for both adding and removing symbols from the list. With your addition we would have to add another function to remove a symbol again. Therefore, we decided to keep the current implementation.