2 EA not synchronized.. help

 

Hi, i'm testing my personal EAs that work together (not every time) .

EA 1 must BUY gold when variable is < 0.3 

EA 2 must SELL silver when the same variable is < 0.3

So that, in the same instant, they should make 1 trade, and my MT4 should open a buy and a sell .

But, unfortunely, if that variable slightly moves above 0.3, only an EA places an order instead of both.

EA 1 has magicnumber "100", EA 2 has magicnumber "101". Which code editing should solve this problem of synchronization and even accept a range of values towards 0.3 to ensure the opening?

Alessandro

 
Alessandro Boraso: But, unfortunely, if that variable slightly moves above 0.3, only an EA places an order instead of both.
  1. Per your post, each EA does something when the variable goes below 0.3. You've said nothing about when it goes above. Until you can concretely state all your conditions, no one can help you.
  2. Do you really expect an answer? We can't see your broken code. There are no mind readers here and our crystal balls are cracked.
 
William Roeder:
  1. Per your post, each EA does something when the variable goes below 0.3. You've said nothing about when it goes above. Until you can concretely state all your conditions, no one can help you.
  2. Do you really expect an answer? We can't see your broken code. There are no mind readers here and our crystal balls are cracked.

Ok William, no problem. 

Ratio is a EA calculated variable comparing Gold's Momentum indicator value and Silver's Momentum indicator value. 

SpreadGoldLow is the user set value that EA must control to open the order. Is set to -0.35 .  If Ratio is less than -0.35 , open buy gold. 

SpreadGoldHigh is the second user set value that EA must control . Is set to +0.35 . If Ratio is more than 0.35, open sell gold.

EA2 for Silver uses the same variables, identically set, but has an opposite condition : If Ratio is less than -0.35, open SELL silver.

And if Ratio is more than +0.35, open BUY silver.

What happens ? Maybe only EA1 open the order for gold, but EA2 doesn't do anything. Instead, in the instant the EAs make the check of Ratio and comparison, they must open together the trade (buy gold+sell silver or sell gold+buy silver). Here is the code, and thanks in advance.

EA1 for gold :

RefreshRates();

//---- CONDIZIONE DI INGRESSO A

  if(Ratio<SpreadGoldLow && Ratio<SpreadGoldLow+0.3)  

     {

      res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"SPREADTRADER_GOLD",100,0,clrNONE);

   

       

      return;

     }

//---- CONDIZIONE DI INGRESSO B

   if(Ratio>SpreadGoldHigh && Ratio>SpreadGoldHigh-0.3)  

     {

   

      res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"SPREADTRADER_GOLD",100,0,clrNONE);

         return;

      

     }

EA2 for silver :

RefreshRates();

//---- CONDIZIONE DI INGRESSO A

  if(Ratio<SpreadGoldLow && Ratio<SpreadGoldLow+0.3)  

     {

      res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"SPREADTRADER_SILVER",101,0,clrNONE);

   

       

      return;

     }

//---- CONDIZIONE DI INGRESSO B

   if(Ratio>SpreadGoldHigh && Ratio>SpreadGoldHigh-0.3)  

     {

   

      res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"SPREADTRADER_SILVER",101,0,clrNONE);

         return;

      

     }

--------------

SpreadGoldLow + 0.3 or -0.3 is the tolerance i tried to check , so that if Ratio changes instanly, the condition continues to be true.

 
Alessandro Boraso:

Ok William, no problem. 

Ratio is a EA calculated variable comparing Gold's Momentum indicator value and Silver's Momentum indicator value. 

SpreadGoldLow is the user set value that EA must control to open the order. Is set to -0.35 .  If Ratio is less than -0.35 , open buy gold. 

SpreadGoldHigh is the second user set value that EA must control . Is set to +0.35 . If Ratio is more than 0.35, open sell gold.

EA2 for Silver uses the same variables, identically set, but has an opposite condition : If Ratio is less than -0.35, open SELL silver.

And if Ratio is more than +0.35, open BUY silver.

What happens ? Maybe only EA1 open the order for gold, but EA2 doesn't do anything. Instead, in the instant the EAs make the check of Ratio and comparison, they must open together the trade (buy gold+sell silver or sell gold+buy silver). Here is the code, and thanks in advance.

Your conditions: (x<y and x<y+z) will only be true when x<y, so why include two parts? Similar for >.

That aside, your coded conditions for buying/selling gold and silver is not according to your description above - you coded that if ratio<-0.35, buy gold and buy silver. and when ratio>0.35, sell gold and sell silver.

