"if else" syntax problem- advice appreciated

 

Hi Guys,

First of all, I want to thank you all for contributing on this fine forum.

I'm writing my own EA, but I still haven't mastered mql4... this problem is very easy to solve for a beginner coder :) :


The idea of this function is not to trade, when last trade was closed with a loss: so a()=0 - don't trade, a()=1 -trade.



bool a()
{
if (OrdersHistoryTotal()==0)

return(1); //coz you have to trade if there are no trades ;)
else
if (OrdersHistoryTotal()>0)
{
OrderSelect(OrdersHistoryTotal(), SELECT_BY_POS, MODE_HISTORY);
if(OrderProfit()<0)
return (0);
else
if (OrderProfit()>0)
return (1);
}

}


With this code the EA opens only one trade and thats all, so I'm making some sort of syntax error in the " if (OrdersHistoryTotal()>0)" block.

I would be grateful for help,

happy pip'n :)

Tom

 

bool _previousOrderMadeProfit = false;

bool DidPreviousOrderMadeProfit()

{

bool result = false;

if(_previousOrderMadeProfit == true)

result = previousOrderMadeProfit;

return(result);

}

Greets JB

 
ziebol1:

Hi Guys,

First of all, I want to thank you all for contributing on this fine forum.

I'm writing my own EA, but I still haven't mastered mql4... this problem is very easy to solve for a beginner coder :) :


The idea of this function is not to trade, when last trade was closed with a loss: so a()=0 - don't trade, a()=1 -trade.



bool a()
{
if (OrdersHistoryTotal()==0)

return(1); //coz you have to trade if there are no trades ;)
else
if (OrdersHistoryTotal()>0)
{
OrderSelect(OrdersHistoryTotal(), SELECT_BY_POS, MODE_HISTORY);
if(OrderProfit()<0)
return (0);
else
if (OrderProfit()>0)
return (1);
}

}


With this code the EA opens only one trade and thats all, so I'm making some sort of syntax error in the " if (OrdersHistoryTotal()>0)" block.

I would be grateful for help,

happy pip'n :)

Tom


Can't remember how the history is indexed off the top of my head. Let's assume it is unordered for now. So you should store the last order ticket in the LastTicket variable and access it that way.

Your check for profit is up the creek as you've omitted the case where profit equals zero. Assuming you only wish to trade if you've made a tangible profit, the function should indicate a profit if greater than zero and no profit in all other cases.

And you are making life hard for yourself by unnecessarily complicating the logic in the if, else statements.

You've also got spaces where they shouldn't be - in the return statements - does this compile cleanly?

Tom, sorry mate, but there's really not much right with this particular chunk of code.

Here's how simple it could be:


bool a()
 {
  if (OrdersHistoryTotal()==0) 
   return(1);
  OrderSelect(LastTicket, SELECT_BY_TICKET, MODE_HISTORY);
  if (OrderProfit()>0)
   return(1); 
  return(0);

 }

 

Thanks guys, that was quick.

I still have to learn a lot.

Thanks again!

Tom

 
ziebol1:

Thanks guys, that was quick.

I still have to learn a lot.

Thanks again!

Tom

No problem. Happy enough to help someone who wants to make the effort. Remember to let us know which (if any) of the solutions work for you.

Reason: