invalid takeprofit for OrderSend function & invalid stoploss for OrderSend function

 

Hello,


I am frequently getting two error invalid takeprofit for OrderSend function and invalid stoploss for OrderSend function.


Question 1 :


I am getting these error in Expert tab not in Journal tab. If order-send is failed, then it should show in Journal tab right?


Question 2 :


I am checking error with OrderSend function but instead of printing error from my code rules it printing invalid takeprofit for OrderSend function & invalid stoploss for OrderSend function


if(CheckMoneyForTrade(gRandomSymbol, gRandomLotSize, OP_BUY) == true)
     {
      ResetLastError();
      if(!OrderSend(gRandomSymbol, OP_BUY, gRandomLotSize, MarketInfo(gRandomSymbol, MODE_ASK), Slippage, OrderSend_StopLoss = (StopLoss > 0) ? MarketInfo(gRandomSymbol, MODE_ASK) - MathMax(StopLoss, gStopLevel) * Point : 0, OrderSend_TakeProfit = (TakeProfit > 0) ? MarketInfo(gRandomSymbol, MODE_ASK) + MathMax(TakeProfit, gStopLevel) * Point : 0, gTradeComment, gBuyMagic, 0, clrNONE))
         Print("Buy Order => Error Code : " + GetLastError());
     }

   if(CheckMoneyForTrade(gRandomSymbol, gRandomLotSize, OP_SELL) == true)
     {
      ResetLastError();
      if(!OrderSend(gRandomSymbol, OP_SELL, gRandomLotSize, MarketInfo(gRandomSymbol, MODE_BID), Slippage, OrderSend_StopLoss = (StopLoss > 0) ? MarketInfo(gRandomSymbol, MODE_BID) + MathMax(StopLoss, gStopLevel) * Point : 0, OrderSend_TakeProfit = (TakeProfit > 0) ? MarketInfo(gRandomSymbol, MODE_BID) - MathMax(TakeProfit, gStopLevel) * Point : 0, gTradeComment, gSellMagic, 0, clrNONE))
         Print("Sell Order => Error Code : " + GetLastError());
     }


It should print Buy Order => Error Code : and Sell Order => Error Code : but it printing invalid takeprofit for OrderSend function and invalid stoploss for OrderSend function

 
anuj71:

It should print Buy Order => Error Code : and Sell Order => Error Code : but it printing invalid takeprofit for OrderSend function and invalid stoploss for OrderSend function

you have not shown what the sl or tp variables are, or your code for gRandomSymbols, or your gRandomLots, however, I think that both the calculations you have above need to be normalised or trancated to number of the symbols digits. Any number that goes beyond the symbols digits will cause those errors.

OrderSend_TakeProfit = (TakeProfit > 0) ? MarketInfo(gRandomSymbol, MODE_BID) - MathMax(TakeProfit, gStopLevel) * Point : 0

assigning varialbes inside like this will not work. this could be reason for the errors also. instead remove the OrderSend_TakeProfit =.

Same goes for OrderSendStopLoss =... remove this and just leave the boolean statement.

 
Michael Charles Schefe #:

you have not shown what the sl or tp variables are, or your code for gRandomSymbols, or your gRandomLots, however, I think that both the calculations you have above need to be normalised or trancated to number of the symbols digits. Any number that goes beyond the symbols digits will cause those errors.

assigning varialbes inside like this will not work. this could be reason for the errors also. instead remove the OrderSend_TakeProfit =.

Same goes for OrderSendStopLoss =... remove this and just leave the boolean statement.

ternary operator working fine inside Orderfunction. Can you answer my second question, why it not printing error message defined by me if Orderfunction return false/error?

 

anuj71:

if(!OrderSend(/*...*/))
   Print("Sell Order => Error Code : " + GetLastError());

It should print Buy Order => Error Code : and Sell Order => Error Code : but it printing invalid takeprofit for OrderSend function and invalid stoploss for OrderSend function

-1 is true or false, what do you think?

https://docs.mql4.com/trading/ordersend

Returned value

Returns number of the ticket assigned to the order by the trade server or -1 if it fails.

 

Reading documentation always help. https://docs.mql4.com/trading/ordersend

OrderSend do not return true or false. You are using bool operator for an integer returned value.

OrderSend - Trade Functions - MQL4 Reference
OrderSend - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderSend - Trade Functions - MQL4 Reference
 
Vladislav Boyko #:
-1 is true or false, what do you think?

I got it. I was not aware of. I thought it will give true or false.


Fabio Cavalloni #:
OrderSend do not return true or false. You are using bool operator for an integer returned value.
What are the values returns? fail/error = -1 and success = 0 ?
 

Is this correct?


if(OrderSend(gRandomSymbol, OP_BUY, gRandomLotSize, MarketInfo(gRandomSymbol, MODE_ASK), Slippage, OrderSend_StopLoss = (StopLoss > 0) ? MarketInfo(gRandomSymbol, MODE_ASK) - MathMax(StopLoss, gStopLevel) * Point : 0, OrderSend_TakeProfit = (TakeProfit > 0) ? MarketInfo(gRandomSymbol, MODE_ASK) + MathMax(TakeProfit, gStopLevel) * Point : 0, gTradeComment, gBuyMagic, 0, clrNONE) < 0)
Print(Buy Order => Error Code : " + GetLastError());
 
anuj71 #:

Is this correct?

if(OrderSend(/*...*/) < 0)
   Print("Buy Order => Error Code : " + GetLastError());

This should work. Although, I, for example, strictly compare with -1. I don't know which option is better, they are probably the same.

anuj71 #:
What are the values returns? fail/error = -1 and success = 0 ?

https://docs.mql4.com/trading/ordersend

Returned value

Returns number of the ticket assigned to the order by the trade server or -1 if it fails.

 
Vladislav Boyko #:

This should work. Although, I, for example, strictly compare with -1. I don't know which option is better, they are probably the same.

When i put < 0 sometime it still not printing with Error code, i guess strictly comparison is best == -1
 
anuj71 #: Is this correct?
      if(!OrderSend(gRandomSymbo

No. OrderSend does not return a boolean.