Breakeven function not working for non-forex pairs

 

Hi, attached is my simple function to breakeven stoploss after price reaches certain profits on that pair. It is working on all the forex pairs i have tested but, not working on other pairs like indices and cryptos. Im trying to figure out how do i amend my code for this but cant really find much at the moment, any help is appreciated :)

I tried printing statements to debug but seems like it is not entering into the "if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && PositionGetString(POSITION_SYMBOL) == Symbol() && PositionGetInteger(POSITION_MAGIC) == Magic)" loop.

void BreakevenStop()
{
   for(int i = PositionsTotal()-1; i >= 0; i--)
   {
      ulong posTicket = PositionGetTicket(i);
      
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && PositionGetString(POSITION_SYMBOL) == Symbol() && PositionGetInteger(POSITION_MAGIC) == Magic)
      {
         double currentSL = NormalizeDouble(PositionGetDouble(POSITION_SL),Digits());
         double currentTP = NormalizeDouble(PositionGetDouble(POSITION_TP),Digits());
         double newSL = NormalizeDouble(PositionGetDouble(POSITION_PRICE_OPEN),Digits());

         if(TotalProfit(Magic) >= risk_money * InpBE && currentSL != newSL)
         {   
            if(ttrade.PositionModify(posTicket, newSL, currentTP))
               Print("Stoploss moved to breakeven for ", Symbol()," after ", InpBE, " ratio reached...");
            else
               Print("Failed to modify stoploss for ", Symbol(), ". Error: ", GetLastError());
         }
      }      

      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL && PositionGetString(POSITION_SYMBOL) == Symbol() && PositionGetInteger(POSITION_MAGIC) == Magic)
      {
         double currentSL = NormalizeDouble(PositionGetDouble(POSITION_SL),Digits());
         double currentTP = NormalizeDouble(PositionGetDouble(POSITION_TP),Digits());
         double newSL = NormalizeDouble(PositionGetDouble(POSITION_PRICE_OPEN),Digits());
         
         if(TotalProfit(Magic) >= risk_money * InpBE && currentSL != newSL)
         {
            if(ttrade.PositionModify(posTicket, newSL, currentTP))
               Print("Stoploss moved to breakeven for ", Symbol()," after ", InpBE, " ratio reached...");
            else
               Print("Failed to modify stoploss for ", Symbol(), ". Error: ", GetLastError());
         }
      }      
   }
}
 
You should share all the code if you need further help.
 
Yashar Seyyedin #:
You should share all the code if you need further help.

Hi, which other parts do u need i can share,

cos rn this is the only portion that has a problem

 
Wei Lun Lim:

Hi, attached is my simple function to breakeven stoploss after price reaches certain profits on that pair. It is working on all the forex pairs i have tested but, not working on other pairs like indices and cryptos. Im trying to figure out how do i amend my code for this but cant really find much at the moment, any help is appreciated :)

I tried printing statements to debug but seems like it is not entering into the "if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && PositionGetString(POSITION_SYMBOL) == Symbol() && PositionGetInteger(POSITION_MAGIC) == Magic)" loop.

Its probably not entering in the BreakEvenStop.

 
Elvis Wangai Muriithi #:

Its probably not entering in the BreakEvenStop.

The function is working fine with Forex pairs, but not with pairs like XAUUSD,US30 and BTCUSD. Im not sure why it is not getting into the loop since those conditions are valid? or did i miss something, which portion of code do u think it could be causing the issue, appreciate the input :)

 
Wei Lun Lim:



         if(TotalProfit(Magic) >= risk_money * InpBE && currentSL != newSL)
         {   
            if(ttrade.PositionModify(posTicket, newSL, currentTP))
               Print("Stoploss moved to breakeven for ", Symbol()," after ", InpBE, " ratio reached...");
            else
               Print("Failed to modify stoploss for ", Symbol(), ". Error: ", GetLastError());
         }

the function uses Magic profit to decide if do a BE or not!! This will work if you have just one open position. 

better to check for that single position profit.


Also check what error code is in journal

 
Hi, I tried troubleshooting and printing the errorcode as a practice already, it is just not entering this loop for non forex pairs only, the fx pairs are working fine and as intended by the code attached
 

User the debugger in MetaEditor. 

Open the expert file in MetaEditor, toggle a breakpoint (F9) on that line:

void BreakevenStop()
{
   for(int i = PositionsTotal()-1; i >= 0; i--)
   {
      ulong posTicket = PositionGetTicket(i);
      
      //...
      //...
}

Then start the debugger on history data (CTRL+F5). You can step over (F10) or step into (F11) to debug your code. Also, the sub-window on the right bottom will display your variables.

https://www.metatrader5.com/en/metaeditor/help/development/debug
Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...
 
Wei Lun Lim #:
Hi, I tried troubleshooting and printing the errorcode as a practice already, it is just not entering this loop for non forex pairs only, the fx pairs are working fine and as intended by the code attached

you insist not to share your code :D

also I can not see Position selection in your code!

void BreakevenStop()
{
   for(int i = PositionsTotal()-1; i >= 0; i--)
   {
   ulong posTicket = PositionGetTicket(i);

   if (!PositionSelectByTicket(posTicket)) continue;
   .
   .
   .
 
Mahdi Ebrahimzadeh #:

you insist not to share your code :D

also I can not see Position selection in your code!

//+------------------------------------------------------------------+
//|                                                                  |
double TotalProfit(int magic)
{
   double pft=0;
   for(int i=PositionsTotal()-1;i>=0;i--)
     {
      ulong ticket=PositionGetTicket(i);
      if(ticket>0)
        {
         if(PositionGetInteger(POSITION_MAGIC)==Magic && PositionGetString(POSITION_SYMBOL)==Symbol())
           {
            pft+=PositionGetDouble(POSITION_PROFIT);
           }
        }
     }
   return(pft);
} 
//+------------------------------------------------------------------+

This is the code to get the profit for that position via magic number, please look at this too :)
Also if im not mistaken the position selection was done alr via positon get ticket and the other conditions specified?

 
Wei Lun Lim #:
This is the code to get the profit for that position via magic number, please look at this too :)

As I guess before, this code returns profit of all positions from chart symbol. you need to have single position profit to compare because as I see you do not want to do BE for a basket of positions.

this one can help you(for buy):

if(PositionGetDouble(POSITION_PRICE_CURRENT) - PositionGetDouble(POSITION_PRICE_OPEN) >= InpBE * Point() && currentSL<PositionGetDouble(POSITION_PRICE_OPEN))
         {   
            if(ttrade.PositionModify(posTicket, newSL, currentTP))
               Print("Stoploss moved to breakeven for ", Symbol()," after ", InpBE, " ratio reached...");
            else
               Print("Failed to modify stoploss for ", Symbol(), ". Error: ", GetLastError());
         }


Wei Lun Lim #:
Also if im not mistaken the position selection was done alr via positon get ticket and the other conditions specified?

yes, based on documentation it has to select position when we call for ticket number, but yet it is mentioned that " To make sure that you receive valid position data, it is recommended to call PositionSelectByTicket() before you access the data."

As we cannot see more from your code, better to check this item too.