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;i0) + 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 +//--- 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