Intererferance with multiple instances of MT5 - different stoplosses on the same EA

 

I have an EA that enters a stoploss based on the low of the last '5' bars.  This is updated each bar to make a trailing stop. There is also a dot drawn once a bar to track the updated stoploss. 

This has been working fine and  I have backtested it multiple times. 

I have recently purchased a third party VPS (not metatraders) and installed 2 instances of MT5 on to the VPS. 

Using the EA on the VPS creates vastly different stoploss'.


Example:

Comparision of trade made at 2021.01.27 22:00:00      

  EA on my computer
EA on my VPS
  Ticker   DKNG.NAS    DKNG.NAS 
  Entry 54.122   54.122   
  Stoploss 52.38
  29.58


The ATR (for below) for both VPS and PC is 12.0.

The attached screenshot 727  show the entry on the PC (the desired affect).

This does not happen on the VPS. The stoploss is far too large.    



Here's the code below:

    double bid = normalizePrice(SymbolInfoDouble(_Symbol, SYMBOL_BID));     double ask = normalizePrice(SymbolInfoDouble(_Symbol, SYMBOL_ASK));     double ATR = normalizePrice(iATR(_Symbol,PERIOD_CURRENT,atrPeriod));     double stoploss_reg_div_buy  = normalizePrice(stopLossLowATR(stop_period,atrPeriod,atr_stoploss_factor));     double entry_reg_div_buy     = ask;          double stoploss_reg_div_sell = normalizePrice(stopLossHighATR(stop_period,atrPeriod,atr_stoploss_factor));     double entry_reg_div_sell    = bid;            double stoploss_hid_div_buy  = normalizePrice(stopLossLowATR(stop_period,atrPeriod,atr_stoploss_factor));     double entry_hid_div_buy     = ask;          double stoploss_hid_div_sell = normalizePrice(stopLossHighATR(stop_period,atrPeriod,atr_stoploss_factor));     double entry_hid_div_sell    = bid;

if(TradingAllowed()==true && find_open_position(magic_nb)==false && open_pending_orders(magic_nb)==false){ //---General entry conditions (for all entries) if(MACD_reg_div_buy[1]!=0 || AO_reg_div_buy[1]!=0 ){ //--- Entry Condition for reguakr divergence buy

Print("ATR:", ATR); double positionSize = optimal_lot_size(risk_per_trade_pct,entry_reg_div_buy,stoploss_reg_div_buy); ZeroMemory(mrequest); ZeroMemory(mresult); mrequest.action = TRADE_ACTION_DEAL; mrequest.symbol = _Symbol; mrequest.volume = positionSize; mrequest.type_filling = ORDER_FILLING_IOC; mrequest.magic = magic_nb; mrequest.deviation=5; mrequest.type = ORDER_TYPE_BUY; mrequest.price = entry_reg_div_buy; Print("Entry: ", entry_reg_div_buy ); mrequest.sl = stoploss_reg_div_buy; Print("Stoploss: ", stoploss_reg_div_buy ); if(!OrderSend(mrequest,mresult)) //--- send order PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error code //--- information about the operation PrintFormat("retcode=%u deal=%I64u order=%I64u",mresult.retcode,mresult.deal,mresult.order); //--- zeroing the request and result values ZeroMemory(mrequest); ZeroMemory(mresult); } // reg div buy

Custom functions:

//-----Find Lowest value in x bars
double findLowest(int lowestRange){
   int low_index=iLowest(Symbol(),PERIOD_CURRENT,MODE_LOW,lowestRange,0);
   if(low_index!=-1){
      double val=iLow(Symbol(),PERIOD_CURRENT,low_index);
      return val;
   }else{
      return low_index;
      Print("Error in iLowest. Error code:",GetLastError());
   }
}


//-----Find Highest value in x bars
double findHighest(int highestRange){
   int high_index=iHighest(Symbol(),PERIOD_CURRENT,MODE_HIGH,highestRange,0);
   if(high_index!=-1){
      double val=iHigh(Symbol(),PERIOD_CURRENT,high_index);
      return val;
   }else{
      return high_index;
      Print("Error in iLowest. Error code:",GetLastError());
   }
}


//--Stop Loss of Lowest value - Atr
double stopLossLowATR(int lowestPeriod,int atrPeriod, double ATRfactor){
      double Lowest = findLowest(lowestPeriod);
      double atr= iATR(Symbol(),0,atrPeriod);
      double stop = Lowest - (atr*ATRfactor);
      return stop;
}


//--Stop Loss of Highest value + Atr
double stopLossHighATR(int highestPeriod,int atrPeriod, double ATRfactor){
       double highest = findHighest(highestPeriod);
       double atr= iATR(Symbol(),0,atrPeriod);
       double stop = highest + (atr*ATRfactor);
       return stop;
}

double normalizePrice(double price){
   double tickSize=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   return(MathRound(price/tickSize)*tickSize);
}



I have copied the EAs and related indicators etc exaclty.

There are two instances of MT5 on the VPS with two of the same EAs. 

Each MT5 was set up in a different folder with different name. Each instance is run from a different broker account and they have different magic numbers. 

 

Can anyone help me with what the issue might be? 

Why does it give the desired results on my PC but on the VPS the stoploss changes? 

Is there something else I'm missing? 

Any advise would be grealty appreciated thank you. 

Virtual hosting for MetaTrader 5
Virtual hosting for MetaTrader 5
  • www.mql5.com
The fastest VPS server for forex trading from the MetaTrader 4/5 terminal developers
Files:
 

If anyone could point me in the right direction I would be greatly appreciative?

 
I managed to fix the issue by creating my own ATR function and using that instead of MQL5's but can anone tell me why the ATR reading was off? 
 
Bruno Harris :
I managed to fix the issue by creating my own ATR function and using that instead of MQL5's but can anone tell me why the ATR reading was off? 

In MQL5, the indicator handle must be created in OnInit, after which the values must be obtained using CopyBuffer.

Reason: