Write Script for martingale only one open trade at a time

 

Hello, Please can some tell the code for using a martingale system and the martingale should follow only one open trade at a time.

The martingale will only trigger when the last closed trade is a loss so the next open trade is like multiplied by 2 lot, so instead of checking for open positions it would check for last closed positions.

Thank you

 
badim004:

Hello, Please can some tell the code for using a martingale system and the martingale should follow only one open trade at a time.

The martingale will only trigger when the last closed trade is a loss so the next open trade is like multiplied by 2 lot, so instead of checking for open positions it would check for last closed positions.

Thank you

See that "Freelance" menu option at the top of this page?

Use it.

No one is going to code this for you for free. :) 

 
badim004: Please can some tell the code for using a martingale system and
  1. Hedging, grid trading, same as Martingale.
              Martingale, Hedging and Grid : MHG - General - MQL5 programming forum (2016)

    Martingale, guaranteed to blow your account eventually. If your strategy is not profitable without, it is definitely not profitable with.
              Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions) - MQL5 programming forum (2015)

    Why it won't work:
              Calculate Loss from Lot Pips - MQL5 programming forum (2017)
              THIS Trading Strategy is a LIE... I took 100,000 TRADES with the Martingale Strategy - YouTube (2020)

  2. You have only four choices:

    1. Search for it (CodeBase or Market). Do you expect us to do your research for you?

    2. Beg at:

    3. MT4: Learn to code it.
      MT5: Begin learning to code it.

      If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

    4. Or pay (Freelance) someone to code it. Top of every page is the link Freelance.
                Hiring to write script - General - MQL5 programming forum (2019)

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
              No free help (2017)

 
Okay Thanks for the replies, I'd try out the links. 
 

I coded a simple EMA crossover and it works but I just want to add only one open trade at a time.

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

//|                                            EMA Crossover Bot.mq4 |

//|                                                        Smiling D |

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

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

#property copyright "Smiling D"

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

datetime dt;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   dt = TimeCurrent();

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

   if(isnewbar()){

      double prev_ma8 = iMA(_Symbol, PERIOD_CURRENT,8,0,MODE_EMA,PRICE_CLOSE,2);

      double prev_ma5 = iMA(_Symbol, PERIOD_CURRENT,5,0,MODE_EMA,PRICE_CLOSE,2);

      double ma8 = iMA(_Symbol, PERIOD_CURRENT,8,0,MODE_EMA,PRICE_CLOSE,1);

      double ma5 = iMA(_Symbol, PERIOD_CURRENT,5,0,MODE_EMA,PRICE_CLOSE,1);

      

      if(prev_ma8>prev_ma5 && ma8<ma5){

         if(OrdersTotal()!=0) closeexisting();

         

         OrderSend(_Symbol, OP_BUY, 0.01, Ask, 10, 0, 0);

         OrderModify(_Symbol, PERIOD_CURRENT, 320.0, 50.0, 0,clrNONE);

      }

      else if(prev_ma8<prev_ma5 && ma8>ma5){

         if(OrdersTotal()!=0) closeexisting();

         

         OrderSend(_Symbol, OP_SELL, 0.01, Bid, 10, 0, 0);

         OrderModify(_Symbol, PERIOD_CURRENT, 320.0, 50.0, 0,clrNONE);

      }

    }

  }

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

void closeexisting(){

   OrderSelect(0, SELECT_BY_POS);

   OrderClose(OrderTicket(), OrderLots(), MarketInfo(_Symbol, MODE_BID+OrderType()), 10);


}


bool isnewbar(){

   if(TimeCurrent() != dt){

      dt = TimeCurrent();

      return true;

   }

   return false;

}

Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
  • 2022.07.12
  • www.mql5.com
MQL5: язык торговых стратегий для MetaTrader 5, позволяет писать собственные торговые роботы, технические индикаторы, скрипты и библиотеки функций
 

Please edit your post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
          Messages Editor

 
int countOrders()
  {
   int tickersum = 0;
   if(OrdersTotal() >0)
     {
      for(int i=1; i<=OrdersTotal(); i++)
        {
         if(OrderSelect(i-1,SELECT_BY_POS)==true)
           {
            if(OrderSymbol() == "XAUUSD" && (OrderMagicNumber()==2000 || OrderMagicNumber()==3000))
              {
               tickersum += 1;
              }
           }
        }
     }

   return tickersum;
  }

Here's the code I use to limit the positions to only 1 open position for a certain symbol with certain magicnumbers. This way it won't count my manually placed orders.

Usage:

void OnTick(){
	if(countOrders()> 0) return;
}
 
William William #:

Here's the code I use to limit the positions to only 1 open position for a certain symbol with certain magicnumbers. This way it won't count my manual placed orders.

Usage:

for(int i=1; i<=OrdersTotal(); i++)
        {
         if(OrderSelect(i-1,SELECT_BY_POS)==true)

That seems a very strange way to do it and is easily misread.

Most coders would use

for(int i=0; i<OrdersTotal(); i++)
        {
         if(OrderSelect(i,SELECT_BY_POS)==true)

or

for(int i=OrdersTotal()-1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS))
 
Keith Watford #:

That seems a very strange way to do it and is easily misread.

Most coders would use

or

Thank you for the correction. The code was written in the first couple of weeks of my mql4 learning. I tried  i=0 but the compiler complained about it during runtime.

 
William William #:

Here's the code I use to limit the positions to only 1 open position for a certain symbol with certain magicnumbers. This way it won't count my manually placed orders.

Usage:

Thanks William for the reply, I'd try it out
 
Keith Watford #:

That seems a very strange way to do it and is easily misread.

Most coders would use

or


Thanks Keith for the correction, Noted

Reason: