<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Forex Software — Modification of the function Max_OpenPos = 1; // Max open positions (c]]></title>
	<link rel="self" href="https://forexsb.com/forum/feed/atom/topic/9871/" />
	<updated>2024-10-30T01:02:57Z</updated>
	<generator>PunBB</generator>
	<id>https://forexsb.com/forum/topic/9871/modification-of-the-function-maxopenpos-1-max-open-positions-c/</id>
		<entry>
			<title type="html"><![CDATA[Modification of the function Max_OpenPos = 1; // Max open positions (c]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/81592/#p81592" />
			<content type="html"><![CDATA[<p>The main steps are:</p><p>Modify Position Counting: Change the function that counts positions to consider all open positions in the account, regardless of magic number or symbol.<br />Update Position Opening Check: Ensure that before opening a new position, the EA checks if there is already at least one open position across the entire account.<br />Maintain Compatibility with Other Protections: Ensure that other existing protections in the EA continue to work as expected.</p><p>Modify the Global Position Counting Function</p><div class="codebox"><pre><code>int CountPositions(void)
{
    return PositionsTotal();
}</code></pre></div><br /><p>Update the SetPosStats Function to Reflect Global Counting</p><br /><div class="codebox"><pre><code>void SetPosStats(void)
{
    posStatCount  = CountPositions();
    posStatLots   = 0;
    posStatProfit = 0;

    for(int i = PositionsTotal() - 1; i &gt;= 0; i -= 1)
    {
        const ulong ticket = PositionGetTicket(i);
        if(ticket == 0 || !PositionSelectByTicket(ticket))
            continue;

        posStatLots   += PositionGetDouble(POSITION_VOLUME);
        posStatProfit += PositionGetDouble(POSITION_PROFIT) +
                         PositionGetDouble(POSITION_SWAP);
    }
}</code></pre></div><br /><p>Update the Check in OpenPosition</p><div class="codebox"><pre><code>void OpenPosition(Signal &amp;signal)
{
    entryProtectionMessage = &quot;&quot;;
    const int spread = (int)((Ask() - Bid()) / _Point);
    
    // Verificações de proteção de entrada
    if (Max_OpenPos &gt; sigma &amp;&amp; posStatCount &gt;= Max_OpenPos)
        entryProtectionMessage += StringFormat(&quot;Protection: Max open positions: %d, current: %d\n&quot;,
                                               Max_OpenPos, posStatCount);
    if (Max_OpenLots &gt; sigma &amp;&amp; posStatLots &gt; Max_OpenLots - sigma)
        entryProtectionMessage += StringFormat(&quot;Protection: Max open lots: %.2f, current: %.2f\n&quot;,
                                               Max_OpenLots, posStatLots);
    if (Max_Spread &gt; sigma &amp;&amp; spread &gt; Max_Spread)
        entryProtectionMessage += StringFormat(&quot;Protection: Max spread: %d, current: %d\n&quot;,
                                               Max_Spread, spread);
    if (MaxDailyLoss &gt; sigma &amp;&amp; dailyLoss &gt;= MaxDailyLoss)
        entryProtectionMessage += StringFormat(&quot;Protection: Max daily loss: %d, current: %.2f\n&quot;,
                                               MaxDailyLoss, dailyLoss);
    if (Max_Daily_DD &gt; sigma &amp;&amp; dailyDrawdown &gt;= Max_Daily_DD)
        entryProtectionMessage += StringFormat(&quot;Protection: Max daily drawdown: %.2f%%, current: %.2f%%\n&quot;,
                                               Max_Daily_DD, dailyDrawdown);
    if (GlobalVariableGet(accEntrySuspendGlobalVarName) &gt; sigma)
        entryProtectionMessage += StringFormat(&quot;New entries are suspended until the Daily reset hour: %d&quot;,
                                               Daily_Reset);

    const int newsIndex = NewsFilterActive();
    if (newsIndex &gt; -1)
    {
        const NewsRecord newsRecord = newsRecords[newsIndex];
        const datetime timeShift = (datetime) MathRound((TimeLocal() - TimeGMT()) / 3600.0) * 3600;
        const string priority = newsRecord.priority == &quot;high&quot; ? &quot;[high]&quot; : &quot;[med]&quot;;
        entryProtectionMessage += StringFormat(&quot;News filter: %s %s %s %s\n&quot;,
                                               priority,
                                               TimeToString(newsRecord.time + timeShift,
                                                            TIME_DATE | TIME_MINUTES),
                                               newsRecord.currency,
                                               newsRecord.title);
    }

    if (entryProtectionMessage != &quot;&quot;)
    {
        entryProtectionMessage = TimeToString(TimeCurrent()) + &quot; &quot; +
                                 &quot;An entry order was canceled:\n&quot; +
                                 entryProtectionMessage;
        return;
    }

    // Verifica a direção de negociação antes de executar a ordem
    const int command = OrderDirectionToCommand(signal.Direction);
    
    // Verifica se a direção da negociação é permitida
    if ((tradeDirection == SomenteCompra &amp;&amp; command != OP_BUY) ||
        (tradeDirection == SomenteVenda &amp;&amp; command != OP_SELL))
    {
        // Não prossegue se a direção da negociação não corresponde
        return;
    }

    const double stopLoss = GetStopLossPrice(command, signal.StopLossPips);
    const double takeProfit = GetTakeProfitPrice(command, signal.TakeProfitPips);
    const double posLots = Entry_Amount;

    if (ManageOrderSend(command, posLots, stopLoss, takeProfit, 0, signal.MagicNumber))
    {
        posStatCount += 1;
        posStatLots += posLots;
    }
}</code></pre></div><br /><br /><p>Update the OnTick Function to Reflect the Changes</p><div class="codebox"><pre><code>void OnTick(void)
{
    if(!MQLInfoInteger(MQL_TESTER))
    {
        UpdateAccountProtection();
        CheckAccountProtection();

        const datetime time = TimeCurrent();
        if(time &gt; lastStatsUpdate + 3)
        {
            lastStatsUpdate = time;
            SetPosStats(); // Agora considera todas as posições
            UpdateStats();
        }

        if(time &gt; lastNewsUpdate + 6*60*60 || !isNewsFeedOk)
        {
            lastNewsUpdate = time;
            LoadNews();
        }
    }

    if(IsForceSessionClose())
    {
        CloseAllPositions();
        return;
    }

    const datetime time = Time(0);
    if(time &gt; barTime)
    {
        barTime = time;
        OnBar();
    }
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[gabdecsters]]></name>
				<uri>https://forexsb.com/forum/user/14801/</uri>
			</author>
			<updated>2024-10-30T01:02:57Z</updated>
			<id>https://forexsb.com/forum/post/81592/#p81592</id>
		</entry>
</feed>