Anyway, since you're running the EAs on different symbols (correct?), they may not receive a tick at the same time at all... so you can add a few Print() statements to verify that when they are triggered, do they see the same value of ratio?

 
Seng Joo Thio:

Your conditions: (x<y and x<y+z) will only be true when x<y, so why include two parts? Similar for >.

That aside, your coded conditions for buying/selling gold and silver is not according to your description above - you coded that if ratio<-0.35, buy gold and buy silver. and when ratio>0.35, sell gold and sell silver.

Anyway, since you're running the EAs on different symbols (correct?), they may not receive a tick at the same time at all... so you can add a few Print() statements to verify that when they are triggered, do they see the same value of ratio?

Hi Seng,

i have merged all the code into a single EA because i guessed it was a problem of using 2 EAs instead of one. Now , a single EA sends both orders using "xauusd" and "xagusd" instead of Symbol() at seems working properly.

For now, i m quite satisfied. The only problem is that , i don't know the reason and i can t find a reference here : Print() commands do work only on the backtester and not on the vps installed mt4.

I have tried to insert the string Print("RATIO LEVEL: ",Ratio); in different zones of the code but no messages appear in the Journal tab. If Print works, i can check the EA's precision in opening the orders at a correct Ratio level without mistakes. 

Did you ever had a similar problem (Print not working) ? 


I also wrote this EA but does not print messages in the Journal Tab :

//+------------------------------------------------------------------+

//|                                                  Print EA.mq4 |

//|                  |

//+------------------------------------------------------------------+

#property copyright   "A.Boraso"

#property link        "http://www.mql5.com"



//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

void OnTick(void)

  {

  double MacdCurrent;


   if(Bars<100)

     {

      Print("bars less than 100");

      return;

     }

   MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0); 

  

            Print("MACD Level : ",MacdCurrent);

         return;

        }

    

//---


//+------------------------------------------------------------------+

Virtual hosting for MetaTrader 5
Virtual hosting for MetaTrader 5
  • www.mql5.com
Virtual hosting for MetaTrader 4/5 is the best VPS solution for Forex. It is cheap, it requires no configuration and it features minimum delays to the server. For only $10 per month, you obtain a virtual platform that works around the clock, saves your profit and pays off. Create a remote copy of the application directly from the platform...
 
Seng Joo Thio:

Print() works, it just prints to Experts tab on my terminal, not Journals...

Thanks Seng! I found it !

Now, the last question is : how to print only every 1 M5 bar (not every few millisecond like now)? In this moment, every Log file is huge data and i only need 5 - minutes printing.

 
Alessandro Boraso:

Hi Seng,

i have merged all the code into a single EA because i guessed it was a problem of using 2 EAs instead of one. Now , a single EA sends both orders using "xauusd" and "xagusd" instead of Symbol() at seems working properly.

For now, i m quite satisfied. The only problem is that , i don't know the reason and i can t find a reference here : Print() commands do work only on the backtester and not on the vps installed mt4.

I have tried to insert the string Print("RATIO LEVEL: ",Ratio); in different zones of the code but no messages appear in the Journal tab. If Print works, i can check the EA's precision in opening the orders at a correct Ratio level without mistakes. 

Did you ever had a similar problem (Print not working) ? 

Print() works.


 
Alessandro Boraso:

Thanks Seng! I found it !

Now, the last question is : how to print only every 1 M5 bar (not every few millisecond like now)? In this moment, every Log file is huge data and i only need 5 - minutes printing.

How to print value every 5 minutes and not continuously ?

 
Alessandro Boraso:

How to print value every 5 minutes and not continuously ?

You can refer to : https://mql4tradingautomation.com/execute-action-once-per-bar-mql4/

How to Execute an Action only Once per Bar with MQL4 - MQL4 Trading Automation
How to Execute an Action only Once per Bar with MQL4 - MQL4 Trading Automation
  • 2019.01.04
  • Luca Spinello
  • mql4tradingautomation.com
When you read about the tick and MQL4 you learned that the tick is a trigger for a portion of code. You also learned that generally there are more ticks happening in a candlestick, whether this is a short or long timeframe. In many cases you may want to execute some code only once in a bar or candlestick. In this article you will see how you...
 

;D very kind

 

Thanks to Seng Joo !

If somebody needs, here is the code on M5 timeframe (with invented value 1 for Ratio variable) :


---------------------------------------------------------

//-global declaration--

datetime LastActionTime;

double Ratio = 1;

-------------------------------------------------------------

  //--------into OnTick() or Start() function:

    if(LastActiontime!=Time[0]){

      //Code to execute once in the bar

      Print("Ratio Rilevato: ",Ratio,"   ",Time[0]);

      

      LastActiontime=Time[0];

   }

   //--- end


//