<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Forum Pasja Informatyki - Najnowsze z tagiem mql</title>
<link>https://forum.pasja-informatyki.pl/tag/mql</link>
<description>Powered by Question2Answer</description>
<item>
<title>bot do handlu w mql otwiera za dużo pozycji.</title>
<link>https://forum.pasja-informatyki.pl/598186/bot-do-handlu-w-mql-otwiera-za-duzo-pozycji</link>
<description>

&lt;p&gt;witam . Ostatnio trochę się bawię i postanowiłem napisać bota do handlu na giełdzie w MQL. mam problem ponieważ otwiera mi pozycje co 15 min. nie wiem jak to zmienić, próbowałem używać też ia ale bez wiekszego skutku. Czy ktoś jest w stanie pomoć mi rozwiązać problem albo chociaż nakierować gdzie popełniam błąd ?&amp;nbsp;
&lt;br&gt;
&amp;nbsp;&lt;/p&gt;



&lt;pre class=&quot;brush:plain;&quot;&gt;
//+------------------------------------------------------------------+
//|                                               tet4.mq5          |
//|                        Copyright 2023, Your Name                 |
//+------------------------------------------------------------------+
#property copyright &quot;Copyright 2023, Your Name&quot;
#property version   &quot;1.05&quot;
#include &amp;lt;Trade\Trade.mqh&amp;gt;
#include &amp;lt;Trade\PositionInfo.mqh&amp;gt;
#include &amp;lt;Trade\OrderInfo.mqh&amp;gt;
#include &amp;lt;Trade\HistoryOrderInfo.mqh&amp;gt;
#include &amp;lt;Trade\DealInfo.mqh&amp;gt;
#include &amp;lt;Object.mqh&amp;gt;
#include &amp;lt;StdLibErr.mqh&amp;gt;

input int      FastMA = 50;          // Fast MA period
input int      SlowMA = 200;         // Slow MA period
input int      MA20Period = 20;      // MA 20 period
input int      RSIPeriod = 14;       // RSI period
input int      ADXPeriod = 14;       // ADX period
input int      MACDFastEMAPeriod = 12; // MACD Fast EMA period
input int      MACDSlowEMAPeriod = 26; // MACD Slow EMA period
input int      MACDSignalSMA = 9;    // MACD Signal SMA period
input double   LotSize = 0.1;        // Lot size
input int      Slippage = 3;          // Slippage
input int      StopLossPips = 30;     // Stop Loss in pips
input int      TakeProfitPips = 30;   // Take Profit in pips
input ulong    MagicNumber = 88888;   // Unique EA identifier
input int      MaxPositions = 1;      // Max number of positions per symbol
input bool     EnableLogging = true;  // Enable logging for debugging

string Symbols[] = {&quot;EURUSD&quot;, &quot;GBPUSD&quot;, &quot;USDJPY&quot;, &quot;USDCHF&quot;, &quot;AUDUSD&quot;};
datetime LastBarTime[];

// Structure to store indicators
struct IndicatorHandles {
    int maFastM15;  // Fast MA on M15
    int maSlowM15;  // Slow MA on M15
    int ma20M15;    // 20-period MA on M15
    int rsiM15;     // RSI on M15
    int macdM15;    // MACD on M15
};

// Array to store indicator handles
IndicatorHandles indicators[];

// Trading object
CTrade Trade;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    Trade.SetDeviationInPoints(Slippage);
    ArrayResize(indicators, ArraySize(Symbols));
    ArrayResize(LastBarTime, ArraySize(Symbols));

    for(int i = 0; i &amp;lt; ArraySize(Symbols); i++)
    {
        string symbol = Symbols[i];
        SymbolSelect(symbol, true);

        // Initialize indicators for M15
        indicators[i].maFastM15 = iMA(symbol, PERIOD_M15, FastMA, 0, MODE_SMA, PRICE_CLOSE);
        indicators[i].maSlowM15 = iMA(symbol, PERIOD_M15, SlowMA, 0, MODE_SMA, PRICE_CLOSE);
        indicators[i].ma20M15 = iMA(symbol, PERIOD_M15, MA20Period, 0, MODE_SMA, PRICE_CLOSE);
        indicators[i].rsiM15 = iRSI(symbol, PERIOD_M15, RSIPeriod, PRICE_CLOSE);
        indicators[i].macdM15 = iMACD(symbol, PERIOD_M15, MACDFastEMAPeriod, MACDSlowEMAPeriod, MACDSignalSMA, PRICE_CLOSE);

        LastBarTime[i] = 0;

        // Check if handles are valid
        if(indicators[i].maFastM15 == INVALID_HANDLE || indicators[i].maSlowM15 == INVALID_HANDLE || 
           indicators[i].ma20M15 == INVALID_HANDLE || indicators[i].rsiM15 == INVALID_HANDLE || 
           indicators[i].macdM15 == INVALID_HANDLE)
        {
            Print(&quot;Failed to create indicators for &quot;, symbol);
            return INIT_FAILED;
        }
    }
    return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    for(int i = 0; i &amp;lt; ArraySize(Symbols); i++)
    {
        string symbol = Symbols[i];
        SymbolSelect(symbol, true);

        // Check for new M15 candle
        if(!IsNewBar(symbol, PERIOD_M15)) continue;

        // Check if there are open positions for this symbol
        if (HasOpenPosition(symbol)) 
        {
            if (EnableLogging) Print(&quot;Already open position for &quot;, symbol);
            continue; // If there are open positions, skip opening new ones
        }

        double fastM15 = 0, slowM15 = 0, ma20M15 = 0, rsiM15 = 0, macdM15Main = 0, macdM15Signal = 0;
        if (!GetIndicatorData(i, fastM15, slowM15, ma20M15, rsiM15, macdM15Main, macdM15Signal))
        {
            Print(&quot;Failed to get indicator data for &quot;, symbol);
            continue;
        }

        // Check for consensus among indicators
        ENUM_POSITION_TYPE trend = GetConsensusTrend(fastM15, slowM15, ma20M15, rsiM15, macdM15Main, macdM15Signal);
        
        // If there is a valid trend signal, open a new position
        if (trend != WRONG_VALUE)
        {
            OpenPosition(symbol, trend); // Open position based on trend
            if (EnableLogging) Print(&quot;Opened position for &quot;, symbol, &quot; with trend: &quot;, trend);
        }
        else
        {
            if (EnableLogging) Print(&quot;No clear trend for &quot;, symbol);
        }
    }
}

//+------------------------------------------------------------------+
//| Determine consensus trend                                       |
//+------------------------------------------------------------------+
ENUM_POSITION_TYPE GetConsensusTrend(double fastM15, double slowM15, double ma20M15, double rsiM15, double macdM15Main, double macdM15Signal)
{
    int bullishCount = 0;
    int bearishCount = 0;

    // Check MA crossovers on M15
    if (fastM15 &amp;gt; slowM15) bullishCount++;
    if (fastM15 &amp;lt; slowM15) bearishCount++;

    // Check RSI on M15
    if (rsiM15 &amp;gt; 50) bullishCount++;
    if (rsiM15 &amp;lt; 50) bearishCount++;

    // Check MACD on M15
    if (macdM15Main &amp;gt; macdM15Signal) bullishCount++;
    if (macdM15Main &amp;lt; macdM15Signal) bearishCount++;

    // Determine trend based on consensus
    if (bullishCount &amp;gt; bearishCount &amp;amp;&amp;amp; bullishCount &amp;gt;= 2) // At least 2 indicators agree
    {
        if (EnableLogging) Print(&quot;Consensus trend for pair: BUY&quot;);
        return POSITION_TYPE_BUY;
    }
    else if (bearishCount &amp;gt; bullishCount &amp;amp;&amp;amp; bearishCount &amp;gt;= 2) // At least 2 indicators agree
    {
        if (EnableLogging) Print(&quot;Consensus trend for pair: SELL&quot;);
        return POSITION_TYPE_SELL;
    }

    return WRONG_VALUE; // No clear consensus
}

//+------------------------------------------------------------------+
//| Check for new bar                                               |
//+------------------------------------------------------------------+
bool IsNewBar(string symbol, ENUM_TIMEFRAMES timeframe)
{
    datetime currentTime[];
    if(CopyTime(symbol, timeframe, 0, 1, currentTime) != 1) return false;

    int index = FindSymbolIndex(symbol);
    if(index == -1) return false;

    if(currentTime[0] != LastBarTime[index])
    {
        LastBarTime[index] = currentTime[0];
        return true;
    }
    return false;
}

//+------------------------------------------------------------------+
//| Find the index of the symbol in the Symbols array               |
//+------------------------------------------------------------------+
int FindSymbolIndex(string symbol)
{
    for(int i = 0; i &amp;lt; ArraySize(Symbols); i++)
    {
        if(Symbols[i] == symbol)
            return i;
    }
    return -1; // Not found
}

//+------------------------------------------------------------------+
//| Manage positions                                                |
//+------------------------------------------------------------------+
void ManagePositions(string symbol, ENUM_POSITION_TYPE trend)
{
    // Check if there are open positions for this symbol
    if (HasOpenPosition(symbol)) 
    {
        if (EnableLogging) Print(&quot;Cannot open position on &quot;, symbol, &quot; - Position already open&quot;);
        return; // If there are open positions, do not open a new one
    }

    // Open a new position if the trend is consistent and there are no open positions
    if (trend != WRONG_VALUE &amp;amp;&amp;amp; CountOpenPositions(symbol) &amp;lt; MaxPositions)
    {
        OpenPosition(symbol, trend);
    }
    else
    {
        if (EnableLogging) Print(&quot;Cannot open position on &quot;, symbol, &quot; - Trend: &quot;, trend);
    }
}

//+------------------------------------------------------------------+
//| Check for existing position                                      |
//+------------------------------------------------------------------+
bool HasOpenPosition(string symbol)
{
    for(int i = PositionsTotal() - 1; i &amp;gt;= 0; i--)
    {
        ulong ticket = PositionGetTicket(i);
        if(ticket &amp;gt; 0 &amp;amp;&amp;amp; 
           PositionGetString(POSITION_SYMBOL) == symbol &amp;amp;&amp;amp;
           PositionGetInteger(POSITION_MAGIC) == MagicNumber)
            return true;
    }
    return false;
}

//+------------------------------------------------------------------+
//| Count open positions                                            |
//+------------------------------------------------------------------+
int CountOpenPositions(string symbol)
{
    int count = 0;
    for(int i = PositionsTotal() - 1; i &amp;gt;= 0; i--)
    {
        ulong ticket = PositionGetTicket(i);
        if(ticket &amp;gt; 0 &amp;amp;&amp;amp; PositionGetString(POSITION_SYMBOL) == symbol &amp;amp;&amp;amp; PositionGetInteger(POSITION_MAGIC) == MagicNumber)
            count++;
    }
    return count;
}

//+------------------------------------------------------------------+
//| Open new position                                              |
//+------------------------------------------------------------------+
void OpenPosition(string symbol, ENUM_POSITION_TYPE trend)
{
    double price = trend == POSITION_TYPE_BUY ? SymbolInfoDouble(symbol, SYMBOL_ASK) : SymbolInfoDouble(symbol, SYMBOL_BID);
    double sl = CalculateSL(symbol, trend, price);
    double tp = CalculateTP(symbol, trend, price);

    if(trend == POSITION_TYPE_BUY)
    {
        if(Trade.Buy(LotSize, symbol, price, sl, tp, NULL))
        {
            if (EnableLogging) Print(&quot;Opened BUY position for &quot;, symbol);
        }
        else
        {
            Print(&quot;Failed to open BUY position for &quot;, symbol);
        }
    }
    else
    {
        if(Trade.Sell(LotSize, symbol, price, sl, tp, NULL))
        {
            if (EnableLogging) Print(&quot;Opened SELL position for &quot;, symbol);
        }
        else
        {
            Print(&quot;Failed to open SELL position for &quot;, symbol);
        }
    }
}

