Class CTrade without a stoploss value

 

Dear devel. Hope you doing well.

I have tried to find something in the documentation/forum, but without success. I am trying to send an order via ctrade class, like a buy with only tp defined, with sl as 0, because my sl will always be a market order when a condition is not satisfied. But I am getting a lot of errors like that:

failed modify #X sell X CODE sl: 0, tp: PRICE -> sl: 0, tp: PRICE [Invalid stops]

I have tried to define sl as 0, as NULL, and nothing works.

Anyonre knows how to send an order via CTrade without define a stoploss value?

Thanks.

Documentação sobre MQL5: Biblioteca Padrão / Classes de negociação / CTrade
Documentação sobre MQL5: Biblioteca Padrão / Classes de negociação / CTrade
  • www.mql5.com
CTrade - Classes de negociação - Biblioteca Padrão - Referência MQL5 - Referência sobre algorítimo/automatização de negociação na linguagem para MetaTrader 5
 
b2tradingclub: I have tried to find something in the documentation/forum, but without success. I am trying to send an order via ctrade class, like a buy with only tp defined, with sl as 0, because my sl will always be a market order when a condition is not satisfied. But I am getting a lot of errors like that: failed modify #X sell X CODE sl: 0, tp: PRICE -> sl: 0, tp: PRICE [Invalid stops] I have tried to define sl as 0, as NULL, and nothing works. Anyonre knows how to send an order via CTrade without define a stoploss value?

Maybe it is not your stop-loss that is the problem. Maybe it is your take-profit that is invalid.

You can test it by setting both stop-loss and take-profit to zero. Then if it works then you know it is the take-profit that is invalid.

If not, then something else is the cause.

Show your code if you need more help.

 
Fernando Carreiro #:

Maybe it is not your stop-loss that is the problem. Maybe it is your take-profit that is invalid.

You can test it by setting both stop-loss and take-profit to zero. Then if it works then you know it is the take-profit that is invalid.

If not, then something else is the cause.

Show your code if you need more help.

Thank you for reply me back, Fernando.

I setted also tp to zero.

CTrade::OrderSend: modify position #2 USDEUR (sl: 0, tp: 0) [invalid stops]

if I set both (SL and TP) there is no error with invalid stops message.

Follow the code:

void BuyLimit(double volume = 0.0, double price = 0.0)
{
        double stopGain = 0.0;
        double stopLoss = 0.0;
        string msg = "Buy At Market";

        if (price > 0.0)
        {
                if (GetStopGain() != 0.0)
                        stopGain = NormalizeDouble((price + ToPoints(GetStopGain())), _Digits);
                if (GetStopLoss() != 0.0)
                        stopLoss = NormalizeDouble((price - ToPoints(GetStopLoss())), _Digits);
                msg = "BuyLimit at " + (string)price;
        }

        _logger.Log(msg);
        _trade.BuyLimit(volume > 0 ? volume : _volume, price, _symbol, stopLoss, stopGain);
}
        bool redefinePositions()
        {
                if (!HasPositionOpen()) return false;

                double price = GetPositionPriceOpen();
                double stopGain = 0.0;
                double stopLoss = 0.0;

                if (IsPositionTypeBuy())
                {
                   if (GetStopGain() != 0.0)
                           stopGain = NormalizeDouble((price + ToPoints(GetStopGain())), _Digits);
                        if (GetStopLoss() != 0.0)
                           stopLoss = NormalizeDouble((price - ToPoints(GetStopLoss())), _Digits);

                        _trade.PositionModify(_symbol, stopLoss, stopGain);
                }
                else
                {
                        if (IsPositionTypeSell())
                        {
                           if (GetStopGain() != 0.0)
                                   stopGain = NormalizeDouble((price - ToPoints(GetStopGain())), _Digits);
                        if (GetStopLoss() != 0.0)
                                stopLoss = NormalizeDouble((price + ToPoints(GetStopLoss())), _Digits);

                                _trade.PositionModify(_symbol, stopLoss, stopGain);
                        }
                }

                _logger.Log("Redefined positions");

                return true;
        }

 
b2tradingclub #: Thank you for reply me back, Fernando. I set also tp to zero. CTrade::OrderSend: modify position #2 USDEUR (sl: 0, tp: 0) [invalid stops] if I set both (SL and TP) there is no error with invalid stops message. 

Where are you getting the error — with the "_trade.BuyLimit" or with the "_trade.PositionModify"?

Please note that PositionModify can only be used on a Position, not on a Pending Order.

Also, please note that you can only modify when the previous and new values are different. Don't try to "modify" if the values have not changed.

 
Fernando Carreiro #:

Where are you getting the error — with the "_trade.BuyLimit" or with the "_trade.PositionModify"?

Please note that PositionModify can only be used on a Position, not on a Pending Order.

Also, please note that you can only modify when the previous and new values are different. Don't try to "modify" if the values have not changed.

Dear Fernando,

Apparently it is from PositionModify, but the error message is present only when some of SL or TP are defined as 0 and I test positions with (if (!HasPositionOpen()) return false;).

 
b2tradingclub #: Dear Fernando, Apparently it is from PositionModify, but the error message is present only when some of SL or TP are defined as 0 and I test positions with (if (!HasPositionOpen()) return false;).

What symbols is this on? What are the contract specifications for Stops Level and Freeze Level?

If your market prices are within the Freeze Level, you cannot modify the stops.

 
Fernando Carreiro #:

What symbols is this on? What are the contract specifications for Stops Level and Freeze Level?

If you market prices are within the Freeze Level, you cannot modify the stops.

Dear Fernando, I have forced stopLoss to 0.0 to test, as below:

                        if (IsPositionTypeSell())
                        {
                           if (GetStopGain() != 0.0)
                                   stopGain = NormalizeDouble((price - ToPoints(GetStopGain())), _Digits);
                        if (GetStopLoss() != 0.0)
                                stopLoss = NormalizeDouble((price + ToPoints(GetStopLoss())), _Digits);

                                _trade.PositionModify(_symbol, 0.0, stopGain);
                                Print("Get Info for Fernando: ", _symbolInfo.FreezeLevel());
                        }

I am priting the FreezeLevel... It's reporting 0.

CTrade::OrderSend: modify position #2 USDEUR (sl: 0, tp: 110805) [invalid stops]

Get Info for Fernando: 0

Apparently CTrade is not allowing me to define SL as 0 because in case of define any value different from zero the error message disappears.

 
b2tradingclub #: Dear Fernando, I have forced stopLoss to 0.0 to test, as below: I am priting the FreezeLevel... It's reporting 0. CTrade::OrderSend: modify position #2 USDEUR (sl: 0, tp: 110805) [invalid stops]

Get Info for Fernando: 0. Apparently CTrade is not allowing me to define SL as 0 because in case of define any value different from zero the error message disappears.

No, as I have already mentioned, I think it is because your TP (i.e. stopGain) is invalid.

How can you have a TP of 110805 for USDEUR?

The S/L and T/P are in price quotes, not points.

 

Dear Fernando,

I think I get it fixed with the code below:

            if (PositionGetDouble(POSITION_SL) != stopLoss)

               _trade.PositionModify(_symbol, stopLoss, PositionGetDouble(POSITION_TP));
            if (PositionGetDouble(POSITION_TP) != stopGain)
               _trade.PositionModify(_symbol, PositionGetDouble(POSITION_SL), stopGain);
            if (PositionGetDouble(POSITION_SL) != stopLoss && PositionGetDouble(POSITION_TP) != stopGain)
   _trade.PositionModify(_symbol, stopLoss, stopGain);
Reason: