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

 
Tim Rittel:

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

Please do not double post.

I have deleted your new topic which is about the same code as here.

As this topic is discussing MQL4 code, I will move this to the MT4 and MQL4 section soon.

That way you should not have people offering solutions that are meant for MQL5.

 

Hy to All,


thanks for your time, yes, sorry, i didnt knew its mql5 here, its the first time i am using this board, ok no doublethrads, i understand. So Keith Says, it would be impossible to handle more than one order at a time, the way i want it to...thats sad...


For clearance, i was looking for a loop, that clones my Ordermanagement to a newly recognized order, so that not one order after another is "handled", but all opened orders simultaneously...the problem is, that my ea is fully virtual, so i cannot simply OrderModify SL or TP, they are in a virtual space...and it seems, that this virtual space is forgotten, as soon, as a new order comes into play...no ideas??

 
Tim Rittel:

Hy to All,


thanks for your time, yes, sorry, i didnt knew its mql5 here, its the first time i am using this board, ok no doublethrads, i understand. So Keith Says, it would be impossible to handle more than one order at a time, the way i want it to...thats sad...


For clearance, i was looking for a loop, that clones my Ordermanagement to a newly recognized order, so that not one order after another is "handled", but all opened orders simultaneously...the problem is, that my ea is fully virtual, so i cannot simply OrderModify SL or TP, they are in a virtual space...and it seems, that this virtual space is forgotten, as soon, as a new order comes into play...no ideas??

My solution is for MQL5. So I suppose useless!

 
Tim Rittel:

Hy to All,


thanks for your time, yes, sorry, i didnt knew its mql5 here, its the first time i am using this board, ok no doublethrads, i understand. So Keith Says, it would be impossible to handle more than one order at a time, the way i want it to...thats sad...


For clearance, i was looking for a loop, that clones my Ordermanagement to a newly recognized order, so that not one order after another is "handled", but all opened orders simultaneously...the problem is, that my ea is fully virtual, so i cannot simply OrderModify SL or TP, they are in a virtual space...and it seems, that this virtual space is forgotten, as soon, as a new order comes into play...no ideas??

could i somehow start a new function (functionBlaBla++??) for every detected order? and this funtion is glued to orderticket?

 
Christos Papageorgiou:

My solution is for MQL5. So I suppose useless!

my fault, but thank You....

 
Tim Rittel:

thats totally right, sorry, if it was a percentage of profit, situation becomes clearer For 50Percent, order one has 80 Profit, trailing stop is 40 from actual price, order two has 60 Profit, trailing stop is 30 from actual price... i want only order two to close when price falling 30points and only order one closed when ita falls 10 points further to minus 40...

Keith Watford:

In the case of a buy.

calculate difference between the current price and the open price 

calculate the percentage and add to the open price or deduct from the current price (whichever way you want to do it)

if this value is higher than the current SL, then modify it.

I am pretty sure that I have already given you the answer.

Edit - realised you are using virtual stops.

 

You can use Global Variables of the Terminal.

Name the GV to include the ticket # in the name.

Then when you check the ticket you can create the GV and store the SL.

On subsequent checks with the order, you can get the value of the SL and see if you need to close the order. If not check if you need to modify the SL and if you do, set the GV to the new value.

When you close the trade, delete the GV.

 
void TrailingStopKlein()
{
   // Variablen bekanntgeben
   double ASK = Ask;
   if(IsTesting()) ASK = Bid+SymbolInfoInteger(Symbol(),SYMBOL_SPREAD_FLOAT);
   double BID = Bid;
if (BackupTrailingStop == true) {
   int b=0,s=0;
   double ProfitB=0,ProfitS=0,OOP,price_b=0,price_s=0,lot=0,NLb=0,NLs=0,LS=0,LB=0;
   for(int h=0; h<OrdersTotal(); h++)
     {
      if(OrderSelect(h,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if((which_MagicNumber_to_trail==OrderMagicNumber() || which_MagicNumber_to_trail==-1) 
         && (which_Comment_to_trail==OrderComment() || which_Comment_to_trail=="ALL")
         && OrderSymbol()==Symbol())
           {
            OOP = OrderOpenPrice();
            lot = OrderLots();
            if(OrderType()==OP_BUY ) {ProfitB+=OrderProfit()+OrderSwap()+OrderCommission();price_b += OOP*lot; LB+=lot; b++;}
            if(OrderType()==OP_SELL) {ProfitS+=OrderProfit()+OrderSwap()+OrderCommission();price_s += OOP*lot; LS+=lot; s++;}
           }
        }
     }
   if(b!=0)
     {
      NLb=price_b/LB;
     }
   if(s!=0)
     {
      NLs=price_s/LS;
     }
   if(!VirtualTrailingStopOn) STOPLEVEL=(int)MarketInfo(Symbol(),MODE_STOPLEVEL);
   int tip,Ticket;
   bool error;
   double SLKlein,OSL;
   int n=0;
   if(b==0) SLBKlein=0;
   if(s==0) SLSKlein=0;
   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)                                                                         //SLBKlein a global Variable???
                       {
                        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
     }}

Wow Mr. Keith, now i am excited, but totally overdosed...


would you have some quick examples for me(i am learning by doing, no knowledge)



edit SLBKlein has to be a global Variable??

 
Tim Rittel:

Wow Mr. Keith, now i am excited, but totally overdosed...


would you have some quick examples for me(i am learning by doing, no knowledge)

Sorry, but I don't have too much time at the moment

The best thing is that you write a loop to scan through the open orders and work out checking for when the SL should be initiated. (ie when 30 pips in profit)

Calculate what the SL should be and Create a GV. Name could be _Symbol+(string)OrderTicket(). Store the SL

Once you have done that you can add the checks to see if the order should be closed or the SL modified.

 
Keith Watford:

Sorry, but I don't have too much time at the moment

The best thing is that you write a loop to scan through the open orders and work out checking for when the SL should be initiated. (ie when 30 pips in profit)

Calculate what the SL should be and Create a GV. Name could be _Symbol+(string)OrderTicket(). Store the SL

Once you have done that you can add the checks to see if the order should be closed or the SL modified.

thanks, chewing the cake, piece by piece ;.)))))

Reason: