ea work fine for all pairs exept on YEN pairs

 

happy day for all
 my expert advisor   idea : putting pending orders in desired place with mouse and keyboard
buylimit = CNTRL + left mouse clic in desired place 
selllimit = ALT + left mouse clic 
it works great smooth and all ...
but
i discovered  that it wont work on YEN pairs like gbpjpy usdjpy...on the other it works great 

the code 

//+------------------------------------------------------------------+
//|                                                  BetaTrader1.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#import "user32.dll"
    bool      GetAsyncKeyState(int nVirtKey);
#import
#define KEYEVENTF_EXTENDEDKEY          0x0001
#define KEYEVENTF_KEYUP                0x0002

extern double Risk = 5 ;
extern int TakeProfit = 50;
extern int StopLoss = 20 ;
extern int MagicNumber = 1234;
int ticket ;
extern double LotSize = 0.01 ;

double pips;
#define VK_LBUTTON         1     //Left mouse button
#define VK_CONTROL         17   //CONTROL key
#define VK_MENU            18   //ALT key
#define VK_B   66                   // B
#define VK_S   83                    // S

double prix ;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
 
    double ticksize=MarketInfo(Symbol(),MODE_TICKSIZE);
  
  if (ticksize==0.00001 || ticksize==0.001)
 
  pips = ticksize*10;
 
  
  else
 
   pips = ticksize;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---***************************************************************getting mouse data********************************//
   if( id==CHARTEVENT_CLICK && GetAsyncKeyState(VK_B ) )
  {
      int      x     =(int)lparam;
      int      y     =(int)dparam;
      datetime dt    =0;
      double   price =0;
      int      window=0;
      ChartXYToTimePrice(0,x,y,window,dt,price); 
      prix =  NormalizeDouble(price,5);
    
 //**********************************************************************Trading****************************************//
 
 
    ticket = OrderSend(Symbol(),2,LotSize,prix,2,prix-(StopLoss*pips),prix +(TakeProfit*pips),NULL,MagicNumber,0,clrNONE);
    
     if (ticket < 0) 
  {
  Print("open buyLimit error ==",GetLastError());
    
   } 
   }
  // ---***************************************************************getting mouse data********************************//
   if( id==CHARTEVENT_CLICK &&GetAsyncKeyState(VK_S ) )
     {
      int      x     =(int)lparam;
      int      y     =(int)dparam;
      datetime dt    =0;
      double   price =0;
      int      window=0;
      ChartXYToTimePrice(0,x,y,window,dt,price); 
      prix =  NormalizeDouble(price,5);
    
   
   //**********************************************************************Trading****************************************//
 
   
    ticket = OrderSend(Symbol(),3,LotSize,prix,2,prix+(StopLoss*pips),prix-(TakeProfit*pips),NULL,MagicNumber,0,clrNONE);
    
    if (ticket < 0) 
  {
  Print("open SellLimit error ==",GetLastError());
    
   
   }
   
   }
 
 
 
 
    
  }
//+------------------------------------------------------------------+

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
nix386:

happy day for all
 my expert advisor   idea : putting pending orders in desired place with mouse and keyboard
buylimit = CNTRL + left mouse clic in desired place 
selllimit = ALT + left mouse clic 
it works great smooth and all ...
but
i discovered  that it wont work on YEN pairs like gbpjpy usdjpy...on the other it works great 

the code 



any idea ??! please 

 
nix386:

any idea ??! please 

Really "your" expert advisor? The error message which it prints to the Experts log is telling you exactly what the problem is...
 
JC:
Really "your" expert advisor? The error message which it prints to the Experts log is telling you exactly what the problem is...

yes mine
 whene it does not work on gbpjpy for exemple it dont telle any message any thing  any error ... like it  is not putted on the chart or like  the mouse and the key are not pressed  and thats only on YEN pairs i will become crazy ....i cant sleep thinking of this only on YEN pairs  it works 1 time / 10

 
nix386:

it dont telle any message any thing  any error

It is Print()ing the exact nature of the problem to the Experts log.

ChartXYToTimePrice() can give you fractional prices, at greater granularity than what can be traded. For example, on USDJPY, it can return a price value such as 123.456789. MT4 will reject this. You need to round the price to 123.457 before passing it to OrderSend().

You are doing the following:

prix =  NormalizeDouble(price,5);

You need to change the 5 to the number of digits for the instrument. For example:

prix =  NormalizeDouble(price, (int)SymbolInfoInteger(Symbol(), SYMBOL_DIGITS));
 
