Why in the world does this
always fail with error 4107 in my demo account?
First question: why are you normalizing the price (at all), and particularly to 4 digits? JPY crosses are 2 or 3 digits.
Second question: are you using this code on a different chart to USDJPY?
I get the same result with plain old Ask,-slippage,.....
Yes, the code is being used on a AUDUSD chart, along with a couple of other pairs that don't cause any problems.
You can't use Ask to place an order on USDJPY if the EA is running on AUDUSD. Please read the documentation.
You have to use MarketInfo() to get USDJPY price.
Ok ...I'll do that now.
I'm curious though, why would the other pairs work properly?
When you use the predefined variables of Bid and Ask, they relate to the chart the code is running on.
So, if you are running this code on the AUDUSD chart, then you are trying to open a Buy order on USDJPY at the Ask price of AUDUSD.
You need to use the correct price. You can either get all the current info on the latest USDJPY tick using:
and then access the appropriate value e.g.
Or you can retrieve just the value you need:
or the older way:
The documentation on SymbolInfoDouble() suggests that SymbolInfoTick() is preferable in this scenario but I haven't encountered the issues using SymbolInfoDouble():
It is recommended to use SymbolInfoTick() if the function is used for getting information about the last tick. It may well be that not a single quote has appeared yet since the terminal is connected to a trading account. In such a case, the requested value will be indefinite.
In most cases, it is enough to use SymbolInfoTick() function allowing a user to receive the values of Ask, Bid, Last, Volume and the time of the last tick's arrival during a single call.
MrMonty: he code is being used on a AUDUSD chart, along with a couple of other pairs that don't cause any problems.