Download MetaTrader 5

BE/TR code not triggered

To add comments, please log in or register
Spinn
45
Spinn  
Hello world !



I switched from NinjaTrader to MT recently and am trying to get the hang of MQL-code, so please be gentle.

I am currently trying to add a 'Breakeven / Trailling' functionality to my EA.

I copied code from  the (in?)famous UniversalMACrossEA.mq4 (https://www.mql5.com/en/forum) and will gladly throw lots of flowers to the gentle coders there :) Special thanks to Codersguru and Firedave !

The EA open and close functions work just fine, and now I would like to add the BE/TR to lock in more profit.

For some reason the subTrailingStop-codeblock does not get executed when  I perform a (visual) backtest :(


Like I said: I coded in NT before, but my MQL4-debugging-skills aren't up to par yet. Can someone glance at my code please and tell me why the BE/TR isn't firing and/or how to debug this ?
Files:
Marco vd Heijden
Moderator
6140
Marco vd Heijden  

Of course it doesn't get executed if you do not call it anywhere.

Also the order has to be selected by OrderSelect() function first.

Spinn
45
Spinn  
Thank you Marco, for pointing me in the right direction !


I made some adjustments to my code (in NT you do not need to 'call' functions explicitly, so this is new for me)


I now have this in my start section: (I mirrored this from that UniversalMACrossEA.mq4 mentioned in my first post)
int start()
{  
   int cnt;
   int total;
     
   if(TrailingStop>0 && subTotalTrade()>0)
   {
      total = OrdersTotal();
      for(cnt=0;cnt<total;cnt++)
      {
         dummyResult = OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
         if(OrderType()<=OP_SELL &&
            OrderSymbol()==s_symbol && OrderMagicNumber()==MagicNumber)
         {
            subTrailingStop(OrderType());
         }
      }
   }  
  return(0);      
  }
I think this code starts counting the number of orders when the chart is initialized. And it keeps doing this counting indefinitely, right ?
Now, if there is an order live that was opened by this EA, then the function 'subTrailingStop' is called, no ? And it is called on a specific OrderType



So further down in my code I have this function defined. (The case is always 1, since I have "extern int              TrailingStopType     = 1;" in my inputs)
void subTrailingStop(int Type)
{
   RefreshRates();
   if(Type==OP_BUY)   // buy position is opened
   DrawDotBlue();  
   {
      switch(TrailingStopType)
      {
//----------------------- AFTER PROFIT TRAILING STOP      
         case 1:
            if(Bid-NormalizeDouble(OrderOpenPrice(),digit)>pPoint*TrailingStop*pipMultiplier &&
              NormalizeDouble(OrderStopLoss(),digit)<Bid-pPoint*TrailingStop*pipMultiplier)
            {
               DrawDotBlue();
               dummyResult = OrderModify(OrderTicket(),NormalizeDouble(OrderOpenPrice(),digit),NormalizeDouble(Bid-pPoint*TrailingStop*pipMultiplier,digit),NormalizeDouble(OrderTakeProfit(),digit),0,Green);
               return;
            }
            break
Should this not activate an after-profit trailing stop on my buy orders ?? Similar code exists for "(Type==OP_SELL)"


What am I missing ?
To add comments, please log in or register