Binary EA buy on new candle - page 2

 
funkynation: i think the problem is by finding the last lost trade?
for(int i=(OrdersHistoryTotal()-1);i>=0;i--)
 {
   result=OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
Where is this code to you look at the OrderCloseTime to verify that it's the last.
 
int checkforlosses()
  {
   static int losses=0;
   static double order_array[][3];
   static int history_total=0;
   if(history_total!=OrdersHistoryTotal())
     {
      int as=0;
      for(int x=OrdersHistoryTotal()-1;x>=0;x--)
        {
         if(OrderSelect(x,SELECT_BY_POS,MODE_HISTORY)
            && OrderMagicNumber()==MagicNumber
            && OrderSymbol()==Symbol()
            && (OrderType()==OP_BUY || OrderType()==OP_SELL))
           {
            as++;
            ArrayResize(order_array,as);
            order_array[as-1][0]=(double)OrderCloseTime();
            order_array[as-1][1]=OrderProfit();
            order_array[as-1][2]=OrderLots();
           }
        }
      if(as>0)
        {
         ArraySort(order_array,WHOLE_ARRAY,0,MODE_ASCEND);
         losses=0;
         for(int x=as-1;x>=0;x--)
           {
            if(order_array[x][1]>=0)
               break;
            else
               losses++;
           }
        }
     }
   return(losses);
  }

Here is one of my functions that I have modified slightly so it should do as you want.

I have not tested it after modifying it 

 
lol i think ive found the reason why it doesnt work..

im using a binary plugin on mt4 (clmforex) and every closed trade gives always profit 0, if its a winner/loser it does it by the balance.

is tried to solve it by the balance, but there is the problem to filter for different symbols... its maybe possible by writing an ea per tf and currencypair.

int checkforlosses2()
{
if (tradecount == 0) 
{ 
double Balance1 =AccountBalance();
if (Balance1 < Balance0) 
{
double losses =1;
}
else
{
losses =0;
tradecount =0;
}
}
if (tradecount == 1) 
{ 
double Balance2 =AccountBalance();
if (Balance2 < Balance1) 
{
losses++;
}
else
{
losses =0;
tradecount =0;
}
}
if (tradecount == 2) 
{ 
double Balance3 =AccountBalance();
if (Balance3 < Balance3) 
{
losses++;
}
else
{
losses =0;
tradecount =0;
}
}
return(losses);
}

ive seen that its posting a comment to every closed order if its profitable or not.

when a trade is closed there is 0 profit, and on the new line the profit is shown by the balance.

is there a way to loop through the comments of closed orders with OrderComment(); like:

if DOWN/LOSS is in OrderComment then losses++


thanks for your help :)

 

hi guys, today i tried the following code, seems to work, keep you up to date how its performing :)


int start()
{
if (AccountBalance() >= DDAccountbalance)
{
int result=0;
if (TotalOrderCount() > 0)
{
return(0);
}
if (IsNewBar() == true)
{
if (checkforlosses() == 0) double Mylot =Lot1;
if (checkforlosses() == 1) Mylot =Lot2;
if (checkforlosses() == 2) Mylot =Lot3;
if(((UseTradingHours == true) && (TradingHours()== true) && (buycondition() == true)) || ((UseTradingHours == false) && (buycondition() == true)))
{
result=OrderSend(Symbol(),OP_BUY,Mylot,Ask,0,0,0,BOExpiry,MagicNumber,0,Blue);
//tradecount++;
}
if (((UseTradingHours == true) && (TradingHours() == true) && (sellcondition() == true)) || ((UseTradingHours == false) && (sellcondition() == true)))
{
result=OrderSend(Symbol(),OP_SELL,Mylot,Bid,0,0,0,BOExpiry,MagicNumber,0,Red);
//tradecount++
}
}
}
else if (AccountBalance() <= DDAccountbalance)
{
Comment(EAName, ">>> MAX DD on Accountbalanced reached, STOPPED trading!!");
}
return(0);
}

