forLoop Misunderstanding stalking multiple opened Orders... - page 2

 
Keith Watford:

Please show relevant code

AND SO ON...

is not any use.

for(int j=0; j<OrdersTotal(); j++)

     {

      if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES)==true)

        {

         tip=OrderType();

         if((which_MagicNumber_to_trail==OrderMagicNumber() || which_MagicNumber_to_trail==-1) 

         && (which_Comment_to_trail==OrderComment() || which_Comment_to_trail=="ALL")

         && OrderSymbol()==Symbol()

         &&(includeManualTrades==true||OrderMagicNumber()!=0))

           {

            OSL    = OrderStopLoss();

            OOP    = OrderOpenPrice();

            Ticket = OrderTicket();

            n++;

            if(tip==OP_BUY)

              {

               if(GeneralNoLossBackup==false)

                 {

                  SLKlein=SlLastBarKlein(OP_BUY,BID,NLb);

                  if(SLKlein<NLb) continue;

                 }

               else

                 {

                  SLKlein=SlLastBarKlein(OP_BUY,BID,OOP);

                 }

               //if (OSL  >= OOP && only_NoLoss) continue;

               if(SLKlein>=OSL+StepTrall*Point && (BID-SLKlein)/Point>STOPLEVEL)

                 {

                  if(VirtualTrailingStopOn)

                    {

                     if(SLBKlein<SLKlein) SLBKlein=SLKlein;

                    if(OrderSymbol()==Symbol()) LineTrailingBuyKlein(DoubleToString(OrderTicket())+" StopProfitBuy",SLBKlein,SafeProfitLine_ColourKlein);

                     if(SLBKlein!=0 && BID<=SLBKlein)

                       {

                        if((WinBackup==true&&OrderProfit()>0)||(LooseBackup==true&&OrderProfit()<0)){

                        if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippageClose,Blue)) {

                        ObjectsDeleteAll(ChartID(),0,OBJ_HLINE);

                       if (OrderProfit()>0 && PlaySounds==true) PlaySound("work.wav");

                       if (OrderProfit()<0 && PlaySounds==true) PlaySound("alert.wav");}

                        continue;

                      }};

                    }

                  else

                    {

                     error=OrderModify(Ticket,OOP,SLKlein,OrderTakeProfit(),0,White);

                     if(!error) Comment("TrailingStopGross Error ",GetLastError(),"  order ",Ticket,"   SLKlein ",SLKlein);

                     else Comment("TrailingStopGross ",Ticket," ",TimeToStr(TimeCurrent(),TIME_MINUTES));

                    }

                 }

              }

            if(tip==OP_SELL)

              {

               if(GeneralNoLossBackup==false)

                 {

                  SLKlein=SlLastBarKlein(OP_SELL,ASK,NLs);

                  if(SLKlein>NLs) continue;

                 }

               else

                 {

                  SLKlein=SlLastBarKlein(OP_SELL,ASK,OOP);

                 }

               //if (OSL  <= OOP && only_NoLoss) continue;

               if((SLKlein<=OSL-StepTrall*Point || OSL==0) && (SLKlein-ASK)/Point>STOPLEVEL)

                 {

                  if(VirtualTrailingStopOn)

                    {

                     if(SLSKlein==0 || SLSKlein>SLKlein) SLSKlein=SLKlein;

                    if(OrderSymbol()==Symbol()) LineTrailingSellKlein(DoubleToString(OrderTicket())+" StopProfitSell",SLSKlein,SafeProfitLine_ColourKlein);

                     if(SLSKlein!=0 && ASK>=SLSKlein)

                       {

                        if((WinBackup==true&&OrderProfit()>0)||(LooseBackup==true&&OrderProfit()<0)){

                        if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippageClose,Red)) {

                        ObjectsDeleteAll(ChartID(),0,OBJ_HLINE);

                       if (OrderProfit()>0 && PlaySounds==true) PlaySound("work.wav");

                       if (OrderProfit()<0 && PlaySounds==true) PlaySound("alert.wav");}

                        continue;

                      }};

                    }

                  else

                    {

                     error=OrderModify(Ticket,OOP,SLKlein,OrderTakeProfit(),0,White);

                     if(!error) Comment("TrailingStopGross Error ",GetLastError(),"  order ",Ticket,"   SLKlein ",SLKlein);

                     else Comment("TrailingStopGross ",Ticket," ",TimeToStr(TimeCurrent(),TIME_MINUTES));

                    }

                 }

              }

           }

        }

        } // if BackupTrailingStop == true

     }} Good Morning Keith....

 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button

 
Sergey Golubev:
for(int j=0; j<OrdersTotal(); j++)
     {
      if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES)==true)
        {
         tip=OrderType();
         if((which_MagicNumber_to_trail==OrderMagicNumber() || which_MagicNumber_to_trail==-1) 
         && (which_Comment_to_trail==OrderComment() || which_Comment_to_trail=="ALL")
         && OrderSymbol()==Symbol()
         &&(includeManualTrades==true||OrderMagicNumber()!=0))
           {
            OSL    = OrderStopLoss();
            OOP    = OrderOpenPrice();
            Ticket = OrderTicket();
            n++;
            if(tip==OP_BUY)
              {
               if(GeneralNoLossBackup==false)
                 {
                  SLKlein=SlLastBarKlein(OP_BUY,BID,NLb);
                  if(SLKlein<NLb) continue;
                 }
               else
                 {
                  SLKlein=SlLastBarKlein(OP_BUY,BID,OOP);
                 }
               //if (OSL  >= OOP && only_NoLoss) continue;
               if(SLKlein>=OSL+StepTrall*Point && (BID-SLKlein)/Point>STOPLEVEL)
                 {
                  if(VirtualTrailingStopOn)
                    {
                     if(SLBKlein<SLKlein) SLBKlein=SLKlein;
                    if(OrderSymbol()==Symbol()) LineTrailingBuyKlein(DoubleToString(OrderTicket())+" StopProfitBuy",SLBKlein,SafeProfitLine_ColourKlein);
                     if(SLBKlein!=0 && BID<=SLBKlein)
                       {
                        if((WinBackup==true&&OrderProfit()>0)||(LooseBackup==true&&OrderProfit()<0)){
                        if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippageClose,Blue)) {
                        ObjectsDeleteAll(ChartID(),0,OBJ_HLINE);
                       if (OrderProfit()>0 && PlaySounds==true) PlaySound("work.wav");
                       if (OrderProfit()<0 && PlaySounds==true) PlaySound("alert.wav");}
                        continue;
                      }};
                    }
                  else
                    {
                     error=OrderModify(Ticket,OOP,SLKlein,OrderTakeProfit(),0,White);
                     if(!error) Comment("TrailingStopGross Error ",GetLastError(),"  order ",Ticket,"   SLKlein ",SLKlein);
                     else Comment("TrailingStopGross ",Ticket," ",TimeToStr(TimeCurrent(),TIME_MINUTES));
                    }
                 }
              }
            if(tip==OP_SELL)
              {
               if(GeneralNoLossBackup==false)
                 {
                  SLKlein=SlLastBarKlein(OP_SELL,ASK,NLs);
                  if(SLKlein>NLs) continue;
                 }
               else
                 {
                  SLKlein=SlLastBarKlein(OP_SELL,ASK,OOP);
                 }
               //if (OSL  <= OOP && only_NoLoss) continue;
               if((SLKlein<=OSL-StepTrall*Point || OSL==0) && (SLKlein-ASK)/Point>STOPLEVEL)
                 {
                  if(VirtualTrailingStopOn)
                    {
                     if(SLSKlein==0 || SLSKlein>SLKlein) SLSKlein=SLKlein;
                    if(OrderSymbol()==Symbol()) LineTrailingSellKlein(DoubleToString(OrderTicket())+" StopProfitSell",SLSKlein,SafeProfitLine_ColourKlein);
                     if(SLSKlein!=0 && ASK>=SLSKlein)
                       {
                        if((WinBackup==true&&OrderProfit()>0)||(LooseBackup==true&&OrderProfit()<0)){
                        if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippageClose,Red)) {
                        ObjectsDeleteAll(ChartID(),0,OBJ_HLINE);
                       if (OrderProfit()>0 && PlaySounds==true) PlaySound("work.wav");
                       if (OrderProfit()<0 && PlaySounds==true) PlaySound("alert.wav");}
                        continue;
                      }};
                    }
                  else
                    {
                     error=OrderModify(Ticket,OOP,SLKlein,OrderTakeProfit(),0,White);
                     if(!error) Comment("TrailingStopGross Error ",GetLastError(),"  order ",Ticket,"   SLKlein ",SLKlein);
                     else Comment("TrailingStopGross ",Ticket," ",TimeToStr(TimeCurrent(),TIME_MINUTES));
                    }
                 }
              }
           }
        }
        } // if BackupTrailingStop == true
     }}
 
Tim Rittel:

I know that the variable names that you use are meaningful to you, but they are meaningless to me and that makes it very difficult for me to try to follow your code. Also the fact that you have function calls but don't include the functions means that I have no idea what you are trying to do.

I will be unable to help you, especially as you haven't even described your problem.

 
Keith Watford:

I know that the variable names that you use are meaningful to you, but they are meaningless to me and that makes it very difficult for me to try to follow your code. Also the fact that you have function calls but don't include the functions means that I have no idea what you are trying to do.

I will be unable to help you, especially as you haven't even described your problem.

Hello, Keith, i hope my next try, will make it clearer...i am so sad :-((

//+------------------------------------------------------------------+
//| Expert Stalking function                                         |
//+------------------------------------------------------------------+
void Stalking()
  {
//---
  int OverviewOrders;
  int SeparateOrder;
  int toSortBy=0;
  int KeepSorted;   
  int toSeparateOrder=0;
  
   for(OverviewOrders=0;OverviewOrders<OrdersTotal();OverviewOrders++)
   {  
     if(OrderSelect(OverviewOrders,SELECT_BY_POS,MODE_TRADES))toSortBy++; 
   }
      for(toSortBy++, KeepSorted=OverviewOrders-1; KeepSorted <= OrdersTotal(); KeepSorted++) 
      {
         if(OrderSelect(KeepSorted, SELECT_BY_POS, MODE_TRADES) ) toSeparateOrder++; toSortBy=0;
      }
         for(toSeparateOrder++, SeparateOrder=1; SeparateOrder>0; SeparateOrder--)
         {
            if(OrderSelect(SeparateOrder,SELECT_BY_TICKET)==true)
            { 
            toSeparateOrder=0; 
            /*------------
            Here comes the stuff that i want to do with this order, for example a virtual Trailing Stop, 
            depending on OrderOpenPrice+ ((MaxOrderProfit-OrderOpenPrice)*Percentage/100)....i do not
            need Help with coding this greyText, just with the coloured above and below, cant crack this nut....
            The thing is, it shall handle all opened orders PARALLEL (different OrderOpenprices and MaxOrderProfits), 
            NOT one after another or with cumulative Orderclose...
            ------------*/
            if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),1,Blue))continue; //is there a way to unique close the selected order??????not All orders?
            }               // if(OrderSelect(SeparateOrder,SELECT_BY_TICKET)==true)
         }                  // for(toSeparateOrder++, SeparateOrder=1; SeparateOrder>0; SeparateOrder--)
}                           // void Stalking()
//+------------------------------------------------------------------+
 
ProblemToSee
 
Christos Papageorgiou:

That's an example of a Trailing. You can easily change it to virtual.

Separate trailing based on order's ticket number.

So you have to set your criteria to check each order by magic number and close only this order. 

Thank You, i will have a look at it right now, didnt knew PositionsTotal() :-)

 
Tim Rittel:

Thank You, i will have a look at it right now, didnt knew PositionsTotal() :-)

You are most welcome Tim. Hope it makes it clearer. At your disposal if you need further help.

 
Tim Rittel:

Hello, Keith, i hope my next try, will make it clearer...i am so sad :-((

Sorry, that doesn't make it any clearer. Maybe I am missing something.

Why all the nested loops? I simply can't see what you are trying to achieve.

You want to calculate with each order separately, so deal with them separately.

You can't calculate with more than one order at a time. They must be checked one after the other. 

 

Like Keith says, it's very unclear what you want to do. I think this is a case of when a person knows what they want to do in their head, but cannot clearly articulate it in writing... What is very clear is that this code is trying to do way too much and should be broken down to easier steps (functions).


Also, this thread had gone into MT5 examples for an MT4 problem... 

Reason: