-
You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.
-
Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
-
Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 -
The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).
-
-
You are setting the stops five (5) pips minus the spread. You can't move stops (or pending prices) closer to the market than the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).
Requirements and Limitations in Making Trades - Appendixes - MQL4 TutorialOn some ECN type brokers the value might be zero (the broker doesn't know). Use a minimum of two (2) PIPs.
Fix № 1 and you might fix № 2 on all symbols with spread less than fifty points.
Okay i have tried your advice and came up with the following
#property version "1.1" #property strict input double LotSize = 0.1; // Global variables int gBuyTicket; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { double priceAsk=Ask+(1000*_Point); double priceBid=Bid+(1000*_Point); double SL=NormalizeDouble(priceBid-(50*_Point),_Digits); double TP=NormalizeDouble(priceBid+(50*_Point),_Digits); Buy(priceAsk,SL,TP); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { } //+------------------------------------------------------------------+ //| Perform a Buy //+------------------------------------------------------------------+ bool Buy(double price,double sl,double tp) { // Verify stop loss & take profit double stopLevel = MarketInfo(_Symbol,MODE_STOPLEVEL) * _Point; RefreshRates(); double upperStopLevel = Ask + stopLevel; double lowerStopLevel = Bid - stopLevel; if(tp <= upperStopLevel && tp != 0.0) tp = upperStopLevel + _Point; if(sl >= lowerStopLevel && sl != 0.0) sl = lowerStopLevel - _Point; //--- buy gBuyTicket=OrderSend(Symbol(),OP_BUYSTOP,LotSize,price,10,sl,tp); if(gBuyTicket<0) PrintFormat("OrderSend error %d",GetLastError()); //--- return(false); }
I have, as stated
1) Based SL/TP on the Bid price
2) Made sure the SL/TP as above the stop level minimum
But im still getting the error OrderSend error 131 when i run it through the mql5 market test.
Its a volume problem im stuck on?
Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
As mentioned, and as you can check into errors enumerations 131 is related to volumes... invalid volumes.... this can be generated or from an invalid calculation, or because its exceed min/max volumes allowed by the broker.
But anyway, for the market pass, it must have the min/max volume checking... it's one of the checking which 'market auto-test' does... (in fact, as you described by tryed volumes 1, and 0.1... i.e. could exists some broker which doesn't offers under 1, or most likely this has been generated on some specific asset which doesn't has volumes under 1... there are prelimina/basic checking that 'market auto-test' dose in order to ensure that program can work for everyone whos will download it, especially if paid).
So, basically, it's one of the checking that auto-test does... so, your volume to open a trade, need to have a further checking for min/max allowed volume.
I also have to write you how to code this simple checking?!?
Hope this help
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
In the following code why does lotsize only pass market test when 1.0
I get Invalid trade volume error 131 when i input 0.1
Sorry to ask a straight question but i am stumped it tests okay on my broker but not passing mql5 market?