-
if(OrderSymbol() == Symbol() && OrderType()==OP_BUY) { if(isNewCandle()) { if(PSARValue(1) < Close[1] && PSARValue(0) > Close[0])
There are no mind readers here and our crystal balls are cracked. Your post is without context. You can not use any Trade Functions until you first select an order. And it matters what direction you loop if you potentially open multiple orders. - Move all non-changing items outside your loop.
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
New candle - MQL4 programming forum #3 2014.04.04I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
-
Why are you testing whether it was on the correct side? If it is current on the wrong side, you want to close the order. Simplify your code.
-
bool res = OrderClose(orderId,OrderLots(),Ask - spread,0);
You buy at the Ask and sell at the Bid.
-
Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the 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 at 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 spread widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134.
-
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I've created an EA that will close the open order if there's a changing in parabolic SAR indicator. For this EA to work I want to check the Indicator for every new candle since the change of the PSAR is in every new candle.
Here's the code for every new candle function:
And for the order close code: The issue is that sometimes for the short position the EA does not close when the PSAR indicator changing direction. I've add the screen capture for the issue. In the backtest this issue is happened twice and I don't know why this is happen.