stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Set Break even with Spread - correct?
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=3664
Page 1 of 1
Author:  genaja [ Wed Jun 04, 2014 8:12 am ]
Post subject:  Set Break even with Spread - correct?

Hi volks,

I changed a "Set SL to break even " with BE plus 2x Spread.

Is this code correct?
  • #property copyright ""

    #include <WinUser32.mqh>
    #include <stdlib.mqh>

    //+------------------------------------------------------------------+
    int start()
    //+------------------------------------------------------------------+
    {
    //----
    bool result;
    int err;
    string symbol = Symbol();
    double spread = Ask - Bid;

    Alert("--- START "+WindowExpertName()+" "+Symbol());

    int ot = OrdersTotal();
    for (int i=0; i<ot; i++)
    {
    int o = OrderSelect(i, SELECT_BY_POS);
    if (OrderSymbol() == symbol)
    if ((OrderType() == OP_BUY) || (OrderType() == OP_SELL))
    {
    double sl = 0;
    if ((OrderType() == OP_BUY) && (OrderStopLoss() < OrderOpenPrice()))
    sl = OrderOpenPrice() + (2*spread) + MarketInfo(Symbol() , MODE_SPREAD)*Point;
    if ((OrderType() == OP_SELL) && (OrderStopLoss() > OrderOpenPrice()))
    sl = OrderOpenPrice() - (2*spread) - MarketInfo(Symbol() , MODE_SPREAD)*Point;

    if (sl != 0)
    Alert(WindowExpertName()+":moving SL to "+sl);
    result=OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0);
    if(result!=TRUE) {
    err = GetLastError();
    Alert(WindowExpertName()+":error when OrderModify()",err," : ",ErrorDescription(err));
    break;
    }
    }
    }
    //----
    Alert("--- END "+WindowExpertName()+" "+Symbol());
    return(0);
    }
    //+------------------------------------------------------------------+
Author:  Elixe [ Wed Jun 04, 2014 10:14 am ]
Post subject:  Set Break even with Spread - correct?

Hi Genaja,

the spread is already added one time to the SL, if you want to double either remove the marketinfo part and put your (2*spread) or double the marketinfo returned value :

Code: Select all

sl = OrderOpenPrice() + (2*spread);
sl = OrderOpenPrice() + ((2*MarketInfo(Symbol() , MODE_SPREAD))*Point);
You should also normalize your SL to your broker digits, just in case :

Code: Select all

sl = NormalizeDouble(sl, Digits);
Author:  genaja [ Wed Jun 04, 2014 1:09 pm ]
Post subject:  Set Break even with Spread - correct?

Elixe ยป 04 Jun 2014, 11:14 wrote:Hi Genaja,

the spread is already added one time to the SL, if you want to double either remove the marketinfo part and put your (2*spread) or double the marketinfo returned value :

Code: Select all

sl = OrderOpenPrice() + (2*spread);
sl = OrderOpenPrice() + ((2*MarketInfo(Symbol() , MODE_SPREAD))*Point);
You should also normalize your SL to your broker digits, just in case :

Code: Select all

sl = NormalizeDouble(sl, Digits);

Hi Elixe,

ohhh! :idea:

MODE_SPREAD is a Value of MarketInfo() !!! What a flashlight!
I tried 2*MODE_SPREAD, but that will not work.

MarketInfo(Symbol() , MODE_SPREAD) is the same value like Ask - Bid.


Thanks a lot, learned a lot :hi:
All times are UTC Page 1 of 1