int checkforlosses()
{
    int losses =0;
    int result =0;
    int last_order_type =-1;
    datetime last_order_time=0;
    for(int i=(OrdersHistoryTotal()-1);i>=0;i--)
     {
      result=OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         if(OrderCloseTime()>last_order_time)
            {
            last_order_time=OrderCloseTime();
            last_order_type=OrderType();
            string order_comment =OrderComment();
            if(StringFind(order_comment,"WIN",0)>0) losses =0;
            if(StringFind(order_comment,"DRAW",0)>0) losses =0;
            if(StringFind(order_comment,"LOSS",0)>0) losses++;
            }
        }
     }
    return(losses);
}
 

hey guys, optimazed little bit, works now pretty good, thanks for your help :)

bool checkforlosses()
{
    int result =0;
    int last_order_type =-1;
    datetime last_order_time=0;
    for(int i=(OrdersHistoryTotal()-1);i>=0;i--)
     {
      result=OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         if(OrderCloseTime()>last_order_time)
            {
            last_order_time=OrderCloseTime();
            last_order_type=OrderType();
            string last_order_symbol=Symbol();
            string order_comment =OrderComment();
            if(StringFind(order_comment,"WIN",0)>0) losses =0; itms++;
            if(StringFind(order_comment,"DRAW",0)>0) losses =0; draws++;
            if(StringFind(order_comment,"LOSS",0)>0) losses++; otms++;
            }
        }
     }
    if (losses == 0) Mylot =Lot1;
    if (losses == 1) Mylot =Lot2;
    if (losses == 2) Mylot =Lot3;
    if (losses == 3) Mylot =Lot4;
    if (losses == 4) Mylot =Lot5;
    if (losses == 5) Mylot =Lot1;
    Print(EAName,">>> Last Order:", last_order_symbol," Losses=",losses);
    return(true);
}
 
Hello, sorry for the mistakes in writing, I am Brazilian, but when I try to run this code, the compiler displays the following error: 'result' undeclared identifier. Is it possible you put the whole code? I use Metatrader 4.
Thanks in advance.
 

optimized a little bit, works now pretty good, but the print part at the end is always executed 6 times... why that? do i have to add a break or continue? thanks for your response :)

bool checkforlosses()
{
    int result =0;
    int last_order_type =-1;
    datetime last_order_time=0;
    for(int i=(OrdersHistoryTotal()-1);i>=0;i--)
     {
      result=OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         if(OrderCloseTime()>last_order_time)
          {
            last_order_time=OrderCloseTime();
            last_order_type=OrderType();
            string last_order_symbol=Symbol();
            string order_comment =OrderComment();
            if(StringFind(order_comment,"WIN",0)>0) 
              {
              losses =0;
              if(StringFind(order_comment,DoubleToString(Lot1 * Payout,2),0)>0) lot1win++; Dailyprofit+= Lot1 * Payout; Totalprofit+= Lot1 * Payout;
              if(StringFind(order_comment,DoubleToString(Lot2 * Payout,2),0)>0) lot2win++; Dailyprofit+= Lot2 * Payout; Totalprofit+= Lot2 * Payout;
              if(StringFind(order_comment,DoubleToString(Lot3 * Payout,2),0)>0) lot3win++; Dailyprofit+= Lot3 * Payout; Totalprofit+= Lot3 * Payout;
              if(StringFind(order_comment,DoubleToString(Lot4 * Payout,2),0)>0) lot4win++; Dailyprofit+= Lot4 * Payout; Totalprofit+= Lot4 * Payout;
              if(StringFind(order_comment,DoubleToString(Lot5 * Payout,2),0)>0) lot5win++; Dailyprofit+= Lot5 * Payout; Totalprofit+= Lot5 * Payout;
              }
            if(StringFind(order_comment,"DRAW",0)>0)
              {
              losses =0; 
              draws++;
              }
            if(StringFind(order_comment,"LOSS",0)>0)
              {
              losses++;
              if(StringFind(order_comment,DoubleToString(Lot1,2),0)>0) lot1loss++; Dailyprofit-= Lot1; Totalprofit-= Lot1;
              if(StringFind(order_comment,DoubleToString(Lot2,2),0)>0) lot2loss++; Dailyprofit-= Lot2; Totalprofit-= Lot2;
              if(StringFind(order_comment,DoubleToString(Lot3,2),0)>0) lot3loss++; Dailyprofit-= Lot3; Totalprofit-= Lot3;
              if(StringFind(order_comment,DoubleToString(Lot4,2),0)>0) lot4loss++; Dailyprofit-= Lot4; Totalprofit-= Lot4;
              if(StringFind(order_comment,DoubleToString(Lot5,2),0)>0) lot5loss++; Dailyprofit-= Lot5; Totalprofit-= Lot5;
              }
           }
        }
      }
      if (losses == 0) Mylot =Lot1; Print(EAName,">>> Last Order:", last_order_symbol," Losses=",losses);
      if (losses == 1) Mylot =Lot2; Print(EAName,">>> Last Order:", last_order_symbol," Losses=",losses);
      if (losses == 2) Mylot =Lot3; Print(EAName,">>> Last Order:", last_order_symbol," Losses=",losses);
      if (losses == 3) Mylot =Lot4; Print(EAName,">>> Last Order:", last_order_symbol," Losses=",losses);
      if (losses == 4) Mylot =Lot5; Print(EAName,">>> Last Order:", last_order_symbol," Losses=",losses);     
      if (losses == 5) Mylot =Lot1; Print(EAName,">>> Last Order:", last_order_symbol," Losses=",losses);
      return(true);
}
 

i tried by putting the break after the first lost order was found, but seems that the Dailyprofit and Totalprofit is not counting correctly... I also noticed that when lot5 is used it doesnt reset losses to 0, it counts up,  why that? 

in the documentation is written, that i can set a break and then store the values for the variables like losses etc. but when i put the break on top of the loop it doenst work.

can somone help? thanks in advance :)


            if(StringFind(order_comment,"LOSS",0)>0)              {              losses++;              if(StringFind(order_comment,DoubleToString(Lot1,2),0)>0) lot1loss++; Dailyprofit-= Lot1; Totalprofit-= Lot1;              if(StringFind(order_comment,DoubleToString(Lot2,2),0)>0) lot2loss++; Dailyprofit-= Lot2; Totalprofit-= Lot2;              if(StringFind(order_comment,DoubleToString(Lot3,2),0)>0) lot3loss++; Dailyprofit-= Lot3; Totalprofit-= Lot3;              if(StringFind(order_comment,DoubleToString(Lot4,2),0)>0) lot4loss++; Dailyprofit-= Lot4; Totalprofit-= Lot4;              if(StringFind(order_comment,DoubleToString(Lot5,2),0)>0) lot5loss++; Dailyprofit-= Lot5; Totalprofit-= Lot5;              }           break;            }
 
funkynation:

optimized a little bit, works now pretty good, but the print part at the end is always executed 6 times... why that? do i have to add a break or continue? thanks for your response :)

        if (losses == 2) Mylot =Lot3; Print(EAName,">>> Last Order:", last_order_symbol," Losses=",losses);

hello,

the cause for your issues here are some syntax "errors"; in the line above, you have forgot the braces, e.g. your if ends at "Mylot=Lot3;", after that Print() is executed; on the next line, if is checked (as it is supposed to do) but again the next Print() is executed and so on.

Tip: in C you can put commas (,) to group your expressions, but it does not happen in MQL

Back OT, I assume you latest comment has the same syntax issues, but i am not really sure, as all the source code appears in one big line (at least in my browser) and as such i am not in position to tell what happens

 

best regards 

 
Hi demos thanks for your response,  i will try today with correct syntaxes like if ()  { then }