nix386:

happy day for all
 my expert advisor   idea : putting pending orders in desired place with mouse and keyboard
buylimit = CNTRL + left mouse clic in desired place 
selllimit = ALT + left mouse clic 
it works great smooth and all ...
but
i discovered  that it wont work on YEN pairs like gbpjpy usdjpy...on the other it works great 

the code 


Don't use NormalizeDouble(). It is a kludge! It is useless! If you do things correctly, you don't need it!

In short, NormalizeDouble() does nothing useful at all, and what does solve the problem is making sure the price is a aligned to the symbol's tick size.

...

prix = NormalizePrice( price );

...

double NormalizePrice( double price )
{
   double tick_size = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE ); // Valid for both MQL4 and MQL5
   return( round( price / tick_size ) * tick_size );
}

EDIT: Also make sure you also align the prices for your stops (Stop-Loss and Take-Profit), not just the opening price.

 
Fernando Carreiro:

Don't use NormalizeDouble(). It is a kludge! It is useless! If you do things correctly, you don't need it!

In short, NormalizeDouble() does nothing useful at all, and what does solve the problem is making sure the price is a aligned to the symbol's tick size.


Honestly I am tired to read this on the forum, not only WHRoeder post it almost every day, but now you start with the same too. The OP will not even understand why you write it.

There is no problem with NormalizeDouble(). It can be used, if done correctly. If you trade non-forex symbol it will not be sufficient to get a correct price.

 
Alain Verleyen: Honestly I am tired to read this on the forum, not only WHRoeder post it almost every day, but now you start with the same too. The OP will not even understand why you write it.

There is no problem with NormalizeDouble(). It can be used, if done correctly. If you trade non-forex symbol it will not be sufficient to get a correct price.

For the simple fact, that no other language has a NormaliseDouble function. It was only created due to "bad" design by the initial versions of MetaTrader which was initially designed only for Forex market.

That "bad" design has since been corrected, but the bad habit of its continued use is due to the persistent usage by fellow coders and due to MetaQuotes not removing it for good from the system.

MT5 has now extended its use to non-forex markets where the correct tick size alignment is much more important than ever before.

In all my code I don't use it at all and all prices are aligned by tick size and all volumes are aligned by lot step, and not once do I get any errors due to not using NormalizeDouble.

Defending its use is only making it more difficult for newbie coders to understand the correct way to set the price and adding to the confusion. Please don't defend its use.

Just because it exists, does not mean its usage is valid or correct. MetaTrader and MQL have many failings and this is one of them. So, defending these faults, does not help anyone.

 
JC:

It is Print()ing the exact nature of the problem to the Experts log.

ChartXYToTimePrice() can give you fractional prices, at greater granularity than what can be traded. For example, on USDJPY, it can return a price value such as 123.456789. MT4 will reject this. You need to round the price to 123.457 before passing it to OrderSend().

You are doing the following:

You need to change the 5 to the number of digits for the instrument. For example:


i had  a dout about thik you very match 

 
Fernando Carreiro:

For the simple fact, that no other language has a NormaliseDouble function. It was only created due to "bad" design by the initial versions of MetaTrader which was initially designed only for Forex market.

That "bad" design has since been corrected, but the bad habit of its continued use is due to the persistent usage by fellow coders and due to MetaQuotes not removing it for good from the system.

MT5 has now extended its use to non-forex markets where the correct tick size alignment is much important than ever before.

In all my code I don't use it at all and all prices are aligned by tick size and all volumes are aligned by lot step, and not once do I get any errors due to not using NormalizeDouble.

Defending its use is only making it more difficult for newbie coders to understand the correct way to set the price and adding to the confusion. Please don't defend its use.

Just because it exists, does not mean its usage is valid or correct. MetaTrader and MQL have many failings and this is one of them. So, defending these faults, does not help anyone.


thinks a lot 

 
Fernando Carreiro:

For the simple fact, that no other language has a NormaliseDouble function. It was only created due to "bad" design by the initial versions of MetaTrader which was initially designed only for Forex market.

My view likes somewhere between yours and Alain's.

NormalizeDouble() can be viewed as equivalent to the Math.Round(number, digits) found in many languages, and thus has legitimate uses.

I'd say that the number of instruments whose tick-size is not a multiple of 10 is now decreasing, not increasing. For example, lots of brokers used to have S&P contracts which moved in increments of 0.25, like the CME e-mini contract. But I now can't immediately think of a broker whose S&P contract isn't priced in increments of 0.01 like the underlying index.

Reason: