fxDreema

    • Register
    • Login
    • Search
    • Back to the main page
    • Categories
    • Recent
    • Tags
    • Popular
    • Search

    MT4 EA not working in Live or Demo account.

    Questions & Answers
    2
    2
    181
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • wadz91
      wadz91 last edited by

      Only works in strategy test. Please see codes below:

      input double InitialLotSize = 0.01;
      input int MaxOrders = 3;
      input bool UseMartingale = true;
      input double MartingaleMultiplier = 2.0;
      input bool StartWithBuy = true;

      bool IsBuy = true;
      int TradeCounter = 0;
      double LastBarClose = 0.0;
      double LotSize = InitialLotSize;

      void OnStart()
      {
      LastBarClose = iClose(Symbol(), 0, 1);
      if (!StartWithBuy)
      {
      IsBuy = false;
      }
      }

      void OnTick()
      {
      // Check if the current bar is closed
      double currentBarClose = iClose(Symbol(), 0, 0);
      if (currentBarClose != LastBarClose)
      {
      LastBarClose = currentBarClose;

          // Close open orders
          CloseOrders();
          
          // Check if we reached the maximum number of orders
          if (TradeCounter == MaxOrders)
          {
              // Switch direction
              IsBuy = !IsBuy;
              TradeCounter = 0;
          }
          
          // Place new order if no order is placed
          if (TradeCounter < MaxOrders)
          {
              PlaceOrder();
          }
      }
      

      }

      void CloseOrders()
      {
      for (int i = OrdersTotal() - 1; i >= 0; i--)
      {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
      if (OrderSymbol() == Symbol() && (OrderType() == OP_BUY || OrderType() == OP_SELL))
      {
      bool closed = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), IsBuy ? MODE_BID : MODE_ASK), 0, clrNONE);
      if (!closed)
      {
      Print("Order close failed: ", GetLastError());
      }
      else
      {
      if (closedInProfit(OrderTicket()))
      {
      // Reset the lot size to the initial value after closing in profit
      LotSize = InitialLotSize;
      }
      }
      }
      }
      }
      }

      void PlaceOrder()
      {
      double price = IsBuy ? Ask : Bid;

      int ticket = OrderSend(Symbol(), IsBuy ? OP_BUY : OP_SELL, LotSize, price, 0, 0, 0, "", 0, clrNONE, 0);
      if (ticket > 0)
      {
          TradeCounter++;
      }
      else
      {
          Print("Order send failed: ", GetLastError());
      }
      
      // Update the lot size for the next trade if using Martingale
      if (UseMartingale)
      {
          LotSize *= MartingaleMultiplier;
      }
      

      }

      bool closedInProfit(int ticket)
      {
      if (OrderSelect(ticket, SELECT_BY_TICKET))
      {
      double profit = OrderProfit();
      return profit >= 0.0;
      }
      return false;
      }

      1 Reply Last reply Reply Quote 0
      • X
        Xfire last edited by

        HI, you should share your project by link. Who can analyse your code here?

        IN GOD WE TRUST

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post

        Online Users

        T
        P
        C
        S
        T
        I
        B
        A
        H

        20
        Online

        146.7k
        Users

        22.4k
        Topics

        122.6k
        Posts

        Powered by NodeBB Forums | Contributors