My Open Position on EA won't close automatically

 

Hey guys,

Im new here in coding, I tried to code together an EAm which simply uses the last 6 Candles, if thosse 6 are down-candles it opens up a long-position. That seems to work, but I cant get my EA to close that position on the next candle.

I would be glad, if theres anyone who can help me on that. I will provide the Code down below:


//+------------------------------------------------------------------+

//|                                            SimpleEALongEURUSD.mq5|

//|                        Copyright 2024, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property strict



//+------------------------------------------------------------------+

//| Expert Advisor Start Function                                    |

//+------------------------------------------------------------------+

void OnTick()

{

    // Überprüfen, ob genügend Kerzen vorhanden sind, um die Bedingung zu prüfen

    if (Bars(_Symbol, _Period) < 7)

        return;



    // Überprüfen, ob die vorherigen sechs Kerzen alle negativ geschlossen haben

    bool condition = true;

    for (int i = 1; i <= 6; i++)

    {

        if (iClose(_Symbol, _Period, i) >= iClose(_Symbol, _Period, i - 1))

        {

            condition = false;

            break;

        }

    }



    // Wenn alle vorherigen sechs Kerzen negativ geschlossen haben

    if (condition)

    {

        // Überprüfen, ob wir bereits eine offene Long-Position haben

        if (!PositionSelect(_Symbol) || (PositionSelect(_Symbol) && PositionGetInteger(POSITION_TYPE) != POSITION_TYPE_BUY))

        {

            // Eröffnen einer Long-Position

            double lotSize = 0.1; // Geben Sie hier die Losgröße ein

            double price = SymbolInfoDouble(_Symbol, SYMBOL_ASK); // Geben Sie hier den Preis ein

            MqlTradeRequest request = {};

            request.action = TRADE_ACTION_DEAL;

            request.symbol = _Symbol;

            request.volume = lotSize;

            request.price = price;

            request.type = ORDER_TYPE_BUY;

            request.type_filling = ORDER_FILLING_FOK;

            request.deviation = 3;

            request.magic = 0;

            request.comment = "Long order";



            MqlTradeResult result = {};

            if (!OrderSend(request, result))

            {

                Print("Error placing long order: ", GetLastError());

            }

            else

            {

                Print("Long order placed successfully");

            }

        }

    }



    // Überprüfen, ob eine offene Long-Position vorhanden ist und die Position bei der nächsten Kerze geschlossen werden soll

    if (PositionSelect(_Symbol) && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)

    {

        // Schließen der Long-Position bei der nächsten Kerze

       if (PositionSelect(_Symbol) && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)

    {

        // Schließen der Buy-Position bei der nächsten Kerze

        if (iClose(_Symbol, _Period, 0) != iClose(_Symbol, _Period, 1))

        {

            // Position schließen

            MqlTradeRequest request = {};

            request.action = TRADE_ACTION_DEAL;

            request.symbol = _Symbol;

            request.volume = PositionGetDouble(POSITION_VOLUME); // Volumen der offenen Position

            request.type = ORDER_TYPE_CLOSE_BY;

            request.price = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Aktuellen Bid-Preis erhalten

            request.deviation = 3;

            request.magic = 0;

            request.comment = "Close order";



            MqlTradeResult result = {};

            if (!OrderSend(request, result))

            {

                Print("Error closing position: ", GetLastError());

            }

            else

            {

                Print("Position closed successfully");

            }

        }

    }

}



}
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2024.05.13
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
David Gauch:

Hey guys,

Im new here in coding, I tried to code together an EAm which simply uses the last 6 Candles, if thosse 6 are down-candles it opens up a long-position. That seems to work, but I cant get my EA to close that position on the next candle.

I would be glad, if theres anyone who can help me on that. I will provide the Code down below:


use the trade class to open positions.

It's simple, and less messy 

#include <Trade/Trade.mqh>
CTrade trade;

Void OnTick()
{
//open BUY trade
trade.Buy(0.01,NULL);
//close trade
trade.PositionClose(...);
}

just a snippet. this code will not compile

 
David Gauch: That seems to work, but I cant get my EA to close that position on the next candle.

Where do you check for a new candle?

Where do you check for an open order? Where do you try to close it?

 
to close "on the next candle" means you need to do something with position time or bar open time. As that guy mentioned, create a reference to the CTrade object to have full control over how you manage trades