Having a problem, maybe you can help?

 

I am new to coding MQL4 and having a problem with something... 

I want to assign a variable before OnTick. so I only want to be in 1 trade at a time. something like this.


bool CanTrade = true;


  if(Cantrade==true)

     {

"buy or sell depending on conditions..."

static bool CanTrade = false;

     } 


     then a condition that would exit the trade are reset the bool to true. My problem is I need to define the CanTrade Condition on the first start but not on each tick... Having a hard time with this. If i define it as a global it still dose not recognize it in the OnTick part of the code. I am hoping that this is simple and I am just missing something here.. Thanks for your help.

 
Levi Swan:

My problem is I need to define the CanTrade Condition on the first start but not on each tick... Having a hard time with this. If i define it as a global it still dose not recognize it in the OnTick part of the code. I am hoping that this is simple and I am just missing something here.. Thanks for your help.

static bool CanTrade = true;

void OnTick() {
   if(exit_condition) {
      if(!CanTrade) CanTrade = true; // Reset the CanTrade
   }

   if(Cantrade) {
      // buy or sell depending on conditions..."
      CanTrade = false;
   }
}
 
You can set the variable in OnInit() ?

 

Hello friend,

How about something like this.

#property strict
extern bool CanTrade = true; 
extern int Total_Order =1;

You may want to take into calculations this also.

extern ENUM_TIMEFRAMES    FRAME=0;
 

That suppose you'll be able to detect when the deal will be closed. 

I would have do it so : 

void OnTick()
{
if(PositionsTotal()==0) { ...Trade... }
}
///////
int PositionsTotal()
  {
   int Pos=0;
   for(int i=0;i<OrdersTotal();i++) Pos=(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderType()<=OP_SELL)?Pos+1:Pos;
   return(Pos);
  }

Long time I didn't touch MQL4, but the code seems okay....

Reason: