From 4744b563f2b173c5499dd39c5fff65988b8410cc Mon Sep 17 00:00:00 2001 From: GODFREY KIGGUNDU <140074856+Kiggundug1@users.noreply.github.com> Date: Tue, 23 Jul 2024 21:30:17 +0300 Subject: [PATCH] Add files via upload --- AD.mq5 | 90 ++++ ADX.mq5 | 155 ++++++ ADXW.mq5 | 189 +++++++ AMA.mq5 | 130 +++++ ASI.mq5 | 112 +++++ ATR.mq5 | 97 ++++ Accelerator.mq5 | 147 ++++++ AccountInfoSample.mq5 | 143 ++++++ Alligator.mq5 | 148 ++++++ ArrayDoubleSample.mq5 | 102 ++++ Awesome_Oscillator.mq5 | 127 +++++ BB.mq5 | 141 ++++++ BW-ZoneTrade.mq5 | 134 +++++ Bears.mq5 | 94 ++++ BitonicSort.mq5 | 217 ++++++++ BlackCrows WhiteSoldiers CCI.mq5 | 657 ++++++++++++++++++++++++ BlackCrows WhiteSoldiers MFI.mq5 | 657 ++++++++++++++++++++++++ BlackCrows WhiteSoldiers RSI.mq5 | 657 ++++++++++++++++++++++++ BlackCrows WhiteSoldiers Stoch.mq5 | 661 +++++++++++++++++++++++++ BullishBearish Engulfing CCI.mq5 | 682 +++++++++++++++++++++++++ BullishBearish Engulfing MFI.mq5 | 682 +++++++++++++++++++++++++ BullishBearish Engulfing RSI.mq5 | 682 +++++++++++++++++++++++++ BullishBearish Engulfing Stoch.mq5 | 688 ++++++++++++++++++++++++++ BullishBearish Harami CCI.mq5 | 682 +++++++++++++++++++++++++ BullishBearish Harami MFI.mq5 | 682 +++++++++++++++++++++++++ BullishBearish Harami RSI.mq5 | 682 +++++++++++++++++++++++++ BullishBearish Harami Stoch.mq5 | 685 +++++++++++++++++++++++++ BullishBearish MeetingLines CCI.mq5 | 654 ++++++++++++++++++++++++ BullishBearish MeetingLines MFI.mq5 | 652 ++++++++++++++++++++++++ BullishBearish MeetingLines RSI.mq5 | 653 ++++++++++++++++++++++++ BullishBearish MeetingLines Stoch.mq5 | 662 +++++++++++++++++++++++++ Bulls.mq5 | 94 ++++ CCI.mq5 | 94 ++++ CHO.mq5 | 131 +++++ CHV.mq5 | 118 +++++ Camarilla Channel.mq5 | 250 ++++++++++ CanvasSample.mq5 | 211 ++++++++ ChartInChart.mq5 | Bin 0 -> 30544 bytes ChartPanel.mq5 | 64 +++ CheckEntry_IAC.mq5 | 33 ++ CheckEntry_MacD.mq5 | 35 ++ ColorBars.mq5 | 82 +++ ColorCandlesDaily.mq5 | 78 +++ ColorLine.mq5 | 133 +++++ Controls.mq5 | 45 ++ Correlation Matrix 3D.mq5 | Bin 0 -> 77280 bytes Custom Moving Average.mq5 | 199 ++++++++ DEMA.mq5 | 70 +++ DPO.mq5 | 69 +++ DarkCloud PiercingLine CCI.mq5 | 682 +++++++++++++++++++++++++ DarkCloud PiercingLine MFI.mq5 | 681 +++++++++++++++++++++++++ DarkCloud PiercingLine RSI.mq5 | 683 +++++++++++++++++++++++++ DarkCloud PiercingLine Stoch.mq5 | 687 +++++++++++++++++++++++++ DeMark Channel.mq5 | 204 ++++++++ DeMarker.mq5 | 111 +++++ Donchian Channel.mq5 | 160 ++++++ Envelopes.mq5 | 108 ++++ ExpertMACD.mq5 | 168 +++++++ ExpertMAMA.mq5 | 175 +++++++ ExpertMAPSAR.mq5 | 171 +++++++ ExpertMAPSARSizeOptimized.mq5 | 176 +++++++ FFT.mq5 | 311 ++++++++++++ Fibonacci Channel.mq5 | 264 ++++++++++ FlameChart.mq5 | 73 +++ Force_Index.mq5 | 101 ++++ FrAMA.mq5 | 81 +++ Fractals.mq5 | 88 ++++ Gator.mq5 | 264 ++++++++++ Gator_2.mq5 | 252 ++++++++++ HangingMan Hammer CCI.mq5 | 679 +++++++++++++++++++++++++ HangingMan Hammer MFI.mq5 | 679 +++++++++++++++++++++++++ HangingMan Hammer RSI.mq5 | 677 +++++++++++++++++++++++++ HangingMan Hammer Stoch.mq5 | 683 +++++++++++++++++++++++++ Heiken_Ashi.mq5 | 94 ++++ HistogramChartSample.mq5 | 66 +++ Ichimoku.mq5 | 130 +++++ Keltner Channel.mq5 | 246 +++++++++ LineChartSample.mq5 | 66 +++ MACD EA.mq5 | 41 ++ MACD Sample.mq5 | 451 +++++++++++++++++ MACD.mq5 | 123 +++++ 81 files changed, 23825 insertions(+) create mode 100644 AD.mq5 create mode 100644 ADX.mq5 create mode 100644 ADXW.mq5 create mode 100644 AMA.mq5 create mode 100644 ASI.mq5 create mode 100644 ATR.mq5 create mode 100644 Accelerator.mq5 create mode 100644 AccountInfoSample.mq5 create mode 100644 Alligator.mq5 create mode 100644 ArrayDoubleSample.mq5 create mode 100644 Awesome_Oscillator.mq5 create mode 100644 BB.mq5 create mode 100644 BW-ZoneTrade.mq5 create mode 100644 Bears.mq5 create mode 100644 BitonicSort.mq5 create mode 100644 BlackCrows WhiteSoldiers CCI.mq5 create mode 100644 BlackCrows WhiteSoldiers MFI.mq5 create mode 100644 BlackCrows WhiteSoldiers RSI.mq5 create mode 100644 BlackCrows WhiteSoldiers Stoch.mq5 create mode 100644 BullishBearish Engulfing CCI.mq5 create mode 100644 BullishBearish Engulfing MFI.mq5 create mode 100644 BullishBearish Engulfing RSI.mq5 create mode 100644 BullishBearish Engulfing Stoch.mq5 create mode 100644 BullishBearish Harami CCI.mq5 create mode 100644 BullishBearish Harami MFI.mq5 create mode 100644 BullishBearish Harami RSI.mq5 create mode 100644 BullishBearish Harami Stoch.mq5 create mode 100644 BullishBearish MeetingLines CCI.mq5 create mode 100644 BullishBearish MeetingLines MFI.mq5 create mode 100644 BullishBearish MeetingLines RSI.mq5 create mode 100644 BullishBearish MeetingLines Stoch.mq5 create mode 100644 Bulls.mq5 create mode 100644 CCI.mq5 create mode 100644 CHO.mq5 create mode 100644 CHV.mq5 create mode 100644 Camarilla Channel.mq5 create mode 100644 CanvasSample.mq5 create mode 100644 ChartInChart.mq5 create mode 100644 ChartPanel.mq5 create mode 100644 CheckEntry_IAC.mq5 create mode 100644 CheckEntry_MacD.mq5 create mode 100644 ColorBars.mq5 create mode 100644 ColorCandlesDaily.mq5 create mode 100644 ColorLine.mq5 create mode 100644 Controls.mq5 create mode 100644 Correlation Matrix 3D.mq5 create mode 100644 Custom Moving Average.mq5 create mode 100644 DEMA.mq5 create mode 100644 DPO.mq5 create mode 100644 DarkCloud PiercingLine CCI.mq5 create mode 100644 DarkCloud PiercingLine MFI.mq5 create mode 100644 DarkCloud PiercingLine RSI.mq5 create mode 100644 DarkCloud PiercingLine Stoch.mq5 create mode 100644 DeMark Channel.mq5 create mode 100644 DeMarker.mq5 create mode 100644 Donchian Channel.mq5 create mode 100644 Envelopes.mq5 create mode 100644 ExpertMACD.mq5 create mode 100644 ExpertMAMA.mq5 create mode 100644 ExpertMAPSAR.mq5 create mode 100644 ExpertMAPSARSizeOptimized.mq5 create mode 100644 FFT.mq5 create mode 100644 Fibonacci Channel.mq5 create mode 100644 FlameChart.mq5 create mode 100644 Force_Index.mq5 create mode 100644 FrAMA.mq5 create mode 100644 Fractals.mq5 create mode 100644 Gator.mq5 create mode 100644 Gator_2.mq5 create mode 100644 HangingMan Hammer CCI.mq5 create mode 100644 HangingMan Hammer MFI.mq5 create mode 100644 HangingMan Hammer RSI.mq5 create mode 100644 HangingMan Hammer Stoch.mq5 create mode 100644 Heiken_Ashi.mq5 create mode 100644 HistogramChartSample.mq5 create mode 100644 Ichimoku.mq5 create mode 100644 Keltner Channel.mq5 create mode 100644 LineChartSample.mq5 create mode 100644 MACD EA.mq5 create mode 100644 MACD Sample.mq5 create mode 100644 MACD.mq5 diff --git a/AD.mq5 b/AD.mq5 new file mode 100644 index 0000000..5783d4f --- /dev/null +++ b/AD.mq5 @@ -0,0 +1,90 @@ +//+------------------------------------------------------------------+ +//| AD.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property description "Accumulation/Distribution" + +#property indicator_separate_window +#property indicator_buffers 1 +#property indicator_plots 1 +#property indicator_type1 DRAW_LINE +#property indicator_color1 LightSeaGreen +#property indicator_label1 "A/D" +//--- input params +input ENUM_APPLIED_VOLUME InpVolumeType=VOLUME_TICK; // Volume type +//--- indicator buffer +double ExtADbuffer[]; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- indicator digits + IndicatorSetInteger(INDICATOR_DIGITS,0); +//--- indicator short name + IndicatorSetString(INDICATOR_SHORTNAME,"A/D"); +//--- index buffer + SetIndexBuffer(0,ExtADbuffer); +//--- set index draw begin + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,1); + } +//+------------------------------------------------------------------+ +//| Accumulation/Distribution | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { +//--- check for bars count + if(rates_total<2) + return(0); //exit with zero result +//--- get current position + int pos=prev_calculated-1; + if(pos<0) + pos=0; +//--- calculate with appropriate volumes + if(InpVolumeType==VOLUME_TICK) + Calculate(rates_total,pos,high,low,close,tick_volume); + else + Calculate(rates_total,pos,high,low,close,volume); +//--- + return(rates_total); + } +//+------------------------------------------------------------------+ +//| Calculating with selected volume | +//+------------------------------------------------------------------+ +void Calculate(const int rates_total,const int pos, + const double &high[], + const double &low[], + const double &close[], + const long &volume[]) + { +//--- main cycle + for(int i=pos; i0) + sum+=ExtADbuffer[i-1]; + ExtADbuffer[i]=sum; + } + } +//+------------------------------------------------------------------+ diff --git a/ADX.mq5 b/ADX.mq5 new file mode 100644 index 0000000..b56d42f --- /dev/null +++ b/ADX.mq5 @@ -0,0 +1,155 @@ +//+------------------------------------------------------------------+ +//| ADX.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property description "Average Directional Movement Index" +#include + +#property indicator_separate_window +#property indicator_buffers 6 +#property indicator_plots 3 +#property indicator_type1 DRAW_LINE +#property indicator_color1 LightSeaGreen +#property indicator_style1 STYLE_SOLID +#property indicator_width1 1 +#property indicator_type2 DRAW_LINE +#property indicator_color2 YellowGreen +#property indicator_style2 STYLE_DOT +#property indicator_width2 1 +#property indicator_type3 DRAW_LINE +#property indicator_color3 Wheat +#property indicator_style3 STYLE_DOT +#property indicator_width3 1 +#property indicator_label1 "ADX" +#property indicator_label2 "+DI" +#property indicator_label3 "-DI" +//--- input parameters +input int InpPeriodADX=14; // Period ADX +//--- indicator buffers +double ExtADXBuffer[]; +double ExtPDIBuffer[]; +double ExtNDIBuffer[]; +double ExtPDBuffer[]; +double ExtNDBuffer[]; +double ExtTmpBuffer[]; + +int ExtADXPeriod; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- check for input parameters + if(InpPeriodADX>=100 || InpPeriodADX<=0) + { + ExtADXPeriod=14; + PrintFormat("Incorrect value for input variable Period_ADX=%d. Indicator will use value=%d for calculations.",InpPeriodADX,ExtADXPeriod); + } + else + ExtADXPeriod=InpPeriodADX; +//--- indicator buffers + SetIndexBuffer(0,ExtADXBuffer); + SetIndexBuffer(1,ExtPDIBuffer); + SetIndexBuffer(2,ExtNDIBuffer); + SetIndexBuffer(3,ExtPDBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(4,ExtNDBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(5,ExtTmpBuffer,INDICATOR_CALCULATIONS); +//--- indicator digits + IndicatorSetInteger(INDICATOR_DIGITS,2); +//--- set draw begin + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtADXPeriod<<1); + PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtADXPeriod); + PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtADXPeriod); +//--- indicator short name + string short_name="ADX("+string(ExtADXPeriod)+")"; + IndicatorSetString(INDICATOR_SHORTNAME,short_name); + PlotIndexSetString(0,PLOT_LABEL,short_name); + } +//+------------------------------------------------------------------+ +//| Custom indicator iteration function | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { +//--- checking for bars count + if(rates_total1) + start=prev_calculated-1; + else + { + start=1; + ExtPDIBuffer[0]=0.0; + ExtNDIBuffer[0]=0.0; + ExtADXBuffer[0]=0.0; + } +//--- main cycle + for(int i=start; itmp_neg) + tmp_neg=0.0; + else + { + if(tmp_pos +//--- +#property indicator_separate_window +#property indicator_buffers 10 +#property indicator_plots 3 +#property indicator_type1 DRAW_LINE +#property indicator_style1 STYLE_SOLID +#property indicator_width1 1 +#property indicator_color1 LightSeaGreen +#property indicator_type2 DRAW_LINE +#property indicator_style2 STYLE_DOT +#property indicator_width2 1 +#property indicator_color2 YellowGreen +#property indicator_type3 DRAW_LINE +#property indicator_style3 STYLE_DOT +#property indicator_width3 1 +#property indicator_color3 Wheat +#property indicator_label1 "ADX Wilder" +#property indicator_label2 "+DI" +#property indicator_label3 "-DI" +//--- input parameters +input int InpPeriodADXW=14; // Period ADX +//--- indicator buffers +double ExtADXWBuffer[]; +double ExtPDIBuffer[]; +double ExtNDIBuffer[]; +double ExtPDSBuffer[]; +double ExtNDSBuffer[]; +double ExtPDBuffer[]; +double ExtNDBuffer[]; +double ExtTRBuffer[]; +double ExtATRBuffer[]; +double ExtDXBuffer[]; + +int ExtADXWPeriod; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- check for input parameters + if(InpPeriodADXW>=100 || InpPeriodADXW<=0) + { + ExtADXWPeriod=14; + PrintFormat("Incorrect value for input variable InpPeriodADXW=%d. Indicator will use value=%d for calculations.",InpPeriodADXW,ExtADXWPeriod); + } + else + ExtADXWPeriod=InpPeriodADXW; +//--- indicator buffers + SetIndexBuffer(0,ExtADXWBuffer); + SetIndexBuffer(1,ExtPDIBuffer); + SetIndexBuffer(2,ExtNDIBuffer); +//--- calculation buffers + SetIndexBuffer(3,ExtPDBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(4,ExtNDBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(5,ExtDXBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(6,ExtTRBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(7,ExtATRBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(8,ExtPDSBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(9,ExtNDSBuffer,INDICATOR_CALCULATIONS); +//--- set draw begin + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtADXWPeriod<<1); + PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtADXWPeriod+1); + PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtADXWPeriod+1); +//--- indicator short name + string short_name="ADX Wilder("+string(ExtADXWPeriod)+")"; + IndicatorSetString(INDICATOR_SHORTNAME,short_name); + PlotIndexSetString(0,PLOT_LABEL,short_name); +//--- indicator digits + IndicatorSetInteger(INDICATOR_DIGITS,2); + } +//+------------------------------------------------------------------+ +//| Custom indicator iteration function | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { +//--- checking for bars count + if(rates_total1) + start=prev_calculated-1; + else + { + start=1; + for(int i=0; i1e-7) + ExtT=InpT; + else + { + ExtT=300.0; + PrintFormat("Input parameter T has wrong value. Indicator will use T = %f.",ExtT); + } +//--- define buffers + SetIndexBuffer(0,ExtASIBuffer); + SetIndexBuffer(1,ExtSIBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(2,ExtTRBuffer,INDICATOR_CALCULATIONS); +//--- draw begin settings + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,1); +//--- number of digits of indicator value + IndicatorSetInteger(INDICATOR_DIGITS,2); +//--- calculate ExtTpoints value + if(_Point>1e-7) + ExtTpoints=ExtT*_Point; + else + ExtTpoints=ExtT*MathPow(10,-_Digits); + } +//+------------------------------------------------------------------+ +//| Custom indicator iteration function | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + if(rates_total<2) + return(0); +//--- start calculation + int pos=prev_calculated-1; +//--- correct position, when it's first iteration + if(pos<=0) + { + pos=1; + ExtASIBuffer[0]=0.0; + ExtSIBuffer[0]=0.0; + ExtTRBuffer[0]=high[0]-low[0]; + } +//--- main cycle + for(int i=pos; i=dLow && dPrevClose<=dHigh)) + { + if(dPrevClose>dHigh) + ER=MathAbs(dHigh-dPrevClose); + if(dPrevCloserates_total || prev_calculated<0) + to_copy=rates_total; + else + { + to_copy=rates_total-prev_calculated; + if(prev_calculated>0) + to_copy++; + } +//--- get FastSMA buffer + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtFastSMAHandle,0,0,to_copy,ExtFastBuffer)<=0) + { + Print("Getting fast SMA is failed! Error ",GetLastError()); + return(0); + } +//--- get SlowSMA buffer + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtSlowSMAHandle,0,0,to_copy,ExtSlowBuffer)<=0) + { + Print("Getting slow SMA is failed! Error ",GetLastError()); + return(0); + } +//--- calculations + int i,start; +//--- first calculation or number of bars was changed + if(prev_calculated=ExtACBuffer[i-1]) + ExtColorBuffer[i]=0.0; // set color Green + else + ExtColorBuffer[i]=1.0; // set color Red + } +//--- return value of prev_calculated for next call + return(rates_total); + } + +//+------------------------------------------------------------------+ diff --git a/AccountInfoSample.mq5 b/AccountInfoSample.mq5 new file mode 100644 index 0000000..999480c --- /dev/null +++ b/AccountInfoSample.mq5 @@ -0,0 +1,143 @@ +//+------------------------------------------------------------------+ +//| TestAccountInfo.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +//--- +#include +#include +//--- +#include "AccountInfoSampleInit.mqh" +//+------------------------------------------------------------------+ +//| Script to testing the use of class CAccountInfo. | +//+------------------------------------------------------------------+ +//--- +//+------------------------------------------------------------------+ +//| Account Info Sample script class | +//+------------------------------------------------------------------+ +class CAccountInfoSample + { +protected: + CAccountInfo m_account; + //--- chart objects + CChartObjectLabel m_label[19]; + CChartObjectLabel m_label_info[19]; + +public: + CAccountInfoSample(void); + ~CAccountInfoSample(void); + //--- + bool Init(void); + void Deinit(void); + void Processing(void); + +private: + void AccountInfoToChart(void); + }; +//--- +CAccountInfoSample ExtScript; +//+------------------------------------------------------------------+ +//| Constructor | +//+------------------------------------------------------------------+ +CAccountInfoSample::CAccountInfoSample(void) + { + } +//+------------------------------------------------------------------+ +//| Destructor | +//+------------------------------------------------------------------+ +CAccountInfoSample::~CAccountInfoSample(void) + { + } +//+------------------------------------------------------------------+ +//| Method Init. | +//+------------------------------------------------------------------+ +bool CAccountInfoSample::Init(void) + { + int i,sy=10; + int dy=16; + color color_label; + color color_info; +//--- tuning colors + color_info =(color)(ChartGetInteger(0,CHART_COLOR_BACKGROUND)^0xFFFFFF); + color_label=(color)(color_info^0x202020); +//--- + if(ChartGetInteger(0,CHART_SHOW_OHLC)) + sy+=16; +//--- creation Labels[] + for(i=0;i<19;i++) + { + m_label[i].Create(0,"Label"+IntegerToString(i),0,20,sy+dy*i); + m_label[i].Description(init_str[i]); + m_label[i].Color(color_label); + m_label[i].FontSize(8); + //--- + m_label_info[i].Create(0,"LabelInfo"+IntegerToString(i),0,120,sy+dy*i); + m_label_info[i].Description(" "); + m_label_info[i].Color(color_info); + m_label_info[i].FontSize(8); + } + AccountInfoToChart(); +//--- redraw chart + ChartRedraw(); +//--- + return(true); + } +//+------------------------------------------------------------------+ +//| Method Deinit. | +//+------------------------------------------------------------------+ +void CAccountInfoSample::Deinit(void) + { + } +//+------------------------------------------------------------------+ +//| Method Processing. | +//+------------------------------------------------------------------+ +void CAccountInfoSample::Processing(void) + { + AccountInfoToChart(); +//--- redraw chart + ChartRedraw(); + Sleep(50); + } +//+------------------------------------------------------------------+ +//| Method InfoToChart. | +//+------------------------------------------------------------------+ +void CAccountInfoSample::AccountInfoToChart(void) + { + m_label_info[0].Description((string)m_account.Login()); + m_label_info[1].Description(m_account.TradeModeDescription()); + m_label_info[2].Description((string)m_account.Leverage()); + m_label_info[3].Description(m_account.MarginModeDescription()); + m_label_info[4].Description((string)m_account.TradeAllowed()); + m_label_info[5].Description((string)m_account.TradeExpert()); + m_label_info[6].Description(DoubleToString(m_account.Balance(),2)); + m_label_info[7].Description(DoubleToString(m_account.Credit(),2)); + m_label_info[8].Description(DoubleToString(m_account.Profit(),2)); + m_label_info[9].Description(DoubleToString(m_account.Equity(),2)); + m_label_info[10].Description(DoubleToString(m_account.Margin(),2)); + m_label_info[11].Description(DoubleToString(m_account.FreeMargin(),2)); + m_label_info[12].Description(DoubleToString(m_account.MarginLevel(),2)); + m_label_info[13].Description(DoubleToString(m_account.MarginCall(),2)); + m_label_info[14].Description(DoubleToString(m_account.MarginStopOut(),2)); + m_label_info[15].Description(m_account.Name()); + m_label_info[16].Description(m_account.Server()); + m_label_info[17].Description(m_account.Currency()); + m_label_info[18].Description(m_account.Company()); + } +//+------------------------------------------------------------------+ +//| Script program start function | +//+------------------------------------------------------------------+ +void OnStart(void) + { +//--- call init function + if(ExtScript.Init()) + { + //--- cycle until the script is not halted + while(!IsStopped()) + ExtScript.Processing(); + } +//--- call deinit function + ExtScript.Deinit(); + } +//+------------------------------------------------------------------+ diff --git a/Alligator.mq5 b/Alligator.mq5 new file mode 100644 index 0000000..59b25f6 --- /dev/null +++ b/Alligator.mq5 @@ -0,0 +1,148 @@ +//+------------------------------------------------------------------+ +//| Alligator.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" + +//--- indicator settings +#property indicator_chart_window +#property indicator_buffers 3 +#property indicator_plots 3 +#property indicator_type1 DRAW_LINE +#property indicator_type2 DRAW_LINE +#property indicator_type3 DRAW_LINE +#property indicator_color1 Blue +#property indicator_color2 Red +#property indicator_color3 Lime +#property indicator_width1 1 +#property indicator_width2 1 +#property indicator_width3 1 +#property indicator_label1 "Jaws" +#property indicator_label2 "Teeth" +#property indicator_label3 "Lips" +//--- input parameters +input int InpJawsPeriod=13; // Jaws period +input int InpJawsShift=8; // Jaws shift +input int InpTeethPeriod=8; // Teeth period +input int InpTeethShift=5; // Teeth shift +input int InpLipsPeriod=5; // Lips period +input int InpLipsShift=3; // Lips shift +input ENUM_MA_METHOD InpMAMethod=MODE_SMMA; // Moving average method +input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_MEDIAN; // Applied price +//--- indicator buffers +double ExtJaws[]; +double ExtTeeth[]; +double ExtLips[]; +//--- handles for moving averages +int ExtJawsHandle; +int ExtTeethHandle; +int ExtLipsHandle; +//--- bars minimum for calculation +int ExtBarsMinimum; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- indicator buffers mapping + SetIndexBuffer(0,ExtJaws,INDICATOR_DATA); + SetIndexBuffer(1,ExtTeeth,INDICATOR_DATA); + SetIndexBuffer(2,ExtLips,INDICATOR_DATA); +//--- set accuracy + IndicatorSetInteger(INDICATOR_DIGITS,_Digits); +//--- sets first bar from what index will be drawn + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpJawsPeriod-1); + PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpTeethPeriod-1); + PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,InpLipsPeriod-1); +//--- line shifts when drawing + PlotIndexSetInteger(0,PLOT_SHIFT,InpJawsShift); + PlotIndexSetInteger(1,PLOT_SHIFT,InpTeethShift); + PlotIndexSetInteger(2,PLOT_SHIFT,InpLipsShift); +//--- name for DataWindow + PlotIndexSetString(0,PLOT_LABEL,"Jaws("+string(InpJawsPeriod)+")"); + PlotIndexSetString(1,PLOT_LABEL,"Teeth("+string(InpTeethPeriod)+")"); + PlotIndexSetString(2,PLOT_LABEL,"Lips("+string(InpLipsPeriod)+")"); +//--- get MA's handles + ExtJawsHandle=iMA(NULL,0,InpJawsPeriod,0,InpMAMethod,InpAppliedPrice); + ExtTeethHandle=iMA(NULL,0,InpTeethPeriod,0,InpMAMethod,InpAppliedPrice); + ExtLipsHandle=iMA(NULL,0,InpLipsPeriod,0,InpMAMethod,InpAppliedPrice); +//--- bars minimum for calculation + ExtBarsMinimum=InpJawsPeriod+InpJawsShift; + if(ExtBarsMinimum<(InpTeethPeriod+InpTeethShift)) + ExtBarsMinimum=InpTeethPeriod+InpTeethShift; + if(ExtBarsMinimum<(InpLipsPeriod+InpLipsPeriod)) + ExtBarsMinimum=InpLipsPeriod+InpLipsPeriod; + } +//+------------------------------------------------------------------+ +//| Alligator OnCalculate function | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + if(rates_totalrates_total || prev_calculated<0) + to_copy=rates_total; + else + { + to_copy=rates_total-prev_calculated; + if(prev_calculated>0) + to_copy++; + } +//--- get ma buffers + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtJawsHandle,0,0,to_copy,ExtJaws)<=0) + { + Print("getting ExtJawsHandle is failed! Error ",GetLastError()); + return(0); + } + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtTeethHandle,0,0,to_copy,ExtTeeth)<=0) + { + Print("getting ExtTeethHandle is failed! Error ",GetLastError()); + return(0); + } + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtLipsHandle,0,0,to_copy,ExtLips)<=0) + { + Print("getting ExtLipsHandle is failed! Error ",GetLastError()); + return(0); + } +//--- return value of prev_calculated for next call + return(rates_total); + } +//+------------------------------------------------------------------+ diff --git a/ArrayDoubleSample.mq5 b/ArrayDoubleSample.mq5 new file mode 100644 index 0000000..ee36284 --- /dev/null +++ b/ArrayDoubleSample.mq5 @@ -0,0 +1,102 @@ +//+------------------------------------------------------------------+ +//| ArrayDoubleSample.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +//--- +#include +#include +//--- +const int ExtArraySize=10000; +const int ExtArrayAdd=100; +string ExtFileName="ArrayDoubleSample.bin"; +//+------------------------------------------------------------------+ +//| Example class CArrayDouble | +//+------------------------------------------------------------------+ +int OnStart(void) + { + int i,pos; + double key; + CFileBin File; + CArrayDouble ArrayDouble; +//--- + printf("Start sample %s.",__FILE__); +//--- fill an array of background information +//--- open file for reading + if(File.Open(ExtFileName,FILE_READ)!=INVALID_HANDLE) + { + //--- read array from file + if(!ArrayDouble.Load(File.Handle())) + { + //--- error reading from file + printf("%s (%4d): error %d",__FILE__,__LINE__,GetLastError()); + } + //--- do not forget close file + File.Close(); + } +//--- check whether enough information in the array + if(ArrayDouble.Total()rates_total || prev_calculated<0) + to_copy=rates_total; + else + { + to_copy=rates_total-prev_calculated; + if(prev_calculated>0) + to_copy++; + } +//--- get FastSMA buffer + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtFastSMAHandle,0,0,to_copy,ExtFastBuffer)<=0) + { + Print("Getting fast SMA is failed! Error ",GetLastError()); + return(0); + } +//--- get SlowSMA buffer + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtSlowSMAHandle,0,0,to_copy,ExtSlowBuffer)<=0) + { + Print("Getting slow SMA is failed! Error ",GetLastError()); + return(0); + } +//--- first calculation or number of bars was changed + int i,start; + if(prev_calculated<=DATA_LIMIT) + { + for(i=0; iExtAOBuffer[i-1]) + ExtColorBuffer[i]=0.0; // set color Green + else + ExtColorBuffer[i]=1.0; // set color Red + } +//--- return value of prev_calculated for next call + return(rates_total); + } + +//+------------------------------------------------------------------+ diff --git a/BB.mq5 b/BB.mq5 new file mode 100644 index 0000000..42f925f --- /dev/null +++ b/BB.mq5 @@ -0,0 +1,141 @@ +//+------------------------------------------------------------------+ +//| BB.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property description "Bollinger Bands" +#include +//--- +#property indicator_chart_window +#property indicator_buffers 4 +#property indicator_plots 3 +#property indicator_type1 DRAW_LINE +#property indicator_color1 LightSeaGreen +#property indicator_type2 DRAW_LINE +#property indicator_color2 LightSeaGreen +#property indicator_type3 DRAW_LINE +#property indicator_color3 LightSeaGreen +#property indicator_label1 "Bands middle" +#property indicator_label2 "Bands upper" +#property indicator_label3 "Bands lower" +//--- input parametrs +input int InpBandsPeriod=20; // Period +input int InpBandsShift=0; // Shift +input double InpBandsDeviations=2.0; // Deviation +//--- global variables +int ExtBandsPeriod,ExtBandsShift; +double ExtBandsDeviations; +int ExtPlotBegin=0; +//--- indicator buffer +double ExtMLBuffer[]; +double ExtTLBuffer[]; +double ExtBLBuffer[]; +double ExtStdDevBuffer[]; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- check for input values + if(InpBandsPeriod<2) + { + ExtBandsPeriod=20; + PrintFormat("Incorrect value for input variable InpBandsPeriod=%d. Indicator will use value=%d for calculations.",InpBandsPeriod,ExtBandsPeriod); + } + else + ExtBandsPeriod=InpBandsPeriod; + if(InpBandsShift<0) + { + ExtBandsShift=0; + PrintFormat("Incorrect value for input variable InpBandsShift=%d. Indicator will use value=%d for calculations.",InpBandsShift,ExtBandsShift); + } + else + ExtBandsShift=InpBandsShift; + if(InpBandsDeviations==0.0) + { + ExtBandsDeviations=2.0; + PrintFormat("Incorrect value for input variable InpBandsDeviations=%f. Indicator will use value=%f for calculations.",InpBandsDeviations,ExtBandsDeviations); + } + else + ExtBandsDeviations=InpBandsDeviations; +//--- define buffers + SetIndexBuffer(0,ExtMLBuffer); + SetIndexBuffer(1,ExtTLBuffer); + SetIndexBuffer(2,ExtBLBuffer); + SetIndexBuffer(3,ExtStdDevBuffer,INDICATOR_CALCULATIONS); +//--- set index labels + PlotIndexSetString(0,PLOT_LABEL,"Bands("+string(ExtBandsPeriod)+") Middle"); + PlotIndexSetString(1,PLOT_LABEL,"Bands("+string(ExtBandsPeriod)+") Upper"); + PlotIndexSetString(2,PLOT_LABEL,"Bands("+string(ExtBandsPeriod)+") Lower"); +//--- indicator name + IndicatorSetString(INDICATOR_SHORTNAME,"Bollinger Bands"); +//--- indexes draw begin settings + ExtPlotBegin=ExtBandsPeriod-1; + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtBandsPeriod); + PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtBandsPeriod); + PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtBandsPeriod); +//--- indexes shift settings + PlotIndexSetInteger(0,PLOT_SHIFT,ExtBandsShift); + PlotIndexSetInteger(1,PLOT_SHIFT,ExtBandsShift); + PlotIndexSetInteger(2,PLOT_SHIFT,ExtBandsShift); +//--- number of digits of indicator value + IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1); + } +//+------------------------------------------------------------------+ +//| Bollinger Bands | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const int begin, + const double &price[]) + { + if(rates_total1) + pos=prev_calculated-1; + else + pos=0; +//--- main cycle + for(int i=pos; i=period) + { + for(int i=0; irates_total || prev_calculated<0) + to_copy=rates_total; + else + { + to_copy=rates_total-prev_calculated; + if(prev_calculated>0) + to_copy++; + } +//--- get AC buffer + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtACHandle,0,0,to_copy,ExtACBuffer)<=0) + { + Print("Getting iAC is failed! Error ",GetLastError()); + return(0); + } +//--- get AO buffer + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtAOHandle,0,0,to_copy,ExtAOBuffer)<=0) + { + Print("Getting iAO is failed! Error ",GetLastError()); + return(0); + } +//--- set first bar from what calculation will start + int start; + if(prev_calculatedExtACBuffer[i-1] && ExtAOBuffer[i]>ExtAOBuffer[i-1]) + ExtColorBuffer[i]=0.0; + //--- check for Red Zone and set Color Red + if(ExtACBuffer[i]rates_total || prev_calculated<0) + to_copy=rates_total; + else + { + to_copy=rates_total-prev_calculated; + if(prev_calculated>0) + to_copy++; + } +//--- get ma buffers + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtEmaHandle,0,0,to_copy,ExtTempBuffer)<=0) + { + Print("getting ExtEmaHandle is failed! Error ",GetLastError()); + return(0); + } +//--- first calculation or number of bars was changed + int start; + if(prev_calculated +#resource "Kernels/bitonicsort.cl" as string cl_program +//+------------------------------------------------------------------+ +//| QuickSortAscending | +//+------------------------------------------------------------------+ +//| The function sorts array[] QuickSort algorithm. | +//| | +//| Arguments: | +//| array : Array with values to sort | +//| first : First element index | +//| last : Last element index | +//| | +//| Return value: None | +//+------------------------------------------------------------------+ +void QuickSortAscending(double &array[],int first,int last) + { + int i,j; + double p_double,t_double; + if(first<0 || last<0) + return; + i=first; + j=last; + while(i>1]; + while(ip_double) + { + if(j==0) + break; + j--; + } + if(i<=j) + { + //-- swap elements i and j + t_double=array[i]; + array[i]=array[j]; + array[j]=t_double; + i++; + if(j==0) + break; + j--; + } + } + if(first>1; +//--- + uint passes_total=0; + uint stages_total=0; +//--- + for(uint temp=data_count; temp>1; temp>>=1) + stages_total++; +//--- GPU calculation start + time_gpu=GetMicrosecondCount(); + for(uint stage=0; stage +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpPeriodCCI =37; // CCI period +input ENUM_APPLIED_PRICE InpPrice=PRICE_CLOSE; // price type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot =0.1; // lot +//--- Expert ID +input long InpMagicNumber=120100; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error + +//--- indicator handle +int ExtIndicatorHandle=INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iCCI(_Symbol, _Period, InpPeriodCCI, InpPrice); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating CCI indicator"); + return(INIT_FAILED); + } +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // long black + (Open(2)-Close(2)>AvgBody(1)) && + (Open(1)-Close(1)>AvgBody(1)) && + (MidPoint(2)AvgBody(1)) && // long white + (Close(2)-Open(2)>AvgBody(1)) && + (Close(1)-Open(1)>AvgBody(1)) && + (MidPoint(2)>MidPoint(3)) && // higher midpoints + (MidPoint(1)>MidPoint(2))) + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_BUY; + ExtPatternInfo="\r\n3 White Soldiers detected"; + ExtDirection="Buy"; + return(true); + } + +//--- result of checking + return(ExtCheckPassed); + } +//+------------------------------------------------------------------+ +//| Returns true in case of successful confirmation check | +//+------------------------------------------------------------------+ +bool CheckConfirmation() + { + ExtConfirmed=false; +//--- if there is no pattern, do not search for confirmation + if(!ExtPatternDetected) + return(true); + +//--- get the value of the stochastic indicator to confirm the signal + double signal=CCI(1); + if(signal==EMPTY_VALUE) + { + //--- failed to get indicator value, check failed + return(false); + } + +//--- check the Buy signal + if(ExtSignalOpen==SIGNAL_BUY && (signal<-50)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: CCI<-50"; + } + +//--- check the Sell signal + if(ExtSignalOpen==SIGNAL_SELL && (signal>50)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: CCI>50"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((CCI(1)<80) && (CCI(2)>80)) || ((CCI(1)<-80) && (CCI(2)>-80))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((CCI(1)>-80) && (CCI(2)<-80)) || ((CCI(1)>80) && (CCI(2)<80))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| CCI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double CCI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the CCI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/BlackCrows WhiteSoldiers MFI.mq5 b/BlackCrows WhiteSoldiers MFI.mq5 new file mode 100644 index 0000000..15fc617 --- /dev/null +++ b/BlackCrows WhiteSoldiers MFI.mq5 @@ -0,0 +1,657 @@ +//+------------------------------------------------------------------+ +//| BlackCrows WhiteSoldiers MFI.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpPeriodMFI =37; // MFI period +input ENUM_APPLIED_VOLUME InpVolume=VOLUME_TICK; // volume type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot =0.1; // lot +//--- Expert ID +input long InpMagicNumber=120200; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error + +//--- indicator handle +int ExtIndicatorHandle=INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iMFI(_Symbol, _Period, InpPeriodMFI, InpVolume); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating MFI indicator"); + return(INIT_FAILED); + } +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // long black + (Open(2)-Close(2)>AvgBody(1)) && + (Open(1)-Close(1)>AvgBody(1)) && + (MidPoint(2)AvgBody(1)) && // long white + (Close(2)-Open(2)>AvgBody(1)) && + (Close(1)-Open(1)>AvgBody(1)) && + (MidPoint(2)>MidPoint(3)) && // higher midpoints + (MidPoint(1)>MidPoint(2))) + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_BUY; + ExtPatternInfo="\r\n3 White Soldiers detected"; + ExtDirection="Buy"; + return(true); + } + +//--- result of checking + return(ExtCheckPassed); + } +//+------------------------------------------------------------------+ +//| Returns true in case of successful confirmation check | +//+------------------------------------------------------------------+ +bool CheckConfirmation() + { + ExtConfirmed=false; +//--- if there is no pattern, do not search for confirmation + if(!ExtPatternDetected) + return(true); + +//--- get the value of the stochastic indicator to confirm the signal + double signal=MFI(1); + if(signal==EMPTY_VALUE) + { + //--- failed to get indicator value, check failed + return(false); + } + +//--- check the Buy signal + if(ExtSignalOpen==SIGNAL_BUY && (signal<40)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: MFI<40"; + } + +//--- check the Sell signal + if(ExtSignalOpen==SIGNAL_SELL && (signal>60)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: MFI>60"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((MFI(1)<70) && (MFI(2)>70)) || ((MFI(1)<30) && (MFI(2)>30))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((MFI(1)>30) && (MFI(2)<30)) || ((MFI(1)>70) && (MFI(2)<70))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| MFI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double MFI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the MFI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/BlackCrows WhiteSoldiers RSI.mq5 b/BlackCrows WhiteSoldiers RSI.mq5 new file mode 100644 index 0000000..c7dc87c --- /dev/null +++ b/BlackCrows WhiteSoldiers RSI.mq5 @@ -0,0 +1,657 @@ +//+------------------------------------------------------------------+ +//| BlackCrows WhiteSoldiers RSI.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpPeriodRSI =37; // RSI period +input ENUM_APPLIED_PRICE InpPrice=PRICE_CLOSE; // price type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot =0.1; // lot +//--- Expert ID +input long InpMagicNumber=120300; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error + +//--- indicator handle +int ExtIndicatorHandle=INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iRSI(_Symbol, _Period, InpPeriodRSI, InpPrice); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating CCI indicator"); + return(INIT_FAILED); + } +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // long black + (Open(2)-Close(2)>AvgBody(1)) && + (Open(1)-Close(1)>AvgBody(1)) && + (MidPoint(2)AvgBody(1)) && // long white + (Close(2)-Open(2)>AvgBody(1)) && + (Close(1)-Open(1)>AvgBody(1)) && + (MidPoint(2)>MidPoint(3)) && // higher midpoints + (MidPoint(1)>MidPoint(2))) + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_BUY; + ExtPatternInfo="\r\n3 White Soldiers detected"; + ExtDirection="Buy"; + return(true); + } + +//--- result of checking + return(ExtCheckPassed); + } +//+------------------------------------------------------------------+ +//| Returns true in case of successful confirmation check | +//+------------------------------------------------------------------+ +bool CheckConfirmation() + { + ExtConfirmed=false; +//--- if there is no pattern, do not search for confirmation + if(!ExtPatternDetected) + return(true); + +//--- get the value of the stochastic indicator to confirm the signal + double signal=RSI(1); + if(signal==EMPTY_VALUE) + { + //--- failed to get indicator value, check failed + return(false); + } + +//--- check the Buy signal + if(ExtSignalOpen==SIGNAL_BUY && (signal<40)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: RSI<40"; + } + +//--- check the Sell signal + if(ExtSignalOpen==SIGNAL_SELL && (signal>60)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: RSI>60"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((RSI(1)<70) && (RSI(2)>70)) || ((RSI(1)<30) && (RSI(2)>30))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((RSI(1)>30) && (RSI(2)<30)) || ((RSI(1)>70) && (RSI(2)<70))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| RSI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double RSI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the RSI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/BlackCrows WhiteSoldiers Stoch.mq5 b/BlackCrows WhiteSoldiers Stoch.mq5 new file mode 100644 index 0000000..b6474d7 --- /dev/null +++ b/BlackCrows WhiteSoldiers Stoch.mq5 @@ -0,0 +1,661 @@ +//+------------------------------------------------------------------+ +//| BlackCrows WhiteSoldiers Stoch.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpStochK =47; // period %K +input int InpStochD =9; // period %D +input int InpStochSlow =13; // smoothing period %K +input ENUM_STO_PRICE InpStochApplied =STO_LOWHIGH; // calculation type +input ENUM_MA_METHOD InpStochMA =MODE_SMA; // smoothing type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot =0.1; // lot +//--- Expert ID +input long InpMagicNumber=120400; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error +//--- indicator handle +int ExtIndicatorHandle=INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iStochastic(_Symbol, _Period, InpStochK, InpStochD, InpStochSlow, InpStochMA, InpStochApplied); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating iStochastic indicator"); + return(INIT_FAILED); + } +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // long black + (Open(2)-Close(2)>AvgBody(1)) && + (Open(1)-Close(1)>AvgBody(1)) && + (MidPoint(2)AvgBody(1)) && // long white + (Close(2)-Open(2)>AvgBody(1)) && + (Close(1)-Open(1)>AvgBody(1)) && + (MidPoint(2)>MidPoint(3)) && // higher midpoints + (MidPoint(1)>MidPoint(2))) + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_BUY; + ExtPatternInfo="\r\n3 White Soldiers detected"; + ExtDirection="Buy"; + return(true); + } + +//--- result of checking + return(ExtCheckPassed); + } +//+------------------------------------------------------------------+ +//| Returns true in case of successful confirmation check | +//+------------------------------------------------------------------+ +bool CheckConfirmation() + { + ExtConfirmed=false; +//--- if there is no pattern, do not search for confirmation + if(!ExtPatternDetected) + return(true); + +//--- get the value of the stochastic indicator to confirm the signal + double signal=StochSignal(1); + if(signal==EMPTY_VALUE) + { + //--- failed to get indicator value, check failed + return(false); + } + +//--- check the Buy signal + if(ExtSignalOpen==SIGNAL_BUY && (signal<30)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: StochSignal<30"; + } + +//--- check the Sell signal + if(ExtSignalOpen==SIGNAL_SELL && (signal>70)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: StochSignal>70"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((StochSignal(1)<80) && (StochSignal(2)>80))|| // 80 crossed downwards + ((StochSignal(1)<20) && (StochSignal(2)>20))) // 20 crossed downwards + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if((((StochSignal(1)>20) && (StochSignal(2)<20)) || // 20 crossed upwards + ((StochSignal(1)>80) && (StochSignal(2)<80)))) // 80 crossed upwards + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Stochastic indicator value at the specified bar | +//+------------------------------------------------------------------+ +double StochSignal(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, SIGNAL_LINE, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the iStochastic indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/BullishBearish Engulfing CCI.mq5 b/BullishBearish Engulfing CCI.mq5 new file mode 100644 index 0000000..c7452bb --- /dev/null +++ b/BullishBearish Engulfing CCI.mq5 @@ -0,0 +1,682 @@ +//+------------------------------------------------------------------+ +//| BullishBearish Engulfing CCI.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpMAPeriod =5; // Trend MA period +input int InpPeriodCCI =37; // CCI period +input ENUM_APPLIED_PRICE InpPrice=PRICE_CLOSE; // price type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot =0.1; // lot +//--- Expert ID +input long InpMagicNumber=121100; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error + +//--- indicator handles +int ExtIndicatorHandle=INVALID_HANDLE; +int ExtTrendMAHandle =INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iCCI(_Symbol, _Period, InpPeriodCCI, InpPrice); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating CCI indicator"); + return(INIT_FAILED); + } + +//--- trend moving average + ExtTrendMAHandle=iMA(_Symbol, _Period, InpMAPeriod, 0, MODE_SMA, PRICE_CLOSE); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating Moving Average indicator"); + return(INIT_FAILED); + } + +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // body of the candle is higher than average value of the body + (Close(1)CloseAvg(2)) && // uptrend + (Open(1)>Close(2))) // Open price of the bearish candle is higher than close price of the bullish candle + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nBearish Engulfing detected"; + ExtDirection="Sell"; + return(true); + } + +//--- check Bullish Engulfing + if((Open(2)>Close(2)) && // previous candle is bearish + (Close(1)-Open(1)>AvgBody(1)) && // body of the bullish candle is higher than average value of the body + (Close(1)>Open(2)) && // close price of the bullish candle is higher than open price of the bearish candle + (MidOpenClose(2)50)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: CCI>50"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((CCI(1)<80) && (CCI(2)>80)) || ((CCI(1)<-80) && (CCI(2)>-80))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((CCI(1)>-80) && (CCI(2)<-80)) || ((CCI(1)>80) && (CCI(2)<80))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| CCI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double CCI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the CCI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ +//| SMA value at the specified bar | +//+------------------------------------------------------------------+ +double CloseAvg(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtTrendMAHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the Simple Moving Average indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/BullishBearish Engulfing MFI.mq5 b/BullishBearish Engulfing MFI.mq5 new file mode 100644 index 0000000..f78377a --- /dev/null +++ b/BullishBearish Engulfing MFI.mq5 @@ -0,0 +1,682 @@ +//+------------------------------------------------------------------+ +//| BullishBearish Engulfing MFI.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpMAPeriod =5; // Trend MA period +input int InpPeriodMFI =37; // MFI period +input ENUM_APPLIED_VOLUME InpVolume=VOLUME_TICK; // volume type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot =0.1; // lot +//--- Expert ID +input long InpMagicNumber=120600; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error + +//--- indicator handles +int ExtIndicatorHandle=INVALID_HANDLE; +int ExtTrendMAHandle =INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iMFI(_Symbol, _Period, InpPeriodMFI, InpVolume); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating MFI indicator"); + return(INIT_FAILED); + } + +//--- trend moving average + ExtTrendMAHandle=iMA(_Symbol, _Period, InpMAPeriod, 0, MODE_SMA, PRICE_CLOSE); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating Moving Average indicator"); + return(INIT_FAILED); + } + +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // body of the candle is higher than average value of the body + (Close(1)CloseAvg(2)) && // uptrend + (Open(1)>Close(2))) // Open price of the bearish candle is higher than close price of the bullish candle + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nBearish Engulfing detected"; + ExtDirection="Sell"; + return(true); + } + +//--- check Bullish Engulfing + if((Open(2)>Close(2)) && // previous candle is bearish + (Close(1)-Open(1)>AvgBody(1)) && // body of the bullish candle is higher than average value of the body + (Close(1)>Open(2)) && // close price of the bullish candle is higher than open price of the bearish candle + (MidOpenClose(2)60)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: MFI>60"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((MFI(1)<70) && (MFI(2)>70)) || ((MFI(1)<30) && (MFI(2)>30))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((MFI(1)>30) && (MFI(2)<30)) || ((MFI(1)>70) && (MFI(2)<70))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| MFI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double MFI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the MFI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ +//| SMA value at the specified bar | +//+------------------------------------------------------------------+ +double CloseAvg(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtTrendMAHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the Simple Moving Average indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/BullishBearish Engulfing RSI.mq5 b/BullishBearish Engulfing RSI.mq5 new file mode 100644 index 0000000..3baf90d --- /dev/null +++ b/BullishBearish Engulfing RSI.mq5 @@ -0,0 +1,682 @@ +//+------------------------------------------------------------------+ +//| BullishBearish Engulfing RSI.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpMAPeriod =5; // Trend MA period +input int InpPeriodRSI =37; // RSI period +input ENUM_APPLIED_PRICE InpPrice=PRICE_CLOSE; // price type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot =0.1; // lot +//--- Expert ID +input long InpMagicNumber=121300; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error + +//--- indicator handles +int ExtIndicatorHandle=INVALID_HANDLE; +int ExtTrendMAHandle =INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iRSI(_Symbol, _Period, InpPeriodRSI, InpPrice); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating CCI indicator"); + return(INIT_FAILED); + } + +//--- trend moving average + ExtTrendMAHandle=iMA(_Symbol, _Period, InpMAPeriod, 0, MODE_SMA, PRICE_CLOSE); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating Moving Average indicator"); + return(INIT_FAILED); + } + +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // body of the candle is higher than average value of the body + (Close(1)CloseAvg(2)) && // uptrend + (Open(1)>Close(2))) // Open price of the bearish candle is higher than close price of the bullish candle + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nBearish Engulfing detected"; + ExtDirection="Sell"; + return(true); + } + +//--- check Bullish Engulfing + if((Open(2)>Close(2)) && // previous candle is bearish + (Close(1)-Open(1)>AvgBody(1)) && // body of the bullish candle is higher than average value of the body + (Close(1)>Open(2)) && // close price of the bullish candle is higher than open price of the bearish candle + (MidOpenClose(2)60)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: RSI>60"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((RSI(1)<70) && (RSI(2)>70)) || ((RSI(1)<30) && (RSI(2)>30))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((RSI(1)>30) && (RSI(2)<30)) || ((RSI(1)>70) && (RSI(2)<70))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| RSI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double RSI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the RSI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ +//| SMA value at the specified bar | +//+------------------------------------------------------------------+ +double CloseAvg(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtTrendMAHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the Simple Moving Average indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/BullishBearish Engulfing Stoch.mq5 b/BullishBearish Engulfing Stoch.mq5 new file mode 100644 index 0000000..68973cc --- /dev/null +++ b/BullishBearish Engulfing Stoch.mq5 @@ -0,0 +1,688 @@ +//+------------------------------------------------------------------+ +//| BullishBearish Engulfing Stoch.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpMAPeriod =5; // Trend MA period +input int InpStochK =47; // period %K +input int InpStochD =9; // period %D +input int InpStochSlow=13; // smoothing period %K +input ENUM_STO_PRICE InpStochApplied =STO_LOWHIGH; // calculation type +input ENUM_MA_METHOD InpStochMA =MODE_SMA; // smoothing type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot =0.1; // lot +//--- Expert ID +input long InpMagicNumber=121400; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error + +//--- indicator handles +int ExtIndicatorHandle=INVALID_HANDLE; +int ExtTrendMAHandle =INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iStochastic(_Symbol, _Period, InpStochK, InpStochD, InpStochSlow, InpStochMA, InpStochApplied); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating iStochastic indicator"); + return(INIT_FAILED); + } + +//--- trend moving average + ExtTrendMAHandle=iMA(_Symbol, _Period, InpMAPeriod,0, MODE_SMA,PRICE_CLOSE); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating Moving Average indicator"); + return(INIT_FAILED); + } + +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } +//--- + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } + +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // body of the candle is higher than average value of the body + (Close(1)CloseAvg(2)) && // uptrend + (Open(1)>Close(2))) // Open price of the bearish candle is higher than close price of the bullish candle + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nBearish Engulfing detected"; + ExtDirection="Sell"; + return(true); + } + +//--- check Bullish Engulfing + if((Open(2)>Close(2)) && // previous candle is bearish + (Close(1)-Open(1)>AvgBody(1)) && // body of the bullish candle is higher than average value of the body + (Close(1)>Open(2)) && // close price of the bullish candle is higher than open price of the bearish candle + (MidOpenClose(2)70)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: StochSignal>70"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((StochSignal(1)<80) && (StochSignal(2)>80))|| // 80 crossed downwards + ((StochSignal(1)<20) && (StochSignal(2)>20))) // 20 crossed downwards + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if((((StochSignal(1)>20) && (StochSignal(2)<20)) || // 20 crossed upwards + ((StochSignal(1)>80) && (StochSignal(2)<80)))) // 80 crossed upwards + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Stochastic indicator value at the specified bar | +//+------------------------------------------------------------------+ +double StochSignal(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, SIGNAL_LINE, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the iStochastic indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ +//| SMA value at the specified bar | +//+------------------------------------------------------------------+ +double CloseAvg(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtTrendMAHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the Simple Moving Average indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/BullishBearish Harami CCI.mq5 b/BullishBearish Harami CCI.mq5 new file mode 100644 index 0000000..02701fc --- /dev/null +++ b/BullishBearish Harami CCI.mq5 @@ -0,0 +1,682 @@ +//+------------------------------------------------------------------+ +//| BullishBearish Harami CCI.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpMAPeriod =5; // Trend MA period +input int InpPeriodCCI =37; // CCI period +input ENUM_APPLIED_PRICE InpPrice=PRICE_CLOSE; // price type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot =0.1; // lot +//--- Expert ID +input long InpMagicNumber=122100; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error + +//--- indicator handles +int ExtIndicatorHandle=INVALID_HANDLE; +int ExtTrendMAHandle =INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iCCI(_Symbol, _Period, InpPeriodCCI, InpPrice); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating CCI indicator"); + return(INIT_FAILED); + } + +//--- trend moving average + ExtTrendMAHandle=iMA(_Symbol, _Period, InpMAPeriod, 0, MODE_SMA, PRICE_CLOSE); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating Moving Average indicator"); + return(INIT_FAILED); + } + +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // the previous candle is bullish, its body is greater than average (long white) + (Close(1)>Open(2)) && // close price of the bearish candle is higher than open price of the bullish candle + (Open(1)CloseAvg(2))) // up trend + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nBearish Harami detected"; + ExtDirection="Sell"; + return(true); + } + +//--- check Bullish Harami + if((Close(1)>Open(1)) && // the last completed bar is bullish (white day) + ((Open(2)-Close(2))>AvgBody(1)) && // the previous candle is bearish, its body is greater than average (long black) + (Close(1)Close(2)) && // open price of the bullish candle is higher than close price of the bearish candle + (MidPoint(2)50)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: CCI>50"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((CCI(1)<80) && (CCI(2)>80)) || ((CCI(1)<-80) && (CCI(2)>-80))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((CCI(1)>-80) && (CCI(2)<-80)) || ((CCI(1)>80) && (CCI(2)<80))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| CCI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double CCI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the CCI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ +//| SMA value at the specified bar | +//+------------------------------------------------------------------+ +double CloseAvg(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtTrendMAHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the Simple Moving Average indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/BullishBearish Harami MFI.mq5 b/BullishBearish Harami MFI.mq5 new file mode 100644 index 0000000..1500d42 --- /dev/null +++ b/BullishBearish Harami MFI.mq5 @@ -0,0 +1,682 @@ +//+------------------------------------------------------------------+ +//| BullishBearish Harami MFI.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpMAPeriod =5; // Trend MA period +input int InpPeriodMFI =37; // MFI period +input ENUM_APPLIED_VOLUME InpVolume=VOLUME_TICK; // volume type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot =0.1; // lot +//--- Expert ID +input long InpMagicNumber=121600; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error + +//--- indicator handles +int ExtIndicatorHandle=INVALID_HANDLE; +int ExtTrendMAHandle =INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iMFI(_Symbol, _Period, InpPeriodMFI, InpVolume); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating MFI indicator"); + return(INIT_FAILED); + } + +//--- trend moving average + ExtTrendMAHandle=iMA(_Symbol, _Period, InpMAPeriod, 0, MODE_SMA, PRICE_CLOSE); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating Moving Average indicator"); + return(INIT_FAILED); + } + +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // the previous candle is bullish, its body is greater than average (long white) + (Close(1)>Open(2)) && // close price of the bearish candle is higher than open price of the bullish candle + (Open(1)CloseAvg(2))) // up trend + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nBearish Harami detected"; + ExtDirection="Sell"; + return(true); + } + +//--- check Bullish Harami + if((Close(1)>Open(1)) && // the last completed bar is bullish (white day) + ((Open(2)-Close(2))>AvgBody(1)) && // the previous candle is bearish, its body is greater than average (long black) + (Close(1)Close(2)) && // open price of the bullish candle is higher than close price of the bearish candle + (MidPoint(2)60)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: MFI>60"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((MFI(1)<70) && (MFI(2)>70)) || ((MFI(1)<30) && (MFI(2)>30))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((MFI(1)>30) && (MFI(2)<30)) || ((MFI(1)>70) && (MFI(2)<70))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| MFI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double MFI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the MFI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ +//| SMA value at the specified bar | +//+------------------------------------------------------------------+ +double CloseAvg(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtTrendMAHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the Simple Moving Average indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/BullishBearish Harami RSI.mq5 b/BullishBearish Harami RSI.mq5 new file mode 100644 index 0000000..9294990 --- /dev/null +++ b/BullishBearish Harami RSI.mq5 @@ -0,0 +1,682 @@ +//+------------------------------------------------------------------+ +//| BullishBearish Harami RSI.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpMAPeriod =5; // Trend MA period +input int InpPeriodRSI =37; // RSI period +input ENUM_APPLIED_PRICE InpPrice=PRICE_CLOSE; // price type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot =0.1; // lot +//--- Expert ID +input long InpMagicNumber=121300; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error + +//--- indicator handles +int ExtIndicatorHandle=INVALID_HANDLE; +int ExtTrendMAHandle =INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iRSI(_Symbol, _Period, InpPeriodRSI, InpPrice); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating CCI indicator"); + return(INIT_FAILED); + } + +//--- trend moving average + ExtTrendMAHandle=iMA(_Symbol, _Period, InpMAPeriod, 0, MODE_SMA, PRICE_CLOSE); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating Moving Average indicator"); + return(INIT_FAILED); + } + +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // the previous candle is bullish, its body is greater than average (long white) + (Close(1)>Open(2)) && // close price of the bearish candle is higher than open price of the bullish candle + (Open(1)CloseAvg(2))) // up trend + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nBearish Harami detected"; + ExtDirection="Sell"; + return(true); + } + +//--- check Bullish Harami + if((Close(1)>Open(1)) && // the last completed bar is bullish (white day) + ((Open(2)-Close(2))>AvgBody(1)) && // the previous candle is bearish, its body is greater than average (long black) + (Close(1)Close(2)) && // open price of the bullish candle is higher than close price of the bearish candle + (MidPoint(2)60)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: RSI>60"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((RSI(1)<70) && (RSI(2)>70)) || ((RSI(1)<30) && (RSI(2)>30))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((RSI(1)>30) && (RSI(2)<30)) || ((RSI(1)>70) && (RSI(2)<70))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| RSI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double RSI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the RSI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ +//| SMA value at the specified bar | +//+------------------------------------------------------------------+ +double CloseAvg(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtTrendMAHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the Simple Moving Average indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/BullishBearish Harami Stoch.mq5 b/BullishBearish Harami Stoch.mq5 new file mode 100644 index 0000000..a729432 --- /dev/null +++ b/BullishBearish Harami Stoch.mq5 @@ -0,0 +1,685 @@ +//+------------------------------------------------------------------+ +//| BullishBearish Harami Stoch.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpMAPeriod =5; // Trend MA period +input int InpStochK =47; // period %K +input int InpStochD =9; // period %D +input int InpStochSlow =13; // smoothing period %K +input ENUM_STO_PRICE InpStochApplied=STO_LOWHIGH; // calculation type +input ENUM_MA_METHOD InpStochMA =MODE_SMA; // smoothing type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot =0.1; // lot +//--- Expert ID +input long InpMagicNumber=122400; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error + +//--- indicator handles +int ExtIndicatorHandle=INVALID_HANDLE; +int ExtTrendMAHandle =INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iStochastic(_Symbol, _Period, InpStochK, InpStochD, InpStochSlow, InpStochMA, InpStochApplied); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating iStochastic indicator"); + return(INIT_FAILED); + } + +//--- trend moving average + ExtTrendMAHandle=iMA(_Symbol, _Period, InpMAPeriod, 0, MODE_SMA, PRICE_CLOSE); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating Moving Average indicator"); + return(INIT_FAILED); + } + +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } +//--- open position not found + return(false); + } + +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // the previous candle is bullish, its body is greater than average (long white) + (Close(1)>Open(2)) && // close price of the bearish candle is higher than open price of the bullish candle + (Open(1)CloseAvg(2))) // up trend + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nBearish Harami detected"; + ExtDirection="Sell"; + return(true); + } + +//--- check Bullish Harami + if((Close(1)>Open(1)) && // the last completed bar is bullish (white day) + ((Open(2)-Close(2))>AvgBody(1)) && // the previous candle is bearish, its body is greater than average (long black) + (Close(1)Close(2)) && // open price of the bullish candle is higher than close price of the bearish candle + (MidPoint(2)70)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: StochSignal>70"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((StochSignal(1)<80) && (StochSignal(2)>80))|| // 80 crossed downwards + ((StochSignal(1)<20) && (StochSignal(2)>20))) // 20 crossed downwards + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if((((StochSignal(1)>20) && (StochSignal(2)<20)) || // 20 crossed upwards + ((StochSignal(1)>80) && (StochSignal(2)<80)))) // 80 crossed upwards + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Stochastic indicator value at the specified bar | +//+------------------------------------------------------------------+ +double StochSignal(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, SIGNAL_LINE, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the iStochastic indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ +//| SMA value at the specified bar | +//+------------------------------------------------------------------+ +double CloseAvg(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtTrendMAHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the Simple Moving Average indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/BullishBearish MeetingLines CCI.mq5 b/BullishBearish MeetingLines CCI.mq5 new file mode 100644 index 0000000..91ec722 --- /dev/null +++ b/BullishBearish MeetingLines CCI.mq5 @@ -0,0 +1,654 @@ +//+------------------------------------------------------------------+ +//| BullishBearish MeetingLines CCI.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpPeriodCCI =37; // CCI period +input ENUM_APPLIED_PRICE InpPrice=PRICE_CLOSE; // price type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot =0.1; // lot +//--- Expert ID +input long InpMagicNumber=123100; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error + +//--- indicator handle +int ExtIndicatorHandle=INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iCCI(_Symbol, _Period, InpPeriodCCI, InpPrice); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating CCI indicator"); + return(INIT_FAILED); + } +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // long white candle + ((Open(1)-Close(1))>AvgBody(1)) && // long black candle + (MathAbs(Close(1)-Close(2))<0.1*AvgBody(1))) // doji close + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nBearish MeetingLines detected"; + ExtDirection="Sell"; + return(true); + } + +//--- check Bullish MeetingLines + if((Open(2)-Close(2)>AvgBody(1)) && // long black candle + ((Close(1)-Open(1))>AvgBody(1)) && // long white candle + (MathAbs(Close(1)-Close(2))<0.1*AvgBody(1))) // doji close + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_BUY; + ExtPatternInfo="\r\nBullish MeetingLines detected"; + ExtDirection="Buy"; + return(true); + } + +//--- result of checking + return(ExtCheckPassed); + } + +//+------------------------------------------------------------------+ +//| Returns true in case of successful confirmation check | +//+------------------------------------------------------------------+ +bool CheckConfirmation() + { + ExtConfirmed=false; +//--- if there is no pattern, do not search for confirmation + if(!ExtPatternDetected) + return(true); + +//--- get the value of the stochastic indicator to confirm the signal + double signal=CCI(1); + if(signal==EMPTY_VALUE) + { + //--- failed to get indicator value, check failed + return(false); + } + +//--- check the Buy signal + if(ExtSignalOpen==SIGNAL_BUY && (signal<-50)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: CCI<-50"; + } + +//--- check the Sell signal + if(ExtSignalOpen==SIGNAL_SELL && (signal>50)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: CCI>50"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((CCI(1)<80) && (CCI(2)>80)) || ((CCI(1)<-80) && (CCI(2)>-80))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((CCI(1)>-80) && (CCI(2)<-80)) || ((CCI(1)>80) && (CCI(2)<80))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| CCI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double CCI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the CCI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/BullishBearish MeetingLines MFI.mq5 b/BullishBearish MeetingLines MFI.mq5 new file mode 100644 index 0000000..283e28c --- /dev/null +++ b/BullishBearish MeetingLines MFI.mq5 @@ -0,0 +1,652 @@ +//+------------------------------------------------------------------+ +//| BullishBearish MeetingLines MFI.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpPeriodMFI =37; // MFI period +input ENUM_APPLIED_VOLUME InpVolume=VOLUME_TICK; // volume type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot=0.1; // lot +//--- Expert ID +input long InpMagicNumber=121200; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error +//--- indicator handle +int ExtIndicatorHandle=INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iMFI(_Symbol, _Period, InpPeriodMFI, InpVolume); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating MFI indicator"); + return(INIT_FAILED); + } +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // long white candle + ((Open(1)-Close(1))>AvgBody(1)) && // long black candle + (MathAbs(Close(1)-Close(2))<0.1*AvgBody(1))) // doji close + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nBearish MeetingLines detected"; + ExtDirection="Sell"; + return(true); + } + +//--- check Bullish MeetingLines + if((Open(2)-Close(2)>AvgBody(1)) && // long black candle + ((Close(1)-Open(1))>AvgBody(1)) && // long white candle + (MathAbs(Close(1)-Close(2))<0.1*AvgBody(1))) // doji close + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_BUY; + ExtPatternInfo="\r\nBullish MeetingLines detected"; + ExtDirection="Buy"; + return(true); + } + +//--- result of checking + return(ExtCheckPassed); + } +//+------------------------------------------------------------------+ +//| Returns true in case of successful confirmation check | +//+------------------------------------------------------------------+ +bool CheckConfirmation() + { + ExtConfirmed=false; +//--- if there is no pattern, do not search for confirmation + if(!ExtPatternDetected) + return(true); + +//--- get the value of the stochastic indicator to confirm the signal + double signal=MFI(1); + if(signal==EMPTY_VALUE) + { + //--- failed to get indicator value, check failed + return(false); + } + +//--- check the Buy signal + if(ExtSignalOpen==SIGNAL_BUY && (signal<40)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: MFI<40"; + } + +//--- check the Sell signal + if(ExtSignalOpen==SIGNAL_SELL && (signal>60)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: MFI>60"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((MFI(1)<70) && (MFI(2)>70)) || ((MFI(1)<30) && (MFI(2)>30))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((MFI(1)>30) && (MFI(2)<30)) || ((MFI(1)>70) && (MFI(2)<70))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| MFI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double MFI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the MFI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/BullishBearish MeetingLines RSI.mq5 b/BullishBearish MeetingLines RSI.mq5 new file mode 100644 index 0000000..2b5bcc9 --- /dev/null +++ b/BullishBearish MeetingLines RSI.mq5 @@ -0,0 +1,653 @@ +//+------------------------------------------------------------------+ +//| BullishBearish MeetingLines RSI.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpPeriodRSI =37; // RSI period +input ENUM_APPLIED_PRICE InpPrice=PRICE_CLOSE; // price type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot =0.1; // lot +//--- Expert ID +input long InpMagicNumber=122300; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error +//--- indicator handle +int ExtIndicatorHandle=INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iRSI(_Symbol, _Period, InpPeriodRSI, InpPrice); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating CCI indicator"); + return(INIT_FAILED); + } +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } + +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // long white candle + ((Open(1)-Close(1))>AvgBody(1)) && // long black candle + (MathAbs(Close(1)-Close(2))<0.1*AvgBody(1))) // doji close + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nBearish MeetingLines detected"; + ExtDirection="Sell"; + return(true); + } + +//--- check Bullish MeetingLines + if((Open(2)-Close(2)>AvgBody(1)) && // long black candle + ((Close(1)-Open(1))>AvgBody(1)) && // long white candle + (MathAbs(Close(1)-Close(2))<0.1*AvgBody(1))) // doji close + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_BUY; + ExtPatternInfo="\r\nBullish MeetingLines detected"; + ExtDirection="Buy"; + return(true); + } + +//--- result of checking + return(ExtCheckPassed); + } +//+------------------------------------------------------------------+ +//| Returns true in case of successful confirmation check | +//+------------------------------------------------------------------+ +bool CheckConfirmation() + { + ExtConfirmed=false; +//--- if there is no pattern, do not search for confirmation + if(!ExtPatternDetected) + return(true); + +//--- get the value of the stochastic indicator to confirm the signal + double signal=RSI(1); + if(signal==EMPTY_VALUE) + { + //--- failed to get indicator value, check failed + return(false); + } + +//--- check the Buy signal + if(ExtSignalOpen==SIGNAL_BUY && (signal<40)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: RSI<40"; + } + +//--- check the Sell signal + if(ExtSignalOpen==SIGNAL_SELL && (signal>60)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: RSI>60"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((RSI(1)<70) && (RSI(2)>70)) || ((RSI(1)<30) && (RSI(2)>30))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((RSI(1)>30) && (RSI(2)<30)) || ((RSI(1)>70) && (RSI(2)<70))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| RSI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double RSI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the RSI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/BullishBearish MeetingLines Stoch.mq5 b/BullishBearish MeetingLines Stoch.mq5 new file mode 100644 index 0000000..132719b --- /dev/null +++ b/BullishBearish MeetingLines Stoch.mq5 @@ -0,0 +1,662 @@ +//+------------------------------------------------------------------+ +//| BullishBearish MeetingLines Stoch.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpStochK =47; // period %K +input int InpStochD =9; // period %D +input int InpStochSlow =13; // smoothing period %K +input ENUM_STO_PRICE InpStochApplied=STO_LOWHIGH; // calculation type +input ENUM_MA_METHOD InpStochMA =MODE_SMA; // smoothing type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot=0.1; // lot +//--- Expert ID +input long InpMagicNumber=123400; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen=0; // Buy/Sell signal +int ExtSignalClose=0; // signal to close a position +string ExtPatternInfo=""; // current pattern information +string ExtDirection=""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed=false; // pattern confirmed +bool ExtCloseByTime=true; // requires closing by time +bool ExtCheckPassed=true; // status checking error +//--- indicator handle +int ExtIndicatorHandle=INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iStochastic(_Symbol, _Period, InpStochK, InpStochD, InpStochSlow, InpStochMA, InpStochApplied); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating iStochastic indicator"); + return(INIT_FAILED); + } +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } + +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // long black + (Open(2)-Close(2)>AvgBody(1)) && + (Open(1)-Close(1)>AvgBody(1)) && + (MidPoint(2)AvgBody(1)) && // long white + (Close(2)-Open(2)>AvgBody(1)) && + (Close(1)-Open(1)>AvgBody(1)) && + (MidPoint(2)>MidPoint(3)) && // higher midpoints + (MidPoint(1)>MidPoint(2))) + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_BUY; + ExtPatternInfo="\r\n3 White Soldiers detected"; + ExtDirection="Buy"; + return(true); + } + +//--- result of checking + return(ExtCheckPassed); + } +//+------------------------------------------------------------------+ +//| Returns true in case of successful confirmation check | +//+------------------------------------------------------------------+ +bool CheckConfirmation() + { + ExtConfirmed=false; +//--- if there is no pattern, do not search for confirmation + if(!ExtPatternDetected) + return(true); + +//--- get the value of the stochastic indicator to confirm the signal + double signal=StochSignal(1); + if(signal==EMPTY_VALUE) + { + //--- failed to get indicator value, check failed + return(false); + } + +//--- check the Buy signal + if(ExtSignalOpen==SIGNAL_BUY && (signal<30)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: StochSignal<30"; + } + +//--- check the Sell signal + if(ExtSignalOpen==SIGNAL_SELL && (signal>70)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: StochSignal>70"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((StochSignal(1)<80) && (StochSignal(2)>80))|| // 80 crossed downwards + ((StochSignal(1)<20) && (StochSignal(2)>20))) // 20 crossed downwards + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if((((StochSignal(1)>20) && (StochSignal(2)<20)) || // 20 crossed upwards + ((StochSignal(1)>80) && (StochSignal(2)<80)))) // 80 crossed upwards + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Stochastic indicator value at the specified bar | +//+------------------------------------------------------------------+ +double StochSignal(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, SIGNAL_LINE, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the iStochastic indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/Bulls.mq5 b/Bulls.mq5 new file mode 100644 index 0000000..1c9e241 --- /dev/null +++ b/Bulls.mq5 @@ -0,0 +1,94 @@ +//+------------------------------------------------------------------+ +//| Bulls.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property description "Bulls Power" +//--- indicator settings +#property indicator_separate_window +#property indicator_buffers 2 +#property indicator_plots 1 +#property indicator_type1 DRAW_HISTOGRAM +#property indicator_color1 Silver +#property indicator_width1 2 +//--- input parameters +input int InpBullsPeriod=13; // Period +//--- indicator buffers +double ExtBullsBuffer[]; +double ExtTempBuffer[]; +//--- MA handle +int ExtEmaHandle; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- indicator buffers mapping + SetIndexBuffer(0,ExtBullsBuffer,INDICATOR_DATA); + SetIndexBuffer(1,ExtTempBuffer,INDICATOR_CALCULATIONS); +//--- set accuracy + IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1); +//--- sets first bar from what index will be drawn + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpBullsPeriod-1); +//--- name for DataWindow and indicator subwindow label + string short_name=StringFormat("Bulls(%d)",InpBullsPeriod); + IndicatorSetString(INDICATOR_SHORTNAME,short_name); +//--- get handle for MA + ExtEmaHandle=iMA(NULL,0,InpBullsPeriod,0,MODE_EMA,PRICE_CLOSE); + } +//+------------------------------------------------------------------+ +//| Average True Range | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + if(rates_totalrates_total || prev_calculated<0) + to_copy=rates_total; + else + { + to_copy=rates_total-prev_calculated; + if(prev_calculated>0) + to_copy++; + } +//--- get ma buffers + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtEmaHandle,0,0,to_copy,ExtTempBuffer)<=0) + { + Print("getting ExtEmaHandle is failed! Error ",GetLastError()); + return(0); + } +//--- first calculation or number of bars was changed + int start; + if(prev_calculated +//--- +#property indicator_separate_window +#property indicator_buffers 4 +#property indicator_plots 1 +#property indicator_type1 DRAW_LINE +#property indicator_color1 LightSeaGreen +#property indicator_level1 -100.0 +#property indicator_level2 100.0 +#property indicator_applied_price PRICE_TYPICAL +//--- input parametrs +input int InpCCIPeriod=14; // Period +//--- indicator buffers +double ExtSPBuffer[]; +double ExtDBuffer[]; +double ExtMBuffer[]; +double ExtCCIBuffer[]; + +int ExtCCIPeriod; +double ExtMultiplyer; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- check for input value of period + if(InpCCIPeriod<=0) + { + ExtCCIPeriod=14; + PrintFormat("Incorrect value for input variable InpCCIPeriod=%d. Indicator will use value=%d for calculations.",InpCCIPeriod,ExtCCIPeriod); + } + else + ExtCCIPeriod=InpCCIPeriod; + ExtMultiplyer=0.015/ExtCCIPeriod; +//--- define buffers + SetIndexBuffer(0,ExtCCIBuffer); + SetIndexBuffer(1,ExtDBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(2,ExtMBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(3,ExtSPBuffer,INDICATOR_CALCULATIONS); +//--- indicator name + string short_name=StringFormat("CCI(%d)",ExtCCIPeriod); + IndicatorSetString(INDICATOR_SHORTNAME,short_name); +//--- indexes draw begin settings + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtCCIPeriod-1); +//--- number of digits of indicator value + IndicatorSetInteger(INDICATOR_DIGITS,2); + } +//+------------------------------------------------------------------+ +//| Custom indicator iteration function | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const int begin, + const double &price[]) + { + int start=(ExtCCIPeriod-1)+begin; + if(rates_total0) + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,start+(ExtCCIPeriod-1)); +//--- calculate position + int pos=prev_calculated-1; + if(pos +//--- indicator settings +#property indicator_separate_window +#property indicator_buffers 4 +#property indicator_plots 1 +#property indicator_type1 DRAW_LINE +#property indicator_color1 LightSeaGreen +//--- input parameters +input int InpFastMA=3; // Fast MA period +input int InpSlowMA=10; // Slow MA period +input ENUM_MA_METHOD InpSmoothMethod=MODE_EMA; // MA method +input ENUM_APPLIED_VOLUME InpVolumeType=VOLUME_TICK; // Volumes +//--- indicator buffers +double ExtCHOBuffer[]; +double ExtFastEMABuffer[]; +double ExtSlowEMABuffer[]; +double ExtADBuffer[]; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- indicator buffers mapping + SetIndexBuffer(0,ExtCHOBuffer,INDICATOR_DATA); + SetIndexBuffer(1,ExtFastEMABuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(2,ExtSlowEMABuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(3,ExtADBuffer,INDICATOR_CALCULATIONS); +//--- set accuracy + IndicatorSetInteger(INDICATOR_DIGITS,0); +//--- sets first bar from what index will be drawn + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpSlowMA); +//--- name for DataWindow and indicator subwindow label + string short_name=StringFormat("CHO(%d,%d)",InpSlowMA,InpFastMA); + IndicatorSetString(INDICATOR_SHORTNAME,short_name); + } +//+------------------------------------------------------------------+ +//| Chaikin Oscillator | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + if(rates_total0) + ExtADBuffer[i]+=ExtADBuffer[i-1]; + } + } + else + { + for(i=start; i0) + ExtADBuffer[i]+=ExtADBuffer[i-1]; + } + } +//--- calculate EMA on array ExtADBuffer + AverageOnArray(InpSmoothMethod,rates_total,prev_calculated,0,InpFastMA,ExtADBuffer,ExtFastEMABuffer); + AverageOnArray(InpSmoothMethod,rates_total,prev_calculated,0,InpSlowMA,ExtADBuffer,ExtSlowEMABuffer); +//--- calculate chaikin oscillator + for(i=start; i +//--- indicator settings +#property indicator_separate_window +#property indicator_buffers 3 +#property indicator_plots 1 +#property indicator_type1 DRAW_LINE +#property indicator_color1 DodgerBlue +//--- enum +enum SmoothMethod + { + SMA=0,// Simple MA + EMA=1 // Exponential MA + }; +//--- input parameters +input int InpSmoothPeriod=10; // Smoothing period +input int InpCHVPeriod=10; // CHV period +input SmoothMethod InpSmoothType=EMA; // Smoothing method +//--- indicator buffers +double ExtCHVBuffer[]; +double ExtHLBuffer[]; +double ExtSHLBuffer[]; + +int ExtSmoothPeriod,ExtCHVPeriod; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { + string ma_name=EnumToString(InpSmoothType); +//--- check inputs + if(InpSmoothPeriod<=0) + { + ExtSmoothPeriod=10; + PrintFormat("Incorrect value for input variable InpSmoothPeriod=%d. Indicator will use value=%d for calculations.",InpSmoothPeriod,ExtSmoothPeriod); + } + else + ExtSmoothPeriod=InpSmoothPeriod; + if(InpCHVPeriod<=0) + { + ExtCHVPeriod=10; + PrintFormat("Incorrect value for input variable InpCHVPeriod=%d. Indicator will use value=%d for calculations.",InpCHVPeriod,ExtCHVPeriod); + } + else + ExtCHVPeriod=InpCHVPeriod; +//--- define buffers + SetIndexBuffer(0,ExtCHVBuffer); + SetIndexBuffer(1,ExtHLBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(2,ExtSHLBuffer,INDICATOR_CALCULATIONS); +//--- set draw begin + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtSmoothPeriod+ExtCHVPeriod-1); +//--- set name and index label + string params=StringFormat("(%d,%s)",ExtSmoothPeriod,ma_name); + IndicatorSetString(INDICATOR_SHORTNAME,"Chaikin Volatility"+params); + PlotIndexSetString(0,PLOT_LABEL,"CHV"+params); +//--- round settings + IndicatorSetInteger(INDICATOR_DIGITS,1); + } +//+------------------------------------------------------------------+ +//| Custom indicator iteration function | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + int i,pos,pos_chv; +//--- check for rates total + pos_chv=ExtCHVPeriod+ExtSmoothPeriod-2; + if(rates_totalPeriodSeconds(PERIOD_D1)) + { + Alert("Timeframe of chart must be D1 or lower. Exit"); + return(INIT_FAILED); + } + +//--- define buffers + SetIndexBuffer(0, ExtH5Buffer); + SetIndexBuffer(1, ExtH4Buffer); + SetIndexBuffer(2, ExtH3Buffer); + SetIndexBuffer(3, ExtH2Buffer); + SetIndexBuffer(4, ExtH1Buffer); + SetIndexBuffer(5, ExtL1Buffer); + SetIndexBuffer(6, ExtL2Buffer); + SetIndexBuffer(7, ExtL3Buffer); + SetIndexBuffer(8, ExtL4Buffer); + SetIndexBuffer(9, ExtL5Buffer); + +//--- indicator name + IndicatorSetString(INDICATOR_SHORTNAME, "Camarilla Channels"); +//--- number of digits of indicator value + IndicatorSetInteger(INDICATOR_DIGITS, _Digits); + +//--- prepare prefix for objects + string number=StringFormat("%I64d", GetTickCount64()); + ExtPrefixUniq=StringSubstr(number, StringLen(number)-4); + ExtPrefixUniq=ExtPrefixUniq+"_CH"; + Print("Indicator \"Camarilla Channels\" started, prefix=", ExtPrefixUniq); + + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Custom indicator iteration function | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + static MqlRates LAST_DAY[]; // previous day + static datetime last_time=0; // reference time + static datetime error_time=0; // error output time + +//--- if the indicator has previously been calculated, start from the bar preceding the last one + int start=prev_calculated-1; + +//--- if this is the first calculation of the indicator, start from the first bar on the chart + if(prev_calculated==0) + start=0; + +//--- calculate levels for all bars in a loop + for(int i=start; i=error_time) + { + error_time=TimeCurrent()+60; + Print("Failed to get previous day by CopyRates(Symbol(), PERIOD_D1, error ", GetLastError()); + } + return(prev_calculated); + } + } + + //--- calculate levels + double range=LAST_DAY[0].high - LAST_DAY[0].low; + double h5=(LAST_DAY[0].high/LAST_DAY[0].low) * LAST_DAY[0].close; + double h4=LAST_DAY[0].close + range*1.1/2.0; + double h3=LAST_DAY[0].close + range*1.1/4.0; + double h2=LAST_DAY[0].close + range*1.1/6.0; + double h1=LAST_DAY[0].close + range*1.1/12.0; + double l1=LAST_DAY[0].close - range*1.1/12.0; + double l2=LAST_DAY[0].close - range*1.1/6.0; + double l3=LAST_DAY[0].close - range*1.1/4.0; + double l4=LAST_DAY[0].close - range*1.1/2.0; + double l5=LAST_DAY[0].close - (h5 -LAST_DAY[0].close); + + //--- write values into buffers + ExtH5Buffer[i]=h5; + ExtH4Buffer[i]=h4; + ExtH3Buffer[i]=h3; + ExtH2Buffer[i]=h2; + ExtH1Buffer[i]=h1; + ExtL1Buffer[i]=l1; + ExtL2Buffer[i]=l2; + ExtL3Buffer[i]=l3; + ExtL4Buffer[i]=l4; + ExtL5Buffer[i]=l5; + } + +//--- draw labels on levels + if(InpShowLabel) + { + ShowPriceLevels(time[rates_total-1], rates_total-1); + ChartRedraw(); + } + +//--- succesfully calculated + return(rates_total); + } +//+------------------------------------------------------------------+ +//| Custom indicator deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- delete all our graphical objects after use + Print("Indicator \"Camarilla Channels\" stopped, delete all objects with prefix=", ExtPrefixUniq); + ObjectsDeleteAll(0, ExtPrefixUniq, 0, OBJ_ARROW_RIGHT_PRICE); + ChartRedraw(0); + } +//+------------------------------------------------------------------+ +//| Show prices' levels | +//+------------------------------------------------------------------+ +void ShowPriceLevels(datetime time, int last_index) + { + ShowRightPrice(ExtPrefixUniq+"_H5", time, ExtH5Buffer[last_index], clrGreen); + ShowRightPrice(ExtPrefixUniq+"_H4", time, ExtH4Buffer[last_index], clrGreen); + ShowRightPrice(ExtPrefixUniq+"_H3", time, ExtH3Buffer[last_index], clrGreen); + ShowRightPrice(ExtPrefixUniq+"_H2", time, ExtH2Buffer[last_index], clrGreen); + ShowRightPrice(ExtPrefixUniq+"_H1", time, ExtH1Buffer[last_index], clrGreen); + ShowRightPrice(ExtPrefixUniq+"_L1", time, ExtL1Buffer[last_index], clrRed); + ShowRightPrice(ExtPrefixUniq+"_L2", time, ExtL2Buffer[last_index], clrRed); + ShowRightPrice(ExtPrefixUniq+"_L3", time, ExtL3Buffer[last_index], clrRed); + ShowRightPrice(ExtPrefixUniq+"_L4", time, ExtL4Buffer[last_index], clrRed); + ShowRightPrice(ExtPrefixUniq+"_L5", time, ExtL5Buffer[last_index], clrRed); + } +//+------------------------------------------------------------------+ +//| Create or Update "Right Price Label" object | +//+------------------------------------------------------------------+ +bool ShowRightPrice(const string name, datetime time, double price, color clr) + { + if(!ObjectCreate(0, name, OBJ_ARROW_RIGHT_PRICE, 0, time, price)) + { + ObjectMove(0, name, 0, time, price); + return(false); + } + +//--- make the label size adaptive + long scale=2; + if(!ChartGetInteger(0, CHART_SCALE, 0, scale)) + { + //--- output an error message to the Experts journal + Print(__FUNCTION__+", ChartGetInteger(CHART_SCALE) failed, error = ", GetLastError()); + } + int width=scale>1 ? 2:1; // if chart scale > 1, then label size = 2 + + ObjectSetInteger(0, name, OBJPROP_COLOR, clr); + ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_SOLID); + ObjectSetInteger(0, name, OBJPROP_WIDTH, width); + ObjectSetInteger(0, name, OBJPROP_BACK, false); + ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false); + ObjectSetInteger(0, name, OBJPROP_SELECTED, false); + ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); + ObjectSetInteger(0, name, OBJPROP_ZORDER, 0); + + return(true); + } +//+------------------------------------------------------------------+ diff --git a/CanvasSample.mq5 b/CanvasSample.mq5 new file mode 100644 index 0000000..f784232 --- /dev/null +++ b/CanvasSample.mq5 @@ -0,0 +1,211 @@ +//+------------------------------------------------------------------+ +//| CanvasSample.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property description "Demonstrating Canvas features" +//--- +#include +//+------------------------------------------------------------------+ +//| inputs | +//+------------------------------------------------------------------+ +input int Width=800; +input int Height=600; +//+------------------------------------------------------------------+ +//| Script program start function | +//+------------------------------------------------------------------+ +int OnStart(void) + { + int total=1024; + int limit=MathMax(Width,Height); + int x1,x2,x3,y1,y2,y3,r; + int x[],y[]; +//--- check + if(Width<100 || Height<100) + { + Print("Too simple."); + return(-1); + } +//--- create canvas + CCanvas canvas; + if(!canvas.CreateBitmapLabel("SampleCanvas",0,0,Width,Height,COLOR_FORMAT_ARGB_RAW)) + { + Print("Error creating canvas: ",GetLastError()); + return(-1); + } +//--- deawing + canvas.Erase(XRGB(0x1F,0x1F,0x1F)); + canvas.Update(); +//--- start randomizer + srand(GetTickCount()); +//--- draw pixels + for(int i=0;itwWo9@u*w~Ou<_b+!1{J+dao;CN@y>p+y>A<}P&tp)&>Mr}&1^?-)TXoC0 zy5(Ns?hE%9_a1eQ@oj{w5AbQ}#%bT1*@FB9@C zlKuPvvLAV!zw*8N36fIc={^{s&u6bT>6wAIlPbP%(C3HlpO8Rb18)O48Wy_r2l39n zr}hrkag5%fuaLTYmo~mZi;lo+7q;^erPxBbHgr$H_W;*kxtr0tcig>+dON<(E6}-v z_xsBFl!JE7*E6>bt|QOu0G|%s&|QJG&vLc3o;RxX%F90r;@pT1xdpT~K(Qv4gtD z_|J&(1u$*ow48EP8_;<2F>3DnTECh))Zl+kEOC)Mh^-;QY(wU^&=*Gn#-T4U!brSK zocVw^$9&%D>U9#yuRfBxaNU`-t-G9Y$sxL%%_(H^A*+Ta%^sQR@9T z7GcT#+LCr`Wyz5#IgNkPX9vKyC6AXwjP`5pK1R{U7*#jiU-5p3PkX@8JMK^Tw&uU_ z>N>uy!*6!M;~Ou%X7r1@!or|yyG#}Ner@tXlA*F z=ki$oZM5qVq+-mlff#jn3V%fc%SSw3Ftv)0_WS_cMN7(M$2~_patGHR_+GDyhyvn< z*7Ltot;dZ}UU5gfljz1=!;U(NNK$Ja8de!oEw5@}iH_}vS;Z^#r5)RB>u323=87(u z(~MwEs-v#`!H(ED{$M@2KaT8%uqhqOQr{w3f!wEcQGs%JzJrJcqK*08oe{e2$3um-H#1cLn;yyD{mV~dbw z-ofa`87Gc~c|IYT?qsIQQH;HCuEINF{ZlW=tgq}~)VS@(kE%YIuU*h8)?gib@V_;1 zeSpu)K&#odSILrZ8P+C(4TyEMLB)JaSVz<&vl311K<71vK9*@>1$9z7a*iT3%oE}Bxi z;%dQfc9oma=8skPVON|h?AEHPxwNqCV!n}?1#+;LXc`|QSMUnGt zsrkjU%$C3K$at-S%Pj_nx3KRhOgqO zwOGw1r~P8ueij>R?YQzA)-q$1-Iuz5La^5AiQ{hyYbVRsugle=Xg!5V6m3SK3Ll$L zszbddw5qYJ38gxOYC@|XcbZVMXkzOxHcChiJBtsxR^R3R2gZ13BO(($P4AAG#0rhs zr&p_GXHh5})*W?s(Jkzf=H5=O59-z7_^QoVWp-9~9o_OfPo<__mHnt;N2-;}#)B%4 zjf2uFewK!goVqh$4bj&FydNV1+eLOWxlh;nM%8>uY%MFrS$6g+_sN~UU2=Zr<7rtx zd+67-meKC8pQ}sG&nr0VPO>gJb4T2!`(oIi~P>rfGu0Qov(#)r(NKDC4-l& z@N-7wHCE8-$LtVYt1aL4nZzzEd4Sa_uIVzn=PGWzb*2~T_g>vb_9m;q%F|+(jgFdPm!)yK*p=ek9d@NS$Fb|Y8Ii=dzfD?y z%m!1^bcbgt&KA#B{b(Hb@pi4WK9)wydOS(BD){vL5G=*93c=nX4w!z{p$(FyWC*Rc zT2@q%xy3RhOK~hgGRJN@ z@4HiLx%JLA7?|Hu#ijw`=CCuXO+2SmQl1zvsT7H0<4iestMhhf=n^TqL+KJJ%5eH}OPnr|tUIJGkz$@$UBY*MId=(H zi`TYZ8^pGBz6NGD;XgmrF5zB<+wXiFFHg<2LGCg+>akm5d7`wPE7-xVB+k@uKZdOP zG>iGGR#Q$Fzw;#M61(#xD8cQv(RJ=_=PnUDS1Zwz6>SEyW-l- z-sVfy9rotSU}NQ+b)p#h=5w82q@2&wBJhP)2%OKe3k)vjK0g%B=RH3hF5#Jv#9_b9 zC9IRmvk^^aF#@Gjp48n8My>?iA#tt*B{=cT5N|D+*&}8N^ZrA#Z zL(cQ^i@k$6Ah~@Gb#gzm=&!rKXJZF*(34t5&eIO*-M~t(YSgMiZtf!M4ppi!yIYPH zr`yfS#dT@%d6Lg!dnnInKJ;e?@+@+BVw>yha=I>`Bkv|OgYc9w)@4}d+uU- zC{GRM>D7H?={)%y4{;n|JZc07$r_Y}Nh(m#c4 z8#q@iR)%McU&e0t%h6JSQl1tl*Q^fM{$cMyQTx}UQmrT2&sdPXR&OU)aQ-=W<;Kd8 z$|m-Z=kz}%nr->L-ep+!5523s?|V0j+8?*h`-%47$8!cx5RJxKcip!>W!L0n_bsO_ zmo=_NIe+UjowQF0rBd!UqP$nf7N2hSjpBA+k8)nY-Xhvtn)CHy&Z|+*{Jo4k&a1_o zSE8IbFPX=ArI>Tv#%P0ioUe{KThzVnP%3FH09@F!Be9K!#Deo(IqB7QQxu0$~%a!v^+j<&T z*xSL@B?eAoIqSsxTkhwyJ3K#{&pVE%yqo4*iltkLex?4hcQsk!p#klfrnrpCRi`tB}`|gj3w{8Nv?*YSa;`8mA$TQ?6{3Q!H*IN8iDQdjV zvd8}Xr9(XNDK+h!!`aH1K z`8@&6UfX8LYKtTMY7tLjdx=?&aH_bR`mH>qzdoB?Q*wp7SVw*rYgP8z$1j+L`IL}z z55}480HanPrxnunU*ObyJBGz+Gba?vv$Q3TCFN$cnRV6CmK?D;ZDt>Jw0Tc7(oF9A zz`%OiM8Gz3uS94Q)7#KqjnL*O)rR)<2yKq`ZD`+!&}PQihBo6W>CMS`oB`&TBo`+2 zCceY?tIke%uZ`RshwEv#vq44{^|agB;1;skdfM%5kkMZ~ZN`4h?PLr2e?9GX_APO` zP@lHJ5W}~z!C)y>w1cHIweO(uD%wHg&1f?ptka3)ip_PxJhzUvuc&eC Xt&2%zgymn2H03ugH7+=tG3x&T{t=c0 literal 0 HcmV?d00001 diff --git a/ChartPanel.mq5 b/ChartPanel.mq5 new file mode 100644 index 0000000..cd445cf --- /dev/null +++ b/ChartPanel.mq5 @@ -0,0 +1,64 @@ +//+------------------------------------------------------------------+ +//| PanelChart.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" +#property indicator_separate_window +#property indicator_plots 0 +#property indicator_buffers 0 +#property indicator_minimum 0.0 +#property indicator_maximum 0.0 +#include "PanelDialog.mqh" +//+------------------------------------------------------------------+ +//| Global Variables | +//+------------------------------------------------------------------+ +CPanelDialog ExtDialog; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +int OnInit(void) + { +//--- create application dialog + if(!ExtDialog.Create(0,"Chart Panel ",0,50,50,390,300)) + return(INIT_FAILED); +//--- run application + if(!ExtDialog.Run()) + return(INIT_FAILED); +//--- succeed + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Custom indicator deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- destroy application dialog + ExtDialog.Destroy(reason); + } +//+------------------------------------------------------------------+ +//| Custom indicator iteration function | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const int begin, + const double &price[]) + { +//--- + +//--- return value of prev_calculated for next call + return(rates_total); + } +//+------------------------------------------------------------------+ +//| ChartEvent function | +//+------------------------------------------------------------------+ +void OnChartEvent(const int id, + const long &lparam, + const double &dparam, + const string &sparam) + { + ExtDialog.ChartEvent(id,lparam,dparam,sparam); + } +//+------------------------------------------------------------------+ diff --git a/CheckEntry_IAC.mq5 b/CheckEntry_IAC.mq5 new file mode 100644 index 0000000..a181a67 --- /dev/null +++ b/CheckEntry_IAC.mq5 @@ -0,0 +1,33 @@ + string CheckEntry() + { + + // create an empty string for the signal + string signal =""; + + // create an Array for several prices + double myPriceArray[]; + + // define the properties of the iAC EA + int iACDefinition =iAC(_Symbol,_Period); + + // sort the price array1 from the current candle downwards + ArraySetAsSeries(myPriceArray,true); + + // Defined MA1, one line,current candle,3 candles, store result + CopyBuffer(iACDefinition,0,0,3,myPriceArray); + + // Get the value of the current candle + float iACValue=myPriceArray[0]; + + // if value is above the zero line + if (iACValue>0) + signal="buy"; + + // if value is below the zero line + if (iACValue<0) + signal="sell"; + + // return signal to main module + return signal; + + } \ No newline at end of file diff --git a/CheckEntry_MacD.mq5 b/CheckEntry_MacD.mq5 new file mode 100644 index 0000000..8a1dcf9 --- /dev/null +++ b/CheckEntry_MacD.mq5 @@ -0,0 +1,35 @@ + string CheckEntry() + { + + // create a string variable for the signal + string signal=""; + + // create an Array for several prices + double myPriceArray[]; + + // define the properties of the MacD EA + int MacDDefinition = iMACD(_Symbol,_Period,12,26,9,PRICE_CLOSE); + + // sort the price array from the current candle downwards + ArraySetAsSeries(myPriceArray,true); + + // Defined MA1, one line,current candle,3 candles, store result + CopyBuffer(MacDDefinition,0,0,3,myPriceArray); + + // Get the value of the current candle + float MacDValue=(myPriceArray[0]); + + // Chart output depending on the value + + if (MacDValue>0) + signal="sell"; + + if (MacDValue<0) + signal="buy"; + + + return signal; + } + + + \ No newline at end of file diff --git a/ColorBars.mq5 b/ColorBars.mq5 new file mode 100644 index 0000000..aa7825c --- /dev/null +++ b/ColorBars.mq5 @@ -0,0 +1,82 @@ +//+------------------------------------------------------------------+ +//| ColorBars.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" + +#property indicator_chart_window +#property indicator_buffers 5 +#property indicator_plots 1 +//--- plot ColorBars +#property indicator_label1 "ColorBars" +#property indicator_type1 DRAW_COLOR_BARS +#property indicator_color1 Green,Red +#property indicator_label1 "Open;High;Low;Close" +//--- indicator buffers +double ExtOpenBuffer[]; +double ExtHighBuffer[]; +double ExtLowBuffer[]; +double ExtCloseBuffer[]; +double ExtColorsBuffer[]; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- indicators + SetIndexBuffer(0,ExtOpenBuffer,INDICATOR_DATA); + SetIndexBuffer(1,ExtHighBuffer,INDICATOR_DATA); + SetIndexBuffer(2,ExtLowBuffer,INDICATOR_DATA); + SetIndexBuffer(3,ExtCloseBuffer,INDICATOR_DATA); + SetIndexBuffer(4,ExtColorsBuffer,INDICATOR_COLOR_INDEX); +//--- don't show indicator data in DataWindow + PlotIndexSetInteger(0,PLOT_SHOW_DATA,false); +//--- set accuracy + IndicatorSetInteger(INDICATOR_DIGITS,_Digits); + } +//+------------------------------------------------------------------+ +//| Custom indicator iteration function | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + int i=0; + bool vol_up=true; +//--- set position for beginning + if(i0) + { + if(tick_volume[i]>tick_volume[i-1]) + vol_up=true; + if(tick_volume[i]rates_total || prev_calculated<0) + to_copy=rates_total; + else + { + to_copy=rates_total-prev_calculated; + if(prev_calculated>0) + to_copy++; + } + //--- copy values of MA into indicator buffer ExtColorLineBuffer + int copied=CopyBuffer(ExtMAHandle,0,0,rates_total,ExtColorLineBuffer); + if(copied<=0) + return(0); + + ticks++; // ticks counting + if(ticks>=5) //it's time to change color scheme + { + ticks=0; // reset counter + modified++; // counter of color changes + if(modified>=3) + modified=0;// reset counter + ResetLastError(); + switch(modified) + { + case 0: // first color scheme + PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,Red); + PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,Blue); + PlotIndexSetInteger(0,PLOT_LINE_COLOR,2,Green); + break; + case 1: // second color scheme + PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,Yellow); + PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,Pink); + PlotIndexSetInteger(0,PLOT_LINE_COLOR,2,LightSlateGray); + break; + default: // third color scheme + PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,LightGoldenrod); + PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,Orchid); + PlotIndexSetInteger(0,PLOT_LINE_COLOR,2,LimeGreen); + } + } + else + { + //--- set start position + int start=prev_calculated-1; + //--- now we set line color for every bar + for(int i=start; i=lECNhi@5JFW@Ehq1Q^-g+x1$$010gF%;O~1z;PQ2jCigLtRxH=?|t;U z%U|cDGV9gTGe|IKFBFnyx~r?Q@|{^()&KiH=SJs7r$=WN;OF${pGW^B(3~6nIvO9r z&)VquXjgt7jCMx%M*E{D^8MN9YWI0pzP%oOK3X509sM-=+vrOf|JOr-hf!aQ-pII5 zMvq31W!&*-W%OD1bEf;WGWue)ELX1zWcvcme~ezrGyC#wPp*D1pSA_6!-8iTW`S^D zAl@5&)zRYBh#$quj^O3&=)OSrQv%N-@y~Ar_T3KW59I&8V1C>|oYUaM=&&erqT`d= zxI04gXQRIg1pafPm> z{QBs&^uphZ0@3Sid~}1(M``*@#zbP6!QKQyX7;d~DKoq{x-i-pZH;b?u1K#pN8cS# z0&J}aKR@h!?z_DiIr-2+Ij zbayXG+xx;Rk2+eslxy2vi&gmx#&=~7kEJK{!luaIb7}iv^e4FsR+wq+c^@r@L*YZ? zJdxhOmBO*vL5<|BcQfD)Kftj*`kl;O`AqT0w=d=DpNe_5`oG*^M|M_kd z^yK-`AFKUCd35SC!I(m5@V}Ay{Mcc6PqZf^K@a*rA=Lb{@WY=4gX^Mm_1m|CyBi1J z)`ZrV55E1I+_@v)w2eKd->jv6vzFS%?&&wXr&smci3!e(hZaePnho9)JUKR%AMQ=C ztvEmGzixvU2|vnvFFQRs7XGSX3A%!vx1D4to=ToA@%K!m4tZ3G()&^G?FmKD!H+vV z@l5#p7io>IKubIqXpmTJAbK1h!XUxJ_?xuZn8-EWMftOPaQE)$FHI<3NYA@n|LESa zjQddjJ`l)X$aie+kJ1k|%17UpRxjoHP6uH*GFO-jwrJ}>e{M;SajX_zOph@uECLw8 zf~h6LQjAASoxT7&&$~}pjgtw4=4xd*lf) z4c)-|D}iTUxC4H`hBJcFNHsF@OB*osTt3Z4pR~_NC5Iu06kUwXD{<*pT3o#)e1*6E zDB;A?ws=qW30sN0VQJq8Kj9O=kNXF_6=m%2lm5Z#B54}a#217YUI-?TX^&k}O4`hi z=Ko@9Vssv!C(^xEc%Qc_e6IRfV+3R!Ul;xPRgi?W%nPnz^e5q?7acv!SB{#ggr)3* z9-yZA%kg=aZkJDF7G2x-L!U_ZwzPbl$js2E9}6}tQjHM3>~PsXKBJ@eb3c}On%6r| zpHS#^C%N^099v_Nb9`s0YSEGDJ;&q_&CFcv$y}oi#ic#JCH$#&I?g^qy(joVTVt`y zIl$f4m(OBNd{~Q8!v6q%=o*GUgUIfMA^rC<&i zUdu1ivHi|HVdu)8A_m>RaE^lCG#*8|$_yFYho0Jzzc1yl<3D&qlA=jjTY}REGUM%x+b)f+NhEqzTHch{ z`>I5vm&G@~(0$sRNFDu{hxt(YDPx1-cvnBwd)X4)^ECMiv_r(d^Nec-oln1a?DK}R zp(1|azVw~qTfZ?1@sqVOo_r9bdiGH@`=c3#+PFGuY*icga4?Yi%Ox}li_OvEDB~O= z87ywvi{_cgbG;6D8p;Q>(`T{DF+#|pQ`pirhc`A-q**RFSWC;EANz5c-B^mKW?u5 z?P0 zeV)i_D)E!qW$vAppO|0PKJ+irN5By!bkNi02mZc=3Qo;Sc8o@R;jOd;!mkuuk((v=dR>YGrl&lyIEKaQC_x zPJJiUd$mEGayDcBM~@X-{aVI-GCI{5@$~3vMSI2mB3Dqo zK~=~D=@Z?yDnCz!v;I|dn^OBLx$aM#Oh!d_0Z+PG7{}7jPoxKWA#b=RpM2Dlf;kc>XFTwUr}D(fLUu}+#{o(-y;j%&W65vvX>_tz zs*qDBCmws#sN|7I2en_%q=i=XjD-%mhRd}_f-Ssy?(u|4MJn@gWH<#pJ9fVjxN|L$ zKEb!QnI0)yQ2&ui_|2+(FE!+TSs*)A(K%;CmR&0?Pq4f!95}soX&r~ay*a@nGWAGs z<#=7Lz_%Q5=@7-rpxRSIAd5b23Ag6fV=k<0!C2Z%|}N==&Tk@#Qh)~3NRRO(b4GA=bQnnQOeHUt;jg0JUNIY#!M8jL0R znQlW*P2hhh{({crq0B|+It}(V<<>i^z}hIb#o;uX4e7tsR?f>>aYS~EY$3KvbAo7A zt3knjabMRsbp`&Zw2f=Enz7Pq?3@E~nb0wxi=BvlpskPd@u)!F35yZKRqRVB zNuC~Wc2#15mS2(gna0Ur&$HQ7b3p@n{9BFIA6>7`o)1 z*e6x*O+WeczNXeg3>-v~GqOokpT6;|0L$fT|4o0$XHkL!v8a4VzWrbInWy$@b z4Z#r=0%o^w3y$+TvD(^I3m&sQWvsK*`Hn^fd~&DBaxvyS{X52&#hM!ShxZ+End9Zo zAiTV(Xs^f}b#ErcMEWhK#rwt!)*gtg%GQ1U8^Sr!XR_}e2QRd#M}bP~?G>P^8VGPo&M>-%61*FGH&do(*9$%>Lc z(i|yqkJoClbFRO35p4{WyxMSi67k_N={<4;`iWT6)(GvI-(8Wr<<6x~1va$4)ivjl zXqgptxuK%YbMl(AP+d-c>8>T^CXI3ow(}CrtR6t$ih@fBv@bFRx|(yjv+qB3^H`GI ziMQkqu_u*mHrAe4)v<89*GSr()zNQ-Dy$w{?)uY;M}Jc9pO6+eCwL|wMNdXmd{(D` zRiSQ4^m##^xg;K;U%M(bs?eP4o5`I`xw1C8E_c`E4iV+_@t~>q{chvcw6S73_0L&( zqFTM?p==Ib&Np>|FU6HjmB)=6*Dc4*&cmf&R7RknkGwPMCmhDF_N#5Y*EsSy z;ql)~oT01UHkNZ-UeS*Kbh^t}=G@_OU1qMGQnB9keka-H#bH6v)WGmeK9N^<&C6$- zV?MG=v1$;lt{`SH{xDle^+m)_4Wr&0nxLi=V!!=b;6J4Y^w(ASPo*AJ*%7YV_Kx>t3g`R3I z>)S^u;ZkWfZizjR{cg3lp2@-!dD!>1ES%~jr`}(m-P7IFSdTZ|?ri7maaWzdNijLD zjjH4lw0eJQ($;II>(sR;S-YSjf?PbZq{T=!7aZ{P9Ly_(e zOm{%A>l2I1Di+kTJ@-R;DrYrIU;S$;&X1{m@l}=FLsrgvTi{Nw51n<(mRm)mW2mH( zj$|}99xf?tEm%RFHlFp8+M;F-P?zYAsEp%Hs7}Hs6jN8&aAMuI4oi(m-@eUI6$8P@mOl2u=h9Z^Ep#uES0Bbbes(U)bg1*V>uy!6rC!eJVC_6zLTy%&iZyx5`DmEk z8p1`xD}jJ@xZK?6{0Cx(JpXgE%Sxqj1l&fo0(%@;x5r1<6J5TNtNajQ^8aVSzI{(4 z&gryk$K+j==V)qohwyvf{95;%59_)(_pJINpc>pQ z4OjNdl`@{u*y>%hX*u)uoXx|JR?^P`v$MqNF;?y&rL|_Y$a>zGsI-Z?Yb*LOz}Roy z`sdj81xkE6x0ja0B5&nxv-`H1;F()pB|!Tb7o8Um(^eMj+REe_YY}6~+*|LaJ+VCI zixM~JsPr1!8buu%ZHKk0(Fv>MkS;eORh9hs_onbOt51x5{MI{$;~{dIMT{m(WaN#C zTjAV2yI<3LZTZC~IbM}em%C_J*8{H!rk)+(XyYJL8+q>VKOc{k=OK8UZ7(nR56yWx z|3*ErC)%e^3#Yu=pA%P&AG7S1uLpv3RIh11DZ9`tFWZ8#Wg}*Rt}TydPu=5-ndSEG z=hlqwid7@h^8RZ2q2Chs)9l@B@~jlHs3y(_n)&yLuNJM4`F^joZJ{@VWh0Wq;*OIF z03tu&;Z#qnkym?Zu^y2hwzH2HZKigv>~eGGdz<#^POB-_)&7D zkEAjkPQNFal-eHmRGZEs6aHlBFLAHDI$esQr6gu_TM$?6>)xqnizCL^2gIqS<(Yg6 zzw`5kV9BT-SL6I-RBAwC-@bxYlhJ9be6io!^Zhbjhhu)(CQG7gt-7j4_M?tx#aj+t znk}8Kfyhm^&un0fetB1W(JzmG%`~Y=s7Db)_ewaW!Am=GP~4Q9uh$jkv@a}ZKep!A ziIGR59H3=bDdnu6+P;5Je($Q~Qfw<&dornG% z2Hjyv&C$}aWg%z7YZ?Woau;oT%@VeKyC@?rcjmj1Y4NC_J*MYI>ixY7?5=xqjgtCW zrj5(_M^2-+#of~coqT7qQjO{jJS)pt>~(KHVMl;QPP&p3W#?T!ACE|WK0NwaSuwtd z`S;vXlzKU>$MLr!_(_nflIQbG;T5r34s_2`IYDes49olw&qMW^QUR&G5s&OC9V@5LhzZHDoWw5@L z*79>tv%*A@{=DOMT6{SKbEW<;c}n$7e>R3^qTbOy+PciLAV%dFsm_xF>6@wR=-OP> z$F9TC<^?=I*HL7COn_q_6`poNinBZFK&8?UjF)bEe= zQ`IV>%LuvU3AZ3&{pzqFDXW);LE{zlR0%pCP&e=2sQo(^_`>yYYZTQju?Nk6Tiu-B z0SkTFM)hfgvsPO>P3!9YcfoebpALU6G0GRKV#RH5LJLQS2aT~pbw`OV;^?TLQ}{_b_2yuM#SdG9Fy9uHrB8XlJ>o-vYW zyEWP4c29UHzCmlc@7}gjo*6fKQFm3o7ev?D>U({8tE2ICDKF}i#5qu-kiP}YMz8;8 zgxbu@(b_Z3?eR0Q{6MYy_B1<4)-PWDT54o@o2#E|c}3u$nt>hByc2JGvT~9?N&be) zi%r2LyQ^MEyDz({l$=^RpjP>u3)s3OE zuk&IYt)B)Fq7Q|~%YwNL!RAfDo$i>P7Aq@u*olTt(LHu>M5&dAolXlDhTth;YY6ua zf$*CK+<7z-XNmD9T1uynA9*+R#A+T)4y|JElZgkX-nzflukF3p z@%OetqZAvP!msdY?k~C)Q|$B3J(Ev8`}V6|ZFcmYCEAvCflmbf5?7W*wTxFwPq`Oa z$6}PCv8+?zWnxP%3*PKKQrH!(I-Av+w0JDdElg4<(!@Xw>u6ES2x5HY^eV$vW;U$io(q&a>tna$@Q^rdFNO?Z?;!u5^&HXz$*Cdxa z)0we{6pmkI9mF9?&o(RJX-O?`mL3T2W!3n;lxF0^xH^B56SVa?-oZ)U@vX#EB{Weo zHpPeLDWXJt1^2s_CEnBD^tzAIrkqzNrO#@nGSzYVPVKavzlV8Me2f#7IrRHkz6!f3 zb7R+7KD%@l_*7&)>wGY~=Cd8u?j1c5T;_9hYLV5cVN?0g^W0FERdYO1V#c3@%G0Ae zm4?kg-Ae}?*2ar^kjouMrDL9yQRBFEto2)s;kH9hFLchdoeto_y><9OPf$54{4zhT z6|U4%bPTs?#B*hYq`j#YX~{%iwIomGT%%FQws0%5F_z!cMVpkfN)1ql*0oeFp;4c< z=Fzt@JY9l_C9Vk#bDah?+gP8KP9=qW&ftXpY_EiRC}giVb(hr={14l8kz>d;&PM2? z%z{8qGFjfMc?~VR)@JXcUFrNGd>FD1rt5OPsf~&qsh>8Vp8xTp!-~I`MJ7_e)O=JN>qyzrSe}EMr;eV9g~zP zMJoq2Y8kcnrzLZlhMO#^L(5o{1?SjjZ&6Y)eLlujP~W4yzCNN3zae^XPkzP{(SXTZ zds(!nx~hgtiz_*&l()d|ON1UzDX{aK)FOBbuiv~YdlK_gg=%$BN2#Y;9H^Guvh^E+ zEo|HLQ_)LHR%$rc8>Y4MSWmCl_$9v+X|MNkO_b{s$AACIXA3jxtL627R?iP_t95<% zO7VQneU_SVg;M`2ExiIX!Z^K3k3RZwp`)~Q)`zYePw`l@uMu1Nc~*}x_E36{ez$dR z`l@9=4IKOR^N41bg$2I-McK>z+pWw!MoOZ@x=GU89Q?U!E#b+f?xL3g$*i>cc?(cc_Y}mOKWUGmHcpG^< z!&kqVo!F~F2|RcTobdWwKDnm$vq4n?wPx>64L`LtFMV(Lv=I)=SG^Txf*)meQL|o} zmmxc)(Y&69w+H!)qGBW9`76}Ecvhgta)3`xzqOdm;Jcr%k@xdA=;UV zNKblO+&!b%Wqoh0YI1!i=ncU+e6}Zd(cDM*v-m5ssjpqf#SlDFYwr6rsdv6DHA{by z+!3){yu-aa-Mw5FzO86n>K(?Cl~j%3IX7QZxLwuu>6m+BHIUxhlYTzy;IgZCs#k9d zlx;nf_m3X~SMg4(`dDR)zW)2Yf@`uGGv9iN%^6IPy&iXT!iz6;g>r*E0krzWxTD{Y zXB3C_6db`*rv^Mu8Zs6*Emou;;cvoK5UNO6FFhpttL zNwvYrgBjT@<2>U0na4TVn~mT7SS0Ch(j%*)bqv>_8fxDR-m;HK?Zvlm)O)UX_##*K zzJC_qzWKUs8#mauh{u$Bu~+VSq2tMNy_r}y_=so(D}`Tb|Jlv>TC9v+qn*Y-R3b%t z_-Xge_hS()rK1eDlO6}uEM;vLCI4GK-uoJ^Df(Aw+p?*e%KiG&Azu8Z)2Y-C|GaV5IbnETH99Gjsjlv)08d2y zraiRj;_z(<{*5D+dO0@Eum*K~?GBu@WT4hdzklR7p4U(6gGapvb}Z}8)l^=oVsU!6 z{Hj@~kW%<~L}q08Vva41%GQL#c<(4H5clO*-<6=;dN_ZNi{+XJkEMNMuh5Q2-~-WB z$WGm&vE!qCmv=(4-djHHauU}gT}E2Z(U;oHT=SPY-)DfeIxD}W$Le}6U6&sI?pTHS zyu7o=-)Lmlc+U|rCRsODUCTYt`7Df+Q~B=K;L%d9RNz?Y-fDaFa5ygWJd%4{%xh2b zZT{xb^@+xuzLH6nR^KlavD2URwHj|cs1~O=PfcIZbQsc1pRI$Zo?6XH<+$MNjy2(r z)Z2tweJiWLvC^$(e^^{74D;f`IVP1p(+)WwAy;_Q=5@eF?vHGWRnVH!A=tAwRmHa` z4X2{YT!iC&dBi3`c7A|37Itv*!nbL(GV>OOiG;OEvO*XCRYe6;p;~b98g{y5v+sDayzdy1<+m>Tb zi?VD*+|QAGD%u<$Z9IA^UQD~!rxH!I_5>(ZN?*{a6>~D%!tbj>;THZ*3nZn_82gG) z5(CmBdpv5sX8xSx+n+ouzJSw2b@6?n(6dgDbDrb6WVqL5q&@K%pxpiL-E#VFdgYI_ z{S|rkMHfkLN?W2Pc3rTKik^OuZ`4tf4f$NIpApEwvcXtF@0~qHz{^McCynO6dFhd_1gRs<()#@{9r1726`Mk43Z8?mrfOXQk}1w9ytS z&H7fc`%=C8NTf^S|8223^r0>CD7w`Dw(eE4RGi3__n_1-`_b#|JaTQX4Dm|3x8(iR zh1V*r!L!C&_XMB&#R~hHP(bgfcEy5Fm0~AXu~N<%Qy1inq?-YWNBVuzi-R>SAg=Fm3s+G_HxZ123|BeZ!& zcUgGLRpG9_J|iU4dW~)C{knIT>u+!lW%lKSYijZLWv4N*AXcT6`k21!N^mwAP(q*)%E4l2IeJvixdRKi0NAa6dQNPDI!*`s}eO)+jOU}ID z|JCkR$Jl$wfTsavB>? z)-ctd(*XW?#=Isp`&nZ8+KA3Qx$a=JR0CiEpC5RrT16KnTP??uV4A&|&t=qo8S|?2 zb3hqyPXBwPhy5V{;`NB z{a=q0+N1qhbRjVs5>0fa_%WQ+;w~FcqZNz1hK~EajG%tgn&4VxHIC+2>yZ(o|FOd` zpT6vgjmkM+5Lx=M!!llikJK7J9ZN^`7;VV-=-Wp!OU?#)Be6F4G`>Pol|M?G3+(Y( zV*mK;7tJSKGD?5SG9R3;MONPOXjt1^j{34Z*3V(&c+7IQGgB>#nC@#Bo-Hl$LVd0J z0_jeOS#aWgu*yU2%t|v?Q_}~1_)h!&W#+ykPax;!&-y;ae5QC{)@og_!-`&@<@hpum#tT&bvf@=-`|Lh{bHux zXAk#>S;^kZa&y0^Ko?jmukwKU)ngKT%DbbOJzA2_IQHodG#wF9_WFG6Ma!q zhw6VgpzeE{{PSONnU5PaP#jrTR#6x+PbWZ$4`auCO|3~B9=#!y~7q?nN zqgnpOvl^pkb=04aM|P-=M~h`RlZSn|JNT+38=+KMKh^ z9?~`&9*1RVdDqAgNn+*1@-BKpG<)}RSy0-=)r6dD)0#n6gLhel)L!Km9nFV?@PU_>R5~85)u$Ta}gc7EN5LTJU=dtwk_u z69e>BHlxpU<+Y@B?32$?XXW?Cou)=#*SX5u51B!_6F%-ie*aB1irIcv#%H);`3Z zx|cW>>2|Lt*Jx$tosm>Gu=b(#Bx_QmcuA}?Rf=A*_-p0vy0p6?YpCB%^vv`cxKggD z<;3c0J)(YNtyWL@rDkt4v`}B$yt=wJv(uQxrwf9if3L21lzr!`94oSl%3BBdtMgWET&s+urm8lBNO@m@^wr0>PLp#(YmL36 zk&lx1QO+{zfLeXX$D_@n6s~vP;rl+3==fcD&&6Ldz5PHwAO4vpW2w+_ z>@KG7^RwV#S~b&ku_;#%aChkdSDvwoCu_0l{j$TX))}G0d6!)tW4#TyQNK!~2d!Gx zXFe7CqH*)AyM(ka^t)4?>XR**$Bx7bpNPLq^+pa=^zcdu<6ZiW6|HY1bITcnR=-jo zSF75sGU{QamZfiu*OM{cOloA}7;g^dHrp7EsW_Y0Qh9$SROp`SsyM#rDwX?iX3*!@ zp$4YaUum(e7yh^4`{t0L^J?Tc*M8XAo<*EN)Tq&d^z|LJZ0u{=h;5Ev70zspm_Oyb zv?^6B^`}Y*=UIcxM|KHS`K*tvxt2Qj_dS=x9KSE7eg>*vMGA6T#?JG5;#24@BR&&> zu($C4ZCR9;{4=)9_d@C?#oR0>Mz|E}i5*IXn3;-uJoTM9mZ8$>3C_xK-$0?X`leT% z9no(m1GKuc?MfHnwReQxlB&=3E~?#sHUs^nWi^M>{)s`$nJRDRI7-*Pv{E&WQbOI=b1Mt2{l?mixQcBRt>8bt`^WIVH>s74 zaHVi-%{EreJR;u%;%{DQozttHFwgGGP8YIjy7En~dqX^sALMtV+x;>YKSbY(4mN)) ze8RpKyatEOPlPt&UU#KG^}Fml;}qGDP52?))%r%P_+S76KW?G%@+h7;xwS<3_8tr z05#E{zugP-vz(vE4>VW=Pk?*)N!59&Oab48so)6LZiuQP8 ztuv?8%CZZ2OV$Oh%PJMSP4zSOmsMf2ZO-q9lPR$|dsNIDZfqsuXtO0Yb{hZAKlad! zNyMR_fu!e%xy&17RbFaRzAAL(JeKhkZIMTk>O+6h)E;NM?4-ZH(zO?y^g_5JzAv5g zQt%I!JHF(uy%SrXw6OW*}bxsD{`{RLfbz2saw1Wb))kK$w>IQ5qML4 z=e?--ih6#IW|&$h$3~(YLwdkYuDrE!S!J{p87@R#82~!+-R7) zC^O5?l^TM%cbQ4#QerZ&XHmiKf!={TZLL+c%~hGpj{JakPV6Dyvn}}33L5HqIC-el zC(iFJs5eD3VHYpS54Aoi6%_xArqh9^w0ZEK%EJ4zdy8rFe7zdWwjS`ZG~pd|i^h2T zhd1zu;HupEMUDl-JjuygZvwY+o!u_j!?tF1|LKfG^k*jHYJLJJ;%W`r44(^na)uqN zSd8NQJgYtSGazec&@48eLq%jAZFN-MTfc|&G4Q!zsO7=6{Q9$ECOP&z}IT2TGX`Lr=nH8Wm^Ng_ET>Y zIN6)wXWhoAnUROykJwoj>&W^rUM*mVgYq}!FoX8Mb8hxJo#@fE@ zpS^a!6TjtmqSZc?A3RCN!`I#2KgwOlh>gtuat1aIk(pXIYz9>|9)Bz)W1NxR&vzrJ zb&CC4Z@LG@a`o&Rtixl^LRB00kIg(=TN|mz$KSYvRA-?5nxAJQ+W+V+b1i-!!ZFTA zRE2YX^0n?f({jFT=&@BUY5K`(2Mq4$Q@rTBiRtx+v+;f{;+}ODSB0Ck64JFQ=Y>LF z&I*mZ%2}ayJ(c>UgF3&HnUp)nEw**>DC7Ow6ys{E5M8t=B=xc#OSAhQ=HIc8hFH$z z#ASHV<_KepT`zwjOs`ywVGD z<&xxnJV)Rdc5N2z;9eIq={r|AV@vY`wWn0N;Wct!k&^)7FDe)Ls{ zzg#-=6Ing48QZk`T^ZOfYj3VIt?zcfpAYQ!^BMd7Vqm|Vo;VAcIenw;)55FJS z@9$@XmyFX;j_}-+(T)FYm^meRXy3E)1-r3^_B|`ER|eyRmb3B4=Y#vE-urMFpQrcXl0HxG!{u_G-v2O=qJNkR@3YD0?C`G-=ExPHzA52Q=nE(I) literal 0 HcmV?d00001 diff --git a/Custom Moving Average.mq5 b/Custom Moving Average.mq5 new file mode 100644 index 0000000..74ebd3b --- /dev/null +++ b/Custom Moving Average.mq5 @@ -0,0 +1,199 @@ +//+------------------------------------------------------------------+ +//| Custom Moving Average.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" + +//--- indicator settings +#property indicator_chart_window +#property indicator_buffers 1 +#property indicator_plots 1 +#property indicator_type1 DRAW_LINE +#property indicator_color1 Red +//--- input parameters +input int InpMAPeriod=13; // Period +input int InpMAShift=0; // Shift +input ENUM_MA_METHOD InpMAMethod=MODE_SMMA; // Method +//--- indicator buffer +double ExtLineBuffer[]; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- indicator buffers mapping + SetIndexBuffer(0,ExtLineBuffer,INDICATOR_DATA); +//--- set accuracy + IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1); +//--- set first bar from what index will be drawn + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod); +//--- line shifts when drawing + PlotIndexSetInteger(0,PLOT_SHIFT,InpMAShift); +//--- name for DataWindow + string short_name; + switch(InpMAMethod) + { + case MODE_EMA : + short_name="EMA"; + break; + case MODE_LWMA : + short_name="LWMA"; + break; + case MODE_SMA : + short_name="SMA"; + break; + case MODE_SMMA : + short_name="SMMA"; + break; + default : + short_name="unknown ma"; + } + IndicatorSetString(INDICATOR_SHORTNAME,short_name+"("+string(InpMAPeriod)+")"); +//--- set drawing line empty value + PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0); + } +//+------------------------------------------------------------------+ +//| Moving Average | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const int begin, + const double &price[]) + { + if(rates_total +//--- indicator settings +#property indicator_chart_window +#property indicator_buffers 3 +#property indicator_plots 1 +#property indicator_type1 DRAW_LINE +#property indicator_color1 DarkBlue +#property indicator_width1 1 +#property indicator_label1 "DEMA" +#property indicator_applied_price PRICE_CLOSE +//--- input parameters +input int InpPeriodEMA=14; // EMA period +input int InpShift=0; // Indicator's shift +//--- indicator buffers +double DemaBuffer[]; +double Ema[]; +double EmaOfEma[]; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- indicator buffers mapping + SetIndexBuffer(0,DemaBuffer,INDICATOR_DATA); + SetIndexBuffer(1,Ema,INDICATOR_CALCULATIONS); + SetIndexBuffer(2,EmaOfEma,INDICATOR_CALCULATIONS); +//--- sets first bar from what index will be drawn + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,2*InpPeriodEMA-2); +//--- sets indicator shift + PlotIndexSetInteger(0,PLOT_SHIFT,InpShift); +//--- name of label + string short_name=StringFormat("DEMA(%d)",InpPeriodEMA); + IndicatorSetString(INDICATOR_SHORTNAME,short_name); + PlotIndexSetString(0,PLOT_LABEL,short_name); + } +//+------------------------------------------------------------------+ +//| Double Exponential Moving Average | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const int begin, + const double &price[]) + { + if(rates_total<2*InpPeriodEMA-2) + return(0); +//--- + int start; + if(prev_calculated==0) + start=0; + else + start=prev_calculated-1; +//--- calculate EMA + ExponentialMAOnBuffer(rates_total,prev_calculated,0,InpPeriodEMA,price,Ema); +//--- calculate EMA on EMA array + ExponentialMAOnBuffer(rates_total,prev_calculated,InpPeriodEMA-1,InpPeriodEMA,Ema,EmaOfEma); +//--- calculate DEMA + for(int i=start; i +//--- indicator settings +#property indicator_separate_window +#property indicator_buffers 2 +#property indicator_plots 1 +#property indicator_type1 DRAW_LINE +#property indicator_color1 DodgerBlue +//--- input parameters +input int InpDetrendPeriod=12; // Period +//--- indicator buffers +double ExtDPOBuffer[]; +double ExtMABuffer[]; + +int ExtMAPeriod; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- get length of cycle for smoothing + ExtMAPeriod=InpDetrendPeriod/2+1; +//--- indicator buffers mapping + SetIndexBuffer(0,ExtDPOBuffer,INDICATOR_DATA); + SetIndexBuffer(1,ExtMABuffer,INDICATOR_CALCULATIONS); +//--- set accuracy + IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1); +//--- set first bar from what index will be drawn + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtMAPeriod-1); +//--- name for DataWindow and indicator subwindow label + string short_name=StringFormat("DPO(%d)",InpDetrendPeriod); + IndicatorSetString(INDICATOR_SHORTNAME,short_name); + } +//+------------------------------------------------------------------+ +//| Detrended Price Oscillator | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const int begin, + const double &price[]) + { + int start; + int first_index=begin+ExtMAPeriod-1; +//--- preliminary filling + if(prev_calculated0) + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,first_index); + } + else + start=prev_calculated-1; +//--- calculate simple moving average + SimpleMAOnBuffer(rates_total,prev_calculated,begin,ExtMAPeriod,price,ExtMABuffer); +//--- the main loop of calculations + for(int i=start; i +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpMAPeriod =5; // Trend MA period +input int InpPeriodCCI =37; // CCI period +input ENUM_APPLIED_PRICE InpPrice=PRICE_CLOSE; // price type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot=0.1; // lot +//--- Expert ID +input long InpMagicNumber=120500; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error +//--- indicator handles +int ExtIndicatorHandle=INVALID_HANDLE; +int ExtTrendMAHandle=INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iCCI(_Symbol, _Period, InpPeriodCCI, InpPrice); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating CCI indicator"); + return(INIT_FAILED); + } + +//--- trend moving average + ExtTrendMAHandle=iMA(_Symbol, _Period, InpMAPeriod,0, MODE_SMA,PRICE_CLOSE); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating Moving Average indicator"); + return(INIT_FAILED); + } + +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // long body of the white candlestick (long white) + (Close(1)Open(2)) && // close within the previous candlestick body (white) + (MidOpenClose(2)>CloseAvg(2)) && // uptrend + (Open(1)>High(2))) // open above the previous day's High price (open at new high) + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nDark Cloud Cover detected"; + ExtDirection="Sell"; + return(true); + } + +//--- check Piercing Line + if((Close(1)-Open(1)>AvgBody(1)) && // long body of the white candlestick (long white) + (Open(2)-Close(2)>AvgBody(1)) && // long body of the previous black candlestick (long black) + (Close(1)>Close(2)) && // close within the body + (Close(1)50)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: CCI>50"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((CCI(1)<80) && (CCI(2)>80)) || ((CCI(1)<-80) && (CCI(2)>-80))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((CCI(1)>-80) && (CCI(2)<-80)) || ((CCI(1)>80) && (CCI(2)<80))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| CCI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double CCI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the CCI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ +//| SMA value at the specified bar | +//+------------------------------------------------------------------+ +double CloseAvg(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtTrendMAHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the Simple Moving Average indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/DarkCloud PiercingLine MFI.mq5 b/DarkCloud PiercingLine MFI.mq5 new file mode 100644 index 0000000..0aad919 --- /dev/null +++ b/DarkCloud PiercingLine MFI.mq5 @@ -0,0 +1,681 @@ +//+------------------------------------------------------------------+ +//| DarkCloud PiercingLine MFI.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpMAPeriod =5; // Trend MA period +input int InpPeriodMFI =37; // MFI period +input ENUM_APPLIED_VOLUME InpVolume=VOLUME_TICK; // volume type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot=0.1; // lot +//--- Expert ID +input long InpMagicNumber=122600; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error +//--- indicator handles +int ExtIndicatorHandle=INVALID_HANDLE; +int ExtTrendMAHandle=INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iMFI(_Symbol, _Period, InpPeriodMFI, InpVolume); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating MFI indicator"); + return(INIT_FAILED); + } + +//--- trend moving average + ExtTrendMAHandle=iMA(_Symbol, _Period, InpMAPeriod,0, MODE_SMA,PRICE_CLOSE); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating Moving Average indicator"); + return(INIT_FAILED); + } + +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // long body of the white candlestick (long white) + (Close(1)Open(2)) && // close within the previous candlestick body (white) + (MidOpenClose(2)>CloseAvg(2)) && // uptrend + (Open(1)>High(2))) // open above the previous day's High price (open at new high) + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nDark Cloud Cover detected"; + ExtDirection="Sell"; + return(true); + } + +//--- check Piercing Line + if((Close(1)-Open(1)>AvgBody(1)) && // long body of the white candlestick (long white) + (Open(2)-Close(2)>AvgBody(1)) && // long body of the previous black candlestick (long black) + (Close(1)>Close(2)) && // close within the body + (Close(1)60)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: MFI>60"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((MFI(1)<70) && (MFI(2)>70)) || ((MFI(1)<30) && (MFI(2)>30))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((MFI(1)>30) && (MFI(2)<30)) || ((MFI(1)>70) && (MFI(2)<70))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| MFI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double MFI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the MFI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ +//| SMA value at the specified bar | +//+------------------------------------------------------------------+ +double CloseAvg(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtTrendMAHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the Simple Moving Average indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/DarkCloud PiercingLine RSI.mq5 b/DarkCloud PiercingLine RSI.mq5 new file mode 100644 index 0000000..635c8f3 --- /dev/null +++ b/DarkCloud PiercingLine RSI.mq5 @@ -0,0 +1,683 @@ +//+------------------------------------------------------------------+ +//| DarkCloud PiercingLine RSI.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpMAPeriod =5; // Trend MA period +input int InpPeriodRSI =37; // RSI period +input ENUM_APPLIED_PRICE InpPrice=PRICE_CLOSE; // price type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot=0.1; // lot +//--- Expert ID +input long InpMagicNumber=120700; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error +//--- indicator handles +int ExtIndicatorHandle=INVALID_HANDLE; +int ExtTrendMAHandle=INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iRSI(_Symbol, _Period, InpPeriodRSI, InpPrice); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating CCI indicator"); + return(INIT_FAILED); + } + +//--- trend moving average + ExtTrendMAHandle=iMA(_Symbol, _Period, InpMAPeriod,0, MODE_SMA,PRICE_CLOSE); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating Moving Average indicator"); + return(INIT_FAILED); + } + +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // long body of the white candlestick (long white) + (Close(1)Open(2)) && // close within the previous candlestick body (white) + (MidOpenClose(2)>CloseAvg(2)) && // uptrend + (Open(1)>High(2))) // open above the previous day's High price (open at new high) + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nDark Cloud Cover detected"; + ExtDirection="Sell"; + return(true); + } + +//--- check Piercing Line + if((Close(1)-Open(1)>AvgBody(1)) && // long body of the white candlestick (long white) + (Open(2)-Close(2)>AvgBody(1)) && // long body of the previous black candlestick (long black) + (Close(1)>Close(2)) && // close within the body + (Close(1)60)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: RSI>60"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((RSI(1)<70) && (RSI(2)>70)) || ((RSI(1)<30) && (RSI(2)>30))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((RSI(1)>30) && (RSI(2)<30)) || ((RSI(1)>70) && (RSI(2)<70))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| RSI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double RSI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the RSI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ +//| SMA value at the specified bar | +//+------------------------------------------------------------------+ +double CloseAvg(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtTrendMAHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the Simple Moving Average indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/DarkCloud PiercingLine Stoch.mq5 b/DarkCloud PiercingLine Stoch.mq5 new file mode 100644 index 0000000..8a9ecf1 --- /dev/null +++ b/DarkCloud PiercingLine Stoch.mq5 @@ -0,0 +1,687 @@ +//+------------------------------------------------------------------+ +//| DarkCloud PiercingLine Stoch.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpMAPeriod =5; // Trend MA period +input int InpStochK =47; // period %K +input int InpStochD =9; // period %D +input int InpStochSlow =13; // smoothing period %K +input ENUM_STO_PRICE InpStochApplied=STO_LOWHIGH; // calculation type +input ENUM_MA_METHOD InpStochMA =MODE_SMA; // smoothing type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot=0.1; // lot +//--- Expert ID +input long InpMagicNumber=120800; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error +//--- indicator handles +int ExtIndicatorHandle=INVALID_HANDLE; +int ExtTrendMAHandle=INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iStochastic(_Symbol, _Period, InpStochK, InpStochD, InpStochSlow, InpStochMA, InpStochApplied); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating iStochastic indicator"); + return(INIT_FAILED); + } + +//--- trend moving average + ExtTrendMAHandle=iMA(_Symbol, _Period, InpMAPeriod,0, MODE_SMA,PRICE_CLOSE); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating Moving Average indicator"); + return(INIT_FAILED); + } + +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeAvgBody(1)) && // long body of the white candlestick (long white) + (Close(1)Open(2)) && // close within the previous candlestick body (white) + (MidOpenClose(2)>CloseAvg(2)) && // uptrend + (Open(1)>High(2))) // open above the previous day's High price (open at new high) + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nDark Cloud Cover detected"; + ExtDirection="Sell"; + return(true); + } + +//--- check Piercing Line + if((Close(1)-Open(1)>AvgBody(1)) && // long body of the white candlestick (long white) + (Open(2)-Close(2)>AvgBody(1)) && // long body of the previous black candlestick (long black) + (Close(1)>Close(2)) && // close within the body + (Close(1)70)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: StochSignal>70"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((StochSignal(1)<80) && (StochSignal(2)>80))|| // 80 crossed downwards + ((StochSignal(1)<20) && (StochSignal(2)>20))) // 20 crossed downwards + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if((((StochSignal(1)>20) && (StochSignal(2)<20)) || // 20 crossed upwards + ((StochSignal(1)>80) && (StochSignal(2)<80)))) // 80 crossed upwards + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Stochastic indicator value at the specified bar | +//+------------------------------------------------------------------+ +double StochSignal(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, SIGNAL_LINE, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the iStochastic indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ +//| SMA value at the specified bar | +//+------------------------------------------------------------------+ +double CloseAvg(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtTrendMAHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the Simple Moving Average indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/DeMark Channel.mq5 b/DeMark Channel.mq5 new file mode 100644 index 0000000..f7b7e89 --- /dev/null +++ b/DeMark Channel.mq5 @@ -0,0 +1,204 @@ +//+------------------------------------------------------------------+ +//| DeMark Channel.mq5 | +//| Copyright 2009-2024, MetaQuotes Ltd | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "2009-2024, MetaQuotes Ltd" +#property link "http://www.mql5.com" +#property description "DeMark Channel" + +#property indicator_chart_window +#property indicator_buffers 3 +#property indicator_plots 3 +#property indicator_type1 DRAW_LINE +#property indicator_color1 clrGreen +#property indicator_type2 DRAW_LINE +#property indicator_color2 clrBlue +#property indicator_type3 DRAW_LINE +#property indicator_color3 clrRed +//--- labels +#property indicator_label1 "PP" +#property indicator_label2 "R1" +#property indicator_label3 "S1" + +//--- input parameter +input bool InpShowLabel=true; // show price of level + +//--- indicator buffers +double ExtPPBuffer[]; +double ExtR1Buffer[]; +double ExtS1Buffer[]; + +//--- unique prefix to identify indicator objects +string ExtPrefixUniq; + +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { +//--- check current timeframe + if(PeriodSeconds()>PeriodSeconds(PERIOD_D1)) + { + Alert("Timeframe of chart must be D1 or lower. Exit"); + return(INIT_FAILED); + } + +//--- define buffers + SetIndexBuffer(0, ExtPPBuffer); + SetIndexBuffer(1, ExtR1Buffer); + SetIndexBuffer(2, ExtS1Buffer); + +//--- indicator name + IndicatorSetString(INDICATOR_SHORTNAME, "DeMark Channel"); +//--- number of digits of indicator value + IndicatorSetInteger(INDICATOR_DIGITS, _Digits); + +//--- prepare prefix for objects + string number=StringFormat("%I64d", GetTickCount64()); + ExtPrefixUniq=StringSubstr(number, StringLen(number)-4); + ExtPrefixUniq=ExtPrefixUniq+"_DM"; + Print("Indicator \"DeMark Channels\" started, prefix=", ExtPrefixUniq); + + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Custom indicator iteration function | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + static MqlRates LAST_DAY[]; // previous day + static datetime last_time=0; // reference time + static datetime error_time=0; // error output time + +//--- if the indicator has previously been calculated, start from the bar preceding the last one + int start=prev_calculated-1; + +//--- if this is the first calculation of the indicator, start from the first bar on the chart + if(prev_calculated==0) + start=0; + +//--- calculate levels for all bars in a loop + for(int i=start; i=error_time) + { + error_time=TimeCurrent()+60; + Print("Failed to get previous day by CopyRates(Symbol(), PERIOD_D1, error ", GetLastError()); + } + return(prev_calculated); + } + } + + //--- preparations + double day_close=LAST_DAY[0].close; + double day_open=LAST_DAY[0].open; + double day_high=LAST_DAY[0].high; + double day_low=LAST_DAY[0].low; + + //--- initial calculation + double x=day_high+day_low+2*day_close; + if(day_closeday_open) + x=2*day_high+day_low+day_close; + + //--- calculate levels + double pivot_point=x/4; + double s1=x/2-day_high; + double r1=x/2-day_low; + + //--- write values into buffers + ExtPPBuffer[i]=pivot_point; + ExtR1Buffer[i]=r1; + ExtS1Buffer[i]=s1; + } + +//--- draw labels on levels + if(InpShowLabel) + { + ShowPriceLevels(time[rates_total-1], rates_total-1); + ChartRedraw(); + } + +//--- succesfully calculated + return(rates_total); + } +//+------------------------------------------------------------------+ +//| Custom indicator deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- delete all our graphical objects after use + Print("Indicator \"DeMark Channels\" stopped, delete all objects with prefix=", ExtPrefixUniq); + ObjectsDeleteAll(0, ExtPrefixUniq, 0, OBJ_ARROW_RIGHT_PRICE); + ChartRedraw(0); + } +//+------------------------------------------------------------------+ +//| Show prices' levels | +//+------------------------------------------------------------------+ +void ShowPriceLevels(datetime time, int last_index) + { + ShowRightPrice(ExtPrefixUniq+"_PP", time, ExtPPBuffer[last_index], clrGreen); + ShowRightPrice(ExtPrefixUniq+"_R1", time, ExtR1Buffer[last_index], clrBlue); + ShowRightPrice(ExtPrefixUniq+"_S1", time, ExtS1Buffer[last_index], clrRed); + } +//+------------------------------------------------------------------+ +//| Create or Update "Right Price Label" object | +//+------------------------------------------------------------------+ +bool ShowRightPrice(const string name, datetime time, double price, color clr) + { + if(!ObjectCreate(0, name, OBJ_ARROW_RIGHT_PRICE, 0, time, price)) + { + ObjectMove(0, name, 0, time, price); + return(false); + } + +//--- make the label size adaptive + long scale=2; + if(!ChartGetInteger(0, CHART_SCALE, 0, scale)) + { + //--- output an error message to the Experts journal + Print(__FUNCTION__+", ChartGetInteger(CHART_SCALE) failed, error = ", GetLastError()); + } + int width=scale>1 ? 2:1; // if chart scale > 1, then label size = 2 + + ObjectSetInteger(0, name, OBJPROP_COLOR, clr); + ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_SOLID); + ObjectSetInteger(0, name, OBJPROP_WIDTH, width); + ObjectSetInteger(0, name, OBJPROP_BACK, false); + ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false); + ObjectSetInteger(0, name, OBJPROP_SELECTED, false); + ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); + ObjectSetInteger(0, name, OBJPROP_ZORDER, 0); + + return(true); + } +//+------------------------------------------------------------------+ diff --git a/DeMarker.mq5 b/DeMarker.mq5 new file mode 100644 index 0000000..7b1a9a2 --- /dev/null +++ b/DeMarker.mq5 @@ -0,0 +1,111 @@ +//+------------------------------------------------------------------+ +//| DeMarker.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#include +//--- indicator settings +#property indicator_separate_window +#property indicator_buffers 5 +#property indicator_plots 1 +#property indicator_type1 DRAW_LINE +#property indicator_color1 DodgerBlue +#property indicator_level1 0.3 +#property indicator_level2 0.7 +//--- input parameters +input int InpDeMarkerPeriod=14; // Period +//--- indicator buffers +double ExtDeMarkerBuffer[]; +double ExtDeMaxBuffer[]; +double ExtDeMinBuffer[]; +double ExtAvgDeMaxBuffer[]; +double ExtAvgDeMinBuffer[]; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- indicator buffers mapping + SetIndexBuffer(0,ExtDeMarkerBuffer,INDICATOR_DATA); + SetIndexBuffer(1,ExtDeMaxBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(2,ExtDeMinBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(3,ExtAvgDeMaxBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(4,ExtAvgDeMinBuffer,INDICATOR_CALCULATIONS); +//--- set accuracy + IndicatorSetInteger(INDICATOR_DIGITS,3); +//--- sets first bar from what index will be drawn + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpDeMarkerPeriod); +//--- name for DataWindow and indicator subwindow label + string short_name=StringFormat("DeM(%d)",InpDeMarkerPeriod); + IndicatorSetString(INDICATOR_SHORTNAME,short_name); + } +//+------------------------------------------------------------------+ +//| DeMarker | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + if(rates_totalhigh[i-1]) + ExtDeMaxBuffer[i]=high[i]-high[i-1]; + else + ExtDeMaxBuffer[i]=0.0; + + if(low[i-1]>low[i]) + ExtDeMinBuffer[i]=low[i-1]-low[i]; + else + ExtDeMinBuffer[i]=0.0; + } + for(i=0; ihigh[i-1]) + ExtDeMaxBuffer[i]=high[i]-high[i-1]; + else + ExtDeMaxBuffer[i]=0.0; + + if(low[i-1]>low[i]) + ExtDeMinBuffer[i]=low[i-1]-low[i]; + else + ExtDeMinBuffer[i]=0.0; + + ExtAvgDeMaxBuffer[i]=SimpleMA(i,InpDeMarkerPeriod,ExtDeMaxBuffer); + ExtAvgDeMinBuffer[i]=SimpleMA(i,InpDeMarkerPeriod,ExtDeMinBuffer); + + double dnum=ExtAvgDeMaxBuffer[i]+ExtAvgDeMinBuffer[i]; + if(dnum!=0) + ExtDeMarkerBuffer[i]=ExtAvgDeMaxBuffer[i]/dnum; + else + ExtDeMarkerBuffer[i]=0.0; + } +//--- OnCalculate done. Return new prev_calculated. + return(rates_total); + } +//+------------------------------------------------------------------+ diff --git a/Donchian Channel.mq5 b/Donchian Channel.mq5 new file mode 100644 index 0000000..a11718a --- /dev/null +++ b/Donchian Channel.mq5 @@ -0,0 +1,160 @@ +//+------------------------------------------------------------------+ +//| Donchian Channel.mq5 | +//| Copyright 2009-2024, MetaQuotes Ltd | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "2009-2024, MetaQuotes Ltd" +#property link "http://www.mql5.com" +#property description "Donchian Channel" +//--- +#property indicator_chart_window +#property indicator_buffers 3 +#property indicator_plots 3 +#property indicator_type1 DRAW_LINE +#property indicator_color1 clrBlue +#property indicator_type2 DRAW_LINE +#property indicator_color2 clrGray +#property indicator_type3 DRAW_LINE +#property indicator_color3 clrRed +//--- labels +#property indicator_label1 "Upper Donchian" +#property indicator_label2 "Middle Donchian" +#property indicator_label3 "Lower Donchian" + +//--- input parameter +input int InpDonchianPeriod=20; // period of the channel +input bool InpShowLabel =true; // show price of the level + +//--- indicator buffers +double ExtUpBuffer[]; +double ExtMdBuffer[]; +double ExtDnBuffer[]; + +//--- unique prefix to identify indicator objects +string ExtPrefixUniq; + +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { +//--- define buffers + SetIndexBuffer(0, ExtUpBuffer); + SetIndexBuffer(1, ExtMdBuffer); + SetIndexBuffer(2, ExtDnBuffer); + +//--- set a 1-bar offset for each line + PlotIndexSetInteger(0, PLOT_SHIFT, 1); + PlotIndexSetInteger(1, PLOT_SHIFT, 1); + PlotIndexSetInteger(2, PLOT_SHIFT, 1); + +//--- indicator name + IndicatorSetString(INDICATOR_SHORTNAME, "Donchian Channel"); +//--- number of digits of indicator value + IndicatorSetInteger(INDICATOR_DIGITS, _Digits); + +//--- prepare prefix for objects + string number=StringFormat("%I64d", GetTickCount64()); + ExtPrefixUniq=StringSubstr(number, StringLen(number)-4); + ExtPrefixUniq=ExtPrefixUniq+"_DN"; + Print("Indicator \"Donchian Channels\" started, prefix=", ExtPrefixUniq); + + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Custom indicator iteration function | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { +//--- if the indicator has previously been calculated, start from the bar preceding the last one + int start=prev_calculated-1; + +//--- if this is the first calculation of the indicator, then move by InpDonchianPeriod bars form the beginning + if(prev_calculated==0) + start=InpDonchianPeriod+1; + +//--- calculate levels for all bars in a loop + for(int i=start; irates_total || prev_calculated<0) + to_copy=rates_total; + else + { + to_copy=rates_total-prev_calculated; + if(prev_calculated>0) + to_copy++; + } +//--- get ma buffer + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtMAHandle,0,0,to_copy,ExtMABuffer)<=0) + { + Print("Getting MA data is failed! Error ",GetLastError()); + return(0); + } +//--- preliminary calculations + int start=prev_calculated-1; + if(start +#include +#include +#include +//+------------------------------------------------------------------+ +//| Inputs | +//+------------------------------------------------------------------+ +//--- inputs for expert +input string Inp_Expert_Title ="ExpertMACD"; +int Expert_MagicNumber =10981; +bool Expert_EveryTick =false; +//--- inputs for signal +input int Inp_Signal_MACD_PeriodFast =12; +input int Inp_Signal_MACD_PeriodSlow =24; +input int Inp_Signal_MACD_PeriodSignal=9; +input int Inp_Signal_MACD_TakeProfit =50; +input int Inp_Signal_MACD_StopLoss =20; +//+------------------------------------------------------------------+ +//| Global expert object | +//+------------------------------------------------------------------+ +CExpert ExtExpert; +//+------------------------------------------------------------------+ +//| Initialization function of the expert | +//+------------------------------------------------------------------+ +int OnInit(void) + { +//--- Initializing expert + if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber)) + { + //--- failed + printf(__FUNCTION__+": error initializing expert"); + ExtExpert.Deinit(); + return(-1); + } +//--- Creation of signal object + CSignalMACD *signal=new CSignalMACD; + if(signal==NULL) + { + //--- failed + printf(__FUNCTION__+": error creating signal"); + ExtExpert.Deinit(); + return(-2); + } +//--- Add signal to expert (will be deleted automatically)) + if(!ExtExpert.InitSignal(signal)) + { + //--- failed + printf(__FUNCTION__+": error initializing signal"); + ExtExpert.Deinit(); + return(-3); + } +//--- Set signal parameters + signal.PeriodFast(Inp_Signal_MACD_PeriodFast); + signal.PeriodSlow(Inp_Signal_MACD_PeriodSlow); + signal.PeriodSignal(Inp_Signal_MACD_PeriodSignal); + signal.TakeLevel(Inp_Signal_MACD_TakeProfit); + signal.StopLevel(Inp_Signal_MACD_StopLoss); +//--- Check signal parameters + if(!signal.ValidationSettings()) + { + //--- failed + printf(__FUNCTION__+": error signal parameters"); + ExtExpert.Deinit(); + return(-4); + } +//--- Creation of trailing object + CTrailingNone *trailing=new CTrailingNone; + if(trailing==NULL) + { + //--- failed + printf(__FUNCTION__+": error creating trailing"); + ExtExpert.Deinit(); + return(-5); + } +//--- Add trailing to expert (will be deleted automatically)) + if(!ExtExpert.InitTrailing(trailing)) + { + //--- failed + printf(__FUNCTION__+": error initializing trailing"); + ExtExpert.Deinit(); + return(-6); + } +//--- Set trailing parameters +//--- Check trailing parameters + if(!trailing.ValidationSettings()) + { + //--- failed + printf(__FUNCTION__+": error trailing parameters"); + ExtExpert.Deinit(); + return(-7); + } +//--- Creation of money object + CMoneyNone *money=new CMoneyNone; + if(money==NULL) + { + //--- failed + printf(__FUNCTION__+": error creating money"); + ExtExpert.Deinit(); + return(-8); + } +//--- Add money to expert (will be deleted automatically)) + if(!ExtExpert.InitMoney(money)) + { + //--- failed + printf(__FUNCTION__+": error initializing money"); + ExtExpert.Deinit(); + return(-9); + } +//--- Set money parameters +//--- Check money parameters + if(!money.ValidationSettings()) + { + //--- failed + printf(__FUNCTION__+": error money parameters"); + ExtExpert.Deinit(); + return(-10); + } +//--- Tuning of all necessary indicators + if(!ExtExpert.InitIndicators()) + { + //--- failed + printf(__FUNCTION__+": error initializing indicators"); + ExtExpert.Deinit(); + return(-11); + } +//--- succeed + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Deinitialization function of the expert | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { + ExtExpert.Deinit(); + } +//+------------------------------------------------------------------+ +//| Function-event handler "tick" | +//+------------------------------------------------------------------+ +void OnTick(void) + { + ExtExpert.OnTick(); + } +//+------------------------------------------------------------------+ +//| Function-event handler "trade" | +//+------------------------------------------------------------------+ +void OnTrade(void) + { + ExtExpert.OnTrade(); + } +//+------------------------------------------------------------------+ +//| Function-event handler "timer" | +//+------------------------------------------------------------------+ +void OnTimer(void) + { + ExtExpert.OnTimer(); + } +//+------------------------------------------------------------------+ diff --git a/ExpertMAMA.mq5 b/ExpertMAMA.mq5 new file mode 100644 index 0000000..b73db45 --- /dev/null +++ b/ExpertMAMA.mq5 @@ -0,0 +1,175 @@ +//+------------------------------------------------------------------+ +//| ExpertMAMA.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" +//+------------------------------------------------------------------+ +//| Include | +//+------------------------------------------------------------------+ +#include +#include +#include +#include +//+------------------------------------------------------------------+ +//| Inputs | +//+------------------------------------------------------------------+ +//--- inputs for expert +input string Inp_Expert_Title ="ExpertMAMA"; +int Expert_MagicNumber =12003; +bool Expert_EveryTick =false; +//--- inputs for signal +input int Inp_Signal_MA_Period =12; +input int Inp_Signal_MA_Shift =6; +input ENUM_MA_METHOD Inp_Signal_MA_Method =MODE_SMA; +input ENUM_APPLIED_PRICE Inp_Signal_MA_Applied =PRICE_CLOSE; +//--- inputs for trailing +input int Inp_Trailing_MA_Period =12; +input int Inp_Trailing_MA_Shift =0; +input ENUM_MA_METHOD Inp_Trailing_MA_Method =MODE_SMA; +input ENUM_APPLIED_PRICE Inp_Trailing_MA_Applied=PRICE_CLOSE; +//+------------------------------------------------------------------+ +//| Global expert object | +//+------------------------------------------------------------------+ +CExpert ExtExpert; +//+------------------------------------------------------------------+ +//| Initialization function of the expert | +//+------------------------------------------------------------------+ +int OnInit(void) + { +//--- Initializing expert + if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber)) + { + //--- failed + printf(__FUNCTION__+": error initializing expert"); + ExtExpert.Deinit(); + return(-1); + } +//--- Creation of signal object + CSignalMA *signal=new CSignalMA; + if(signal==NULL) + { + //--- failed + printf(__FUNCTION__+": error creating signal"); + ExtExpert.Deinit(); + return(-2); + } +//--- Add signal to expert (will be deleted automatically)) + if(!ExtExpert.InitSignal(signal)) + { + //--- failed + printf(__FUNCTION__+": error initializing signal"); + ExtExpert.Deinit(); + return(-3); + } +//--- Set signal parameters + signal.PeriodMA(Inp_Signal_MA_Period); + signal.Shift(Inp_Signal_MA_Shift); + signal.Method(Inp_Signal_MA_Method); + signal.Applied(Inp_Signal_MA_Applied); +//--- Check signal parameters + if(!signal.ValidationSettings()) + { + //--- failed + printf(__FUNCTION__+": error signal parameters"); + ExtExpert.Deinit(); + return(-4); + } +//--- Creation of trailing object + CTrailingMA *trailing=new CTrailingMA; + if(trailing==NULL) + { + //--- failed + printf(__FUNCTION__+": error creating trailing"); + ExtExpert.Deinit(); + return(-5); + } +//--- Add trailing to expert (will be deleted automatically)) + if(!ExtExpert.InitTrailing(trailing)) + { + //--- failed + printf(__FUNCTION__+": error initializing trailing"); + ExtExpert.Deinit(); + return(-6); + } +//--- Set trailing parameters + trailing.Period(Inp_Trailing_MA_Period); + trailing.Shift(Inp_Trailing_MA_Shift); + trailing.Method(Inp_Trailing_MA_Method); + trailing.Applied(Inp_Trailing_MA_Applied); +//--- Check trailing parameters + if(!trailing.ValidationSettings()) + { + //--- failed + printf(__FUNCTION__+": error trailing parameters"); + ExtExpert.Deinit(); + return(-7); + } +//--- Creation of money object + CMoneyNone *money=new CMoneyNone; + if(money==NULL) + { + //--- failed + printf(__FUNCTION__+": error creating money"); + ExtExpert.Deinit(); + return(-8); + } +//--- Add money to expert (will be deleted automatically)) + if(!ExtExpert.InitMoney(money)) + { + //--- failed + printf(__FUNCTION__+": error initializing money"); + ExtExpert.Deinit(); + return(-9); + } +//--- Set money parameters +//--- Check money parameters + if(!money.ValidationSettings()) + { + //--- failed + printf(__FUNCTION__+": error money parameters"); + ExtExpert.Deinit(); + return(-10); + } +//--- Tuning of all necessary indicators + if(!ExtExpert.InitIndicators()) + { + //--- failed + printf(__FUNCTION__+": error initializing indicators"); + ExtExpert.Deinit(); + return(-11); + } +//--- succeed + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Deinitialization function of the expert | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { + ExtExpert.Deinit(); + } +//+------------------------------------------------------------------+ +//| Function-event handler "tick" | +//+------------------------------------------------------------------+ +void OnTick(void) + { + ExtExpert.OnTick(); + } +//+------------------------------------------------------------------+ +//| Function-event handler "trade" | +//+------------------------------------------------------------------+ +void OnTrade(void) + { + ExtExpert.OnTrade(); + } +//+------------------------------------------------------------------+ +//| Function-event handler "timer" | +//+------------------------------------------------------------------+ +void OnTimer(void) + { + ExtExpert.OnTimer(); + } +//+------------------------------------------------------------------+ diff --git a/ExpertMAPSAR.mq5 b/ExpertMAPSAR.mq5 new file mode 100644 index 0000000..81c2490 --- /dev/null +++ b/ExpertMAPSAR.mq5 @@ -0,0 +1,171 @@ +//+------------------------------------------------------------------+ +//| ExpertMAPSAR.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" +//+------------------------------------------------------------------+ +//| Include | +//+------------------------------------------------------------------+ +#include +#include +#include +#include +//+------------------------------------------------------------------+ +//| Inputs | +//+------------------------------------------------------------------+ +//--- inputs for expert +input string Inp_Expert_Title ="ExpertMAPSAR"; +int Expert_MagicNumber =14598; +bool Expert_EveryTick =false; +//--- inputs for signal +input int Inp_Signal_MA_Period =12; +input int Inp_Signal_MA_Shift =6; +input ENUM_MA_METHOD Inp_Signal_MA_Method =MODE_SMA; +input ENUM_APPLIED_PRICE Inp_Signal_MA_Applied =PRICE_CLOSE; +//--- inputs for trailing +input double Inp_Trailing_ParabolicSAR_Step =0.02; +input double Inp_Trailing_ParabolicSAR_Maximum=0.2; +//+------------------------------------------------------------------+ +//| Global expert object | +//+------------------------------------------------------------------+ +CExpert ExtExpert; +//+------------------------------------------------------------------+ +//| Initialization function of the expert | +//+------------------------------------------------------------------+ +int OnInit(void) + { +//--- Initializing expert + if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber)) + { + //--- failed + printf(__FUNCTION__+": error initializing expert"); + ExtExpert.Deinit(); + return(-1); + } +//--- Creation of signal object + CSignalMA *signal=new CSignalMA; + if(signal==NULL) + { + //--- failed + printf(__FUNCTION__+": error creating signal"); + ExtExpert.Deinit(); + return(-2); + } +//--- Add signal to expert (will be deleted automatically)) + if(!ExtExpert.InitSignal(signal)) + { + //--- failed + printf(__FUNCTION__+": error initializing signal"); + ExtExpert.Deinit(); + return(-3); + } +//--- Set signal parameters + signal.PeriodMA(Inp_Signal_MA_Period); + signal.Shift(Inp_Signal_MA_Shift); + signal.Method(Inp_Signal_MA_Method); + signal.Applied(Inp_Signal_MA_Applied); +//--- Check signal parameters + if(!signal.ValidationSettings()) + { + //--- failed + printf(__FUNCTION__+": error signal parameters"); + ExtExpert.Deinit(); + return(-4); + } +//--- Creation of trailing object + CTrailingPSAR *trailing=new CTrailingPSAR; + if(trailing==NULL) + { + //--- failed + printf(__FUNCTION__+": error creating trailing"); + ExtExpert.Deinit(); + return(-5); + } +//--- Add trailing to expert (will be deleted automatically)) + if(!ExtExpert.InitTrailing(trailing)) + { + //--- failed + printf(__FUNCTION__+": error initializing trailing"); + ExtExpert.Deinit(); + return(-6); + } +//--- Set trailing parameters + trailing.Step(Inp_Trailing_ParabolicSAR_Step); + trailing.Maximum(Inp_Trailing_ParabolicSAR_Maximum); +//--- Check trailing parameters + if(!trailing.ValidationSettings()) + { + //--- failed + printf(__FUNCTION__+": error trailing parameters"); + ExtExpert.Deinit(); + return(-7); + } +//--- Creation of money object + CMoneyNone *money=new CMoneyNone; + if(money==NULL) + { + //--- failed + printf(__FUNCTION__+": error creating money"); + ExtExpert.Deinit(); + return(-8); + } +//--- Add money to expert (will be deleted automatically)) + if(!ExtExpert.InitMoney(money)) + { + //--- failed + printf(__FUNCTION__+": error initializing money"); + ExtExpert.Deinit(); + return(-9); + } +//--- Set money parameters +//--- Check money parameters + if(!money.ValidationSettings()) + { + //--- failed + printf(__FUNCTION__+": error money parameters"); + ExtExpert.Deinit(); + return(-10); + } +//--- Tuning of all necessary indicators + if(!ExtExpert.InitIndicators()) + { + //--- failed + printf(__FUNCTION__+": error initializing indicators"); + ExtExpert.Deinit(); + return(-11); + } +//--- succeed + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Deinitialization function of the expert | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { + ExtExpert.Deinit(); + } +//+------------------------------------------------------------------+ +//| Function-event handler "tick" | +//+------------------------------------------------------------------+ +void OnTick(void) + { + ExtExpert.OnTick(); + } +//+------------------------------------------------------------------+ +//| Function-event handler "trade" | +//+------------------------------------------------------------------+ +void OnTrade(void) + { + ExtExpert.OnTrade(); + } +//+------------------------------------------------------------------+ +//| Function-event handler "timer" | +//+------------------------------------------------------------------+ +void OnTimer(void) + { + ExtExpert.OnTimer(); + } +//+------------------------------------------------------------------+ diff --git a/ExpertMAPSARSizeOptimized.mq5 b/ExpertMAPSARSizeOptimized.mq5 new file mode 100644 index 0000000..ce4baa2 --- /dev/null +++ b/ExpertMAPSARSizeOptimized.mq5 @@ -0,0 +1,176 @@ +//+------------------------------------------------------------------+ +//| ExpertMAPSARSizeOptimized.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" +//+------------------------------------------------------------------+ +//| Include | +//+------------------------------------------------------------------+ +#include +#include +#include +#include +//+------------------------------------------------------------------+ +//| Inputs | +//+------------------------------------------------------------------+ +//--- inputs for expert +input string Inp_Expert_Title ="ExpertMAPSARSizeOptimized"; +int Expert_MagicNumber =27893; +bool Expert_EveryTick =false; +//--- inputs for signal +input int Inp_Signal_MA_Period =12; +input int Inp_Signal_MA_Shift =6; +input ENUM_MA_METHOD Inp_Signal_MA_Method =MODE_SMA; +input ENUM_APPLIED_PRICE Inp_Signal_MA_Applied =PRICE_CLOSE; +//--- inputs for trailing +input double Inp_Trailing_ParabolicSAR_Step =0.02; +input double Inp_Trailing_ParabolicSAR_Maximum =0.2; +//--- inputs for money +input double Inp_Money_SizeOptimized_DecreaseFactor=3.0; +input double Inp_Money_SizeOptimized_Percent =10.0; +//+------------------------------------------------------------------+ +//| Global expert object | +//+------------------------------------------------------------------+ +CExpert ExtExpert; +//+------------------------------------------------------------------+ +//| Initialization function of the expert | +//+------------------------------------------------------------------+ +int OnInit(void) + { +//--- Initializing expert + if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber)) + { + //--- failed + printf(__FUNCTION__+": error initializing expert"); + ExtExpert.Deinit(); + return(-1); + } +//--- Creation of signal object + CSignalMA *signal=new CSignalMA; + if(signal==NULL) + { + //--- failed + printf(__FUNCTION__+": error creating signal"); + ExtExpert.Deinit(); + return(-2); + } +//--- Add signal to expert (will be deleted automatically)) + if(!ExtExpert.InitSignal(signal)) + { + //--- failed + printf(__FUNCTION__+": error initializing signal"); + ExtExpert.Deinit(); + return(-3); + } +//--- Set signal parameters + signal.PeriodMA(Inp_Signal_MA_Period); + signal.Shift(Inp_Signal_MA_Shift); + signal.Method(Inp_Signal_MA_Method); + signal.Applied(Inp_Signal_MA_Applied); +//--- Check signal parameters + if(!signal.ValidationSettings()) + { + //--- failed + printf(__FUNCTION__+": error signal parameters"); + ExtExpert.Deinit(); + return(-4); + } +//--- Creation of trailing object + CTrailingPSAR *trailing=new CTrailingPSAR; + if(trailing==NULL) + { + //--- failed + printf(__FUNCTION__+": error creating trailing"); + ExtExpert.Deinit(); + return(-5); + } +//--- Add trailing to expert (will be deleted automatically)) + if(!ExtExpert.InitTrailing(trailing)) + { + //--- failed + printf(__FUNCTION__+": error initializing trailing"); + ExtExpert.Deinit(); + return(-6); + } +//--- Set trailing parameters + trailing.Step(Inp_Trailing_ParabolicSAR_Step); + trailing.Maximum(Inp_Trailing_ParabolicSAR_Maximum); +//--- Check trailing parameters + if(!trailing.ValidationSettings()) + { + //--- failed + printf(__FUNCTION__+": error trailing parameters"); + ExtExpert.Deinit(); + return(-7); + } +//--- Creation of money object + CMoneySizeOptimized *money=new CMoneySizeOptimized; + if(money==NULL) + { + //--- failed + printf(__FUNCTION__+": error creating money"); + ExtExpert.Deinit(); + return(-8); + } +//--- Add money to expert (will be deleted automatically)) + if(!ExtExpert.InitMoney(money)) + { + //--- failed + printf(__FUNCTION__+": error initializing money"); + ExtExpert.Deinit(); + return(-9); + } +//--- Set money parameters + money.DecreaseFactor(Inp_Money_SizeOptimized_DecreaseFactor); + money.Percent(Inp_Money_SizeOptimized_Percent); +//--- Check money parameters + if(!money.ValidationSettings()) + { + //--- failed + printf(__FUNCTION__+": error money parameters"); + ExtExpert.Deinit(); + return(-10); + } +//--- Tuning of all necessary indicators + if(!ExtExpert.InitIndicators()) + { + //--- failed + printf(__FUNCTION__+": error initializing indicators"); + ExtExpert.Deinit(); + return(-11); + } +//--- succeed + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Deinitialization function of the expert | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { + ExtExpert.Deinit(); + } +//+------------------------------------------------------------------+ +//| Function-event handler "tick" | +//+------------------------------------------------------------------+ +void OnTick(void) + { + ExtExpert.OnTick(); + } +//+------------------------------------------------------------------+ +//| Function-event handler "trade" | +//+------------------------------------------------------------------+ +void OnTrade(void) + { + ExtExpert.OnTrade(); + } +//+------------------------------------------------------------------+ +//| Function-event handler "timer" | +//+------------------------------------------------------------------+ +void OnTimer(void) + { + ExtExpert.OnTimer(); + } +//+------------------------------------------------------------------+ diff --git a/FFT.mq5 b/FFT.mq5 new file mode 100644 index 0000000..12c1459 --- /dev/null +++ b/FFT.mq5 @@ -0,0 +1,311 @@ +//+------------------------------------------------------------------+ +//| FFT.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#resource "Kernels/fft.cl" as string cl_program +#define kernel_init "fft_init" +#define kernel_stage "fft_stage" +#define kernel_scale "fft_scale" +#define NUM_POINTS 16384 +#define FFT_DIRECTION 1 +//+------------------------------------------------------------------+ +//| Fast Fourier transform and its inverse (both recursively) | +//| Copyright (C) 2004, Jerome R. Breitenbach. All rights reserved. | +//| Reference: | +//| Matthew Scarpino, "OpenCL in Action: How to accelerate graphics | +//| and computations", Manning, 2012, Chapter 14. | +//+------------------------------------------------------------------+ +//| Recursive direct FFT transform | +//+------------------------------------------------------------------+ +void fft(const int N,double &x_real[],double &x_imag[],double &X_real[],double &X_imag[]) + { +//--- prepare temporary arrays + double XX_real[],XX_imag[]; + ArrayResize(XX_real,N); + ArrayResize(XX_imag,N); +//--- calculate FFT by a recursion + fft_rec(N,0,1,x_real,x_imag,X_real,X_imag,XX_real,XX_imag); + } +//+------------------------------------------------------------------+ +//| Recursive inverse FFT transform | +//+------------------------------------------------------------------+ +void ifft(const int N,double &x_real[],double &x_imag[],double &X_real[],double &X_imag[]) + { + int N2=N/2; // half the number of points in IFFT +//--- calculate IFFT via reciprocity property of DFT + fft(N,X_real,X_imag,x_real,x_imag); + x_real[0]=x_real[0]/N; + x_imag[0]=x_imag[0]/N; + x_real[N2]=x_real[N2]/N; + x_imag[N2]=x_imag[N2]/N; + for(int i=1; i0) + fft(N,data_real,data_imag,XX_real,XX_imag); + else + ifft(N,XX_real,XX_imag,data_real,data_imag); +//--- CPU calculation finished + time_cpu=ulong((GetMicrosecondCount()-time_cpu)); +//--- copy calculated data + ArrayCopy(data_real,XX_real,0,0,WHOLE_ARRAY); + ArrayCopy(data_imag,XX_imag,0,0,WHOLE_ARRAY); +//--- + return(true); + } +//+------------------------------------------------------------------+ +//| FFT_GPU | +//+------------------------------------------------------------------+ +bool FFT_GPU(int direction,int power,double &data_real[],double &data_imag[],ulong &time_gpu) + { +//--- calculate the number of points + int num_points=1; + for(int i=0;inum_points) + points_per_group=num_points; +//--- set kernel arguments + OpenCL.SetArgumentBuffer(0,0,0); + OpenCL.SetArgumentBuffer(0,1,1); + OpenCL.SetArgumentLocalMemory(0,2,local_mem_size); + OpenCL.SetArgument(0,3,points_per_group); + OpenCL.SetArgument(0,4,num_points); + OpenCL.SetArgument(0,5,direction); +//--- OpenCL execute settings + int task_dimension=1; + uint global_size=(uint)((num_points/points_per_group)*local_size); + uint global_work_offset[1]={0}; + uint global_work_size[1]; + global_work_size[0]=global_size; + uint local_work_size[1]; + local_work_size[0]=local_size; +//--- GPU calculation start + time_gpu=GetMicrosecondCount(); +//-- execute kernel fft_init + if(!OpenCL.Execute(0,task_dimension,global_work_offset,global_work_size,local_work_size)) + { + PrintFormat("fft_init: Error in CLExecute. Error code=%d",GetLastError()); + return(false); + } +//-- further stages of the FFT + if(num_points>points_per_group) + { + //--- set arguments for kernel 1 + OpenCL.SetArgumentBuffer(1,0,1); + OpenCL.SetArgument(1,2,points_per_group); + OpenCL.SetArgument(1,3,direction); + for(int stage=2; stage<=num_points/points_per_group; stage<<=1) + { + OpenCL.SetArgument(1,1,stage); + //-- execute kernel fft_stage + if(!OpenCL.Execute(1,task_dimension,global_work_offset,global_work_size,local_work_size)) + { + PrintFormat("fft_stage: Error in CLExecute. Error code=%d",GetLastError()); + return(false); + } + } + } +//--- scale values if performing the inverse FFT + if(direction<0) + { + OpenCL.SetArgumentBuffer(2,0,1); + OpenCL.SetArgument(2,1,points_per_group); + OpenCL.SetArgument(2,2,num_points); + //-- execute kernel fft_scale + if(!OpenCL.Execute(2,task_dimension,global_work_offset,global_work_size,local_work_size)) + { + PrintFormat("fft_scale: Error in CLExecute. Error code=%d",GetLastError()); + return(false); + } + } +//--- read the results from GPU memory + if(!OpenCL.BufferRead(1,data,0,0,2*num_points)) + { + PrintFormat("Error in BufferRead for data_buffer2. Error code=%d",GetLastError()); + return(false); + } +//--- GPU calculation finished + time_gpu=ulong((GetMicrosecondCount()-time_gpu)); +//--- copy calculated data and release OpenCL handles + for(int i=0; iPeriodSeconds(PERIOD_D1)) + { + Alert("Timeframe of chart must be D1 or lower. Exit"); + return(INIT_FAILED); + } + +//--- define buffers + SetIndexBuffer(0, ExtPPBuffer); + SetIndexBuffer(1, ExtR1Buffer); + SetIndexBuffer(2, ExtR2Buffer); + SetIndexBuffer(3, ExtS1Buffer); + SetIndexBuffer(4, ExtS2Buffer); + if(InpShowAllLevels) + { + SetIndexBuffer(5, ExtR3Buffer, INDICATOR_DATA); + SetIndexBuffer(6, ExtR4Buffer, INDICATOR_DATA); + SetIndexBuffer(7, ExtS3Buffer, INDICATOR_DATA); + SetIndexBuffer(8, ExtS4Buffer, INDICATOR_DATA); + //--- set plot type + PlotIndexSetInteger(5, PLOT_DRAW_TYPE, DRAW_LINE); + PlotIndexSetInteger(6, PLOT_DRAW_TYPE, DRAW_LINE); + PlotIndexSetInteger(7, PLOT_DRAW_TYPE, DRAW_LINE); + PlotIndexSetInteger(8, PLOT_DRAW_TYPE, DRAW_LINE); + //--- set line style + PlotIndexSetInteger(5, PLOT_LINE_STYLE, STYLE_DASH); + PlotIndexSetInteger(6, PLOT_LINE_STYLE, STYLE_SOLID); + PlotIndexSetInteger(7, PLOT_LINE_STYLE, STYLE_DASH); + PlotIndexSetInteger(8, PLOT_LINE_STYLE, STYLE_SOLID); + //--- set line color + PlotIndexSetInteger(5, PLOT_LINE_COLOR, clrGold); + PlotIndexSetInteger(6, PLOT_LINE_COLOR, clrGold); + PlotIndexSetInteger(7, PLOT_LINE_COLOR, clrGold); + PlotIndexSetInteger(8, PLOT_LINE_COLOR, clrGold); + } + else + { + SetIndexBuffer(5, ExtR3Buffer, INDICATOR_CALCULATIONS); + SetIndexBuffer(6, ExtR4Buffer, INDICATOR_CALCULATIONS); + SetIndexBuffer(7, ExtS3Buffer, INDICATOR_CALCULATIONS); + SetIndexBuffer(8, ExtS4Buffer, INDICATOR_CALCULATIONS); + } + +//--- indicator name + IndicatorSetString(INDICATOR_SHORTNAME, "Fibonacci Daily Channels"); +//--- number of digits of indicator value + IndicatorSetInteger(INDICATOR_DIGITS, _Digits); + +//--- prepare prefix for objects + string number=StringFormat("%I64d", GetTickCount64()); + ExtPrefixUniq=StringSubstr(number, StringLen(number)-4); + ExtPrefixUniq=ExtPrefixUniq+"_FB"; + Print("Indicator \"Fibonacci Channels\" started, prefix=", ExtPrefixUniq); + + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Custom indicator iteration function | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + static MqlRates LAST_DAY[]; // previous day + static datetime last_time=0; // reference time + static datetime error_time=0; // error output time + +//--- if the indicator has previously been calculated, start from the bar preceding the last one + int start=prev_calculated-1; + +//--- if this is the first calculation of the indicator, start from the first bar on the chart + if(prev_calculated==0) + start=0; + +//--- calculate levels for all bars in a loop + for(int i=start; i=error_time) + { + error_time=TimeCurrent()+60; + Print("Failed to get previous day by CopyRates(Symbol(), PERIOD_D1, error ", GetLastError()); + } + return(prev_calculated); + } + } + + //--- calculate Pivot levels + double range=LAST_DAY[0].high-LAST_DAY[0].low; + double pivot_point=(LAST_DAY[0].high+LAST_DAY[0].low+LAST_DAY[0].close)/3; + double r1=pivot_point+0.382*range; + double r2=pivot_point+0.618*range; + double s1=pivot_point-0.382*range; + double s2=pivot_point-0.618*range; + + //--- write values into buffers + ExtPPBuffer[i]=pivot_point; + ExtR1Buffer[i]=r1; + ExtR2Buffer[i]=r2; + ExtS1Buffer[i]=s1; + ExtS2Buffer[i]=s2; + + //--- additional levels + double r3=pivot_point+range; + double r4=pivot_point+1.618*range; + double s3=pivot_point-range; + double s4=pivot_point-1.618*range; + ExtR3Buffer[i]=r3; + ExtR4Buffer[i]=r4; + ExtS3Buffer[i]=s3; + ExtS4Buffer[i]=s4; + } + +//--- draw labels on levels + if(InpShowLabel) + { + ShowPriceLevels(time[rates_total-1], rates_total-1); + ChartRedraw(); + } + +//--- succesfully calculated + return(rates_total); + } +//+------------------------------------------------------------------+ +//| Custom indicator deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- delete all our graphical objects after use + Print("Indicator \"Fibonacci Channels\" stopped, delete all objects with prefix=", ExtPrefixUniq); + ObjectsDeleteAll(0, ExtPrefixUniq, 0, OBJ_ARROW_RIGHT_PRICE); + ChartRedraw(0); + } +//+------------------------------------------------------------------+ +//| Show prices' levels | +//+------------------------------------------------------------------+ +void ShowPriceLevels(datetime time, int last_index) + { + ShowRightPrice(ExtPrefixUniq+"_PP", time, ExtPPBuffer[last_index], clrBlue); + ShowRightPrice(ExtPrefixUniq+"_R1", time, ExtR1Buffer[last_index], clrOrange); + ShowRightPrice(ExtPrefixUniq+"_R2", time, ExtR2Buffer[last_index], clrRed); + ShowRightPrice(ExtPrefixUniq+"_S1", time, ExtS1Buffer[last_index], clrTeal); + ShowRightPrice(ExtPrefixUniq+"_S2", time, ExtS2Buffer[last_index], clrGreen); + + if(InpShowAllLevels) + { + ShowRightPrice(ExtPrefixUniq+"_R3", time, ExtR3Buffer[last_index], clrGold); + ShowRightPrice(ExtPrefixUniq+"_R4", time, ExtR4Buffer[last_index], clrGold); + ShowRightPrice(ExtPrefixUniq+"_S3", time, ExtS3Buffer[last_index], clrGold); + ShowRightPrice(ExtPrefixUniq+"_S4", time, ExtS4Buffer[last_index], clrGold); + } + } +//+------------------------------------------------------------------+ +//| Create or Update "Right Price Label" object | +//+------------------------------------------------------------------+ +bool ShowRightPrice(const string name, datetime time, double price, color clr) + { + if(!ObjectCreate(0, name, OBJ_ARROW_RIGHT_PRICE, 0, time, price)) + { + ObjectMove(0, name, 0, time, price); + return(false); + } + +//--- make the label size adaptive + long scale=2; + if(!ChartGetInteger(0, CHART_SCALE, 0, scale)) + { + //--- output an error message to the Experts journal + Print(__FUNCTION__+", ChartGetInteger(CHART_SCALE) failed, error = ", GetLastError()); + } + int width=scale>1 ? 2:1; // if chart scale > 1, then label size = 2 + + ObjectSetInteger(0, name, OBJPROP_COLOR, clr); + ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_SOLID); + ObjectSetInteger(0, name, OBJPROP_WIDTH, width); + ObjectSetInteger(0, name, OBJPROP_BACK, false); + ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false); + ObjectSetInteger(0, name, OBJPROP_SELECTED, false); + ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); + ObjectSetInteger(0, name, OBJPROP_ZORDER, 0); + + return(true); + } +//+------------------------------------------------------------------+ diff --git a/FlameChart.mq5 b/FlameChart.mq5 new file mode 100644 index 0000000..84baabe --- /dev/null +++ b/FlameChart.mq5 @@ -0,0 +1,73 @@ +//+------------------------------------------------------------------+ +//| FlameChart.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#include +//+------------------------------------------------------------------+ +//| Indicator properties | +//+------------------------------------------------------------------+ +#property indicator_chart_window +#property indicator_buffers 0 +#property indicator_plots 0 +//+------------------------------------------------------------------+ +//| Input parameters | +//+------------------------------------------------------------------+ +input int InpFuturefBars =50; +//+------------------------------------------------------------------+ +//| global variables | +//+------------------------------------------------------------------+ +CFlameCanvas ExtCanvas; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + if(!ExtCanvas.FlameCreate("FlameChart",TimeCurrent(),InpFuturefBars,0)) + return(INIT_FAILED); +//--- succeed + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Custom indicator deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { + } +//+------------------------------------------------------------------+ +//| Custom indicator iteration function | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + datetime xb1=time[rates_total-1]; + double yb1=high[rates_total-1]; + datetime xe1=xb1+InpFuturefBars*PeriodSeconds(); + double ye1=ChartGetDouble(0,CHART_PRICE_MAX); + datetime xb2=xb1; + double yb2=low[rates_total-1]; + datetime xe2=xe1; + double ye2=ChartGetDouble(0,CHART_PRICE_MIN); +//--- "nozzle" is defined using two lines: ((xb1,yb1),(xe1,ye1)) and ((xb2,yb2),(xe2,ye2)) + ExtCanvas.FlameSet(xb1,yb1,xe1,ye1,xb2,yb2,xe2,ye2); +//--- result + return(rates_total); + } +//+------------------------------------------------------------------+ +//| Custom indicator chart's event handler | +//+------------------------------------------------------------------+ +void OnChartEvent(const int id,const long& lparam,const double& dparam,const string& sparam) + { + ExtCanvas.ChartEventHandler(id,lparam,dparam,sparam); + } +//+------------------------------------------------------------------+ diff --git a/Force_Index.mq5 b/Force_Index.mq5 new file mode 100644 index 0000000..0b6e13a --- /dev/null +++ b/Force_Index.mq5 @@ -0,0 +1,101 @@ +//+------------------------------------------------------------------+ +//| Force_Index.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +//--- indicator settings +#property indicator_separate_window +#property indicator_buffers 2 +#property indicator_plots 1 +#property indicator_type1 DRAW_LINE +#property indicator_color1 DodgerBlue +//--- input parameters +input int InpForcePeriod=13; // Period +input ENUM_MA_METHOD InpMAMethod=MODE_SMA; // MA method +input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price +input ENUM_APPLIED_VOLUME InpAppliedVolume=VOLUME_TICK; // Volumes +//--- indicator buffers +double ExtForceBuffer[]; +double ExtMABuffer[]; + +int ExtMAHandle; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- indicator buffers mapping + SetIndexBuffer(0,ExtForceBuffer,INDICATOR_DATA); + SetIndexBuffer(1,ExtMABuffer,INDICATOR_CALCULATIONS); +//--- sets first bar from what index will be drawn + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpForcePeriod); +//--- name for DataWindow and indicator subwindow label + string short_name=StringFormat("Force(%d)",InpForcePeriod); + IndicatorSetString(INDICATOR_SHORTNAME,short_name); +//--- get MA handle + ExtMAHandle=iMA(NULL,0,InpForcePeriod,0,InpMAMethod,InpAppliedPrice); + } +//+------------------------------------------------------------------+ +//| Force Index | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + if(rates_totalrates_total || prev_calculated<0) + to_copy=rates_total; + else + { + to_copy=rates_total-prev_calculated; + if(prev_calculated>0) + to_copy++; + } +//--- get ma buffer + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtMAHandle,0,0,to_copy,ExtMABuffer)<=0) + { + Print("Getting MA data is failed! Error ",GetLastError()); + return(0); + } +//--- preliminary calculations + int i,start; + if(prev_calculatedhigh[i+1] && high[i]>high[i+2] && high[i]>=high[i-1] && high[i]>=high[i-2]) + ExtUpperBuffer[i]=high[i]; + else + ExtUpperBuffer[i]=EMPTY_VALUE; + + //--- Lower Fractal + if(low[i]InpTeethPeriod>InpLipsPeriod; +// 2.InpJawsShift>InpTeethShift>InpLipsShift; +// 3.InpJawsPeriod>InpJawsShift; +// 4.InpTeethPeriod>InpTeethShift; +// 5.InpLipsPeriod>InpLipsShift. +//******************************************************************** +//--- indicator settings +#property indicator_separate_window +#property indicator_buffers 7 +#property indicator_plots 2 +#property indicator_type1 DRAW_COLOR_HISTOGRAM +#property indicator_type2 DRAW_COLOR_HISTOGRAM +#property indicator_color1 Green,Red +#property indicator_color2 Green,Red +#property indicator_width1 2 +#property indicator_width2 2 +#property indicator_label1 "Gator Upper" +#property indicator_label2 "Gator Lower" +//--- input parameters +input int InpJawsPeriod=13; // Jaws period +input int InpJawsShift=8; // Jaws shift +input int InpTeethPeriod=8; // Teeth period +input int InpTeethShift=5; // Teeth shift +input int InpLipsPeriod=5; // Lips period +input int InpLipsShift=3; // Lips shift +input ENUM_MA_METHOD InpMAMethod=MODE_SMMA; // Moving average method +input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_MEDIAN; // Applied price +//--- indicator buffers +double ExtUpperBuffer[]; +double ExtUpColorsBuffer[]; +double ExtLowerBuffer[]; +double ExtLoColorsBuffer[]; +double ExtJawsBuffer[]; +double ExtTeethBuffer[]; +double ExtLipsBuffer[]; + +int ExtJawsHandle; +int ExtTeethHandle; +int ExtLipsHandle; +int ExtUpperShift; +int ExtLowerShift; +bool ExtFlag; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { +//--- indicator buffers mapping + SetIndexBuffer(0,ExtUpperBuffer,INDICATOR_DATA); + SetIndexBuffer(1,ExtUpColorsBuffer,INDICATOR_COLOR_INDEX); + SetIndexBuffer(2,ExtLowerBuffer,INDICATOR_DATA); + SetIndexBuffer(3,ExtLoColorsBuffer,INDICATOR_COLOR_INDEX); +//--- MAs + SetIndexBuffer(4,ExtJawsBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(5,ExtTeethBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(6,ExtLipsBuffer,INDICATOR_CALCULATIONS); +//--- get handles + ExtJawsHandle=iMA(NULL,0,InpJawsPeriod,0,InpMAMethod,InpAppliedPrice); + ExtTeethHandle=iMA(NULL,0,InpTeethPeriod,0,InpMAMethod,InpAppliedPrice); + ExtLipsHandle=iMA(NULL,0,InpLipsPeriod,0,InpMAMethod,InpAppliedPrice); +//--- set indicator digits + IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1); +//--- sets first bar from what index will be drawn + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpTeethShift+InpTeethPeriod); + PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpLipsShift+InpLipsPeriod); +//--- line shifts when drawing + PlotIndexSetInteger(0,PLOT_SHIFT,InpTeethShift); + PlotIndexSetInteger(1,PLOT_SHIFT,InpLipsShift); +//--- name for indicator subwindow label + string short_name=StringFormat("Gator(%d,%d,%d)",InpJawsPeriod,InpTeethPeriod,InpLipsPeriod); + IndicatorSetString(INDICATOR_SHORTNAME,short_name); +//--- sets drawing line empty value + PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0); + PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0); +//--- calculate global variables values + ExtUpperShift=InpJawsShift-InpTeethShift; + ExtLowerShift=InpTeethShift-InpLipsShift; +//--- check for input parameters + ExtFlag=CheckForInput(); + if(!ExtFlag) + Print("Wrong input parameters. Indicator won't work."); +//--- initialization done. 0 returned if ExtFlag is true + return(!ExtFlag); + } +//+------------------------------------------------------------------+ +//| Gator Oscillator | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + int shift; +//--- check for rules and bars count + if(ExtUpperShift>ExtLowerShift) + shift=ExtUpperShift; + else + shift=ExtLowerShift; + if(!ExtFlag || shift>rates_total) + return(0); +//--- not all data may be calculated + int calculated=BarsCalculated(ExtJawsHandle); + if(calculatedrates_total || prev_calculated<0) + to_copy=rates_total; + else + { + to_copy=rates_total-prev_calculated; + if(prev_calculated>0) + to_copy++; + } +//--- get ma buffers + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtJawsHandle,0,0,to_copy,ExtJawsBuffer)<=0) + { + Print("getting ExtJawsHandle is failed! Error",GetLastError()); + return(0); + } + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtTeethHandle,0,0,to_copy,ExtTeethBuffer)<=0) + { + Print("getting ExtTeethHandle is failed! Error",GetLastError()); + return(0); + } + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtLipsHandle,0,0,to_copy,ExtLipsBuffer)<=0) + { + Print("getting ExtLipsHandle is failed! Error",GetLastError()); + return(0); + } +//--- last 2 counted bars will be recounted + int pos=prev_calculated-2; + if(pos=lower_limit) + { + //--- calculate down buffer value + cur_value=-MathAbs(ExtTeethBuffer[i-ExtLowerShift]-ExtLipsBuffer[i]); + prev_value=ExtLowerBuffer[i-1]; + ExtLowerBuffer[i]=cur_value; + //--- set down buffer color + if(prev_value==cur_value) + ExtLoColorsBuffer[i]=ExtLoColorsBuffer[i-1]; + else + { + if(prev_value=upper_limit) + { + //--- calculate up buffer value + cur_value=MathAbs(ExtJawsBuffer[i-ExtUpperShift]-ExtTeethBuffer[i]); + ExtUpperBuffer[i]=cur_value; + prev_value=ExtUpperBuffer[i-1]; + //--- set up buffer color + if(prev_value==cur_value) + ExtUpColorsBuffer[i]=ExtUpColorsBuffer[i-1]; + else + { + if(prev_valueInpTeethPeriod>InpLipsPeriod; | +//| 2.InpJawsShift>InpTeethShift>InpLipsShift; | +//| 3.InpJawsPeriod>InpJawsShift; | +//| 4.InpTeethPeriod>InpTeethShift; | +//| 5.InpLipsPeriod>InpLipsShift. | +//+------------------------------------------------------------------+ +bool CheckForInput() + { +//--- 1 + if(InpJawsPeriod<=InpTeethPeriod || InpTeethPeriod<=InpLipsPeriod) + return(false); +//--- 2 + if(InpJawsShift<=InpTeethShift || InpTeethShift<=InpLipsShift) + return(false); +//--- 3 + if(InpJawsPeriod<=InpJawsShift) + return(false); +//--- 4 + if(InpTeethPeriod<=InpTeethShift) + return(false); +//--- 5 + if(InpLipsPeriod<=InpLipsShift) + return(false); +//--- all right + return(true); + } +//+------------------------------------------------------------------+ diff --git a/Gator_2.mq5 b/Gator_2.mq5 new file mode 100644 index 0000000..668d982 --- /dev/null +++ b/Gator_2.mq5 @@ -0,0 +1,252 @@ +//+------------------------------------------------------------------+ +//| Gator.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property description "Gator Oscillator" +#property description "based on shifted Alligator buffers" +//******************************************************************** +// Attention! Following correlations must be obeyed: +// 1.InpJawsPeriod>InpTeethPeriod>InpLipsPeriod; +// 2.InpJawsShift>InpTeethShift>InpLipsShift; +// 3.InpJawsPeriod>InpJawsShift; +// 4.InpTeethPeriod>InpTeethShift; +// 5.InpLipsPeriod>InpLipsShift. +//******************************************************************** +//--- indicator settings +#property indicator_separate_window +#property indicator_buffers 7 +#property indicator_plots 2 +#property indicator_type1 DRAW_COLOR_HISTOGRAM +#property indicator_type2 DRAW_COLOR_HISTOGRAM +#property indicator_color1 Green,Red +#property indicator_color2 Green,Red +#property indicator_width1 2 +#property indicator_width2 2 +#property indicator_label1 "Gator Upper" +#property indicator_label2 "Gator Lower" +//--- input parameters +input int InpJawsPeriod=13; // Jaws period +input int InpJawsShift=8; // Jaws shift +input int InpTeethPeriod=8; // Teeth period +input int InpTeethShift=5; // Teeth shift +input int InpLipsPeriod=5; // Lips period +input int InpLipsShift=3; // Lips shift +input ENUM_MA_METHOD InpMAMethod=MODE_SMMA; // Moving average method +input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_MEDIAN; // Applied price +//--- indicator buffers +double ExtUpperBuffer[]; +double ExtUpColorsBuffer[]; +double ExtLowerBuffer[]; +double ExtLoColorsBuffer[]; +double ExtJawsBuffer[]; +double ExtTeethBuffer[]; +double ExtLipsBuffer[]; + +int ExtAlligatorHandle; +int ExtUpperShift; +int ExtLowerShift; +bool ExtFlag; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { +//--- indicator buffers mapping + SetIndexBuffer(0,ExtUpperBuffer,INDICATOR_DATA); + SetIndexBuffer(1,ExtUpColorsBuffer,INDICATOR_COLOR_INDEX); + SetIndexBuffer(2,ExtLowerBuffer,INDICATOR_DATA); + SetIndexBuffer(3,ExtLoColorsBuffer,INDICATOR_COLOR_INDEX); +//--- MAs + SetIndexBuffer(4,ExtJawsBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(5,ExtTeethBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(6,ExtLipsBuffer,INDICATOR_CALCULATIONS); +//--- get handles + ExtAlligatorHandle=iAlligator(NULL,0, + InpJawsPeriod,0, + InpTeethPeriod,0, + InpLipsPeriod,0, + InpMAMethod,InpAppliedPrice); +//--- set indicator digits + IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1); +//--- sets first bar from what index will be drawn + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpTeethShift+InpTeethPeriod); + PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpLipsShift+InpLipsPeriod); +//--- line shifts when drawing + PlotIndexSetInteger(0,PLOT_SHIFT,InpTeethShift); + PlotIndexSetInteger(1,PLOT_SHIFT,InpLipsShift); +//--- name for indicator subwindow label + string short_name=StringFormat("Gator(%d,%d,%d)",InpJawsPeriod,InpTeethPeriod,InpLipsPeriod); + IndicatorSetString(INDICATOR_SHORTNAME,short_name); +//--- sets drawing line empty value + PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0); + PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0); +//--- calculate global variables values + ExtUpperShift=InpJawsShift-InpTeethShift; + ExtLowerShift=InpTeethShift-InpLipsShift; +//--- check for input parameters + ExtFlag=CheckForInput(); + if(!ExtFlag) + Print("Wrong input parameters. Indicator won't work."); +//--- initialization done. 0 returned if ExtFlag is true + return(ExtFlag?0:1); + } +//+------------------------------------------------------------------+ +//| Gator Oscillator | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + int shift; +//--- check for rules and bars count + if(ExtUpperShift>ExtLowerShift) + shift=ExtUpperShift; + else + shift=ExtLowerShift; + if(!ExtFlag || shift>rates_total) + return(0); +//--- not all data may be calculated + int calculated=BarsCalculated(ExtAlligatorHandle); + if(calculatedrates_total || prev_calculated<0) + to_copy=rates_total; + else + { + to_copy=rates_total-prev_calculated; + if(prev_calculated>0) + to_copy++; + } +//--- get alligator buffers + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtAlligatorHandle,0,0,to_copy,ExtJawsBuffer)<=0) + { + Print("getting ExtAlligatorHandle buffer 0 is failed! Error ",GetLastError()); + return(0); + } + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtAlligatorHandle,1,0,to_copy,ExtTeethBuffer)<=0) + { + Print("getting ExtAlligatorHandle buffer 1 is failed! Error ",GetLastError()); + return(0); + } + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtAlligatorHandle,2,0,to_copy,ExtLipsBuffer)<=0) + { + Print("getting ExtAlligatorHandle buffer 2 is failed! Error ",GetLastError()); + return(0); + } +//--- last counted bar will be recounted + int pos=prev_calculated-1; + if(pos=lower_limit) + { + //--- calculate down buffer value + cur_value=-fabs(ExtTeethBuffer[i-ExtLowerShift]-ExtLipsBuffer[i]); + prev_value=ExtLowerBuffer[i-1]; + ExtLowerBuffer[i]=cur_value; + //--- set down buffer color + if(prev_value==cur_value) + ExtLoColorsBuffer[i]=ExtLoColorsBuffer[i-1]; + else + { + if(prev_value=upper_limit) + { + //--- calculate up buffer value + cur_value=fabs(ExtJawsBuffer[i-ExtUpperShift]-ExtTeethBuffer[i]); + ExtUpperBuffer[i]=cur_value; + prev_value=ExtUpperBuffer[i-1]; + //--- set up buffer color + if(prev_value==cur_value) + ExtUpColorsBuffer[i]=ExtUpColorsBuffer[i-1]; + else + { + if(prev_valueInpTeethPeriod>InpLipsPeriod; | +//| 2.InpJawsShift>InpTeethShift>InpLipsShift; | +//| 3.InpJawsPeriod>InpJawsShift; | +//| 4.InpTeethPeriod>InpTeethShift; | +//| 5.InpLipsPeriod>InpLipsShift. | +//+------------------------------------------------------------------+ +bool CheckForInput() + { +//--- 1 + if(InpJawsPeriod<=InpTeethPeriod || InpTeethPeriod<=InpLipsPeriod) + return(false); +//--- 2 + if(InpJawsShift<=InpTeethShift || InpTeethShift<=InpLipsShift) + return(false); +//--- 3 + if(InpJawsPeriod<=InpJawsShift) + return(false); +//--- 4 + if(InpTeethPeriod<=InpTeethShift) + return(false); +//--- 5 + if(InpLipsPeriod<=InpLipsShift) + return(false); +//--- all right + return(true); + } +//+------------------------------------------------------------------+ diff --git a/HangingMan Hammer CCI.mq5 b/HangingMan Hammer CCI.mq5 new file mode 100644 index 0000000..55e039c --- /dev/null +++ b/HangingMan Hammer CCI.mq5 @@ -0,0 +1,679 @@ +//+------------------------------------------------------------------+ +//| HangingMan Hammer CCI.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpMAPeriod =5; // Trend MA period +input int InpPeriodCCI =37; // CCI period +input ENUM_APPLIED_PRICE InpPrice=PRICE_CLOSE; // price type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot=0.1; // lot +//--- Expert ID +input long InpMagicNumber=124100; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error +//--- indicator handles +int ExtIndicatorHandle=INVALID_HANDLE; +int ExtTrendMAHandle=INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iCCI(_Symbol, _Period, InpPeriodCCI, InpPrice); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating CCI indicator"); + return(INIT_FAILED); + } + +//--- trend moving average + ExtTrendMAHandle=iMA(_Symbol, _Period, InpMAPeriod, 0, MODE_SMA, PRICE_CLOSE); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating Moving Average indicator"); + return(INIT_FAILED); + } + +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeCloseAvg(2)) && // up trend + (MathMin(Open(1), Close(1))> (High(1)-(High(1)-Low(1))/3.0)) && // body in upper 1/3 + (Close(1)>Close(2)) && (Open(1)>Open(2))) // body gap + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nHanging Man detected"; + ExtDirection="Sell"; + return(true); + } + + +//--- check Hammer + if((MidPoint(1)(High(1)-(High(1)-Low(1))/3.0)) && // body in upper 1/3 + (Close(1)50)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: CCI>50"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((CCI(1)<80) && (CCI(2)>80)) || ((CCI(1)<-80) && (CCI(2)>-80))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((CCI(1)>-80) && (CCI(2)<-80)) || ((CCI(1)>80) && (CCI(2)<80))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| CCI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double CCI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the CCI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ +//| SMA value at the specified bar | +//+------------------------------------------------------------------+ +double CloseAvg(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtTrendMAHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the Simple Moving Average indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/HangingMan Hammer MFI.mq5 b/HangingMan Hammer MFI.mq5 new file mode 100644 index 0000000..7094331 --- /dev/null +++ b/HangingMan Hammer MFI.mq5 @@ -0,0 +1,679 @@ +//+------------------------------------------------------------------+ +//| HangingMan Hammer MFI.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpMAPeriod =5; // Trend MA period +input int InpPeriodMFI =37; // MFI period +input ENUM_APPLIED_VOLUME InpVolume=VOLUME_TICK; // volume type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot=0.1; // lot +//--- Expert ID +input long InpMagicNumber=123600; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error +//--- indicator handles +int ExtIndicatorHandle=INVALID_HANDLE; +int ExtTrendMAHandle=INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iMFI(_Symbol, _Period, InpPeriodMFI, InpVolume); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating MFI indicator"); + return(INIT_FAILED); + } + +//--- trend moving average + ExtTrendMAHandle=iMA(_Symbol, _Period, InpMAPeriod, 0, MODE_SMA, PRICE_CLOSE); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating Moving Average indicator"); + return(INIT_FAILED); + } + +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } + +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeCloseAvg(2)) && // up trend + (MathMin(Open(1), Close(1))> (High(1)-(High(1)-Low(1))/3.0)) && // body in upper 1/3 + (Close(1)>Close(2)) && (Open(1)>Open(2))) // body gap + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nHanging Man detected"; + ExtDirection="Sell"; + return(true); + } + + +//--- check Hammer + if((MidPoint(1)(High(1)-(High(1)-Low(1))/3.0)) && // body in upper 1/3 + (Close(1)60)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: MFI>60"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((MFI(1)<70) && (MFI(2)>70)) || ((MFI(1)<30) && (MFI(2)>30))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((MFI(1)>30) && (MFI(2)<30)) || ((MFI(1)>70) && (MFI(2)<70))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| MFI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double MFI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the MFI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ +//| SMA value at the specified bar | +//+------------------------------------------------------------------+ +double CloseAvg(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtTrendMAHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the Simple Moving Average indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/HangingMan Hammer RSI.mq5 b/HangingMan Hammer RSI.mq5 new file mode 100644 index 0000000..eb95155 --- /dev/null +++ b/HangingMan Hammer RSI.mq5 @@ -0,0 +1,677 @@ +//+------------------------------------------------------------------+ +//| HangingMan Hammer RSI.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpMAPeriod =5; // Trend MA period +input int InpPeriodRSI =37; // RSI period +input ENUM_APPLIED_PRICE InpPrice=PRICE_CLOSE; // price type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot=0.1; // lot +//--- Expert ID +input long InpMagicNumber=123300; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error +//--- indicator handles +int ExtIndicatorHandle=INVALID_HANDLE; +int ExtTrendMAHandle=INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iRSI(_Symbol, _Period, InpPeriodRSI, InpPrice); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating CCI indicator"); + return(INIT_FAILED); + } + +//--- trend moving average + ExtTrendMAHandle=iMA(_Symbol, _Period, InpMAPeriod, 0, MODE_SMA, PRICE_CLOSE); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating Moving Average indicator"); + return(INIT_FAILED); + } + +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } + + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeCloseAvg(2)) && // up trend + (MathMin(Open(1), Close(1))> (High(1)-(High(1)-Low(1))/3.0)) && // body in upper 1/3 + (Close(1)>Close(2)) && (Open(1)>Open(2))) // body gap + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nHanging Man detected"; + ExtDirection="Sell"; + return(true); + } + +//--- check Hammer + if((MidPoint(1)(High(1)-(High(1)-Low(1))/3.0)) && // body in upper 1/3 + (Close(1)60)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: RSI>60"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((RSI(1)<70) && (RSI(2)>70)) || ((RSI(1)<30) && (RSI(2)>30))) + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if(((RSI(1)>30) && (RSI(2)<30)) || ((RSI(1)>70) && (RSI(2)<70))) + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| RSI indicator value at the specified bar | +//+------------------------------------------------------------------+ +double RSI(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the RSI indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ +//| SMA value at the specified bar | +//+------------------------------------------------------------------+ +double CloseAvg(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtTrendMAHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the Simple Moving Average indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/HangingMan Hammer Stoch.mq5 b/HangingMan Hammer Stoch.mq5 new file mode 100644 index 0000000..e27e186 --- /dev/null +++ b/HangingMan Hammer Stoch.mq5 @@ -0,0 +1,683 @@ +//+------------------------------------------------------------------+ +//| HangingMan Hammer Stoch.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" + +#include +#include + +#define SIGNAL_BUY 1 // Buy signal +#define SIGNAL_NOT 0 // no trading signal +#define SIGNAL_SELL -1 // Sell signal + +#define CLOSE_LONG 2 // signal to close Long +#define CLOSE_SHORT -2 // signal to close Short + +//--- Input parameters +input int InpAverBodyPeriod=12; // period for calculating average candlestick size +input int InpMAPeriod =5; // Trend MA period +input int InpStochK =47; // period %K +input int InpStochD =9; // period %D +input int InpStochSlow =13; // smoothing period %K +input ENUM_STO_PRICE InpStochApplied=STO_LOWHIGH; // calculation type +input ENUM_MA_METHOD InpStochMA =MODE_SMA; // smoothing type + +//--- trade parameters +input uint InpDuration=10; // position holding time in bars +input uint InpSL =200; // Stop Loss in points +input uint InpTP =200; // Take Profit in points +input uint InpSlippage=10; // slippage in points +//--- money management parameters +input double InpLot=0.1; // lot +//--- Expert ID +input long InpMagicNumber=124400; // Magic Number + +//--- global variables +int ExtAvgBodyPeriod; // average candlestick calculation period +int ExtSignalOpen =0; // Buy/Sell signal +int ExtSignalClose =0; // signal to close a position +string ExtPatternInfo =""; // current pattern information +string ExtDirection =""; // position opening direction +bool ExtPatternDetected=false; // pattern detected +bool ExtConfirmed =false; // pattern confirmed +bool ExtCloseByTime =true; // requires closing by time +bool ExtCheckPassed =true; // status checking error +//--- indicator handles +int ExtIndicatorHandle=INVALID_HANDLE; +int ExtTrendMAHandle=INVALID_HANDLE; + +//--- service objects +CTrade ExtTrade; +CSymbolInfo ExtSymbolInfo; +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + Print("InpSL=", InpSL); + Print("InpTP=", InpTP); +//--- set parameters for trading operations + ExtTrade.SetDeviationInPoints(InpSlippage); // slippage + ExtTrade.SetExpertMagicNumber(InpMagicNumber); // Expert Advisor ID + ExtTrade.LogLevel(LOG_LEVEL_ERRORS); // logging level + + ExtAvgBodyPeriod=InpAverBodyPeriod; +//--- indicator initialization + ExtIndicatorHandle=iStochastic(_Symbol, _Period, InpStochK, InpStochD, InpStochSlow, InpStochMA, InpStochApplied); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating iStochastic indicator"); + return(INIT_FAILED); + } + +//--- trend moving average + ExtTrendMAHandle=iMA(_Symbol, _Period, InpMAPeriod,0, MODE_SMA,PRICE_CLOSE); + if(ExtIndicatorHandle==INVALID_HANDLE) + { + Print("Error creating Moving Average indicator"); + return(INIT_FAILED); + } + +//--- OK + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { +//--- release indicator handle + IndicatorRelease(ExtIndicatorHandle); + } +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { +//--- save the next bar start time; all checks at bar opening only + static datetime next_bar_open=0; + +//--- Phase 1 - check the emergence of a new bar and update the status + if(TimeCurrent()>=next_bar_open) + { + //--- get the current state of environment on the new bar + // namely, set the values of global variables: + // ExtPatternDetected - pattern detection + // ExtConfirmed - pattern confirmation + // ExtSignalOpen - signal to open + // ExtSignalClose - signal to close + // ExtPatternInfo - current pattern information + if(CheckState()) + { + //--- set the new bar opening time + next_bar_open=TimeCurrent(); + next_bar_open-=next_bar_open%PeriodSeconds(_Period); + next_bar_open+=PeriodSeconds(_Period); + + //--- report the emergence of a new bar only once within a bar + if(ExtPatternDetected && ExtConfirmed) + Print(ExtPatternInfo); + } + else + { + //--- error getting the status, retry on the next tick + return; + } + } + +//--- Phase 2 - if there is a signal and no position in this direction + if(ExtSignalOpen && !PositionExist(ExtSignalOpen)) + { + Print("\r\nSignal to open position ", ExtDirection); + PositionOpen(); + if(PositionExist(ExtSignalOpen)) + ExtSignalOpen=SIGNAL_NOT; + } + +//--- Phase 3 - close if there is a signal to close + if(ExtSignalClose && PositionExist(ExtSignalClose)) + { + Print("\r\nSignal to close position ", ExtDirection); + CloseBySignal(ExtSignalClose); + if(!PositionExist(ExtSignalClose)) + ExtSignalClose=SIGNAL_NOT; + } + +//--- Phase 4 - close upon expiration + if(ExtCloseByTime && PositionExpiredByTimeExist()) + { + CloseByTime(); + ExtCloseByTime=PositionExpiredByTimeExist(); + } + } +//+------------------------------------------------------------------+ +//| Get the current environment and check for a pattern | +//+------------------------------------------------------------------+ +bool CheckState() + { +//--- check if there is a pattern + if(!CheckPattern()) + { + Print("Error, failed to check pattern"); + return(false); + } + +//--- check for confirmation + if(!CheckConfirmation()) + { + Print("Error, failed to check pattern confirmation"); + return(false); + } +//--- if there is no confirmation, cancel the signal + if(!ExtConfirmed) + ExtSignalOpen=SIGNAL_NOT; + +//--- check if there is a signal to close a position + if(!CheckCloseSignal()) + { + Print("Error, failed to check the closing signal"); + return(false); + } + +//--- if positions are to be closed after certain holding time in bars + if(InpDuration) + ExtCloseByTime=true; // set flag to close upon expiration + +//--- all checks done + return(true); + } +//+------------------------------------------------------------------+ +//| Open a position in the direction of the signal | +//+------------------------------------------------------------------+ +bool PositionOpen() + { + ExtSymbolInfo.Refresh(); + ExtSymbolInfo.RefreshRates(); + + double price=0; +//--- Stop Loss and Take Profit are not set by default + double stoploss=0.0; + double takeprofit=0.0; + + int digits=ExtSymbolInfo.Digits(); + double point=ExtSymbolInfo.Point(); + double spread=ExtSymbolInfo.Ask()-ExtSymbolInfo.Bid(); + +//--- uptrend + if(ExtSignalOpen==SIGNAL_BUY) + { + price=NormalizeDouble(ExtSymbolInfo.Ask(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price-spread, digits); + } + else + stoploss = NormalizeDouble(price-InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price+spread, digits); + } + else + takeprofit = NormalizeDouble(price+InpTP*point, digits); + } + + if(!ExtTrade.Buy(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s buy %G at %G (sl=%G tp=%G) failed. Ask=%G error=%d", + Symbol(), InpLot, price, stoploss, takeprofit, ExtSymbolInfo.Ask(), GetLastError()); + return(false); + } + } + +//--- downtrend + if(ExtSignalOpen==SIGNAL_SELL) + { + price=NormalizeDouble(ExtSymbolInfo.Bid(), digits); + //--- if Stop Loss is set + if(InpSL>0) + { + if(spread>=InpSL*point) + { + PrintFormat("StopLoss (%d points) <= current spread = %.0f points. Spread value will be used", InpSL, spread/point); + stoploss = NormalizeDouble(price+spread, digits); + } + else + stoploss = NormalizeDouble(price+InpSL*point, digits); + } + //--- if Take Profit is set + if(InpTP>0) + { + if(spread>=InpTP*point) + { + PrintFormat("TakeProfit (%d points) < current spread = %.0f points. Spread value will be used", InpTP, spread/point); + takeprofit = NormalizeDouble(price-spread, digits); + } + else + takeprofit = NormalizeDouble(price-InpTP*point, digits); + } + + if(!ExtTrade.Sell(InpLot, Symbol(), price, stoploss, takeprofit)) + { + PrintFormat("Failed %s sell at %G (sl=%G tp=%G) failed. Bid=%G error=%d", + Symbol(), price, stoploss, takeprofit, ExtSymbolInfo.Bid(), GetLastError()); + ExtTrade.PrintResult(); + Print(" "); + return(false); + } + } +//--- + return(true); + } +//+------------------------------------------------------------------+ +//| Close a position based on the specified signal | +//+------------------------------------------------------------------+ +void CloseBySignal(int type_close) + { +//--- if there is no signal to close, return successful completion + if(type_close==SIGNAL_NOT) + return; +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalClose)==0) + return; + +//--- closing direction + long type; + switch(type_close) + { + case CLOSE_SHORT: + type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + type=POSITION_TYPE_BUY; + break; + default: + Print("Error! Signal to close not detected"); + return; + } + +//--- check all positions and close ours based on the signal + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + if(PositionGetInteger(POSITION_TYPE)==type) + { + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Close positions upon holding time expiration in bars | +//+------------------------------------------------------------------+ +void CloseByTime() + { +//--- if there are no positions opened by our EA + if(PositionExist(ExtSignalOpen)==0) + return; + +//--- check all positions and close ours based on the holding time in bars + int positions=PositionsTotal(); + for(int i=positions-1; i>=0; i--) + { + ulong ticket=PositionGetTicket(i); + if(ticket!=0) + { + //--- get the name of the symbol and the position id (magic) + string symbol=PositionGetString(POSITION_SYMBOL); + long magic =PositionGetInteger(POSITION_MAGIC); + //--- if they correspond to our values + if(symbol==Symbol() && magic==InpMagicNumber) + { + //--- position opening time + datetime open_time=(datetime)PositionGetInteger(POSITION_TIME); + //--- check position holding time in bars + if(BarsHold(open_time)>=(int)InpDuration) + { + Print("\r\nTime to close position #", ticket); + ExtTrade.PositionClose(ticket, InpSlippage); + ExtTrade.PrintResult(); + Print(" "); + } + } + } + } + } +//+------------------------------------------------------------------+ +//| Returns true if there are open positions | +//+------------------------------------------------------------------+ +bool PositionExist(int signal_direction) + { + bool check_type=(signal_direction!=SIGNAL_NOT); + +//--- what positions to search + ENUM_POSITION_TYPE search_type=WRONG_VALUE; + if(check_type) + switch(signal_direction) + { + case SIGNAL_BUY: + search_type=POSITION_TYPE_BUY; + break; + case SIGNAL_SELL: + search_type=POSITION_TYPE_SELL; + break; + case CLOSE_LONG: + search_type=POSITION_TYPE_BUY; + break; + case CLOSE_SHORT: + search_type=POSITION_TYPE_SELL; + break; + default: + //--- entry direction is not specified; nothing to search + return(false); + } + +//--- go through the list of all positions + int positions=PositionsTotal(); + for(int i=0; i=(int)InpDuration)) + return(true); + } + } + } + +//--- open position not found + return(false); + } +//+------------------------------------------------------------------+ +//| Checks position closing time in bars | +//+------------------------------------------------------------------+ +int BarsHold(datetime open_time) + { +//--- first run a basic simple check + if(TimeCurrent()-open_timeCloseAvg(2)) && // up trend + (MathMin(Open(1), Close(1))> (High(1)-(High(1)-Low(1))/3.0)) && // body in upper 1/3 + (Close(1)>Close(2)) && (Open(1)>Open(2))) // body gap + { + ExtPatternDetected=true; + ExtSignalOpen=SIGNAL_SELL; + ExtPatternInfo="\r\nHanging Man detected"; + ExtDirection="Sell"; + return(true); + } + + +//--- check Hammer + if((MidPoint(1)(High(1)-(High(1)-Low(1))/3.0)) && // body in upper 1/3 + (Close(1)70)) + { + ExtConfirmed=true; + ExtPatternInfo+="\r\n Confirmed: StochSignal>70"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Check if there is a signal to close | +//+------------------------------------------------------------------+ +bool CheckCloseSignal() + { + ExtSignalClose=false; +//--- if there is a signal to enter the market, do not check the signal to close + if(ExtSignalOpen!=SIGNAL_NOT) + return(true); + +//--- check if there is a signal to close a long position + if(((StochSignal(1)<80) && (StochSignal(2)>80))|| // 80 crossed downwards + ((StochSignal(1)<20) && (StochSignal(2)>20))) // 20 crossed downwards + { + //--- there is a signal to close a long position + ExtSignalClose=CLOSE_LONG; + ExtDirection="Long"; + } + +//--- check if there is a signal to close a short position + if((((StochSignal(1)>20) && (StochSignal(2)<20)) || // 20 crossed upwards + ((StochSignal(1)>80) && (StochSignal(2)<80)))) // 80 crossed upwards + { + //--- there is a signal to close a short position + ExtSignalClose=CLOSE_SHORT; + ExtDirection="Short"; + } + +//--- successful completion of the check + return(true); + } +//+------------------------------------------------------------------+ +//| Stochastic indicator value at the specified bar | +//+------------------------------------------------------------------+ +double StochSignal(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtIndicatorHandle, SIGNAL_LINE, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the iStochastic indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ +//| SMA value at the specified bar | +//+------------------------------------------------------------------+ +double CloseAvg(int index) + { + double indicator_values[]; + if(CopyBuffer(ExtTrendMAHandle, 0, index, 1, indicator_values)<0) + { + //--- if the copying fails, report the error code + PrintFormat("Failed to copy data from the Simple Moving Average indicator, error code %d", GetLastError()); + return(EMPTY_VALUE); + } + return(indicator_values[0]); + } +//+------------------------------------------------------------------+ diff --git a/Heiken_Ashi.mq5 b/Heiken_Ashi.mq5 new file mode 100644 index 0000000..781adc8 --- /dev/null +++ b/Heiken_Ashi.mq5 @@ -0,0 +1,94 @@ +//+------------------------------------------------------------------+ +//| ProjectName | +//| Copyright 2020, CompanyName | +//| http://www.companyname.net | +//+------------------------------------------------------------------+ + +//+------------------------------------------------------------------+ +//| Heiken_Ashi.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +//--- indicator settings +#property indicator_chart_window +#property indicator_buffers 5 +#property indicator_plots 1 +#property indicator_type1 DRAW_COLOR_CANDLES +#property indicator_color1 DodgerBlue, Red +#property indicator_label1 "Heiken Ashi Open;Heiken Ashi High;Heiken Ashi Low;Heiken Ashi Close" +//--- indicator buffers +double ExtOBuffer[]; +double ExtHBuffer[]; +double ExtLBuffer[]; +double ExtCBuffer[]; +double ExtColorBuffer[]; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- indicator buffers mapping + SetIndexBuffer(0,ExtOBuffer,INDICATOR_DATA); + SetIndexBuffer(1,ExtHBuffer,INDICATOR_DATA); + SetIndexBuffer(2,ExtLBuffer,INDICATOR_DATA); + SetIndexBuffer(3,ExtCBuffer,INDICATOR_DATA); + SetIndexBuffer(4,ExtColorBuffer,INDICATOR_COLOR_INDEX); +//--- + IndicatorSetInteger(INDICATOR_DIGITS,_Digits); +//--- sets first bar from what index will be drawn + IndicatorSetString(INDICATOR_SHORTNAME,"Heiken Ashi"); +//--- sets drawing line empty value + PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0); + } +//+------------------------------------------------------------------+ +//| Heiken Ashi | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + int start; +//--- preliminary calculations + if(prev_calculated==0) + { + ExtLBuffer[0]=low[0]; + ExtHBuffer[0]=high[0]; + ExtOBuffer[0]=open[0]; + ExtCBuffer[0]=close[0]; + start=1; + } + else + start=prev_calculated-1; + +//--- the main loop of calculations + for(int i=start; i +//+------------------------------------------------------------------+ +//| inputs | +//+------------------------------------------------------------------+ +input bool Accumulative=true; +//+------------------------------------------------------------------+ +//| Script program start function | +//+------------------------------------------------------------------+ +int OnStart(void) + { + int k=100; + double arr[10]; +//--- create chart + CHistogramChart chart; + if(!chart.CreateBitmapLabel("SampleHistogramChart",10,10,600,450)) + { + Print("Error creating histogram chart: ",GetLastError()); + return(-1); + } + if(Accumulative) + { + chart.Accumulative(); + chart.VScaleParams(20*k*10,-10*k*10,20); + } + else + chart.VScaleParams(20*k,-10*k,20); + chart.ShowValue(true); + chart.ShowScaleTop(false); + chart.ShowScaleBottom(false); + chart.ShowScaleRight(false); + chart.ShowLegend(); + for(int j=0;j<5;j++) + { + for(int i=0;i<10;i++) + { + k=-k; + if(k>0) + arr[i]=k*(i+10-j); + else + arr[i]=k*(i+10-j)/2; + } + chart.SeriesAdd(arr,"Item"+IntegerToString(j)); + } +//--- play with values + while(!IsStopped()) + { + int i=rand()%5; + int j=rand()%10; + k=rand()%3000-1000; + chart.ValueUpdate(i,j,k); + Sleep(200); + } +//--- finish + chart.Destroy(); + return(0); + } +//+------------------------------------------------------------------+ diff --git a/Ichimoku.mq5 b/Ichimoku.mq5 new file mode 100644 index 0000000..e398029 --- /dev/null +++ b/Ichimoku.mq5 @@ -0,0 +1,130 @@ +//+------------------------------------------------------------------+ +//| Ichimoku.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property description "Ichimoku Kinko Hyo" +//--- indicator settings +#property indicator_chart_window +#property indicator_buffers 5 +#property indicator_plots 4 +#property indicator_type1 DRAW_LINE +#property indicator_type2 DRAW_LINE +#property indicator_type3 DRAW_FILLING +#property indicator_type4 DRAW_LINE +#property indicator_color1 Red +#property indicator_color2 Blue +#property indicator_color3 SandyBrown,Thistle +#property indicator_color4 Lime +#property indicator_label1 "Tenkan-sen" +#property indicator_label2 "Kijun-sen" +#property indicator_label3 "Senkou Span A;Senkou Span B" +#property indicator_label4 "Chikou Span" +//--- input parameters +input int InpTenkan=9; // Tenkan-sen +input int InpKijun=26; // Kijun-sen +input int InpSenkou=52; // Senkou Span B +//--- indicator buffers +double ExtTenkanBuffer[]; +double ExtKijunBuffer[]; +double ExtSpanABuffer[]; +double ExtSpanBBuffer[]; +double ExtChikouBuffer[]; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- indicator buffers mapping + SetIndexBuffer(0,ExtTenkanBuffer,INDICATOR_DATA); + SetIndexBuffer(1,ExtKijunBuffer,INDICATOR_DATA); + SetIndexBuffer(2,ExtSpanABuffer,INDICATOR_DATA); + SetIndexBuffer(3,ExtSpanBBuffer,INDICATOR_DATA); + SetIndexBuffer(4,ExtChikouBuffer,INDICATOR_DATA); +//--- + IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1); +//--- sets first bar from what index will be drawn + PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpTenkan); + PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpKijun); + PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,InpSenkou-1); +//--- lines shifts when drawing + PlotIndexSetInteger(2,PLOT_SHIFT,InpKijun); + PlotIndexSetInteger(3,PLOT_SHIFT,-InpKijun); +//--- change labels for DataWindow + PlotIndexSetString(0,PLOT_LABEL,"Tenkan-sen("+string(InpTenkan)+")"); + PlotIndexSetString(1,PLOT_LABEL,"Kijun-sen("+string(InpKijun)+")"); + PlotIndexSetString(2,PLOT_LABEL,"Senkou Span A;Senkou Span B("+string(InpSenkou)+")"); + } +//+------------------------------------------------------------------+ +//| Ichimoku Kinko Hyo | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + int start; +//--- + if(prev_calculated==0) + start=0; + else + start=prev_calculated-1; +//--- main loop + for(int i=start; ifrom_index-range && i>=0; i--) + if(resfrom_index-range && i>=0; i--) + if(res>array[i]) + res=array[i]; +//--- + return(res); + } +//+------------------------------------------------------------------+ diff --git a/Keltner Channel.mq5 b/Keltner Channel.mq5 new file mode 100644 index 0000000..e460f21 --- /dev/null +++ b/Keltner Channel.mq5 @@ -0,0 +1,246 @@ +//+------------------------------------------------------------------+ +//| Keltner Channel.mq5 | +//| Copyright 2009-2024, MetaQuotes Ltd | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "2009-2024, MetaQuotes Ltd" +#property link "http://www.mql5.com" +#property description "Keltner Channel" + +#property indicator_chart_window +#property indicator_buffers 3 +#property indicator_plots 3 +#property indicator_type1 DRAW_LINE +#property indicator_color1 clrBlue +#property indicator_type2 DRAW_LINE +#property indicator_color2 clrGray +#property indicator_type3 DRAW_LINE +#property indicator_color3 clrRed +//--- labels +#property indicator_label1 "Upper Keltner" +#property indicator_label2 "Middle Keltner" +#property indicator_label3 "Lower Keltner" + +//--- input parameters +input int InpEMAPeriod=20; // Period of EMA +input int InpATRPeriod=10; // Period of ATR +input double InpATRFactor=2.0; // ATR multiplier +input bool InpShowLabel=true; // Show price of level + + +//--- global variables for parameters +int ExtEMAPeriod; +int ExtATRPeriod; +double ExtATRFactor; + +//--- indicator buffers +double ExtUppBuffer[]; +double ExtEMABuffer[]; +double ExtDwnBuffer[]; + +//--- indicator handles +int ExtEMAHandle; +int ExtATRHandle; + +//--- unique prefix to identify indicator objects +string ExtPrefixUniq; +int ExtPeriod; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { +//--- check for input values + if(InpEMAPeriod<10) + { + ExtEMAPeriod=20; + PrintFormat("Incorrect value for input variable InpEMAPeriod=%d. Indicator will use value=%d for calculations.", + InpEMAPeriod, ExtEMAPeriod); + } + else + ExtEMAPeriod=InpEMAPeriod; + + if(InpATRPeriod<3) + { + ExtATRPeriod=10; + PrintFormat("Incorrect value for input variable InpATRPeriod=%d. Indicator will use value=%d for calculations.", + InpATRPeriod, ExtATRPeriod); + } + else + ExtATRPeriod=InpATRPeriod; + + if(InpATRFactor<1.0) + { + ExtATRFactor=2.0; + PrintFormat("Incorrect value for input variable InpBandsDeviations=%f. Indicator will use value=%f for calculations.", + InpATRFactor, ExtATRFactor); + } + else + ExtATRFactor=InpATRFactor; + +//--- define buffers + SetIndexBuffer(0, ExtUppBuffer); + SetIndexBuffer(1, ExtEMABuffer); + SetIndexBuffer(2, ExtDwnBuffer); + +//--- indexes draw begin settings + PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpEMAPeriod+1); + PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, InpEMAPeriod+1); + PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, InpEMAPeriod+1); + +//--- set a 1-bar offset for each line + PlotIndexSetInteger(0, PLOT_SHIFT, 1); + PlotIndexSetInteger(1, PLOT_SHIFT, 1); + PlotIndexSetInteger(2, PLOT_SHIFT, 1); + +//--- set drawing line empty value + PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0.0); + PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0.0); + PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, 0.0); + +//--- indicator name + IndicatorSetString(INDICATOR_SHORTNAME, "Keltner Channel"); +//--- number of digits of indicator value + IndicatorSetInteger(INDICATOR_DIGITS, _Digits); + +//--- create indicators + ExtEMAHandle=iMA(NULL, 0, InpEMAPeriod, 0, MODE_EMA, PRICE_CLOSE); + ExtATRHandle=iATR(NULL, 0, InpATRPeriod); + + ExtPeriod=PeriodSeconds(_Period); + +//--- prepare prefix for objects + string number=StringFormat("%I64d", GetTickCount64()); + ExtPrefixUniq=StringSubstr(number, StringLen(number)-4); + ExtPrefixUniq=ExtPrefixUniq+"_KLT"; + Print("Indicator \"Keltner Channels\" started, prefix=", ExtPrefixUniq); + + return(INIT_SUCCEEDED); + } +//+------------------------------------------------------------------+ +//| Custom indicator iteration function | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { +//--- if this is the first calculation of the indicator + if(prev_calculated==0) + { + //--- populate the beginning values, for which the indicator cannot be calculated, with empty values + ArrayFill(ExtUppBuffer, 0, rates_total, 0); + ArrayFill(ExtEMABuffer, 0, rates_total, 0); + ArrayFill(ExtDwnBuffer, 0, rates_total, 0); + + //--- get EMA values into the indicator buffer + if(CopyBuffer(ExtEMAHandle, 0, 0, rates_total, ExtEMABuffer)<0) + return(0); + + //--- get ATR indicator values into a dynamic array + double atr[]; + if(CopyBuffer(ExtATRHandle, 0, 0, rates_total, atr)<0) + return(0); + + //--- shift from the beginning by the required number of bars + int start=MathMax(InpEMAPeriod, InpATRPeriod)+1; + + //--- fill in the values of the upper and lower channel borders + for(int i=start; i +//+------------------------------------------------------------------+ +//| inputs | +//+------------------------------------------------------------------+ +input bool Accumulative=false; +//+------------------------------------------------------------------+ +//| Script program start function | +//+------------------------------------------------------------------+ +int OnStart(void) + { + int k=100; + double arr[10]; +//--- create chart + CLineChart chart; +//--- create chart + if(!chart.CreateBitmapLabel("SampleHistogrammChart",10,10,600,450)) + { + Print("Error creating line chart: ",GetLastError()); + return(-1); + } + if(Accumulative) + { + chart.Accumulative(); + chart.VScaleParams(20*k*10,-10*k*10,20); + } + else + chart.VScaleParams(20*k,-10*k,15); + chart.ShowScaleTop(false); + chart.ShowScaleRight(false); + chart.ShowLegend(); + chart.Filled(); + for(int j=0;j<5;j++) + { + for(int i=0;i<10;i++) + { + k=-k; + if(k>0) + arr[i]=k*(i+10-j); + else + arr[i]=k*(i+10-j)/2; + } + chart.SeriesAdd(arr,"Item"+IntegerToString(j)); + } +//--- play with values + while(!IsStopped()) + { + int i=rand()%5; + int j=rand()%10; + k=rand()%3000-1000; + chart.ValueUpdate(i,j,k); + Sleep(200); + } +//--- finish + chart.Destroy(); + return(0); + } +//+------------------------------------------------------------------+ diff --git a/MACD EA.mq5 b/MACD EA.mq5 new file mode 100644 index 0000000..f2523d8 --- /dev/null +++ b/MACD EA.mq5 @@ -0,0 +1,41 @@ +//+------------------------------------------------------------------+ +//| MACD EA.mq5 | +//| Copyright 2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "1.00" +//+------------------------------------------------------------------+ +//| Expert initialization function | +//+------------------------------------------------------------------+ +int OnInit() + { + // MACD parameters + int fast_length = 3; + int slow_length = 10; + int signal_smoothing = 16; + int ma_type = MODE_SMA; + + // Initialization code here + + return(INIT_SUCCEEDED); + } + +//+------------------------------------------------------------------+ +//| Expert deinitialization function | +//+------------------------------------------------------------------+ +void OnDeinit(const int reason) + { + // Deinitialization code here + } + +//+------------------------------------------------------------------+ +//| Expert tick function | +//+------------------------------------------------------------------+ +void OnTick() + { + // MACD calculation and trading logic here + } + +//+------------------------------------------------------------------+ diff --git a/MACD Sample.mq5 b/MACD Sample.mq5 new file mode 100644 index 0000000..b7bffb8 --- /dev/null +++ b/MACD Sample.mq5 @@ -0,0 +1,451 @@ +//+------------------------------------------------------------------+ +//| MACD Sample.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property version "5.50" +#property description "It is important to make sure that the expert works with a normal" +#property description "chart and the user did not make any mistakes setting input" +#property description "variables (Lots, TakeProfit, TrailingStop) in our case," +#property description "we check TakeProfit on a chart of more than 2*trend_period bars" + +#define MACD_MAGIC 1234502 +//--- +#include +#include +#include +#include +//--- +input double InpLots =0.1; // Lots +input int InpTakeProfit =50; // Take Profit (in pips) +input int InpTrailingStop =30; // Trailing Stop Level (in pips) +input int InpMACDOpenLevel =3; // MACD open level (in pips) +input int InpMACDCloseLevel=2; // MACD close level (in pips) +input int InpMATrendPeriod =26; // MA trend period +//--- +int ExtTimeOut=10; // time out in seconds between trade operations +//+------------------------------------------------------------------+ +//| MACD Sample expert class | +//+------------------------------------------------------------------+ +class CSampleExpert + { +protected: + double m_adjusted_point; // point value adjusted for 3 or 5 points + CTrade m_trade; // trading object + CSymbolInfo m_symbol; // symbol info object + CPositionInfo m_position; // trade position object + CAccountInfo m_account; // account info wrapper + //--- indicators + int m_handle_macd; // MACD indicator handle + int m_handle_ema; // moving average indicator handle + //--- indicator buffers + double m_buff_MACD_main[]; // MACD indicator main buffer + double m_buff_MACD_signal[]; // MACD indicator signal buffer + double m_buff_EMA[]; // EMA indicator buffer + //--- indicator data for processing + double m_macd_current; + double m_macd_previous; + double m_signal_current; + double m_signal_previous; + double m_ema_current; + double m_ema_previous; + //--- + double m_macd_open_level; + double m_macd_close_level; + double m_traling_stop; + double m_take_profit; + +public: + CSampleExpert(void); + ~CSampleExpert(void); + bool Init(void); + void Deinit(void); + bool Processing(void); + +protected: + bool InitCheckParameters(const int digits_adjust); + bool InitIndicators(void); + bool LongClosed(void); + bool ShortClosed(void); + bool LongModified(void); + bool ShortModified(void); + bool LongOpened(void); + bool ShortOpened(void); + }; +//--- global expert +CSampleExpert ExtExpert; +//+------------------------------------------------------------------+ +//| Constructor | +//+------------------------------------------------------------------+ +CSampleExpert::CSampleExpert(void) : m_adjusted_point(0), + m_handle_macd(INVALID_HANDLE), + m_handle_ema(INVALID_HANDLE), + m_macd_current(0), + m_macd_previous(0), + m_signal_current(0), + m_signal_previous(0), + m_ema_current(0), + m_ema_previous(0), + m_macd_open_level(0), + m_macd_close_level(0), + m_traling_stop(0), + m_take_profit(0) + { + ArraySetAsSeries(m_buff_MACD_main,true); + ArraySetAsSeries(m_buff_MACD_signal,true); + ArraySetAsSeries(m_buff_EMA,true); + } +//+------------------------------------------------------------------+ +//| Destructor | +//+------------------------------------------------------------------+ +CSampleExpert::~CSampleExpert(void) + { + } +//+------------------------------------------------------------------+ +//| Initialization and checking for input parameters | +//+------------------------------------------------------------------+ +bool CSampleExpert::Init(void) + { +//--- initialize common information + m_symbol.Name(Symbol()); // symbol + m_trade.SetExpertMagicNumber(MACD_MAGIC); // magic + m_trade.SetMarginMode(); + m_trade.SetTypeFillingBySymbol(Symbol()); +//--- 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; +//--- set default deviation for trading in adjusted points + m_macd_open_level =InpMACDOpenLevel*m_adjusted_point; + m_macd_close_level=InpMACDCloseLevel*m_adjusted_point; + m_traling_stop =InpTrailingStop*m_adjusted_point; + m_take_profit =InpTakeProfit*m_adjusted_point; +//--- set default deviation for trading in adjusted points + m_trade.SetDeviationInPoints(3*digits_adjust); +//--- + if(!InitCheckParameters(digits_adjust)) + return(false); + if(!InitIndicators()) + return(false); +//--- succeed + return(true); + } +//+------------------------------------------------------------------+ +//| Checking for input parameters | +//+------------------------------------------------------------------+ +bool CSampleExpert::InitCheckParameters(const int digits_adjust) + { +//--- initial data checks + if(InpTakeProfit*digits_adjustm_symbol.LotsMax()) + { + printf("Lots amount must be in the range from %f to %f",m_symbol.LotsMin(),m_symbol.LotsMax()); + return(false); + } + if(MathAbs(InpLots/m_symbol.LotsStep()-MathRound(InpLots/m_symbol.LotsStep()))>1.0E-10) + { + printf("Lots amount is not corresponding with lot step %f",m_symbol.LotsStep()); + return(false); + } +//--- warning + if(InpTakeProfit<=InpTrailingStop) + printf("Warning: Trailing Stop must be less than Take Profit"); +//--- succeed + return(true); + } +//+------------------------------------------------------------------+ +//| Initialization of the indicators | +//+------------------------------------------------------------------+ +bool CSampleExpert::InitIndicators(void) + { +//--- create MACD indicator + if(m_handle_macd==INVALID_HANDLE) + if((m_handle_macd=iMACD(NULL,0,12,26,9,PRICE_CLOSE))==INVALID_HANDLE) + { + printf("Error creating MACD indicator"); + return(false); + } +//--- create EMA indicator and add it to collection + if(m_handle_ema==INVALID_HANDLE) + if((m_handle_ema=iMA(NULL,0,InpMATrendPeriod,0,MODE_EMA,PRICE_CLOSE))==INVALID_HANDLE) + { + printf("Error creating EMA indicator"); + return(false); + } +//--- succeed + return(true); + } +//+------------------------------------------------------------------+ +//| Check for long position closing | +//+------------------------------------------------------------------+ +bool CSampleExpert::LongClosed(void) + { + bool res=false; +//--- should it be closed? + if(m_macd_current>0) + if(m_macd_currentm_signal_previous) + if(m_macd_current>m_macd_close_level) + { + //--- close position + if(m_trade.PositionClose(Symbol())) + printf("Long position by %s to be closed",Symbol()); + else + printf("Error closing position by %s : '%s'",Symbol(),m_trade.ResultComment()); + //--- processed and cannot be modified + res=true; + } +//--- result + return(res); + } +//+------------------------------------------------------------------+ +//| Check for short position closing | +//+------------------------------------------------------------------+ +bool CSampleExpert::ShortClosed(void) + { + bool res=false; +//--- should it be closed? + if(m_macd_current<0) + if(m_macd_current>m_signal_current && m_macd_previousm_macd_close_level) + { + //--- close position + if(m_trade.PositionClose(Symbol())) + printf("Short position by %s to be closed",Symbol()); + else + printf("Error closing position by %s : '%s'",Symbol(),m_trade.ResultComment()); + //--- processed and cannot be modified + res=true; + } +//--- result + return(res); + } +//+------------------------------------------------------------------+ +//| Check for long position modifying | +//+------------------------------------------------------------------+ +bool CSampleExpert::LongModified(void) + { + bool res=false; +//--- check for trailing stop + if(InpTrailingStop>0) + { + if(m_symbol.Bid()-m_position.PriceOpen()>m_adjusted_point*InpTrailingStop) + { + double sl=NormalizeDouble(m_symbol.Bid()-m_traling_stop,m_symbol.Digits()); + double tp=m_position.TakeProfit(); + if(m_position.StopLoss()0) + { + if((m_position.PriceOpen()-m_symbol.Ask())>(m_adjusted_point*InpTrailingStop)) + { + double sl=NormalizeDouble(m_symbol.Ask()+m_traling_stop,m_symbol.Digits()); + double tp=m_position.TakeProfit(); + if(m_position.StopLoss()>sl || m_position.StopLoss()==0.0) + { + //--- modify position + if(m_trade.PositionModify(Symbol(),sl,tp)) + printf("Short position by %s to be modified",Symbol()); + else + { + printf("Error modifying position by %s : '%s'",Symbol(),m_trade.ResultComment()); + printf("Modify parameters : SL=%f,TP=%f",sl,tp); + } + //--- modified and must exit from expert + res=true; + } + } + } +//--- result + return(res); + } +//+------------------------------------------------------------------+ +//| Check for long position opening | +//+------------------------------------------------------------------+ +bool CSampleExpert::LongOpened(void) + { + bool res=false; +//--- check for long position (BUY) possibility + if(m_macd_current<0) + if(m_macd_current>m_signal_current && m_macd_previous(m_macd_open_level) && m_ema_current>m_ema_previous) + { + double price=m_symbol.Ask(); + double tp =m_symbol.Bid()+m_take_profit; + //--- check for free money + if(m_account.FreeMarginCheck(Symbol(),ORDER_TYPE_BUY,InpLots,price)<0.0) + printf("We have no money. Free Margin = %f",m_account.FreeMargin()); + else + { + //--- open position + if(m_trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,InpLots,price,0.0,tp)) + printf("Position by %s to be opened",Symbol()); + else + { + printf("Error opening BUY position by %s : '%s'",Symbol(),m_trade.ResultComment()); + printf("Open parameters : price=%f,TP=%f",price,tp); + } + } + //--- in any case we must exit from expert + res=true; + } +//--- result + return(res); + } +//+------------------------------------------------------------------+ +//| Check for short position opening | +//+------------------------------------------------------------------+ +bool CSampleExpert::ShortOpened(void) + { + bool res=false; +//--- check for short position (SELL) possibility + if(m_macd_current>0) + if(m_macd_currentm_signal_previous) + if(m_macd_current>(m_macd_open_level) && m_ema_current=limit_time) + { + //--- check for data + if(Bars(Symbol(),Period())>2*InpMATrendPeriod) + { + //--- change limit time by timeout in seconds if processed + if(ExtExpert.Processing()) + limit_time=TimeCurrent()+ExtTimeOut; + } + } + } +//+------------------------------------------------------------------+ diff --git a/MACD.mq5 b/MACD.mq5 new file mode 100644 index 0000000..988713b --- /dev/null +++ b/MACD.mq5 @@ -0,0 +1,123 @@ +//+------------------------------------------------------------------+ +//| MACD.mq5 | +//| Copyright 2000-2024, MetaQuotes Ltd. | +//| https://www.mql5.com | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2000-2024, MetaQuotes Ltd." +#property link "https://www.mql5.com" +#property description "Moving Average Convergence/Divergence" +#include +//--- indicator settings +#property indicator_separate_window +#property indicator_buffers 4 +#property indicator_plots 2 +#property indicator_type1 DRAW_HISTOGRAM +#property indicator_type2 DRAW_LINE +#property indicator_color1 Silver +#property indicator_color2 Red +#property indicator_width1 2 +#property indicator_width2 1 +#property indicator_label1 "MACD" +#property indicator_label2 "Signal" +//--- input parameters +input int InpFastEMA=12; // Fast EMA period +input int InpSlowEMA=26; // Slow EMA period +input int InpSignalSMA=9; // Signal SMA period +input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price +//--- indicator buffers +double ExtMacdBuffer[]; +double ExtSignalBuffer[]; +double ExtFastMaBuffer[]; +double ExtSlowMaBuffer[]; + +int ExtFastMaHandle; +int ExtSlowMaHandle; +//+------------------------------------------------------------------+ +//| Custom indicator initialization function | +//+------------------------------------------------------------------+ +void OnInit() + { +//--- indicator buffers mapping + SetIndexBuffer(0,ExtMacdBuffer,INDICATOR_DATA); + SetIndexBuffer(1,ExtSignalBuffer,INDICATOR_DATA); + SetIndexBuffer(2,ExtFastMaBuffer,INDICATOR_CALCULATIONS); + SetIndexBuffer(3,ExtSlowMaBuffer,INDICATOR_CALCULATIONS); +//--- sets first bar from what index will be drawn + PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpSignalSMA-1); +//--- name for indicator subwindow label + string short_name=StringFormat("MACD(%d,%d,%d)",InpFastEMA,InpSlowEMA,InpSignalSMA); + IndicatorSetString(INDICATOR_SHORTNAME,short_name); +//--- get MA handles + ExtFastMaHandle=iMA(NULL,0,InpFastEMA,0,MODE_EMA,InpAppliedPrice); + ExtSlowMaHandle=iMA(NULL,0,InpSlowEMA,0,MODE_EMA,InpAppliedPrice); + } +//+------------------------------------------------------------------+ +//| Moving Averages Convergence/Divergence | +//+------------------------------------------------------------------+ +int OnCalculate(const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[]) + { + if(rates_totalrates_total || prev_calculated<0) + to_copy=rates_total; + else + { + to_copy=rates_total-prev_calculated; + if(prev_calculated>0) + to_copy++; + } +//--- get Fast EMA buffer + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtFastMaHandle,0,0,to_copy,ExtFastMaBuffer)<=0) + { + Print("Getting fast EMA is failed! Error ",GetLastError()); + return(0); + } +//--- get SlowSMA buffer + if(IsStopped()) // checking for stop flag + return(0); + if(CopyBuffer(ExtSlowMaHandle,0,0,to_copy,ExtSlowMaBuffer)<=0) + { + Print("Getting slow SMA is failed! Error ",GetLastError()); + return(0); + } +//--- + int start; + if(prev_calculated==0) + start=0; + else + start=prev_calculated-1; +//--- calculate MACD + for(int i=start; i