N Candles v3

Download script
Author: Copyright © 2017, Vladimir Karputov

This script is an automated trading tool designed for the MetaTrader 5 platform. Its primary function is to monitor price movements and execute trades based on a specific pattern of consecutive "identical" candles.

Core Trading Logic

The tool operates by looking at a sequence of recent candles to identify a trend. It focuses on identifying:

  • Bullish Sequences: A series of "Bull" candles (where the closing price is higher than the opening price).
  • Bearish Sequences: A series of "Bear" candles (where the closing price is lower than the opening price).

The user can define exactly how many consecutive candles of the same type are required to trigger a trade. For example, if set to 3, the script will look for three identical bullish or bearish candles in a row before taking action.

Key Features and Functionalities

  • Automated Execution: Once the candle pattern is identified, the script automatically opens Buy or Sell positions. It includes logic to ensure that trades are only opened if there is enough available volume/margin to cover the trade.
  • Risk Management Tools:
    • Take Profit (TP): Automatically sets a target price to lock in profits.
    • Stop Loss (SL): Automatically sets a safety net to limit potential losses on every trade.
    • Trailing Stop: A dynamic feature that "follows" the price as it moves in your favor. It adjusts the stop loss level automatically to protect gains, moving only when the price reaches a specific "step" or distance.
  • Position Limits: The tool allows users to set a maximum number of open positions in a single direction (Buy or Sell). This prevents the script from opening an unlimited number of trades at once, helping to manage overall risk exposure.
  • Smart Adjustments: The script automatically detects the precision of different currency pairs (e.g., 3-digit vs. 5-digit brokers) and adjusts your profit and loss settings accordingly to ensure they are calculated correctly regardless of the asset traded.

Summary of Workflow

  1. Monitor: The script constantly watches new candles as they form on the chart.
  2. Analyze: It checks if the last $N$ candles match the same direction (all up or all down).
  3. Verify: It checks if there is enough balance to trade and if the maximum position limit hasn't been reached.
  4. Trade: It opens the trade with predefined Take Profit, Stop Loss, and Trailing Stop parameters.
  5. Manage: It actively updates the Trailing Stop as the market fluctuates to secure potential profits.
Price Data Components
8 Views
0 Downloads
0 Favorites
N Candles v3
ÿþ//+------------------------------------------------------------------+

//|                                                N-_Candles_v3.mq5 |

//|                              Copyright © 2017, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

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

#property copyright "Copyright © 2017, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "3.001"

#property description "We look for N-of identical candles which go in a row"

#property description "New in version 2: Take Profit, Stop Loss, Trailing"

#property description "New in version 3: Maximum number of open positions in a certain direction"

//---

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>  

CPositionInfo  m_position;                   // trade position object

CTrade         m_trade;                      // trading object

CSymbolInfo    m_symbol;                     // symbol info object

//--- input parameter

input uchar    InpN_candles   = 3;           // N identical candles which go in a row 

input double   InpLot         = 0.01;        // Lot

input ushort   InpTakeProfit  = 50;          // Take Profit (in pips)

input ushort   InpStopLoss    = 50;          // Stop Loss (in pips)

input ushort   InpTrailingStop= 10;          // Trailing Stop ("0" -> not trailing)

input ushort   InpTrailingStep= 4;           // Trailing Step (use if Trailing Stop >0)

input uchar    InpMaxPositions= 2;           // Maximum positions certain direction

input ulong    m_magic        = 427474216;   // magic number

input ulong    m_slippage     = 30;          // slippage

//---

double         m_adjusted_point;             // point value adjusted for 3 or 5 points

double         ExtTakeProfit  = 0.0;

double         ExtStopLoss    = 0.0;

double         ExtTrailingStop= 0.0;

double         ExtTrailingStep= 0.0;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   m_symbol.Name(Symbol());                  // sets symbol name

   RefreshRates();

   m_symbol.Refresh();



   string err_text="";

   if(!CheckVolumeValue(InpLot,err_text))

     {

      Print(err_text);

      return(INIT_PARAMETERS_INCORRECT);

     }

//---

   m_trade.SetExpertMagicNumber(m_magic);

//---

   if(IsFillingTypeAllowed(Symbol(),SYMBOL_FILLING_FOK))

      m_trade.SetTypeFilling(ORDER_FILLING_FOK);

   else if(IsFillingTypeAllowed(Symbol(),SYMBOL_FILLING_IOC))

      m_trade.SetTypeFilling(ORDER_FILLING_IOC);

   else

      m_trade.SetTypeFilling(ORDER_FILLING_RETURN);

//---

   m_trade.SetDeviationInPoints(m_slippage);

//--- tuning for 3 or 5 digits

   int digits_adjust=1;

   if(m_symbol.Digits()==3 || m_symbol.Digits()==5)

      digits_adjust=10;

   m_adjusted_point=m_symbol.Point()*digits_adjust;

   ExtTakeProfit  = InpTakeProfit   * m_adjusted_point;

   ExtStopLoss    = InpStopLoss     * m_adjusted_point;

   ExtTrailingStop= InpTrailingStop * m_adjusted_point;

   ExtTrailingStep= InpTrailingStep * m_adjusted_point;

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//--- we work only at the time of the birth of new bar

   static datetime PrevBars=0;

   datetime time_0=iTime(m_symbol.Name(),Period(),0);

   if(time_0==PrevBars)

      return;

   PrevBars=time_0;



   if(ExtTrailingStop>0.0)

      for(int i=PositionsTotal()-1;i>=0;i--)

         if(m_position.SelectByIndex(i))

            if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)

              {

               if(!RefreshRates())

                 {

                  PrevBars=iTime(m_symbol.Name(),Period(),1);

                  return;

                 }

               TrailingStop(m_position.Ticket(),ExtTrailingStop,ExtTrailingStep);

              }



   MqlRates rates[];

   int copied=CopyRates(NULL,0,1,InpN_candles,rates);

//--- Example:

//--- rates[0].time -> D'2015.05.28 00:00:00'

//--- rates[2].time -> D'2015.06.01 00:00:00'

   if(copied<=0)

     {

      Print("Error copying price data ",GetLastError());

      return;

     }



   bool result=true;

//--- Bull candle. Bear candle.

   int type_of_candles=0;     // "1" -> Bull candle. "-1" ->Bear candle



   for(int i=0;i<copied;i++)

     {

      //--- We define type of the most distant candle

      if(i==0)

        {

         if(rates[i].open<rates[i].close)

            type_of_candles=1;

         else if(rates[i].open>rates[i].close)

            type_of_candles=-1;

         else

           {

            result=false;

            break;

           }



         continue;

        }



      if(type_of_candles==1) // "1" -> Bull candle

        {

         if(rates[i].open>rates[i].close)

           {

            result=false;

            break;

           }

        }

      else // "-1" ->Bear candle

        {

         if(rates[i].open<rates[i].close)

           {

            result=false;

            break;

           }

        }

     }



   if(!result)

      return;



//--- We here. Means we have found N-of candles in a row

   if(type_of_candles==1) // "1" -> Bull candle

     {

      if(!RefreshRates())

        {

         PrevBars=iTime(m_symbol.Name(),Period(),1);

         return;

        }

      if(CalculatePositions(POSITION_TYPE_BUY)<InpMaxPositions)

         OpenBuy(m_symbol.Bid()-ExtStopLoss,m_symbol.Ask()+ExtTakeProfit);

     }

   else

     {

      if(!RefreshRates())

        {

         PrevBars=iTime(m_symbol.Name(),Period(),1);

         return;

        }

      if(CalculatePositions(POSITION_TYPE_SELL)<InpMaxPositions)

         OpenSell(m_symbol.Ask()+ExtStopLoss,m_symbol.Bid()-ExtTakeProfit);

     }

   int d=0;

  }

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

//| Refreshes the symbol quotes data                                 |

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

bool RefreshRates()

  {

//--- refresh rates

   if(!m_symbol.RefreshRates())

      return(false);

//--- protection against the return value of "zero"

   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)

      return(false);

//---

   return(true);

  }

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

//| Check the correctness of the order volume                        |

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

bool CheckVolumeValue(double volume,string &error_description)

  {

//--- minimal allowed volume for trade operations

   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

   if(volume<min_volume)

     {

      error_description=StringFormat("Volume is less than the minimal allowed SYMBOL_VOLUME_MIN=%.2f",min_volume);

      return(false);

     }



//--- maximal allowed volume of trade operations

   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   if(volume>max_volume)

     {

      error_description=StringFormat("Volume is greater than the maximal allowed SYMBOL_VOLUME_MAX=%.2f",max_volume);

      return(false);

     }



//--- get minimal step of volume changing

   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);



   int ratio=(int)MathRound(volume/volume_step);

   if(MathAbs(ratio*volume_step-volume)>0.0000001)

     {

      error_description=StringFormat("Volume is not a multiple of the minimal step SYMBOL_VOLUME_STEP=%.2f, the closest correct volume is %.2f",

                                     volume_step,ratio*volume_step);

      return(false);

     }

   error_description="Correct volume value";

   return(true);

  }

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

//| Checks if the specified filling mode is allowed                  | 

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

bool IsFillingTypeAllowed(string symbol,int fill_type)

  {

//--- Obtain the value of the property that describes allowed filling modes 

   int filling=(int)SymbolInfoInteger(symbol,SYMBOL_FILLING_MODE);

//--- Return true, if mode fill_type is allowed 

   return((filling & fill_type)==fill_type);

  }

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

