tradertom11:
Why is it that when back testing an EA when a sl or tp is hit, a trade in the opposite direction is automatically opened?
When I have nothing in my code which would open it.
I have tried trade.sell and ordersend, they both do the same thing.
- Nothing is automatic; your code is doing it.
- Yes, you do; add a Print statement before every open call in your code and find out why.
- Of course, they do. Your problem is with the calling code.
-
Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
How To Ask Questions The Smart Way. (2004)
Be precise and informative about your problemWe can't see your broken code.
-
Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
Messages Editor - tradertom11:
double sl = NormalizeDouble(bidPrice + stopLevels * Point(),_Digits); double tp = NormalizeDouble(bidPrice - stopLevels * Point(),_Digits);
-
You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by 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 shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).
-
-
You used NormalizeDouble, It's use is usually wrong, as it is in your case.
-
Floating point has a infinite number of decimals, it's your not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
Double-precision floating-point format - Wikipedia, the free encyclopediaSee also The == operand. - MQL4 programming forum (2013)
-
Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.
-
SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum (2011)And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)
-
Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
Trailing Bar Entry EA - MQL4 programming forum (2013)
Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012) -
Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
(MT4 2013)) (MT5 2022)) -
MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
MT4:NormalizeDouble - MQL5 programming forum (2017)
How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017) -
Prices you get from the terminal are already correct (normalized).
-
PIP, Point, or Tick are all different in general.
What is a TICK? - MQL4 programming forum (2014)
-
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Sorry newbie here.
Simple question hopefully someone can explain.
Why is it that when back testing an EA when a sl or tp is hit, a trade in the opposite direction is automatically opened?
When I have nothing in my code which would open it.
I have tried trade.sell and ordersend, they both do the same thing.
I am obviously missing some thing?
Thank you in advance.
double bidPrice = SymbolInfoDouble(Symbol(),SYMBOL_BID);
long stopLevels=SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL);
double sl = NormalizeDouble(bidPrice + stopLevels * Point(),_Digits);
double tp = NormalizeDouble(bidPrice - stopLevels * Point(),_Digits);
trade.Sell(vol1, Symbol(), bidPrice, sl, tp, NULL );