I need help for finding the consecutive lose streak for the last 24hrs. - page 2

 
dio1zervas #:

i tried to combine the code you learned me with the one in the example. That is my try. i Hope i am in a good way.

Thank's for the teaching!

I really appreciate it.

1.  Please insert the code correctly: when editing a message, press the button        Codeand paste your code into the pop-up window. (The first time I corrected your message)

2. Now we need the next step: state what we are looking for: the last three deals?

 
Vladimir Karputov #:

1.  Please insert the code correctly: when editing a message, press the button        and paste your code into the pop-up window. (The first time I corrected your message)

2. Now we need the next step: state what we are looking for: the last three deals?

so i need to change the function 

 for (uint i=0;i<total;i++)

to 

 

for (uint i=total-1;i>=total-3;i--) 

so that we take only the last 3 deals? and then modify my last if LIKE

 
if (profit<0 && HistoryDealGetDouble(HistoryDealGetTicket(i)-1,DEAL_PROFIT)<0)
          {lose_streak +=1;
          }//if
          else
          {win_streak=true;
          
          }//else 

so that i can count and check if there is a losestreak?

I am a bit confused. Thats where i exactly losing control.

 
Vladimir Karputov #:

1.  Please insert the code correctly: when editing a message, press the button        and paste your code into the pop-up window. (The first time I corrected your message)

2. Now we need the next step: state what we are looking for: the last three deals?

#include <Trade/Trade.mqh>

int EMA_21_M5 = 0; 

double ema_21[];

CTrade trade;
ulong trade_ticket = 0;


int OnInit(){
   //setting up ema21close
   EMA_21_M5 = iMA(_Symbol,_Period, 21, 0, MODE_EMA,PRICE_CLOSE);
   
   ArraySetAsSeries(ema_21, true);
   
   return (INIT_SUCCEEDED);
}

void OnTick()
{
datetime time_local = TimeLocal();
string hoursAndMinutes = TimeToString(time_local,TIME_MINUTES);

 MqlRates PriceInformation[];
 CopyBuffer(EMA_21_M5, 0, 1, 5, ema_21);
   
 ArraySetAsSeries(PriceInformation, true);
   //taking real time data for the symbol we want
 int Data=CopyRates(Symbol(), Period(), 0, Bars(Symbol(), Period()), PriceInformation);
   
 if (PositionSelectByTicket(trade_ticket) == false) trade_ticket = 0;
   


   MqlDateTime STime;
   TimeToStruct(time_local,STime);
   STime.hour=0;
   STime.min=0;
   STime.sec=0;
   datetime time_start_day=StructToTime(STime);
   HistorySelect(time_start_day,time_local+3600*24*3);
   uint total=HistoryDealsTotal();
   ulong ticket=0;
   double price;
   double profit;
   datetime time;
   int lose_streak=0;
   bool win_streak=false;
   long type;
   long entry;
   for (uint i=total-1;i==total-3;i--)
   {
      if(ticket=HistoryDealGetTicket(i)>0)
      {
         profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
         Print("the ticket number is: ",ticket," and the profit is: ",profit);
         if(profit<0)
         {
            lose_streak =1;
         }//inside if
         else if(profit<0 && HistoryDealGetDouble(HistoryDealGetTicket(i)-1,DEAL_PROFIT)<0)
         {
            lose_streak = 2;
         }//else if no1
         else if(profit<0 &&HistoryDealGetDouble(HistoryDealGetTicket(i)-1,DEAL_PROFIT)<0 && HistoryDealGetDouble(HistoryDealGetTicket(i)-2,DEAL_PROFIT)<0)
         {
            lose_streak = 3;
         }//else if no2
         else
         {
            win_streak = true;
            
            Sleep(18000000);
            win_streak=false;
            lose_streak = 0;
         }//else
         
      }//if
   }//for
   
   if ((StringSubstr(hoursAndMinutes,0,5) > "09:00") && (StringSubstr(hoursAndMinutes,0,5)< "13:00")){
     if (ema_21[1] > PriceInformation[1].open && ema_21[1] < PriceInformation[1].close && trade_ticket <= 0 && lose_streak==0){
      //ask is the real time price we are going to have action (buy)
         double ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
         //STOP LOSS Variable
         double sl = ask-50*_Point;
         //TAKE PROFIT Variable
         double tp = ask+50*_Point;
         
         trade.Buy(0.10,//setting up the lottage
         _Symbol,ask,sl,tp,NULL);
         trade_ticket = trade.ResultOrder();
         Sleep(300000);
     }//if
     else if(ema_21[1] > PriceInformation[1].open && ema_21[1] < PriceInformation[1].close && trade_ticket <= 0 && lose_streak==1)
     {
      //ask is the real time price we are going to have action (buy)
         double ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
         //STOP LOSS Variable
         double sl = ask-50*_Point;
         //TAKE PROFIT Variable
         double tp = ask+50*_Point;
         
         trade.Buy(0.20,//setting up the lottage
         _Symbol,ask,sl,tp,NULL);
         trade_ticket = trade.ResultOrder();
         Sleep(300000);
     }//else if no1
    else if(ema_21[1] > PriceInformation[1].open && ema_21[1] < PriceInformation[1].close && trade_ticket <= 0 && lose_streak==2)
    {
      //ask is the real time price we are going to have action (buy)
         double ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
         //STOP LOSS Variable
         double sl = ask-50*_Point;
         //TAKE PROFIT Variable
         double tp = ask+50*_Point;
         
         trade.Buy(0.30,//setting up the lottage
         _Symbol,ask,sl,tp,NULL);
         trade_ticket = trade.ResultOrder();
         Sleep(300000);
    }//else if no2
     else if (ema_21[1]<PriceInformation[1].open && ema_21[1]>PriceInformation[1].close && trade_ticket <= 0 && lose_streak==0 ){
         //ask is the real time price we are going to have action (sell)
         double bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
         //STOP LOSS Variable
         double sl = bid+50*_Point;
         //TAKE PROFIT Variable
         double tp = bid-50*_Point;
         
          trade.Sell(0.10,//setting up the lottage
         _Symbol,bid,sl,tp,NULL);
         trade_ticket = trade.ResultOrder();
         Sleep(300000);
      
   
   }//else if no3
   else if(ema_21[1]<PriceInformation[1].open && ema_21[1]>PriceInformation[1].close && trade_ticket <= 0 && lose_streak==1 )
   {
          //ask is the real time price we are going to have action (sell)
         double bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
         //STOP LOSS Variable
         double sl = bid+50*_Point;
         //TAKE PROFIT Variable
         double tp = bid-50*_Point;
         
          trade.Sell(0.20,//setting up the lottage
         _Symbol,bid,sl,tp,NULL);
         trade_ticket = trade.ResultOrder();
         Sleep(300000);
      
   }//else if no4
   else if(ema_21[1]<PriceInformation[1].open && ema_21[1]>PriceInformation[1].close && trade_ticket <= 0 && lose_streak==2 )
   {
        //ask is the real time price we are going to have action (sell)
         double bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
         //STOP LOSS Variable
         double sl = bid+50*_Point;
         //TAKE PROFIT Variable
         double tp = bid-50*_Point;
         
          trade.Sell(0.30,//setting up the lottage
         _Symbol,bid,sl,tp,NULL);
         trade_ticket = trade.ResultOrder();
         Sleep(300000);
      
   
   }//else if no5
   
   }//if
   
   
   
}//ontick

so this is my whole try makking it work. but it doesnt. any help will be aprecciated

 

Suggestion: Create functions to make analysing and editing the code easier.

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   datetime curDay=iTime(_Symbol,PERIOD_D1,0);
   datetime curTime=iTime(_Symbol,_Period,0);
   static datetime preDay=0,preTime=curTime;
   if(preDay==curDay || preTime==curTime)
      return;
//---
   int losses=HistoryDealGetStreak();
   if(losses==WRONG_VALUE)
     {
      preDay=curDay;
      return;
     }   
//---
   bool tradeTime=IsTradeTime(StartTime,StopTime);
   ulong ticket;
   ENUM_POSITION_TYPE type;
   PositionGetTicketType(ticket,type);
   ENUM_SIGNAL_TYPE signal=Signal();
//---
   if(tradeTime && type==WRONG_VALUE && signal==SIGNAL_BULL)
      if(trade.Buy(LotSize+LotSize*losses))
         preTime=curTime;
//---
   if(type==POSITION_TYPE_BUY && signal==SIGNAL_BEAR)
      trade.PositionClose(ticket);  
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int HistoryDealGetStreak()
  {
//---
   HistorySelect(iTime(_Symbol,PERIOD_D1,0),TimeCurrent());
   int total=HistoryDealsTotal();
   ulong ticket;
   int losses=0;
   for(int i=total-1;i>=0;i--)
     {
      if((ticket=HistoryDealGetTicket(i))>0)
        { 
         if(HistoryDealGetDouble(ticket,DEAL_PROFIT)>0)
            return(WRONG_VALUE);
         losses++;
        }
     }  
//---
   return(losses);
  }   
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool IsTradeTime(int start_time,int stop_time)
  {
   MqlDateTime dt;
   TimeToStruct(TimeCurrent(),dt);
//---
   return(dt.hour>=start_time && dt.hour<stop_time);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void PositionGetTicketType(ulong& ticket,ENUM_POSITION_TYPE& type)
  {
   int total=PositionsTotal();
   if(total==0)
     {
      ticket=type=WRONG_VALUE; 
      return;
     }
   ticket=PositionGetTicket(0);
   type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); 
//---
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
enum ENUM_SIGNAL_TYPE
  {
   SIGNAL_NONE,
   SIGNAL_BULL,
   SIGNAL_BEAR
  };
//---
ENUM_SIGNAL_TYPE Signal()
  {
   double Ema_21[];
   MqlRates Rates[];
   ArraySetAsSeries(Ema_21,true);
   if(CopyBuffer(EMA_21_M5,0,1,1,Ema_21)==-1 || 
      CopyRates(_Symbol,_Period,1,1,Rates)==-1)
      return(SIGNAL_NONE);
//---
   return((Ema_21[0]>Rates[0].open && Ema_21[0]<Rates[0].close)?SIGNAL_BULL:
          (Ema_21[0]<Rates[0].open && Ema_21[0]>Rates[0].close)?SIGNAL_BEAR:SIGNAL_NONE);
  }   
//+------------------------------------------------------------------+
Files:
expert.mq5  5 kb
 
Ernst Van Der Merwe #:

Suggestion: Create functions to make analysing and editing the code easier.

thnx for your reply. i am trying to find out a way to make it work since you commented till now.The things that are missing are:

1)look back to the current days history and count losestreak

2)set up the lotsize of the bet depends on the losestreak that we have

3)if we win dont trade until next day at 9:00 in the morning

4)i want to make it trade only in the candle opening because if it is in atrade that it hits tp or sl and previous candle gives trade signal it takes the trade and thats not very profitable as i have checked it manually during the last months.

Thank's for your time.

Really appreciate it!

Here is my code:

#include <Trade/Trade.mqh>

int EMA_21_M5 = 0; 

double ema_21[];
//needed so that we can trade
CTrade trade;
ulong trade_ticket = 0;


int OnInit(){
   //setting up ema21close
   EMA_21_M5 = iMA(_Symbol,_Period, 21, 0, MODE_EMA,PRICE_CLOSE);
   
   ArraySetAsSeries(ema_21, true);
   
   return (INIT_SUCCEEDED);
}

void OnTick()
{

   // get the local time
   datetime time = TimeLocal();
   //format the time and create a string
   string hoursAndMinutes = TimeToString(time,TIME_MINUTES);
   
   MqlRates PriceInformation[];
   CopyBuffer(EMA_21_M5, 0, 1, 5, ema_21);
   
   ArraySetAsSeries(PriceInformation, true);
   //taking real time data for the symbol we want
   int Data=CopyRates(Symbol(), Period(), 0, Bars(Symbol(), Period()), PriceInformation);
   
   if (PositionSelectByTicket(trade_ticket) == false) trade_ticket = 0;
   
   //setting up the timeline we want our bot to take trades
   if ((StringSubstr(hoursAndMinutes,0,5) > "09:00") && (StringSubstr(hoursAndMinutes,0,5)< "13:00")){
   
      //setting up buy
      if (ema_21[1] > PriceInformation[1].open && ema_21[1] < PriceInformation[1].close && trade_ticket <= 0){
         //ask is the real time price we are going to have action (buy)
         double ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
         //STOP LOSS Variable
         double sl = ask-50*_Point;
         //TAKE PROFIT Variable
         double tp = ask+50*_Point;
         
         trade.Buy(0.10,//setting up the lottage
         _Symbol,ask,sl,tp,NULL);
         trade_ticket = trade.ResultOrder();
         //Sleep(300000);
            
      } 
      //setting up sell
      else if (ema_21[1]<PriceInformation[1].open && ema_21[1]>PriceInformation[1].close && trade_ticket <= 0){
         //bid is the real time price we are going to have action (sell)
         double bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
         //STOP LOSS Variable
         double sl = bid+50*_Point;
         //TAKE PROFIT Variable
         double tp = bid-50*_Point;
         
          trade.Sell(0.10,//setting up the lottage
         _Symbol,bid,sl,tp,NULL);
         trade_ticket = trade.ResultOrder();
         //Sleep(300000);
         
   
   }

   }
}
 
dio1zervas #:

thnx for your reply. i am trying to find out a way to make it work since you commented till now.The things that are missing are:

1)look back to the current days history and count losestreak

2)set up the lotsize of the bet depends on the losestreak that we have

3)if we win dont trade until next day at 9:00 in the morning

4)i want to make it trade only in the candle opening because if it is in atrade that it hits tp or sl and previous candle gives trade signal it takes the trade and thats not very profitable as i have checked it manually during the last months.

Thank's for your time.

Really appreciate it!

Here is my code:

The EA attached to the previous post does all of that ?!


 
Ernst Van Der Merwe #:

The EA attached to the previous post does all of that ?!


ok i made some changes that i needed and the program it seems to be working perfectly. Buy i have one question. Can you explain me why the lot size is going from 0.1 to 0.3 to 0.5 instead of 0.1 to 0.2 to 0.4?

i am trying to make it increase 0.1 each time for the losestreak.

the thing that i changed is the lotsize=0.1 from 0.2 in your programm and i added sl tp + that i dont want the position to close if another signal appears.

 ///
      //ask is the real time price we are going to have action (buy)
         double ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
         //STOP LOSS Variable
         double sl = ask-50*_Point;
         //TAKE PROFIT Variable
         double tp = ask+50*_Point;
   ///
         //bid is the real time price we are going to have action (sell)
         double bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
         //STOP LOSS Variable
         double sl_sell = bid+50*_Point;
         //TAKE PROFIT Variable
         double tp_sell = bid-50*_Point;
               
//---
   if(tradeTime && type==WRONG_VALUE && signal==SIGNAL_BULL)
      if(trade.Buy(0.1+LotSize*losses,_Symbol,ask,sl,tp,NULL))
         preTime=curTime;
//---
   if(tradeTime && type==WRONG_VALUE && signal==SIGNAL_BEAR)
      if(trade.Sell(0.1+LotSize*losses,_Symbol,bid,sl_sell,tp_sell,NULL))
         preTime=curTime;
//---
                                                ///if(type==POSITION_TYPE_BUY && signal==SIGNAL_BEAR)
                                                ///trade.PositionClose(ticket);  
//---
                                                ///if(type==POSITION_TYPE_SELL && signal==SIGNAL_BULL)
                                                ///trade.PositionClose(ticket);   
  }
 

It counts the opening and closing of a losing trade as two losses.

//---
   if(tradeTime && type==WRONG_VALUE && signal==SIGNAL_BULL)
     { 
      double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
      //STOP LOSS Variable
      double sl = ask-50*_Point;
      //TAKE PROFIT Variable
      double tp = ask+50*_Point;
      //---
      if(trade.Buy(LotSize+LotSize*losses/2,_Symbol,ask,sl,tp))
         preTime=curTime;
     }
//---
   if(tradeTime && type==WRONG_VALUE && signal==SIGNAL_BULL)
     {
      //bid is the real time price we are going to have action (sell)
      double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
      //STOP LOSS Variable
      double sl = bid+50*_Point;
      //TAKE PROFIT Variable
      double tp = bid-50*_Point;
      //---
      if(trade.Sell(LotSize+LotSize*losses/2,_Symbol,bid,sl,tp))
         preTime=curTime;
     }
//---
 
Ernst Van Der Merwe #:

It counts the opening and closing of a losing trade as two losses.

i want it to change like 0.1 to 0.2 to 0.4 to 0.8 not 0.1 to 0.2 to 0.3 to 0.4. In some words i want if i lose a trade the next trade try to minimize the days lose to 0. thats why i want to double the lot so that if i win the next trade i will cover the previous los. for example in a 3 losestreak i would have lost 30$ lets say with 0.1 lot first trade and always 5pips sl tp. first lost trade -5 second -10 third -15 so the fourth trade would be or +30 or -30 

Also a maximum number of dayly trades to 4 so i thought of adding one extra parametre in the IF statements: i choosed losses<8 instead of losses<4 since you told me every loss counts for 2.

is that correct?

if(tradeTime && type==WRONG_VALUE && signal==SIGNAL_BULL && losses<8)
if(tradeTime && type==WRONG_VALUE && signal==SIGNAL_BEAR && losses<8)
Reason: