Only open position if spread <= x

 

Hi is it possible to only open a position when spread for example is less than 5?

I tried that with 

if(SymbolInfoInteger(Symbol(), SYMBOL_SPREAD) <= 5){
    buy();
}

and in buy() i do

Print(SymbolInfoInteger(Symbol(), SYMBOL_SPREAD));

and it shows me also values higher than 5


Does anyone know what is wrong?

 
Not possible with the code you posted. Check your real code.
 

Alain you're right sorry.

This is my code:

bool checkBuySignal(){
   if(active && (activeTrades <= maxPositions) && (SymbolInfoInteger(Symbol(), SYMBOL_SPREAD) <= maxSpread) && (TimeLocal() > TimeStampLastTrade+40)){
      return true;
   } else {
      return false;
   }
}

extern int maxSpread is 5 

It returns true and prints current spread of 7

 

But you're right in the situation i posted above it works all right. Don't understand :( 

 
Top-SecreT:

Alain you're right sorry.

This is my code:

extern int maxSpread is 5 

It returns true and prints current spread of 7

 

But you're right in the situation i posted above it works all right. Don't understand :( 

I still can't help you as you don't post all the relevant code.

Are you sure maxSpread is 5 at the time this code is executed ?

 

thank you i found out what the problem is.

the spread changes between the statement and the ordersend (within 1 tick)

 
Top-SecreT:

thank you i found out what the problem is.

the spread changes between the statement and the ordersend (within 1 tick)

That's not possible for the spread to change within the same tick.

If you check the spread AFTER the ordersend, there is a great chance you missed some tick(s).

 

aaaaaaaaaaah i see my mistake...

at first i checked only AFTER the order. so i thought it would be send on false spread

for debugging i now added a print line before order :D 

 

I have no idea why someone would delete my code?

 Well, here it is again.

if( (MarketInfo(NULL,MODE_SPREAD) < 5))
{
    MarketInfo(NULL,MODE_TRADEALLOWED);
    return;
};
 
Hi Cuong i saw your comment but then it was deleted. Have no idea why
 
Cuong Truong:

I have no idea why someone would delete my code?

 Well, here it is again.

This code is senseless, but you are insisting so we will keep it :-D
 

I wanted to use this code:

if( (!MarketInfo(NULL,MODE_SPREAD) < 5))
{
    MarketInfo(NULL,MODE_TRADENOTALLOWED);
    return;
};

 Too bad, the MQL4 does not have the build in function of MODE_TRADENOTALLOWED

Reason: