Invalid Stops when modifying orders with CTrade

 

{
StopLoss = NormalizeDouble((Bid - (15*pipstotrade*Point())), SymbolInfoInteger(CurrentSymbol, SYMBOL_DIGITS));
TakeProfit = NormalizeDouble((Bid + (30*pipstotrade*Point())), SymbolInfoInteger(CurrentSymbol, SYMBOL_DIGITS));
trade.PositionOpen(CurrentSymbol, ORDER_TYPE_BUY, lotsize, Ask, StopLoss, TakeProfit);
}
...
atr = iATR(CurrentSymbol, 0, InpAtrPeriod);
double atrarray[];
ArraySetAsSeries(atrarray, true);
CopyBuffer(atr, 0, 0, 1, atrarray);
   
if(StringSubstr(CurrentSymbol,3,3) == "JPY")
  pipstotrade = 100*atrarray[0];
else
  pipstotrade = 10000*atrarray[0];

...
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
   {
   CurrentPrice = tick.bid;            
   PriceChange = CurrentPrice - PositionGetDouble(POSITION_PRICE_OPEN);
   PriceChange = NormalizeDouble(PriceChange,_Digits);
   NewTakeProfit = PositionGetDouble(POSITION_TP) + PriceChange;
   NewStopLoss = PositionGetDouble(POSITION_SL) + PriceChange;
   }

Above are the sections of code in the EA I'm trying to write that are based around setting SL/TP and modifying them for open orders. The code is the same for sell orders but Bid and Ask are swapped


I regularly get errors when running backtests that they can't modify the orders due to invalid stops on both buy and sell orders - but some trades are successfully modified.


What is curious about this is that this is something that is designed to trade the daily chart and the modification of the SL/TP isn't supposed to change how far they are from the price, just to track price as it moves in the right direction.

As such, I would have assumed it was far clear of whatever limit there is for stoplevels on the broker I'm using (FXTM) as there is often a few hundred pips between the two limits.

Also I should note that SYMBOL_TRADES_STOP_LEVEL always returns as 0 but I understand from other posts on this forum that that is somewhat meaningless as it can just be representative of what information makes it into the terminal.


Any help?


Cheers

 
atr = iATR(CurrentSymbol, 0, InpAtrPeriod);

CopyBuffer(atr, 0, 0, 1, atrarray);

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. 2004
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int.) You get that in OnInit. In OnTick (after the indicator has updated its buffers,) you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 2020.07.05
          How to call indicators in MQL5 - MQL5 Articles 12 March 2010
 
William Roeder:

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. 2004
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int.) You get that in OnInit. In OnTick (after the indicator has updated its buffers,) you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 2020.07.05
          How to call indicators in MQL5 - MQL5 Articles 12 March 2010

Hi William,

Thank you for your attempt to help clean up my code.

That hasn't helped the issue I was actually asking about and as I expected would happen when I made the change I am still having the same issue.

As dumb and inefficient as that bit may have been, I was able to place trades, modify some of them, and close them too.


My question was about a process that is independent of iATR and CopyBuffer.

I'd be grateful if any answers could remain on topic and attempt explain to me the bit that doesn't work, instead of focusing on the bit that's stupid but does.

 
  1. Paul Mansion: My question was about a process that is independent of iATR and CopyBuffer.

    No it was not. If you read zero for ATR, then pipstotrade is also zero and your open sets TP/SL to Bid or somewhere close. That could definitely affect your stop management.

  2. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?

 

I think you're still misunderstanding my question?


I'm asking about modifying trades that already have stoplosses and takeprofits some 200 pips apart. Having TP/SL on or extremely close to 0 would have sent an order that failed to open a new position.


This is to try to do something like move both + 10 pips, for orders that started with SL and TP far enough away from the prices that they had no issue being opened in the first place.

 
Paul Mansion:

I think you're still misunderstanding my question?


I'm asking about modifying trades that already have stoplosses and takeprofits some 200 pips apart. Having TP/SL on or extremely close to 0 would have sent an order that failed to open a new position.


This is to try to do something like move both + 10 pips, for orders that started with SL and TP far enough away from the prices that they had no issue being opened in the first place.

Try printing the NewTakeProfit and NewStopLoss variables, then you can see what errors have occurred

Reason: