Download MetaTrader 5

Only open position if spread <= x

To add comments, please log in or register
Daniela Bluemel
597
Daniela Bluemel 2016.04.07 09:17 

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?

Alain Verleyen
Moderator
31085
Alain Verleyen 2016.04.07 09:49  
Not possible with the code you posted. Check your real code.
Daniela Bluemel
597
Daniela Bluemel 2016.04.07 10:41  

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 :( 

Alain Verleyen
Moderator
31085
Alain Verleyen 2016.04.07 10:48  
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 ?

Daniela Bluemel
597
Daniela Bluemel 2016.04.07 13:17  

thank you i found out what the problem is.

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

Alain Verleyen
Moderator
31085
Alain Verleyen 2016.04.07 17:06  
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).

Daniela Bluemel
597
Daniela Bluemel 2016.04.07 18:21  

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 

Cuong Truong
4281
Cuong Truong 2016.04.08 01:18  

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;
};
Daniela Bluemel
597
Daniela Bluemel 2016.04.08 06:55  
Hi Cuong i saw your comment but then it was deleted. Have no idea why
Alain Verleyen
Moderator
31085
Alain Verleyen 2016.04.08 12:53  
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
Cuong Truong
4281
Cuong Truong 2016.04.13 11:37  

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

To add comments, please log in or register