//| Open Buy position                                                |

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

void OpenBuy(double sl,double tp)

  {

   sl=m_symbol.NormalizePrice(sl);

   tp=m_symbol.NormalizePrice(tp);

//--- check volume before OrderSend to avoid "not enough money" error (CTrade)

   double chek_volime_lot=m_trade.CheckVolume(m_symbol.Name(),InpLot,m_symbol.Ask(),ORDER_TYPE_BUY);



   if(chek_volime_lot!=0.0)

      if(chek_volime_lot>=InpLot)

        {

         if(m_trade.Buy(InpLot,NULL,m_symbol.Ask(),sl,tp))

           {

            if(m_trade.ResultDeal()==0)

              {

               Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),

                     ", description of result: ",m_trade.ResultRetcodeDescription());

              }

            else

              {

               Print("Buy -> true. Result Retcode: ",m_trade.ResultRetcode(),

                     ", description of result: ",m_trade.ResultRetcodeDescription());

              }

           }

         else

           {

            Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),

                  ", description of result: ",m_trade.ResultRetcodeDescription());

           }

        }

//---

  }

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

//| Open Sell position                                               |

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

void OpenSell(double sl,double tp)

  {

   sl=m_symbol.NormalizePrice(sl);

   tp=m_symbol.NormalizePrice(tp);

//--- check volume before OrderSend to avoid "not enough money" error (CTrade)

   double chek_volime_lot=m_trade.CheckVolume(m_symbol.Name(),InpLot,m_symbol.Bid(),ORDER_TYPE_SELL);



   if(chek_volime_lot!=0.0)

      if(chek_volime_lot>=InpLot)

        {

         if(m_trade.Sell(InpLot,NULL,m_symbol.Bid(),sl,tp))

           {

            if(m_trade.ResultDeal()==0)

              {

               Print("Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),

                     ", description of result: ",m_trade.ResultRetcodeDescription());

              }

            else

              {

               Print("Sell -> true. Result Retcode: ",m_trade.ResultRetcode(),

                     ", description of result: ",m_trade.ResultRetcodeDescription());

              }

           }

         else

           {

            Print("Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),

                  ", description of result: ",m_trade.ResultRetcodeDescription());

           }

        }

//---

  }

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

//| Trailing position                                                |

//|  With this approach, it is expected that the position            |

//|  is ALREADY SELECTED and the prices are updated                  |

//|  in the RefreshRates () method                                   |

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

void TrailingStop(const ulong ticket,const double trailing,const double step)

  {

   if(m_position.PositionType()==POSITION_TYPE_BUY)

     {

      //--- when the position still does not have StopLoss

      if(m_position.StopLoss()==0)

        {

         //--- while StopLoss is equal 0.0, we don't consider TrailingStep

         if(m_symbol.Bid()-ExtTrailingStop>m_position.PriceOpen())

           {

            //--- modification of a position

            m_trade.PositionModify(m_position.Ticket(),m_position.PriceOpen(),m_position.TakeProfit());

           }

        }

      //--- when the position already has StopLoss

      else

        {

         //--- now TrailingStep needs to be considered

         if(m_symbol.Bid()-ExtTrailingStop-ExtTrailingStep>m_position.StopLoss())

           {

            //--- modification of a position

            m_trade.PositionModify(m_position.Ticket(),

                                   m_symbol.NormalizePrice(m_symbol.Bid()-ExtTrailingStop),m_position.TakeProfit());

           }

        }

     }



   if(m_position.PositionType()==POSITION_TYPE_SELL)

     {

      //--- when the position still does not have StopLoss

      if(m_position.StopLoss()==0)

        {

         //--- while StopLoss is equal 0.0, we don't consider TrailingStep

         if(m_symbol.Ask()+ExtTrailingStop<m_position.PriceOpen())

           {

            //--- modification of a position

            m_trade.PositionModify(m_position.Ticket(),m_position.PriceOpen(),m_position.TakeProfit());

           }

        }

      //--- when the position already has StopLoss

      else

        {

         //--- now TrailingStep needs to be considered

         if(m_symbol.Ask()+ExtTrailingStop+ExtTrailingStep<m_position.StopLoss())

           {

            //--- modification of a position

            m_trade.PositionModify(m_position.Ticket(),

                                   m_symbol.NormalizePrice(m_symbol.Ask()+ExtTrailingStop),m_position.TakeProfit());

           }

        }

     }

  }

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

//| Calculate positions                                              |

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

int CalculatePositions(ENUM_POSITION_TYPE pos_type)

  {

   int count=0;



   for(int i=PositionsTotal()-1;i>=0;i--)

      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)

            if(m_position.PositionType()==pos_type)

               count++;

//---

   return(count);

  }

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

Comments