//+------------------------------------------------------------------+
//| Calculate Stop Loss                                             |
//+------------------------------------------------------------------+
double CalculateSL(string symbol, ENUM_POSITION_TYPE trend, double entryPrice)
{
    double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
    double slDistance = StopLossPips * 10 * point; // 30 pips

    return trend == POSITION_TYPE_BUY ? entryPrice - slDistance : entryPrice + slDistance;
}

//+------------------------------------------------------------------+
//| Calculate Take Profit                                           |
//+------------------------------------------------------------------+
double CalculateTP(string symbol, ENUM_POSITION_TYPE trend, double entryPrice)
{
    double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
    double tpDistance = TakeProfitPips * 10 * point; // 30 pips

    return trend == POSITION_TYPE_BUY ? entryPrice + tpDistance : entryPrice - tpDistance;
}

//+------------------------------------------------------------------+
//| Get indicator data                                             |
//+------------------------------------------------------------------+
bool GetIndicatorData(int index, double &amp;amp;fastM15, double &amp;amp;slowM15, double &amp;amp;ma20M15, double &amp;amp;rsiM15, double &amp;amp;macdM15Main, double &amp;amp;macdM15Signal)
{
    double fastM15Buffer[], slowM15Buffer[], ma20M15Buffer[], rsiM15Buffer[], macdM15Buffer[], macdM15SignalBuffer[];

    if(CopyBuffer(indicators[index].maFastM15, 0, 0, 1, fastM15Buffer) != 1 ||
       CopyBuffer(indicators[index].maSlowM15, 0, 0, 1, slowM15Buffer) != 1 ||
       CopyBuffer(indicators[index].ma20M15, 0, 0, 1, ma20M15Buffer) != 1 ||
       CopyBuffer(indicators[index].rsiM15, 0, 0, 1, rsiM15Buffer) != 1 ||
       CopyBuffer(indicators[index].macdM15, 0, 0, 1, macdM15Buffer) != 1 ||
       CopyBuffer(indicators[index].macdM15, 1, 0, 1, macdM15SignalBuffer) != 1)
    {
        return false;
    }

    fastM15 = fastM15Buffer[0];
    slowM15 = slowM15Buffer[0];
    ma20M15 = ma20M15Buffer[0];
    rsiM15 = rsiM15Buffer[0];
    macdM15Main = macdM15Buffer[0];
    macdM15Signal = macdM15SignalBuffer[0];

    return true;
}

//+------------------------------------------------------------------+&lt;/pre&gt;</description>
<category>Inne języki</category>
<guid isPermaLink="true">https://forum.pasja-informatyki.pl/598186/bot-do-handlu-w-mql-otwiera-za-duzo-pozycji</guid>
<pubDate>Wed, 05 Mar 2025 17:30:37 +0000</pubDate>
</item>
<item>
<title>MQL - opisanie alertów aby wyswietłały się tylko raz</title>
<link>https://forum.pasja-informatyki.pl/313891/mql-opisanie-alertow-aby-wyswietlaly-sie-tylko-raz</link>
<description>Nie wiem czy ktoś tutaj się tym zajmuję ale potrzebuję pomocy przy oprogramowaniu alertów. Nie wiem jak to zrobić aby wyświetlały się tylko podczas zamknięcia danej świecy a nie przez cały czas kiedy świeca się tworzy. Jeżeli ktoś bawi się w tym języku i chciałby pomóc będę wdzięczny.</description>
<category>Inne języki</category>
<guid isPermaLink="true">https://forum.pasja-informatyki.pl/313891/mql-opisanie-alertow-aby-wyswietlaly-sie-tylko-raz</guid>
<pubDate>Wed, 27 Dec 2017 19:14:03 +0000</pubDate>
</item>
</channel>
</rss>