Here are MT4 reccommandations for a SELL order, buy on Bid, SL and TP on Ask :
https://book.mql4.com/trading/orders
StopLoss = Ask + minimum distance = 1.2989 + 0.0005 = 1.2994, and TakeProfit = Ask - minimum distance = 1.2989 - 0.0005 = 1.2984.
Now i will do the reverse : fix my sl and tp of a Buy Order on the Ask,
this EA will have the proosibility to choose if he SL and TP are on the Bid or on the Ask
extern double Lots = 0.1; extern double TakeProfit = 100; extern double StopLoss = 100; extern int MagicNumber = 15986; extern int MA_Per = 30; extern bool buy_on_bid = true; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- static datetime time_0; if( time_0 == Time[0] ) return; time_0 = Time[0]; //----------------------------------------------------------------------- int buyticket = -1; int sellticket = -1; double price = 0; double spread = MarketInfo(Symbol(),MODE_SPREAD); double stop_mini = MarketInfo(Symbol(),MODE_STOPLEVEL); double atr = iATR(NULL,0,20,1); double ma_1 = iMA(NULL,0,MA_Per,0,3,0,1); double ma_2 = iMA(NULL,0,MA_Per,0,3,0,2); double ma_3 = iMA(NULL,0,MA_Per,0,3,0,3); double buy_sl = 0, buy_tp=0; if( ma_3 > ma_2 && ma_2< ma_1 ) { price = 0.0; if( buy_on_bid ) price = Bid; else price = Ask; if( StopLoss > stop_mini ) buy_sl = price - StopLoss*Point; else buy_sl = price - stop_mini*Point; if( TakeProfit > stop_mini ) buy_tp = price + TakeProfit*Point;// + spread*Point; else buy_tp = price + stop_mini*Point; buyticket = -1; buyticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,buy_sl,buy_tp,"",MagicNumber,0,Green); } } //+------------------------------------------------------------------+
And if I put SL and TP on a buy order on the ASK, here are the result :
now here is the result, 12 dollar win if TP is hit, and 8 dollar loss if SL is hit
You have that back to front.
If you want profit and/or loss to be equal, for a buy you add the TP to the Entry price and deduct the SL from the Entry price
You have that back to front.
If you want profit and/or loss to be equal, for a buy you add the TP to the Entry price and deduct the SL from the Entry price
This is what I want, and I also want my TP or SL if they are 100 point give 10 dollar;
This is what the code do if SL and TP are added to Entry price, but it is illogic, what about the spread ?
This is want I want, and I also want my TP or SL if they are 100 point give 10 dollar;
This is what the code do if SL and TP are added to Entry price, but it is illogic, what about the spread ?
This is what I want, and I also want my TP or SL if they are 100 point give 10 dollar;
This is what the code do if SL and TP are added to Entry price, but it is illogic, what about the spread ?
- You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
- Account Balance * percent = RISK = |OrderOpenPrice - OrderStopLoss| * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
- Do NOT use TickValue by itself - DeltaPerlot
- You must normalize lots properly and check against min and max.
- You must also check FreeMargin to avoid stop out
- 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 made a simple EA following MT4 recommandations.
I buy on the ask, my stoploss (SL = Bid -stoploss;) and takeprofit (TP = Bid + takeprofit;) are on the Bid, just like MT4 say it has to be, this way we take account of the spread.
if my SL and TP are 100 point, it should male 10 dollar profit if the takeprofit is hit, and 10 dollar loss if the SL is hit.
Backtest is on EURUSD 1 Mn, each bar, spread fixed 20 point :
now here is the result, 12 dollar win if TP is hit, and 8 dollar loss if SL is hit