• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 4:05am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 4:05am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Printable Version

Similar Threads

Max trades per magic number code for my EA 3 replies

Max Spread EA? 2 replies

How to code to get MIN and MAX openprice of all "Open Trade" 4 replies

Separating Count of Max Pending Orders and Max Open Trades 2 replies

Max Spread on EURAUD 0 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe

Max Spread Code for EA - Is this correct?

  • Post #1
  • Quote
  • First Post: Jan 22, 2019 6:37pm Jan 22, 2019 6:37pm
  •  Burton
  • | Joined Jan 2008 | Status: Member | 215 Posts
Hello,

Could someone please tell me if this code I have added to an EA to prevent orders being taken if the spread exceeds 2 pips is correct?

All help greatly appreciated

extern double MaxSpread = 2.0;
double spread = MarketInfo(Symbol(), MODE_SPREAD);

then I added this code to the existing Buy/Sell enter orders ( && spread <= MaxSpread )

if (isBuy && !isSell && !(lastType == 1 && spread <= MaxSpread && lastDay == Today())){
if (BuyNow()>0) {

else if(isSell && !isBuy && !(lastType == 2 && spread <= MaxSpread && lastDay == Today())){
if (SellNow()>0) {
  • Post #2
  • Quote
  • Jan 22, 2019 7:30pm Jan 22, 2019 7:30pm
  •  hayseed
  • Joined Nov 2006 | Status: Member | 3,597 Posts
Quoting Burton
Disliked
if (isBuy && !isSell && !(lastType == 1 && spread <= M axSpread&& lastDay == Today())){ if (BuyNow()>0)
Ignored
//-----

hey burton..... having the spread <= MaxSpread inside the ! statement looks incorrect.....

if spread is a major concern, you can put it first then encapsulate everything inside it..... but at least move it outside the not statement......h

//------ not sure what the lastday is about



Inserted Code
if(isBuy  && !isSell && spread <= MaxSpread && !(lastType == 1 && lastDay == Today()))
    
if(isSell && !isBuy  && spread <= MaxSpread && !(lastType == 2 && lastDay == Today()))
to trade and code, keep both simple... no call to impress....h
  • Post #3
  • Quote
  • Jan 22, 2019 7:55pm Jan 22, 2019 7:55pm
  •  arby1108
  • | Joined Dec 2016 | Status: Member | 57 Posts
extern double MaxSpread = 2.0;

if(Digits==3||Digits==5) MaxSpread = MaxSpread *10;

double spread = MarketInfo(Symbol(), MODE_SPREAD);

if(isBuy && !isSell && spread <= MaxSpread && !(lastType == 1 && lastDay == Today()))
if(isSell && !isBuy && spread <= MaxSpread && !(lastType == 2 && lastDay == Today()))
  • Post #4
  • Quote
  • Jan 22, 2019 9:10pm Jan 22, 2019 9:10pm
  •  Burton
  • | Joined Jan 2008 | Status: Member | 215 Posts
Quoting hayseed
Disliked
{quote} //----- hey burton..... having the spread <= MaxSpread inside the ! statement looks incorrect..... if spread is a major concern, you can put it first then encapsulate everything inside it..... but at least move it outside the not statement......h //------ not sure what the lastday is about if(isBuy && !isSell && spread <= MaxSpread && !(lastType == 1 && lastDay == Today())) if(isSell && !isBuy && spread <= MaxSpread && !(lastType == 2 && lastDay == Today()))
Ignored
Thank you hayseed for your quick reply, yes understood, will implement
  • Post #5
  • Quote
  • Jan 22, 2019 9:14pm Jan 22, 2019 9:14pm
  •  Burton
  • | Joined Jan 2008 | Status: Member | 215 Posts
Quoting arby1108
Disliked
extern double MaxSpread = 2.0; if(Digits==3||Digits==5) MaxSpread = MaxSpread *10; double spread = MarketInfo(Symbol(), MODE_SPREAD); if(isBuy && !isSell && spread <= MaxSpread && !(lastType == 1 && lastDay == Today())) if(isSell && !isBuy && spread <= MaxSpread && !(lastType == 2 && lastDay == Today()))
Ignored
Hi arby1108, great, yes 3 & 5 digit code, ok added, thank you for your quick reply, much appreciated
  • Post #6
  • Quote
  • Jan 22, 2019 10:00pm Jan 22, 2019 10:00pm
  •  hayseed
  • Joined Nov 2006 | Status: Member | 3,597 Posts
Quoting Burton
Disliked
Hello,
Ignored
//-----

hey burton..... just noticed i left off the ( ) surrounding the spread part...... that would be better form......h

Inserted Code
if(isBuy && !isSell && (spread <= MaxSpread) && !(lastType == 1 && lastDay == Today()))
to trade and code, keep both simple... no call to impress....h
  • Post #7
  • Quote
  • Jan 23, 2019 1:06am Jan 23, 2019 1:06am
  •  Burton
  • | Joined Jan 2008 | Status: Member | 215 Posts
Quoting hayseed
Disliked
{quote} //----- hey burton..... just noticed i left off the ( ) surrounding the spread part...... that would be better form......h if(isBuy && !isSell && (spread <= MaxSpread) && !(lastType == 1 && lastDay == Today()))
Ignored
Great, thank you hayseed, added
  • Post #8
  • Quote
  • Jan 25, 2019 10:46am Jan 25, 2019 10:46am
  •  ionone
  • Joined Oct 2014 | Status: Member | 424 Posts
int spread = (Ask-Bid)/Point
  • Post #9
  • Quote
  • Edited Jan 26, 2019 11:55am Jan 25, 2019 2:35pm | Edited Jan 26, 2019 11:55am
  •  Nicholishen
  • Joined Jul 2005 | Status: zzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzz | 1,289 Posts
Quoting ionone
Disliked
int spread = (Ask-Bid)/Point
Ignored
Generically speaking this is the right answer, but it needs to be rounded before it's cast to int. Because of the way doubles are stored in memory, the result of (Ask-Bid) /point could result in something like 1.9999999999 and after the implicit cast to int - the result is 1 instead of 2. So this should really be...

Inserted Code
int spread = int(round((Ask - Bid) / _Point));

Note: this is the point-spread not pips.
2
  • Post #10
  • Quote
  • Jan 25, 2019 10:48pm Jan 25, 2019 10:48pm
  •  Burton
  • | Joined Jan 2008 | Status: Member | 215 Posts
Quoting Nicholishen
Disliked
{quote} Generically speaking this is the right answer, but it needs to be rounded before it's cast to int. Because of the way double are stored in memory, the result of (Ask-Bid) /point could result in something like 1.9999999999 and after the implied cast to int the result is 1 instead of 2. So this should really be... int spread = int(round((Ask - Bid) / _Point)); Note this is the point spread not pips.
Ignored
Hello ionone, and Nicholishen for your replies

But now I am confused on what/which code I should be using?

extern double MaxSpread = 2.0;

if(Digits==3||Digits==5) MaxSpread = MaxSpread *10;
double spread = MarketInfo(Symbol(), MODE_SPREAD);

or

int spread = int(round((Ask - Bid) / _Point));

As someone with basic coding abilities could you please give me the total correct code to use?

All help greatly appreciated
  • Post #11
  • Quote
  • Jan 26, 2019 12:19pm Jan 26, 2019 12:19pm
  •  Nicholishen
  • Joined Jul 2005 | Status: zzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzz | 1,289 Posts
Quoting Burton
Disliked
{quote} Hello ionone, and Nicholishen for your replies But now I am confused on what/which code I should be using? extern double MaxSpread = 2.0; if(Digits==3||Digits==5) MaxSpread = MaxSpread *10; double spread = MarketInfo(Symbol(), MODE_SPREAD); or int spread = int(round((Ask - Bid) / _Point)); As someone with basic coding abilities could you please give me the total correct code to use? All help greatly appreciated
Ignored
The MarketInfo function has been deprecated and new MQL programs should use SymbolInfoInteger instead. That being said you could use SYMBOL_SPREAD if you absolutely trust MetaQuotes to release their new terminal upgrades without bugs (LOL). Since you are working with PIPS and not POINTS you need to convert all of your math in your program to account for such. Since spread is the integer value of the number of points you will need to divide by the pip-modifier (instead of multiply). If you want an absolute fail-safe function for getting the spread in pips then you can use the following.

Inserted Code
void OnStart()
{
   double spread;
   if (spread_pips(spread))
      Print(spread);
}
//+------------------------------------------------------------------+
bool spread_pips(double &spread, string symbol=NULL)
{
   if (symbol == NULL)
      symbol = Symbol();
   MqlTick tick;
   if (!SymbolInfoTick(symbol, tick))
      return false;
   int digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
   double pip_mod = SymbolInfoDouble(symbol, SYMBOL_POINT);
   if (digits == 3 || digits == 5)
      pip_mod *= 10;
   spread = NormalizeDouble((tick.ask - tick.bid) / pip_mod, 1);
   return spread >= 0.0;
}
1
  • Post #12
  • Quote
  • Jan 26, 2019 12:50pm Jan 26, 2019 12:50pm
  •  ionone
  • Joined Oct 2014 | Status: Member | 424 Posts
Quoting Nicholishen
Disliked
{quote} The MarketInfo function has been deprecated and new MQL programs should use SymbolInfoInteger instead. That being said you could use SYMBOL_SPREAD if you absolutely trust MetaQuotes to release their new terminal upgrades without bugs (LOL). Since you are working with PIPS and not POINTS you need to convert all of your math in your program to account for such. Since spread is the integer value of the number of points you will need to divide by the pip-modifier (instead of multiply). If you want an absolute fail-safe function for getting the...
Ignored
yeah i don't liek too much relying on data from the broker either, so i try to keep things as sturdy pas possible using only sure functions

Jeff
1
  • Post #13
  • Quote
  • Last Post: Edited Jan 27, 2019 1:56am Jan 26, 2019 2:24pm | Edited Jan 27, 2019 1:56am
  •  Burton
  • | Joined Jan 2008 | Status: Member | 215 Posts
Quoting Nicholishen
Disliked
{quote} The MarketInfo function has been deprecated and new MQL programs should use SymbolInfoInteger instead. That being said you could use SYMBOL_SPREAD if you absolutely trust MetaQuotes to release their new terminal upgrades without bugs (LOL). Since you are working with PIPS and not POINTS you need to convert all of your math in your program to account for such. Since spread is the integer value of the number of points you will need to divide by the pip-modifier (instead of multiply). If you want an absolute fail-safe function for getting the...
Ignored
Thank you, thank you Nicholishen for that solid correct code

POINTS code use: int spread = int(round((Ask - Bid) / _Point));

PIPS code use: void OnStart() ........................... posted above

Your help greatly appreciated
  • Platform Tech
  • /
  • Max Spread Code for EA - Is this correct?
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2021