Invalid Stops

 

Hi, My ea is sometimes triggering "Invalid Stops". I believe that's why it calculate the stop value based on my medium price, and in Brazil's index, price always end with 0 or 5. And the Error only appears when finishing with 1,2,3,4,6,7,8 and 9.

What should i go to avoid that?
Thanks!


 
int SetOrder(int type, double lot)
{
  double price = 0, sl = 0, tp = 0;
  ENUM_ORDER_TYPE cmd = ORDER_TYPE_BUY;
  if (type == OP_BUY)
  {
    price = Ask; cmd = ORDER_TYPE_BUY;
    if (StopLoss!=0) sl = NormalizeDouble(price - PipsToPoints(StopLoss), _Digits);
    if (TakeProfit!=0) tp = NormalizeDouble(price + PipsToPoints(TakeProfit), _Digits);
  }
  if (type == OP_SELL)
  {
    price = Bid; cmd = ORDER_TYPE_SELL;
    if (StopLoss!=0) sl = NormalizeDouble(price + PipsToPoints(StopLoss), _Digits);
    if (TakeProfit!=0) tp = NormalizeDouble(price - PipsToPoints(TakeProfit), _Digits);
  }
//+------------------------------------------------------------------+
double PipsToPoints(double pips)
{
  //------------------------------------------------------------------
  if (_Digits == 5 || _Digits == 3) pips *= 10;
  return (pips * _Point);
  //------------------------------------------------------------------
}
//+------------------------------------------------------------------+
double PointsToPips(double points)
{
  if (_Digits == 5 || _Digits == 3) points /= 10;
  return (points / _Point);
}
//+------------------------------------------------------------------+

This may help somehow.

 
Help! please. I know somehow mathround should do the job, but i cannot make it happen...
 
your pip-point conversion is wrong.

P.S. don't work with pips ever.
always base all your calculations and inputs on points.

ALSO, you won't need NormalizeDouble for prices, ever.
if your prices are not checked to be in correct decimal format, round them base on TickSize.
which is done automatically if you use standard libraries, NormalizePrice() function.
again, it's never needed if you do the calculations right
 

Hi, thanks for your answer, but i don't have the ability to understand everything you said.

How should be the code without the pippoint? Just delete this final part, and what should i do on :

int SetOrder(int type, double lot)
{
  double price = 0, sl = 0, tp = 0;
  ENUM_ORDER_TYPE cmd = ORDER_TYPE_BUY;
  if (type == OP_BUY)
  {
    price = Ask; cmd = ORDER_TYPE_BUY;
    if (StopLoss!=0) sl = NormalizeDouble(price - PipsToPoints(StopLoss), _Digits);
    if (TakeProfit!=0) tp = NormalizeDouble(price + PipsToPoints(TakeProfit), _Digits);
  }
  if (type == OP_SELL)
  {
    price = Bid; cmd = ORDER_TYPE_SELL;
    if (StopLoss!=0) sl = NormalizeDouble(price + PipsToPoints(StopLoss), _Digits);
    if (TakeProfit!=0) tp = NormalizeDouble(price - PipsToPoints(TakeProfit), _Digits);
  }

and on reajust :

void ManageOrder()
{
  //------------------------------------------------------------------
  string closeHour = "";
  string ch1 = IntegerToString(DailyCloseHour); if (DailyCloseHour<10) ch1 = "0"+ch1;
  string cm1 = IntegerToString(DailyCloseMinute); if (DailyCloseMinute<10) cm1 = "0"+cm1;
  closeHour = ch1+":"+cm1;
  //------------------------------------------------------------------
  for (int i = PositionsTotal()-1; i >= 0; i --)
  {
    //------------------------------------------------------------------
    ulong ticket = PositionGetTicket(i);
    _res = PositionSelectByTicket(ticket);
    if (PositionGetString(POSITION_SYMBOL)!=Symbol()) continue;
    if (PositionGetInteger(POSITION_MAGIC)!=MagicNumber) continue;
    //------------------------------------------------------------------
    int type = (int)PositionGetInteger(POSITION_TYPE);
    double sl = PositionGetDouble(POSITION_SL), tp = PositionGetDouble(POSITION_TP), op = PositionGetDouble(POSITION_PRICE_OPEN);
    if (sl==0 && tp==0)
    {
      if (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
      {
        if (StopLoss!=0) sl = NormalizeDouble(op-PipsToPoints(StopLoss), _Digits);
        if (TakeProfit!=0) tp = NormalizeDouble(op+PipsToPoints(TakeProfit), _Digits);
      }
      if (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
      {
        if (StopLoss!=0) sl = NormalizeDouble(op+PipsToPoints(StopLoss), _Digits);
        if (TakeProfit!=0) tp = NormalizeDouble(op-PipsToPoints(TakeProfit), _Digits);
      }
      if (sl!=0 || tp!=0) _res = trade.PositionModify(Symbol(), sl, tp);
    }
    //------------------------------------------------------------------
 

Thanks! My mind is confused after many hours trying to figure it out.

 
void ManageOrder()
{
  //------------------------------------------------------------------
  string closeHour = "";
  string ch1 = IntegerToString(DailyCloseHour); if (DailyCloseHour<10) ch1 = "0"+ch1;
  string cm1 = IntegerToString(DailyCloseMinute); if (DailyCloseMinute<10) cm1 = "0"+cm1;
  closeHour = ch1+":"+cm1;
  //------------------------------------------------------------------
  for (int i = PositionsTotal()-1; i >= 0; i --)
  {
    //------------------------------------------------------------------
    ulong ticket = PositionGetTicket(i);
    _res = PositionSelectByTicket(ticket);
    if (PositionGetString(POSITION_SYMBOL)!=Symbol()) continue;
    if (PositionGetInteger(POSITION_MAGIC)!=MagicNumber) continue;
    //------------------------------------------------------------------
    int type = (int)PositionGetInteger(POSITION_TYPE);
    double sl = PositionGetDouble(POSITION_SL), tp = PositionGetDouble(POSITION_TP), op = PositionGetDouble(POSITION_PRICE_OPEN);
    if (sl==0 && tp==0)
    {
      if (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
      {
        if (StopLoss!=0) sl = NormalizeDouble(op-PipsToPoints(StopLoss), _Digits);
        if (TakeProfit!=0) tp = NormalizeDouble(op+PipsToPoints(TakeProfit), _Digits);
      }
      if (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
      {
        if (StopLoss!=0) sl = NormalizeDouble(op+PipsToPoints(StopLoss), _Digits);
        if (TakeProfit!=0) tp = NormalizeDouble(op-PipsToPoints(TakeProfit), _Digits);
      }
      if (sl!=0 || tp!=0) _res = trade.PositionModify(Symbol(), sl, tp);
    }

you have op (open price), and StopLoss (which is the desired stop loss.) this is what I mean by using points only. and forget about pips.
StopLoss variable is of type integer, and is the number of points, of the SL.

if you do it this way, then you can use this format to calculate new prices (like SL) from any known prices (like OP). and you definitely won't need Normalize... functions :
double NEW_PRICE = KNOWN_PRICE + (INTEGER_VALUE * _Point);

if (StopLoss!=0) sl = op + (StopLoss*_Point);

if your StopLoss variable, is in pips, then yes, you can convert it to points, before using it as above.

conversion :

int PipToPoints(int Pips)       // this function gets the number of pips, and returns the number of points.
{
        if(_Digits==5 || _Digits==3) return (Pips*10);
        return Pips;
}

int PointToPips(int Points)
{
        if(_Digits==5 || _Digits==3) return (Points/10);
        return Points;
}

// Reminder : the returned values are number of points, or pips. these values are not the price. (prices are double variables)
 

That's not my code, i ordered via Freelance, and just now I realized stoploss is working like this.
I put the Stoploss in point, not in pips. All this final conversion is useless for me, right? And on my code there's no NormalizePrice, would this be my solution so?

My problem is when i open a trade on 90000 and another on 90005, the OpenPrice will be 90002.5, which will carry a problem to my stop, that will be 89352.5, and if it's not ending with 0 or 5, it wont work...
I'm kind of confused, may you tell me what to do?

 
lol, it's because your account is in NETTING mode, while the coder made the EA for HEDGING accounts.
it can be fixed, but not by Normalizing anything. I never considered coding for NETTING accounts, so can't help you.
 

Hum.........
Weird! It should be for netting!

The only thing that doesn't work is this stop and others things that need correction on price.

 
Lucas Tavares:

That's not my code, i ordered via Freelance, and just now I realized stoploss is working like this.
I put the Stoploss in point, not in pips. All this final conversion is useless for me, right? And on my code there's no NormalizePrice, would this be my solution so?

My problem is when i open a trade on 90000 and another on 90005, the OpenPrice will be 90002.5, which will carry a problem to my stop, that will be 89352.5, and if it's not ending with 0 or 5, it wont work...
I'm kind of confused, may you tell me what to do?

you can try rounding the OpenPrice (round it by TickSize) before using for calculating the SL, as I mentioned earlier.
 
I included :
At beggining
#include <Trade\SymbolInfo.mqh>
CSymbolInfo mysymbol;
int SetOrder(int type, double lot)
{
  double price = 0, sl = 0, tp = 0;
  ENUM_ORDER_TYPE cmd = ORDER_TYPE_BUY;
  if (type == OP_BUY)
  {
    price = Ask; cmd = ORDER_TYPE_BUY;
    if (StopLoss!=0) sl = mysymbol.NormalizePrice(price-StopLoss);
    if (TakeProfit!=0) tp = mysymbol.NormalizePrice(price+TakeProfit);
  }
  if (type == OP_SELL)
  {
    price = Bid; cmd = ORDER_TYPE_SELL;
    if (StopLoss!=0)  sl = mysymbol.NormalizePrice(price+StopLoss);
    if (TakeProfit!=0) tp = mysymbol.NormalizePrice(price-TakeProfit);
  }
And on the part that analyzes if i entered again, and make the new Stoploss price :
 int type = (int)PositionGetInteger(POSITION_TYPE);
    double sl = PositionGetDouble(POSITION_SL), tp = PositionGetDouble(POSITION_TP), op = PositionGetDouble(POSITION_PRICE_OPEN);
    if (sl==0 && tp==0)
    {
      if (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
      {
        if (StopLoss!=0) sl = mysymbol.NormalizePrice(op-StopLoss);
        if (TakeProfit!=0) tp = mysymbol.NormalizePrice(op+TakeProfit);
      }
      if (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
      {
        if (StopLoss!=0) sl = mysymbol.NormalizePrice(op+StopLoss);
        if (TakeProfit!=0) tp = mysymbol.NormalizePrice(op-TakeProfit);
      }
      if (sl!=0 || tp!=0) _res = trade.PositionModify(Symbol(), sl, tp);
    }
And keep with the same error...
Reason: