Scanning the history counting consecutive losses and profits

 

I want to scan the history of the trades to check if I had 3 consecutive wins or 3 consecutive losses 

I wrote a code and this one scan only the last two trades even if I increased the Bistory and make it equal to 4 or 5

 

extern int     Bistory              =3;



int OrdHistory=OrdersHistoryTotal();
int ProfCout=0, LosCount=0;
for(cnt = OrdersHistoryTotal()-1; cnt >=0; cnt--)
 {OrderSelect(cnt, SELECT_BY_POS,MODE_HISTORY);
   if (OrdersHistoryTotal() == 0) break;
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic )
   {for(int x=Bistory;x>=1;x--)
      {
          if(OrderSelect(OrdHistory-x,SELECT_BY_POS,MODE_HISTORY) && OrderProfit()>=0)ProfCout++;
          if(OrderSelect(OrdHistory-x,SELECT_BY_POS,MODE_HISTORY) && OrderProfit() <0)LosCount++;
      }}}

I though of making one loop instead of two but it didn't work 

any help is highly appricated

 

Print the value of OrdersHistoryTotal()

and see how many there are.

 

Hello phy, 

I am currently backtesting on a visual mode so I keep track of the last trades visually and I have a comment function to show the last trade if it was on profit or loss 

I have used a simpler approach that is part of the above code to  count the profitable trades and losing trades but there is one problem 

the counter does not reset for example when a profitable trade happen after three loss the values of the counter are stuck so I am not sure if the counter  take the last 3 consecutive orders or not 

int ProfCout=0, LosCount=0;
int OrdHistory=OrdersHistoryTotal();
if(OrdHistory>0 && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{ for(int cnt=Bistory;cnt>=1;cnt--)
    {
         if(OrderSelect(OrdHistory-cnt,SELECT_BY_POS,MODE_HISTORY) && OrderProfit()>=0)ProfCout++;
         if(OrderSelect(OrdHistory-cnt,SELECT_BY_POS,MODE_HISTORY) && OrderProfit()<0)LosCount++;
     }
 

another approach that didn't work as well 


int CP=0,CL=0;
     
for(cnt=OrdersTotal()-1;cnt>=0;cnt--){
   OrderSelect(cnt,SELECT_BY_POS);
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic){
         OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY);
            if(OrderProfit() >= 0) CP++;
              else if(OrderProfit()<0)break;
               OrderSelect(OrdersHistoryTotal()-2, SELECT_BY_POS, MODE_HISTORY);
                  if(OrderProfit() >= 0)CP++;
                   else if(OrderProfit()<0)break;
                     OrderSelect(OrdersHistoryTotal()-3, SELECT_BY_POS, MODE_HISTORY);
                        if(OrderProfit() >= 0)CP++;
                         else if(OrderProfit()<0)break;
                        
         OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY);
            if(OrderProfit() < 0)CL++;
             else if(OrderProfit()>=0)break;
               OrderSelect(OrdersHistoryTotal()-2, SELECT_BY_POS, MODE_HISTORY);
                  if(OrderProfit() < 0)CL++;
                   else if(OrderProfit()>=0)break;
                     OrderSelect(OrdersHistoryTotal()-3, SELECT_BY_POS, MODE_HISTORY);
                        if(OrderProfit() <0)CL++;
                           else if(OrderProfit()>=0)break; }}
 
fadiy wrote >>

I want to scan the history of the trades to check if I had 3 consecutive wins or 3 consecutive losses

I wrote a code and this one scan only the last two trades even if I increased the Bistory and make it equal to 4 or 5




I though of making one loop instead of two but it didn't work

any help is highly appricated

Why you need this loop ??

for(cnt = OrdersHistoryTotal()-1; cnt >=0; cnt--)

in this loop you always check only last "Bistory" orders

{for(int x=Bistory;x>=1;x--)
{
if(OrderSelect(OrdHistory-x,SELECT_BY_POS,MODE_HISTORY) && OrderProfit()>=0)ProfCout++;
if(OrderSelect(OrdHistory-x,SELECT_BY_POS,MODE_HISTORY) && OrderProfit() <0)LosCount++;
}}}

 
rafaell:

Why you need this loop ??

for(cnt = OrdersHistoryTotal()-1; cnt >=0; cnt--)

in this loop you always check only last "Bistory" orders

{for(int x=Bistory;x>=1;x--)
{
if(OrderSelect(OrdHistory-x,SELECT_BY_POS,MODE_HISTORY) && OrderProfit()>=0)ProfCout++;
if(OrderSelect(OrdHistory-x,SELECT_BY_POS,MODE_HISTORY) && OrderProfit() <0)LosCount++;
}}}

Hi rafaell, 

You are right that part works,I created the to filter the trades by symbol and magic and forget to add that condtion next to OrderProfit, I did that now and the code work

Thanks 

 

Hi,

I think this way is good:

extern int     Bistory              =3;

   int ProfCout=0, LosCount=0;
   int OrdHistory=OrdersHistoryTotal();
   if(OrdHistory>0 && OrderSymbol()==Symbol() && OrderMagicNumber()==16384)
   { for(int cnnt=Bistory;cnnt>=1;cnnt--)
      {
         if(OrderSelect(OrdHistory-cnnt,SELECT_BY_POS,MODE_HISTORY) && OrderProfit()>=0)LosCount=0;//ProfCout++;
         if(OrderSelect(OrdHistory-cnnt,SELECT_BY_POS,MODE_HISTORY) && OrderProfit()<0)LosCount++;
      }
   }
   Comment(LosCount);

update

if(OrderSelect(OrdHistory-cnnt,SELECT_BY_POS,MODE_HISTORY) && OrderProfit()>=0)ProfCout++;
to
if(OrderSelect(OrdHistory-cnnt,SELECT_BY_POS,MODE_HISTORY) && OrderProfit()>=0)LosCount=0;
Regards
 

Help needed on this subject,

I am tracking the consecutive losses for binary options, i have a count system working.


*******  I just need code to stop further trades for 10 candles? how can i count 10 candles after 3 losses? then reset the loss value mPutLossCount= 0 and continue to bid. *********

"mPutLossCount is tracking the consecutive loss count for Put bids (the same for call not shown here)."



   if(New_Bar()==true)

     {

      datetime lCurrentTime=iTime(Symbol(),PERIOD_M1,0);

      if

      (mBetPending && mBetStatus=="Put" && mPutLossCount < 3) // check betting status  of Put losses in a row if >3 i want the program to stop bidding for 10 candles

        {

         mAskForPendingReset=true;

         drawArrowDown();

         // Now is the result : win or loose ?

         if(Close[1]<mPutPrice)

           {

            // Won !

            mAccountBalance=mAccountBalance+mInvestment*(mPayout/100.0+1.0);


            resetVariables();


            mNbWins=mNbWins+1;

               mInvestment=100;

               mAmountbid = mInvestment;

            drawArrowCheck(false);


           }

         else if(Close[1]>mPutPrice)

           {

            // Looose  !

            resetVariables();

            mNbLoss=mNbLoss+1;

            mPutLossCount = mPutLossCount +1; // add to the lossing count

            mInvestment=mInvestment;

            mAmountbid = mInvestment;

            if (mAmountbid > mHighestbid)

            {

            mHighestbid = mAmountbid;

            }

etc.... test draw result then testing call bids.....

 
void ConsecutiveLossesCheck(int ConsecutiveLosses, int MagicNumber)
{
   int losses = 0;
   int h = OrdersHistoryTotal()-1;
   for(int c=ConsecutiveLosses; c>=1; c--)
   {
      OrderSelect(h, SELECT_BY_POS, MODE_HISTORY);
      if(OrderMagicNumber()==MagicNumber && OrderProfit()<0)
         losses ++;
      h--;
   };
   if(losses >= ConsecutiveLosses)
   {
      ExpertRemove();
      Print("EA removed after ", ConsecutiveLosses, " consecutive losses.");
   };
};

 
mirza_algo #:
void ConsecutiveLossesCheck(int ConsecutiveLosses, int MagicNumber)
{
   int losses = 0;
   int h = OrdersHistoryTotal()-1;
   for(int c=ConsecutiveLosses; c>=1; c--)
   {
      OrderSelect(h, SELECT_BY_POS, MODE_HISTORY);
      if(OrderMagicNumber()==MagicNumber && OrderProfit()<0)
         losses ++;
      h--;
   };
   if(losses >= ConsecutiveLosses)
   {
      ExpertRemove();
      Print("EA removed after ", ConsecutiveLosses, " consecutive losses.");
   };
};


Why reply to an old topic?

What is your code supposed to do? It makes no sense at all!