1. Welcome back! Thank you for being a part of this Traders Community. Let's discuss and share :)
    Selamat datang kembali! Trimakasih telah menjadi bagian dari Komunitas Trader ini. Mari berdiskusi dan berbagi :)
    Dismiss Notice

Suggestion Help with code. new to coding

Discussion in 'Belajar MQL dari Nol' started by Booker21, 27 Sep 2024.

  1. Booker21

    Booker21 New Member

    Equity
    Credit
    Ref Point
    Hello
    I´m working on a simple martingale code but for some reason the multiply lot size is not taking into consideration. Trades are always the same size.

    Below is the code, if someone can help me identify what´s wrong i will appreciate.

    //--- Global Variables (Editable inputs)
    input double initialLotSize = 0.01; // Initial lot size (editable)
    input double maxLotSize = 3.2; // Max lot size (editable)
    input double takeProfitPips = 10; // Take profit in pips (editable)
    input double stopLossPips = 10; // Stop loss in pips (editable)
    input int emaPeriod = 20; // EMA period (editable)
    input int MAGIC_NUMBER = 123456; // Unique identifier for this EA's trades (editable)
    input double lotMultiplier = 2.0; // Multiplier for lot size after a loss (editable)

    //--- Other Variables
    double lotSize;

    //+------------------------------------------------------------------+
    //| Expert initialization function |
    //+------------------------------------------------------------------+
    int OnInit()
    {
    lotSize = initialLotSize;
    return(INIT_SUCCEEDED);
    }
    //+------------------------------------------------------------------+
    //| Expert deinitialization function |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason)
    {
    }
    //+------------------------------------------------------------------+
    //| Expert tick function |
    //+------------------------------------------------------------------+
    void OnTick()
    {
    // Get the current price and the EMA
    double price = iClose(Symbol(), 0, 0);
    double ema = iMA(Symbol(), 0, emaPeriod, 0, MODE_EMA, PRICE_CLOSE, 0);

    // Check if there are no open positions
    if (OrdersTotal() == 0)
    {
    // Buy Condition: Price is above EMA
    if (price > ema)
    {
    OpenBuyOrder();
    }
    // Sell Condition: Price is below EMA
    else if (price < ema)
    {
    OpenSellOrder();
    }
    }
    }
    //+------------------------------------------------------------------+
    //| Function to open a buy order |
    //+------------------------------------------------------------------+
    void OpenBuyOrder()
    {
    double sl = NormalizeDouble(Bid - stopLossPips * Point, Digits);
    double tp = NormalizeDouble(Bid + takeProfitPips * Point, Digits);

    if (lotSize <= maxLotSize)
    {
    int ticket = OrderSend(Symbol(), OP_BUY, lotSize, Ask, 3, sl, tp, "Buy Order", MAGIC_NUMBER, 0, Green);
    if (ticket < 0)
    {
    Print("Error opening buy order: ", GetLastError());
    }
    }
    }
    //+------------------------------------------------------------------+
    //| Function to open a sell order |
    //+------------------------------------------------------------------+
    void OpenSellOrder()
    {
    double sl = NormalizeDouble(Ask + stopLossPips * Point, Digits);
    double tp = NormalizeDouble(Ask - takeProfitPips * Point, Digits);

    if (lotSize <= maxLotSize)
    {
    int ticket = OrderSend(Symbol(), OP_SELL, lotSize, Bid, 3, sl, tp, "Sell Order", MAGIC_NUMBER, 0, Red);
    if (ticket < 0)
    {
    Print("Error opening sell order: ", GetLastError());
    }
    }
    }
    //+------------------------------------------------------------------+
    //| Function to check the last closed trade |
    //+------------------------------------------------------------------+
    void CheckLastTrade()
    {
    // Check if the last trade was a loss
    if (OrderSelect(OrdersHistoryTotal() - 1, SELECT_BY_POS, MODE_HISTORY))
    {
    if (OrderProfit() < 0)
    {
    // Increase lot size according to the Martingale strategy with multiplier
    lotSize *= lotMultiplier;
    if (lotSize > maxLotSize)
    {
    lotSize = maxLotSize;
    }
    }
    else
    {
    // Reset to initial lot size after a win
    lotSize = initialLotSize;
    }
    }
    }
     
    • Dumb Dumb x 1
  2. Niguru

    Niguru Member Credit Hunter

    Equity
    Credit
    Ref Point

    You should call your procedure (CheckLastTrade) at the beginning of OnTick.
     
  3. Booker21

    Booker21 New Member

    Equity
    Credit
    Ref Point
    Thank you, that solved it!
     

Share This Page