Exp_2XMA_Ichimoku_Oscillator

Download script
Author: Copyright © 2018, Nikolay Kositsin

The 2XMA_Ichimoku_Oscillator is a sophisticated automated trading system designed for the MetaTrader 5 platform. It combines several advanced technical analysis concepts—specifically Exponential Moving Averages (EMA), Ichimoku components, and smoothed oscillators—to identify high-probability market trends and execute trades automatically.

Here is a breakdown of how the logic works in plain English:

Core Intelligence & Trend Detection

The script functions by monitoring the relationship between different price moving averages to determine the "momentum" of the market.

  • Dual Smoothing: It utilizes two distinct "smoothed" calculation paths (Upper and Down) which allow it to filter out market noise while remaining responsive to actual trend changes.
  • Dynamic Inputs: The system is highly customizable, allowing users to choose from various smoothing methods (such as SMA, EMA, Jurik, or VIDYA) and different price types (like Median Price, Typical Price, or Weighted Close) to suit specific market conditions.
  • Timeframe Awareness: While it can run on various charts, it is designed to process data accurately across different timeframes, ensuring that the signals remain consistent regardless of the zoom level.

Signal Generation Logic

The "Oscillator" part of the script translates complex price movements into a simplified signal system:

  • Trend Identification: The tool looks for specific transitions in the trend values. It identifies when the market shifts from a neutral or bearish state to a bullish one (and vice versa).
  • Confirmation Signals: Instead of trading on every minor fluctuation, the script waits for a confirmed "Signal Bar." This ensures that a trade is only triggered when there is enough momentum to sustain a move.
  • Dual-Directional Monitoring: The logic simultaneously tracks both Buy and Sell opportunities. If the trend signals a reversal, the script can automatically prepare to close an existing position or open a new one in the opposite direction.

Trade Management & Execution

Once the logic identifies a valid entry point, the script handles the "heavy lifting" of trade management:

  • Automated Entry: The system calculates the exact moment to enter based on the trend signal and immediately places the order for you.
  • Money Management (MM): It features a built-in risk management module. You can choose how the script handles your position sizes—whether based on your total account balance, your free margin, or a fixed lot size.
  • Safety Net: Every trade is governed by user-defined Stop Loss and Take Profit levels. This ensures that if a trade goes against you, the script will exit automatically to protect your capital.
  • Dynamic Closures: In addition to standard profit/loss targets, the script can close positions early if the underlying trend signal changes, helping to preserve gains or minimize losses during sudden market shifts.

Summary for the User

In short, this script acts as an automated "trend follower." It analyzes smoothed price data to find out where the market is heading, identifies the safest moments to enter based on those trends, and manages the risk of every trade automatically so you don't have to watch the charts 24/7.

10 Views
0 Downloads
0 Favorites
Exp_2XMA_Ichimoku_Oscillator
ÿþ//+------------------------------------------------------------------+

//|                                  Exp_2XMA_Ichimoku_Oscillator.mq5 |

//|                               Copyright © 2018, Nikolay Kositsin | 

//|                              Khabarovsk,   farria@mail.redcom.ru | 

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

#property copyright "Copyright © 2018, Nikolay Kositsin"

#property link      "farria@mail.redcom.ru"

#property version   "1.00"

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

//  ">@3>2K5 0;3>@8B<K                           | 

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

#include <TradeAlgorithms.mqh>

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

//|  5@5G8A;5=85 4;O 20@80=B>2 @0AGQB0 ;>B0     |

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

/*enum MarginMode  - ?5@5G8A;5=85 >1JO2;5=> 2 D09;5 TradeAlgorithms.mqh

  {

   FREEMARGIN=0,     //MM >B A2>1>4=KE A@54AB2 =0 AGQB5

   BALANCE,          //MM >B 10;0=A0 A@54AB2 =0 AGQB5

   LOSSFREEMARGIN,   //MM ?> C1KB:0< >B A2>1>4=KE A@54AB2 =0 AGQB5

   LOSSBALANCE,      //MM ?> C1KB:0< >B 10;0=A0 A@54AB2 =0 AGQB5

   LOT               //>B 157 87<5=5=8O

  }; */

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

//|  ?8A0=85 :;0AA0 CXMA                        |

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

#include <SmoothAlgorithms.mqh> 

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

//|  >1JO2;5=85 ?5@5G8A;5=89                     |

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

enum MODE_PRICE //"8? :>=AB0=BK

  {

   OPEN = 0,     //> F5=0< >B:@KB8O

   LOW,          //> <8=8<C<0<

   HIGH,         //> <0:A8<C<0<

   CLOSE         //> F5=0< 70:@KB8O

  };

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

//|  >1JO2;5=85 ?5@5G8A;5=89                     |

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

enum Applied_price_      //"8? :>=AB0=BK

  {

   PRICE_CLOSE_ = 1,     //Close

   PRICE_OPEN_,          //Open

   PRICE_HIGH_,          //High

   PRICE_LOW_,           //Low

   PRICE_MEDIAN_,        //Median Price (HL/2)

   PRICE_TYPICAL_,       //Typical Price (HLC/3)

   PRICE_WEIGHTED_,      //Weighted Close (HLCC/4)

   PRICE_SIMPL_,         //Simpl Price (OC/2)

   PRICE_QUARTER_,       //Quarted Price (HLOC/4) 

   PRICE_TRENDFOLLOW0_,  //TrendFollow_1 Price 

   PRICE_TRENDFOLLOW1_,  //TrendFollow_2 Price

   PRICE_DEMARK_         //Demark Price

  };

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

//|  >1JO2;5=85 ?5@5G8A;5=89                     |

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

/*enum Smooth_Method - ?5@5G8A;5=85 >1JO2;5=> 2 D09;5 SmoothAlgorithms.mqh

  {

   MODE_SMA_,  //SMA

   MODE_EMA_,  //EMA

   MODE_SMMA_, //SMMA

   MODE_LWMA_, //LWMA

   MODE_JJMA,  //JJMA

   MODE_JurX,  //JurX

   MODE_ParMA, //ParMA

   MODE_T3,    //T3

   MODE_VIDYA, //VIDYA

   MODE_AMA,   //AMA

  }; */

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

//| E>4=K5 ?0@0<5B@K M:A?5@B0                     |

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

input double MM=0.1;              //>;O D8=0=A>2KE @5AC@A>2 >B 45?>78B0 2 A45;:5

input MarginMode MMMode=LOT;      //A?>A>1 >?@545;5=8O @07<5@0 ;>B0

input int    StopLoss_=1000;      //AB>?;>AA 2 ?C=:B0E

input int    TakeProfit_=2000;    //B59:?@>D8B 2 ?C=:B0E

input int    Deviation_=10;       //<0:A. >B:;>=5=85 F5=K 2 ?C=:B0E

input bool   BuyPosOpen=true;     // 07@5H5=85 4;O 2E>40 2 ;>=3

input bool   SellPosOpen=true;    // 07@5H5=85 4;O 2E>40 2 H>@B

input bool   BuyPosClose=true;     // 07@5H5=85 4;O 2KE>40 87 ;>=3>2

input bool   SellPosClose=true;    // 07@5H5=85 4;O 2KE>40 87 H>@B>2

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

//| E>4=K5 ?0@0<5B@K 8=48:0B>@0                   |

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

input ENUM_TIMEFRAMES InpInd_Timeframe=PERIOD_H4; //B09<D@59< 8=48:0B>@0

input uint Up_period1=6; //1 ?5@8>4, 8A?>;L7C5<K9 4;O 2KG8A;5=8O =082KAH53> 7=0G5=8O F5=K

input uint Dn_period1=6; //1 ?5@8>4, 8A?>;L7C5<<K9 4;O 2KG8A;5=8O =08=87H53> 7=0G5=8O F5=K

//----

input uint Up_period2=9; //2 ?5@8>4, 8A?>;L7C5<K9 4;O 2KG8A;5=8O =082KAH53> 7=0G5=8O F5=K

input uint Dn_period2=9; //2 ?5@8>4, 8A?>;L7C5<<K9 4;O 2KG8A;5=8O =08=87H53> 7=0G5=8O F5=K

//---- 

input MODE_PRICE Up_mode1=HIGH;  //1 B09<A5@8O 4;O ?>8A:0 <0:A8<C<>2 

input MODE_PRICE Dn_mode1=LOW;   //1 B09<A5@8O 4;O ?>8A:0 <8=8<C<>2 

//---- 

input MODE_PRICE Up_mode2=HIGH;  //2 B09<A5@8O 4;O ?>8A:0 <0:A8<C<>2 

input MODE_PRICE Dn_mode2=LOW;   //2 B09<A5@8O 4;O ?>8A:0 <8=8<C<>2  

//---- 

input Smooth_Method XMA1_Method=MODE_SMA_; //1 <5B>4 CA@54=5=8O

input Smooth_Method XMA2_Method=MODE_SMA_; //2 <5B>4 CA@54=5=8O

//----

input int XLength1=25; //1 3;C18=0 A3;06820=8O 

input int XLength2=80; //2 3;C18=0 A3;06820=8O

//----                  

input int XPhase=15; //?0@0<5B@ CA@54=5=8O,

//---- 4;O JJMA 87<5=ONI89AO 2 ?@545;0E -100 ... +100, 2;8O5B =0 :0G5AB2> ?5@5E>4=>3> ?@>F5AA0;

//---- ;O VIDIA MB> ?5@8>4 CMO, 4;O AMA MB> ?5@8>4 <54;5==>9 A:>;L7OI59

input uint SignalBar=1;                           //=><5@ 10@0 4;O ?>;CG5=8O A83=0;0 2E>40

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



int TimeShiftSec;

//---- 1JO2;5=85 F5;KE ?5@5<5==KE 4;O E5=4;>2 8=48:0B>@>2

int InpInd_Handle;

//---- >1JO2;5=85 F5;KE ?5@5<5==KE =0G0;0 >BAG5B0 40==KE

int min_rates_total;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---- ?>;CG5=85 E5=4;0 8=48:0B>@0 2XMA_Ichimoku_Oscillator

   InpInd_Handle=iCustom(Symbol(),InpInd_Timeframe,"2XMA_Ichimoku_Oscillator",

      Up_period1,Dn_period1,Up_period2,Dn_period2,Up_mode1,Dn_mode1,Up_mode2,Dn_mode2,XMA1_Method,XMA2_Method,XLength1,XLength2,XPhase,0);

   if(InpInd_Handle==INVALID_HANDLE)

     {

      Print(" 5 C40;>AL ?>;CG8BL E5=4; 8=48:0B>@0 2XMA_Ichimoku_Oscillator");

      return(INIT_FAILED);

     }



//---- 8=8F80;870F8O ?5@5<5==>9 4;O E@0=5=8O ?5@8>40 3@0D8:0 2 A5:C=40E  

   TimeShiftSec=PeriodSeconds(InpInd_Timeframe);



//---- 8=8F80;870F8O ?5@5<5==KE =0G0;0 >BAGQB0 40==KE

   //---- =8F80;870F8O ?5@5<5==KE =0G0;0 >BAGQB0 40==KE

   int min_rates_1=int(MathMax(Up_period1,Dn_period1));

   min_rates_1+=GetStartBars(XMA1_Method,XLength1,XPhase);

   int min_rates_2=int(MathMax(Up_period2,Dn_period2));

   min_rates_2+=GetStartBars(XMA2_Method,XLength2,XPhase);

   min_rates_total=int(MathMax(min_rates_1,min_rates_2));

   min_rates_total+=int(SignalBar+1);

//--- 7025@H5=85 8=8F80;870F88

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//----

   GlobalVariableDel_(Symbol());

//----

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---- ?@>25@:0 :>;8G5AB20 10@>2 =0 4>AB0B>G=>ABL 4;O @0AGQB0

   if(BarsCalculated(InpInd_Handle)<min_rates_total) return;



//---- ?>43@C7:0 8AB>@88 4;O =>@<0;L=>9 @01>BK DC=:F89 IsNewBar() 8 SeriesInfoInteger()  

   LoadHistory(TimeCurrent()-PeriodSeconds(InpInd_Timeframe)-1,Symbol(),InpInd_Timeframe);



//---- 1JO2;5=85 AB0B8G5A:8E ?5@5<5==KE

   static bool Recount=true;

   static bool BUY_Open=false,BUY_Close=false;

   static bool SELL_Open=false,SELL_Close=false;

   static datetime UpSignalTime,DnSignalTime;

   static CIsNewBar NB;



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

//| ?@545;5=85 A83=0;>2 4;O A45;>:              |

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

   if(!SignalBar || NB.IsNewBar(Symbol(),InpInd_Timeframe) || Recount) // ?@>25@:0 =0 ?>O2;5=85 =>2>3> 10@0

     {

      //---- >1=C;8< B>@3>2K5 A83=0;K

      BUY_Open=false;

      SELL_Open=false;

      BUY_Close=false;

      SELL_Close=false;

      Recount=false;

      //---- 1JO2;5=85 ;>:0;L=KE ?5@5<5==KE

      double TrendValue[2];



      //---- :>?8@C5< 2=>2L ?>O282H85AO 40==K5 2 <0AA82K

      if(CopyBuffer(InpInd_Handle,1,SignalBar,2,TrendValue)<=0) {Recount=true; return;}



      //---- >;CG8< A83=0;K 4;O ?>:C?:8

      if(TrendValue[1]==3 || TrendValue[1]==0)

        {

         if(BuyPosOpen && (TrendValue[0]==4 ||  TrendValue[0]==1)) BUY_Open=true;

         if(SellPosClose)SELL_Close=true;

         UpSignalTime=datetime(SeriesInfoInteger(Symbol(),InpInd_Timeframe,SERIES_LASTBAR_DATE))+TimeShiftSec;

        }



      //---- >;CG8< A83=0;K 4;O ?@>4068

      if(TrendValue[1]==1 || TrendValue[1]==4)

        {

         if(SellPosOpen && (TrendValue[0]==0 ||  TrendValue[0]==3)) SELL_Open=true;

         if(BuyPosClose) BUY_Close=true;

         DnSignalTime=datetime(SeriesInfoInteger(Symbol(),InpInd_Timeframe,SERIES_LASTBAR_DATE))+TimeShiftSec;

        }

     }



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

//| !>25@H5=85 A45;>:                            |

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

//---- 0:@K205< ;>=3

   BuyPositionClose(BUY_Close,Symbol(),Deviation_);



//---- 0:@K205< H>@B   

   SellPositionClose(SELL_Close,Symbol(),Deviation_);



//---- B:@K205< ;>=3

   BuyPositionOpen(BUY_Open,Symbol(),UpSignalTime,MM,MMMode,Deviation_,StopLoss_,TakeProfit_);



//---- B:@K205< H>@B

   SellPositionOpen(SELL_Open,Symbol(),DnSignalTime,MM,MMMode,Deviation_,StopLoss_,TakeProfit_);

//----

  }

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

Comments