How to properly move all SL's to breakevenBuy()?

 
Full code in attachments.
i can't understand why i can't properly move all active orders SL's to BreakevenPrice()
Function simply  not looping thru all SL's and return "Error 1" which means i try change SL with same SL, but BreakevenBuy() already moved father

(ELI5 On attached image: I want to move SL's from 2 to 7 onto 7 level, how?)
    Void ClassicTrail()   
  {
   for(int i=OrdersTotal()-1; i > 0; i--)
     {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        {
           {
            if(OrderStopLoss()!=BreakevenBuy() && (OrderType() == OP_BUY) && (CountBuyPositions()>1))
              {
               OrderModify(OrderTicket(), OrderOpenPrice(), BreakevenBuy(), OrderTakeProfit(),clrNONE);
              }
           }
        }
     }
  }


MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
Files:
 

Depending on whether your condition is fit for whether the trade is in a loss or in a profit you can use this simple function for when your trade is actively in a loss with a condition.

One of the reasons why your stoploss is further away could be because you are looping through the previos order and not current order. 

double breakevenlevel()
{
int i, hstTotal=OrdersHistoryTotal();
   for(i=0; i<hstTotal; i++)
     {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        if(OrderType() == OP_BUY)
        if(OrderMagicNumber() == "MN")
        return(OrderOpenPrice());
}

// condition to break even

void OnTick()
{

if(condition)
OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), breakevenlevel(),clrNONE); 
// to modify the stoploss you could either return NULL (no stoploss) or you can just accept the loss. 
// Takeprofit is breakeven level, if this was in the orderstoploss command, the trade will terminate immediately after condition is met depending on the condition.
// if trade is actively in profit with condition then you would switch the breakevenlevel from ordertakeprofit to orderstoploss

}
 
Adj007 #:

Depending on whether your condition is fit for whether the trade is in a loss or in a profit you can use this simple function for when your trade is actively in a loss with a condition.

One of the reasons why your stoploss is further away could be because you are looping through the previos order and not current order. 

double breakevenlevel()
{
int i, hstTotal=OrdersHistoryTotal();
   for(i=0; i<hstTotal; i++)
     {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        if(OrderType() == OP_BUY)
        if(OrderMagicNumber() == "MN")
        return(OrderOpenPrice());
}

// condition to break even

Why are you using OrdersHistoryTotal() ?

Why are you returning the OrderOpenPrice() for the first buy found in the loop? How is that supposed to be the break-even level if there are multiple trades open?

Why are you checking OrderMagicNumber() against a string?

 

This is where organisation comes into play imo. If you re read what I just wrote I've emphasised  simple function for when your trade is actively in a loss with a condition. The code above is just a simple guidance on how to achieve something similar, theres no wrong answer keith. 


int i, hstTotal=OrdersHistoryTotal() 
for(i=0; i<hstTotal; i++)


helps me focus on the current trade

Returning OrderOpenPrice() helps identify the breakeven level per trade.


For multiple trades I would genuinely just put a magic number on each trade, apply it onto multiple functions and run it through each for loop returning that specific orderprices.


Very simple, I have a similar EA to what this person is trying to accomplish and it runs ok. 

 
void ClassicTrail()   //1 Вариант: Классический тралл
  {
   for(int i=OrdersTotal()-1; i > 0; --i)
     {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        {
         Print(OrderTicket());
         Print(OrderStopLoss());
         Print("BreakevenPrice:",BreakevenBuy());
           {
            if((NormalizeDouble(OrderStopLoss(),Digits) <= NormalizeDouble(BreakevenBuy(),Digits)) && ((OrderType() == OP_BUY) && (CountBuyPositions()>1)))
              {

               OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(BreakevenBuy(),Digits), OrderTakeProfit(),0,clrNONE);
               if(GetLastError()==1)
               {
                  break;
               }
              }
           }
        }
     }
  } 
Are there possible workaround simply to catch if
(GetLastError()==1)
we skip this one and go to next order (i)?
How to properly code it?
 
Adj007 #:

This is where organisation comes into play imo. If you re read what I just wrote I've emphasised  simple function for when your trade is actively in a loss with a condition. The code above is just a simple guidance on how to achieve something similar, theres no wrong answer keith. 

There are wrong answers and your answer is definitely one of them.

You haven't answered the questions

Why are you using OrdersHistoryTotal() ?

Why are you returning the OrderOpenPrice() for the first buy found in the loop? How is that supposed to be the break-even level if there are multiple trades open?

Why are you checking OrderMagicNumber() against a string?

 
Breakeven price 1.276
OrderStopLoss()!=BreakevenBuy()
why stop loss for #2 order 1.1233 can't be changed to 1.276. Losing my mind here 
img attached


void OnTick()
{
..
ClassicTrail();
..
}

...


double BreakevenBuy()
  {
   double SummBuyLots_Prices=0;
   double SummLots=0;
   double AvereageBuyLevel=0;
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS))
         continue;
      if(OrderType()!=OP_BUY || OrderSymbol()!=Symbol() || OrderMagicNumber()=="")
         continue;
        {
         SummLots+=OrderLots();
         SummBuyLots_Prices+=OrderLots()*OrderOpenPrice();
        }
     }
   if(SummLots>0)
     {
      AvereageBuyLevel=SummBuyLots_Prices/SummLots;
      AvereageBuyLevel=NormalizeDouble(AvereageBuyLevel,4);
     }
   return (AvereageBuyLevel);
  }


...


void ClassicTrail()
  {
   for(int i=OrdersTotal()-1; i >= 0; i--)
     {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        {
           {
            if(OrderStopLoss()!=BreakevenBuy() && (OrderType() == OP_BUY) && (CountBuyPositions()>1))
              {
               Print (OrderTicket());
               Print (OrderStopLoss());
               OrderModify(OrderTicket(), OrderOpenPrice(), BreakevenBuy(), OrderTakeProfit(),clrNONE);
              }
           }
        }
     }
  }
Files:
1.png  164 kb
 
Keith Watford #:

There are wrong answers and your answer is definitely one of them.

You haven't answered the questions

Why are you using OrdersHistoryTotal() ?

Why are you returning the OrderOpenPrice() for the first buy found in the loop? How is that supposed to be the break-even level if there are multiple trades open?

Why are you checking OrderMagicNumber() against a string?

Well everyone is entitled to an opinion but in coding there aren’t any wrong answers which clearly depicts your ego. I’ve also answered your questions so you can go back to what I’ve wrote and read it again. I’m not going to entertain your rhetorical quizzes which are just considered rude. 
 
Adj007 #:
Well everyone is entitled to an opinion but in coding there aren’t any wrong answers which clearly depicts your ego. I’ve also answered your questions so you can go back to what I’ve wrote and read it again. I’m not going to entertain your rhetorical quizzes which are just considered rude. 

Suit yourself.

 
Adj007 #:Well everyone is entitled to an opinion but in coding there aren’t any wrong answers which clearly depicts your ego.
if(OrderMagicNumber() == "MN")

What do you think comparing a number to a string does? It is obviously wrong. That is a fact, not an opinion.

It is clearly your ego doesn't want help from people trying to help you. Live in ignorance.

 
William Roeder #:

What do you think comparing a number to a string does? It is obviously wrong. That is a fact, not an opinion.

It is clearly your ego doesn't want help from people trying to help you. Live in ignorance.


What’s funny is that this is actually in the source code the person had first published hence I did it that way. And also that Keith added that issue after I answered all of his questions. 

What would have been better is to include that in one of the first bunch of questions being asked. Not to jump from question to question, it shows a clear sign of indirect intentional disinterest. Even before being told I’m still “wrong” with my first reply, I did further explanation. 

You don’t need to jump in and quote on quote “live in ignorance” it’s really not necessary to get personal with me. I’ve probably done and up to par with many “experienced” programmers on here so honestly don’t need your redundant “live in ignorance” quote. You want to talk to me and discuss respectfully how I’m wrong ? Then do that instead. Furthermore, this conversation ended a few hours ago, you don’t need to reenact the scenario, also unnecessary. Continue being toxic on here, really shows your character. 

Reason: