<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Forex Software — Protection - Maximum Spread]]></title>
	<link rel="self" href="https://forexsb.com/forum/feed/atom/topic/6212/" />
	<updated>2023-02-24T19:04:31Z</updated>
	<generator>PunBB</generator>
	<id>https://forexsb.com/forum/topic/6212/protection-maximum-spread/</id>
		<entry>
			<title type="html"><![CDATA[Re: Protection - Maximum Spread]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/73711/#p73711" />
			<content type="html"><![CDATA[<p>Is this feature added by default or not yet?</p>]]></content>
			<author>
				<name><![CDATA[stalin]]></name>
				<uri>https://forexsb.com/forum/user/14340/</uri>
			</author>
			<updated>2023-02-24T19:04:31Z</updated>
			<id>https://forexsb.com/forum/post/73711/#p73711</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Protection - Maximum Spread]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/55586/#p55586" />
			<content type="html"><![CDATA[<p>Yes, please consider adding this as a default function on both the individual and Portfolio EA. It will be very useful (and essential).</p>]]></content>
			<author>
				<name><![CDATA[BK777]]></name>
				<uri>https://forexsb.com/forum/user/9994/</uri>
			</author>
			<updated>2019-05-19T10:57:58Z</updated>
			<id>https://forexsb.com/forum/post/55586/#p55586</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Protection - Maximum Spread]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/55296/#p55296" />
			<content type="html"><![CDATA[<p>Hi,</p><p>I need to add this, but I can&#039;t make sense of how to do it.&nbsp; I tried, but am now getting compile errors.&nbsp; The links in the original post possibly showing the additional code in context now seem to be dead.</p><p>Could someone provide clear instructions that a non-programmer can follow, please?&nbsp; Thank you.</p>]]></content>
			<author>
				<name><![CDATA[Dunn]]></name>
				<uri>https://forexsb.com/forum/user/11151/</uri>
			</author>
			<updated>2019-04-25T20:11:22Z</updated>
			<id>https://forexsb.com/forum/post/55296/#p55296</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Protection - Maximum Spread]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/55102/#p55102" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>Roughey wrote:</cite><blockquote><p>i can find that in code. just look at top </p><div class="codebox"><pre><code>#define TRADE_RETRY_COUNT 4
#define TRADE_RETRY_WAIT  100</code></pre></div></blockquote></div><p>Thanks mate maybe I should look a bit harder </p>]]></content>
			<author>
				<name><![CDATA[Michael1]]></name>
				<uri>https://forexsb.com/forum/user/10884/</uri>
			</author>
			<updated>2019-04-11T22:32:36Z</updated>
			<id>https://forexsb.com/forum/post/55102/#p55102</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Protection - Maximum Spread]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/55097/#p55097" />
			<content type="html"><![CDATA[<p>i can find that in code. just look at top </p><div class="codebox"><pre><code>#define TRADE_RETRY_COUNT 4
#define TRADE_RETRY_WAIT  100</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Roughey]]></name>
				<uri>https://forexsb.com/forum/user/10939/</uri>
			</author>
			<updated>2019-04-11T16:35:39Z</updated>
			<id>https://forexsb.com/forum/post/55097/#p55097</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Protection - Maximum Spread]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/55084/#p55084" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>geektrader wrote:</cite><blockquote><p>I´ve advanced the function a bit to retry a few times if spread might possibly change below our threshold. If you want to use that, here it is:</p><div class="codebox"><pre><code>bool IsWithinMaxSpread()
{
    bool WithinMaxSpread = true;

    for (int attempt = 0; attempt &lt; TRADE_RETRY_COUNT; attempt++)
    {
        RefreshRates();
        WithinMaxSpread = true;

        if (Maximum_Spread_Points &gt; 0)
        {
            double spread = NormalizeDouble(((Ask - Bid) / _Point), 0);
            //Need NormalizeDouble here because of rounding errors in MT4 that otherwise occur (confirmed in several backtests).

            if (spread &gt; Maximum_Spread_Points)
            {
                Print(&quot;The current spread of &quot;, DoubleToString(spread, 0), &quot; points, is higher than the maximum allowed of &quot;, DoubleToString(Maximum_Spread_Points, 0), &quot; points. Try &quot;, IntegerToString(attempt + 1), &quot; of &quot;, IntegerToString(TRADE_RETRY_COUNT), &quot;.&quot;);
                WithinMaxSpread = false;
            }
        }
        Sleep(TRADE_RETRY_WAIT);
    }
    
    return(WithinMaxSpread);
}</code></pre></div></blockquote></div><p>Hi Geek, just wondering where in this code can I put how many times to retry the trade? <br />Where can I put how long to sleep for before retrying?</p>]]></content>
			<author>
				<name><![CDATA[Michael1]]></name>
				<uri>https://forexsb.com/forum/user/10884/</uri>
			</author>
			<updated>2019-04-11T00:58:01Z</updated>
			<id>https://forexsb.com/forum/post/55084/#p55084</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Protection - Maximum Spread]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/54116/#p54116" />
			<content type="html"><![CDATA[<p>I´ve advanced the function a bit to retry a few times if spread might possibly change below our threshold. If you want to use that, here it is:</p><div class="codebox"><pre><code>bool IsWithinMaxSpread()
{
    bool WithinMaxSpread = true;

    for (int attempt = 0; attempt &lt; TRADE_RETRY_COUNT; attempt++)
    {
        RefreshRates();
        WithinMaxSpread = true;

        if (Maximum_Spread_Points &gt; 0)
        {
            double spread = NormalizeDouble(((Ask - Bid) / _Point), 0);
            //Need NormalizeDouble here because of rounding errors in MT4 that otherwise occur (confirmed in several backtests).

            if (spread &gt; Maximum_Spread_Points)
            {
                Print(&quot;The current spread of &quot;, DoubleToString(spread, 0), &quot; points, is higher than the maximum allowed of &quot;, DoubleToString(Maximum_Spread_Points, 0), &quot; points. Try &quot;, IntegerToString(attempt + 1), &quot; of &quot;, IntegerToString(TRADE_RETRY_COUNT), &quot;.&quot;);
                WithinMaxSpread = false;
            }
        }
        Sleep(TRADE_RETRY_WAIT);
    }
    
    return(WithinMaxSpread);
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[geektrader]]></name>
				<uri>https://forexsb.com/forum/user/1841/</uri>
			</author>
			<updated>2019-01-24T06:21:57Z</updated>
			<id>https://forexsb.com/forum/post/54116/#p54116</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Protection - Maximum Spread]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/53886/#p53886" />
			<content type="html"><![CDATA[<p>In case you can add this to the default code that EA Studio exports, I´ve written an implementation that follows your coding style (I hope, haha) that you can use. Add this:</p><p>Input parameters section:</p><div class="codebox"><pre><code>input int           Maximum_Spread_Points = 0;</code></pre></div><p>To the OpenPosition function (need to add it there as we want to be able to tell if the EA tried to place a trade during the time the spread was to high or not):</p><div class="codebox"><pre><code>void OpenPosition(int command)
{
    if (!IsWithinMaxSpread())
        return;

        [EXISTING CODE FROM HERE ON]</code></pre></div><p>And a new function at the end of the MQ4 code:</p><div class="codebox"><pre><code>bool IsWithinMaxSpread()
{
    bool WithinMaxSpread = true;

    if (Maximum_Spread_Points &gt; 0)
    {
        double spread = NormalizeDouble(((Ask - Bid) / _Point), 0);
        //Need NormalizeDouble here because of rounding errors in MT4 that otherwise occur (confirmed in several backtests).

        if (spread &gt; Maximum_Spread_Points)
        {
            Print(&quot;The current spread of &quot;, DoubleToString(spread, 0), &quot; points, is higher than the maximum allowed of &quot;, DoubleToString(Maximum_Spread_Points, 0), &quot; points. Skipping trade!&quot;);
            WithinMaxSpread = false;
        }
    }

    return(WithinMaxSpread);
}</code></pre></div><br /><p>Example validation-output (backtest and live tested)</p><div class="codebox"><pre><code>2019.01.17 01:19:39.190    2018.09.26 05:00:00  10000000 SuperStrat GBPJPY H1 GBPJPY,H1: The current spread of 7 points, is higher than the maximum allowed of 6 points. Skipping trade!
2019.01.17 01:19:39.190    2018.09.25 13:00:00  10000000 SuperStrat GBPJPY H1 GBPJPY,H1: The current spread of 7 points, is higher than the maximum allowed of 6 points. Skipping trade!
2019.01.17 01:19:39.189    2018.09.24 04:00:00  10000000 SuperStrat GBPJPY H1 GBPJPY,H1: The current spread of 7 points, is higher than the maximum allowed of 6 points. Skipping trade!
2019.01.17 01:19:39.189    2018.09.20 03:00:00  10000000 SuperStrat GBPJPY H1 GBPJPY,H1: The current spread of 7 points, is higher than the maximum allowed of 6 points. Skipping trade!
2019.01.17 01:19:39.189    2018.09.19 18:00:00  10000000 SuperStrat GBPJPY H1 GBPJPY,H1: The current spread of 7 points, is higher than the maximum allowed of 6 points. Skipping trade!</code></pre></div><p>Note that &quot;Maximum_Spread_Points&quot; is default at 0, which means the protection is disabled. Only if it is &gt; 0, it will be used. This way you can default turn it off by leaving it at 0 if freshly exported from EA Studio and users can turn it on only if they really need it ;-)</p><p>By the amount of views this forum thread has (it is the second most viewed after &quot;auto lot size calculation&quot;), I can tell that people <strong>really seem to be looking for this feature in EA Studio &quot;out of the box&quot;</strong> and I´d say it´s a feature that every serious trader really NEEDS to use to protect from &quot;catastrophic&quot; entries that can kill the account in the worst case. if you just think Brexit / black swan events (CHF comes to mind) where we had spreads &gt; 500 pips at times, it would be fatal to enter a trade at that spread and, if trading bigger amounts, can cost you a few thousand dollars or more quickly as right when you enter the trade you will be at -500 pips loss. And if that happens, <strong>EA Studio´s MT4 code will also not be able to set the stop loss (in case it´s &lt; 500 pips) and the trade would go completely uncontrolled from there, not even having a stop loss (and this, by the way, is another feature you should think about: if the EA is unable to set a stop loss to a just opened position after say 3 times, it should close it right away as something BAD just happened - I have that in my own EAs all the time. Yes, one might think I am paranoid, but it´s a bit like driving without a seat belt, usually you never need it, but that one time you didn´t use it, can cost your life, or in this case, your whole account).</strong> Hence I think that the spread protection feature should be a *standard* in any serious platform / for any serious trader that does not just trade on demo.</p>]]></content>
			<author>
				<name><![CDATA[geektrader]]></name>
				<uri>https://forexsb.com/forum/user/1841/</uri>
			</author>
			<updated>2019-01-16T23:53:45Z</updated>
			<id>https://forexsb.com/forum/post/53886/#p53886</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Protection - Maximum Spread]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/53877/#p53877" />
			<content type="html"><![CDATA[<p>Hi Popov,</p><p>don´t you think it would make sense to add such a spread-protection to the exported MT4 / MT5 code by default? You could set it to &quot;0&quot; as default (disabled) or 99999, so only people that want to use it can use it. I think that´s more new-user-friendly than having them modify the code on their own, as especially new users are not comfortable with that and just want a working strategy exported, but still will need spread-protection as it surely makes no sense to open a trade if the backtest was done at 1 pip spread and then there is a spread spike to 20 pips (for whatever reason) and their order gets messed up right from the start as it enters at such a high spread.</p><p>In terms of usability and to get more subscriptions, that really might be a good idea - keeping it easy (but expendable for experienced users) is always the best way to get as many users as possible.</p>]]></content>
			<author>
				<name><![CDATA[geektrader]]></name>
				<uri>https://forexsb.com/forum/user/1841/</uri>
			</author>
			<updated>2019-01-16T21:49:09Z</updated>
			<id>https://forexsb.com/forum/post/53877/#p53877</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Protection - Maximum Spread]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/52423/#p52423" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>bisentim wrote:</cite><blockquote><p>Hi All,</p><p>Has this Portfolio spread protection code by ChristianS been tested for performance, anyone??</p><div class="quotebox"><cite>ChristianS wrote:</cite><blockquote><p>Great, thanks for your input.</p><p>So for the EA studio portfolio we can insert for maximum spread this solution:</p><div class="codebox"><pre><code>static input int    Maximum_Spread     = 25; // Maximum Spread (points)</code></pre></div><p>and add in the function void ManageSignal(Signal &amp;signal)</p><div class="codebox"><pre><code>Position position=CreatePosition(signal.MagicNumber);
   if(position.Type==OP_FLAT &amp;&amp; signal.Scope==ORDER_SCOPE_ENTRY)
     {
       //Add protection maximum spread
      if(Ask-Bid&gt;_Point*Maximum_Spread)
      return; 
      
      
      if(signal.Direction==ORDER_DIRECTION_BUY ||
         signal.Direction==ORDER_DIRECTION_SELL)
         if (OrdersTotal()&lt;MaxTrades) {
         OpenPosition(signal);}
     }</code></pre></div></blockquote></div></blockquote></div><br /><p>This should not work correctly. Sometimes it is opening trades.<br />I put it into this code section and it is working.</p><div class="codebox"><pre><code>void OpenPosition(Signal &amp;signal)
  {
       //Add protection maximum spread
      if(Ask-Bid&gt;_Point*Maximum_Spread)
      return; </code></pre></div>]]></content>
			<author>
				<name><![CDATA[deadlef]]></name>
				<uri>https://forexsb.com/forum/user/9894/</uri>
			</author>
			<updated>2018-10-01T18:20:54Z</updated>
			<id>https://forexsb.com/forum/post/52423/#p52423</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Protection - Maximum Spread]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/49989/#p49989" />
			<content type="html"><![CDATA[<p>Hi All,</p><p>Has this Portfolio spread protection code by ChristianS been tested for performance, anyone??</p><div class="quotebox"><cite>ChristianS wrote:</cite><blockquote><p>Great, thanks for your input.</p><p>So for the EA studio portfolio we can insert for maximum spread this solution:</p><div class="codebox"><pre><code>static input int    Maximum_Spread     = 25; // Maximum Spread (points)</code></pre></div><p>and add in the function void ManageSignal(Signal &amp;signal)</p><div class="codebox"><pre><code>Position position=CreatePosition(signal.MagicNumber);
   if(position.Type==OP_FLAT &amp;&amp; signal.Scope==ORDER_SCOPE_ENTRY)
     {
       //Add protection maximum spread
      if(Ask-Bid&gt;_Point*Maximum_Spread)
      return; 
      
      
      if(signal.Direction==ORDER_DIRECTION_BUY ||
         signal.Direction==ORDER_DIRECTION_SELL)
         if (OrdersTotal()&lt;MaxTrades) {
         OpenPosition(signal);}
     }</code></pre></div></blockquote></div>]]></content>
			<author>
				<name><![CDATA[bisentim]]></name>
				<uri>https://forexsb.com/forum/user/8245/</uri>
			</author>
			<updated>2018-04-04T07:35:44Z</updated>
			<id>https://forexsb.com/forum/post/49989/#p49989</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Protection - Maximum Spread]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/49858/#p49858" />
			<content type="html"><![CDATA[<p>The &quot;return;&quot; in you second code must be inside the braces.</p>]]></content>
			<author>
				<name><![CDATA[Popov]]></name>
				<uri>https://forexsb.com/forum/user/2/</uri>
			</author>
			<updated>2018-03-27T20:33:04Z</updated>
			<id>https://forexsb.com/forum/post/49858/#p49858</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Protection - Maximum Spread]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/49857/#p49857" />
			<content type="html"><![CDATA[<p>Something is wrong here.</p><p>First i set Max Spread to 50. The Gold Spread currently is 200.<br /><a href="https://postimg.org/image/p7oumqu6l/"><span class="postimg"><img src="https://s14.postimg.org/p7oumqu6l/set1.png" alt="https://s14.postimg.org/p7oumqu6l/set1.png" /></span></a><br />Now the results. Here looks okay.<br /><a href="https://postimg.org/image/ytif2kvhp/"><span class="postimg"><img src="https://s14.postimg.org/ytif2kvhp/res1.png" alt="https://s14.postimg.org/ytif2kvhp/res1.png" /></span></a></p><p>Now i set Max Spread to 300. The Gold Spread currently is 200.<br /><a href="https://postimg.org/image/5ecqtmoe5/"><span class="postimg"><img src="https://s14.postimg.org/5ecqtmoe5/set2.png" alt="https://s14.postimg.org/5ecqtmoe5/set2.png" /></span></a></p><p>And now there is something wrong. Now it must trade but this is in journal and make no trades.<br />And this i have checked with EURUSD and other pairs.<br /><a href="https://postimg.org/image/eywdgkqlp/"><span class="postimg"><img src="https://s14.postimg.org/eywdgkqlp/res2.png" alt="https://s14.postimg.org/eywdgkqlp/res2.png" /></span></a></p>]]></content>
			<author>
				<name><![CDATA[deadlef]]></name>
				<uri>https://forexsb.com/forum/user/9894/</uri>
			</author>
			<updated>2018-03-27T20:14:46Z</updated>
			<id>https://forexsb.com/forum/post/49857/#p49857</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Protection - Maximum Spread]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/49856/#p49856" />
			<content type="html"><![CDATA[<p>Your code is correct. This is the way MT provide Bid and Ask prices needed for calculating the real spread.</p>]]></content>
			<author>
				<name><![CDATA[Popov]]></name>
				<uri>https://forexsb.com/forum/user/2/</uri>
			</author>
			<updated>2018-03-27T19:49:23Z</updated>
			<id>https://forexsb.com/forum/post/49856/#p49856</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Protection - Maximum Spread]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/49855/#p49855" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>deadlef wrote:</cite><blockquote><p>This is working for mt4 but the mt5 code not work. can you check with a print function</p></blockquote></div><p>Just checked again if i add this to mq5 file.<br /></p><div class="codebox"><pre><code> MqlTick tick;
   SymbolInfoTick(_Symbol,tick);
   if(tick.ask-tick.bid&gt;_Point*Maximum_Spread)
      return;</code></pre></div><p>it looks working well..but when i add this with a comment in journal<br /></p><div class="codebox"><pre><code> MqlTick tick;
   SymbolInfoTick(_Symbol,tick);
   if(tick.ask-tick.bid&gt;_Point*Maximum_Spread)
{
Print(&quot;The current spread is higher than the maximum allowed.&quot;);
}
return;</code></pre></div><p>it doesnt work..can someone help me out</p>]]></content>
			<author>
				<name><![CDATA[deadlef]]></name>
				<uri>https://forexsb.com/forum/user/9894/</uri>
			</author>
			<updated>2018-03-27T19:45:55Z</updated>
			<id>https://forexsb.com/forum/post/49855/#p49855</id>
		</entry>
</feed>
