├── Creating Expert Using MQL5 Wizard ├── ExpGen.mq5 └── SignalSidus.mqh ├── Creating Indicator based on Modules of Expert Trading Signals ├── ExpertInd.mqh ├── IndSignals.mq5 ├── SignalMACDInd.mqh └── SignalMAInd.mqh ├── Example of Creating Advisor using OOP ├── CheckTrade.mqh ├── ExpSidusOOP.mq5 ├── Sidus.mqh └── Trade.mqh ├── Example of Creating Advisor └── ExpSidus.mq5 ├── Example of Creating Indicator ├── Impulse keeper +.mq5 └── Impulse keeper.mq5 ├── Genetic Algorithms ├── part1 │ ├── ExpMAMACD.mq5 │ ├── ExpertInd.mqh │ ├── GenScript.mq5 │ ├── IndSignals.mq5 │ ├── MAMACDFitness.mqh │ ├── SignalMACDInd.mqh │ ├── SignalMAInd.mqh │ └── UGAlib.mqh └── part2 │ ├── GenScript.mq5 │ ├── IndSignalsLow.mq5 │ ├── MAMACDFitness.mqh │ ├── SignalMACDIndLow.mqh │ ├── SignalMAIndLow.mqh │ └── UGAlib.mqh ├── Graphic Objects ├── Impulse keeper.mq5 ├── OBJPROP_ANGLE.mq5 ├── OBJPROP_PRICE.mq5 ├── OBJ_CHART.mq5 ├── TextOut++.mq5 ├── TextOut+.mq5 └── TextOut.mq5 ├── Indicator Handle └── MACD.mq5 ├── Indicator Properties ├── ADX.mq5 └── RSI.mq5 ├── Money Management and Expert Evaluation ├── ExpSidusOOP+.mq5 ├── ExpSidusOOP.mq5 └── Trade.mqh ├── Object Oriented Approach ├── CImpulsekeeper+.mq5 ├── CImpulsekeeper.mq5 └── IKSignal.mqh ├── OnChartEvent └── Impulse keeper.mq5 ├── OnInit ├── Custom Moving Average.mq5 ├── MACD.mq5 └── RSI.mq5 ├── PlaySound └── Impulse keeper.mq5 ├── Python.zip ├── README.md └── Testing Experts ├── ExpSidusOOP.mq5 └── IndTrade.mq5 /Creating Expert Using MQL5 Wizard/ExpGen.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| ExpGen.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | //+------------------------------------------------------------------+ 10 | //| Include | 11 | //+------------------------------------------------------------------+ 12 | #include 13 | //--- available signals 14 | #include 15 | //--- available trailing 16 | #include 17 | //--- available money management 18 | #include 19 | //+------------------------------------------------------------------+ 20 | //| Inputs | 21 | //+------------------------------------------------------------------+ 22 | //--- inputs for expert 23 | input string Expert_Title ="ExpGen"; // Document name 24 | ulong Expert_MagicNumber =2176; // 25 | bool Expert_EveryTick =false; // 26 | //--- inputs for main signal 27 | input int Signal_ThresholdOpen =20; // Signal threshold value to open [0...100] 28 | input int Signal_ThresholdClose =20; // Signal threshold value to close [0...100] 29 | input double Signal_PriceLevel =0.0; // Price level to execute a deal 30 | input double Signal_StopLevel =50.0; // Stop Loss level (in points) 31 | input double Signal_TakeLevel =50.0; // Take Profit level (in points) 32 | input int Signal_Expiration =4; // Expiration of pending orders (in bars) 33 | input int Signal_MA_NumberOpenPosition =3; // Sidus(5,80,20,80) Bars number checked to cross 34 | input int Signal_MA_Pattern_0 =60; // Sidus(5,80,20,80) Model 0 35 | input int Signal_MA_Pattern_1 =10; // Sidus(5,80,20,80) Model 1 36 | input int Signal_MA_Pattern_2 =100; // Sidus(5,80,20,80) Model 2 37 | input double Signal_MA_Weight =1.0; // Sidus(5,80,20,80) Weight [0...1.0] 38 | //--- inputs for trailing 39 | input int Trailing_FixedPips_StopLevel =100; // Stop Loss trailing level (in points) 40 | input int Trailing_FixedPips_ProfitLevel=50; // Take Profit trailing level (in points) 41 | //--- inputs for money 42 | input double Money_FixLot_Percent =10.0; // Percent 43 | input double Money_FixLot_Lots =1.0; // Fixed volume 44 | //+------------------------------------------------------------------+ 45 | //| Global expert object | 46 | //+------------------------------------------------------------------+ 47 | CExpert ExtExpert; 48 | //+------------------------------------------------------------------+ 49 | //| Initialization function of the expert | 50 | //+------------------------------------------------------------------+ 51 | int OnInit() 52 | { 53 | //--- Initializing expert 54 | if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber)) 55 | { 56 | //--- failed 57 | printf(__FUNCTION__+": error initializing expert"); 58 | ExtExpert.Deinit(); 59 | return(INIT_FAILED); 60 | } 61 | //--- Creating signal 62 | CExpertSignal *signal=new CExpertSignal; 63 | if(signal==NULL) 64 | { 65 | //--- failed 66 | printf(__FUNCTION__+": error creating signal"); 67 | ExtExpert.Deinit(); 68 | return(INIT_FAILED); 69 | } 70 | //--- 71 | ExtExpert.InitSignal(signal); 72 | signal.ThresholdOpen(Signal_ThresholdOpen); 73 | signal.ThresholdClose(Signal_ThresholdClose); 74 | signal.PriceLevel(Signal_PriceLevel); 75 | signal.StopLevel(Signal_StopLevel); 76 | signal.TakeLevel(Signal_TakeLevel); 77 | signal.Expiration(Signal_Expiration); 78 | //--- Creating filter CSignalSidus 79 | CSignalSidus *filter0=new CSignalSidus; 80 | if(filter0==NULL) 81 | { 82 | //--- failed 83 | printf(__FUNCTION__+": error creating filter0"); 84 | ExtExpert.Deinit(); 85 | return(INIT_FAILED); 86 | } 87 | signal.AddFilter(filter0); 88 | //--- Set filter parameters 89 | filter0.NumberOpenPosition(Signal_MA_NumberOpenPosition); 90 | filter0.Pattern_0(Signal_MA_Pattern_0); 91 | filter0.Pattern_1(Signal_MA_Pattern_1); 92 | filter0.Pattern_2(Signal_MA_Pattern_2); 93 | filter0.Weight(Signal_MA_Weight); 94 | //--- Creation of trailing object 95 | CTrailingFixedPips *trailing=new CTrailingFixedPips; 96 | if(trailing==NULL) 97 | { 98 | //--- failed 99 | printf(__FUNCTION__+": error creating trailing"); 100 | ExtExpert.Deinit(); 101 | return(INIT_FAILED); 102 | } 103 | //--- Add trailing to expert (will be deleted automatically)) 104 | if(!ExtExpert.InitTrailing(trailing)) 105 | { 106 | //--- failed 107 | printf(__FUNCTION__+": error initializing trailing"); 108 | ExtExpert.Deinit(); 109 | return(INIT_FAILED); 110 | } 111 | //--- Set trailing parameters 112 | trailing.StopLevel(Trailing_FixedPips_StopLevel); 113 | trailing.ProfitLevel(Trailing_FixedPips_ProfitLevel); 114 | //--- Creation of money object 115 | CMoneyFixedLot *money=new CMoneyFixedLot; 116 | if(money==NULL) 117 | { 118 | //--- failed 119 | printf(__FUNCTION__+": error creating money"); 120 | ExtExpert.Deinit(); 121 | return(INIT_FAILED); 122 | } 123 | //--- Add money to expert (will be deleted automatically)) 124 | if(!ExtExpert.InitMoney(money)) 125 | { 126 | //--- failed 127 | printf(__FUNCTION__+": error initializing money"); 128 | ExtExpert.Deinit(); 129 | return(INIT_FAILED); 130 | } 131 | //--- Set money parameters 132 | money.Percent(Money_FixLot_Percent); 133 | money.Lots(Money_FixLot_Lots); 134 | //--- Check all trading objects parameters 135 | if(!ExtExpert.ValidationSettings()) 136 | { 137 | //--- failed 138 | ExtExpert.Deinit(); 139 | return(INIT_FAILED); 140 | } 141 | //--- Tuning of all necessary indicators 142 | if(!ExtExpert.InitIndicators()) 143 | { 144 | //--- failed 145 | printf(__FUNCTION__+": error initializing indicators"); 146 | ExtExpert.Deinit(); 147 | return(INIT_FAILED); 148 | } 149 | //--- ok 150 | return(INIT_SUCCEEDED); 151 | } 152 | //+------------------------------------------------------------------+ 153 | //| Deinitialization function of the expert | 154 | //+------------------------------------------------------------------+ 155 | void OnDeinit(const int reason) 156 | { 157 | ExtExpert.Deinit(); 158 | } 159 | //+------------------------------------------------------------------+ 160 | //| "Tick" event handler function | 161 | //+------------------------------------------------------------------+ 162 | void OnTick() 163 | { 164 | ExtExpert.OnTick(); 165 | } 166 | //+------------------------------------------------------------------+ 167 | //| "Trade" event handler function | 168 | //+------------------------------------------------------------------+ 169 | void OnTrade() 170 | { 171 | ExtExpert.OnTrade(); 172 | } 173 | //+------------------------------------------------------------------+ 174 | //| "Timer" event handler function | 175 | //+------------------------------------------------------------------+ 176 | void OnTimer() 177 | { 178 | ExtExpert.OnTimer(); 179 | } 180 | //+------------------------------------------------------------------+ 181 | -------------------------------------------------------------------------------- /Creating Expert Using MQL5 Wizard/SignalSidus.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| SignalSidus.mqh | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | 9 | #include 10 | 11 | // wizard description start 12 | //+------------------------------------------------------------------+ 13 | //| Description of the class | 14 | //| Title=Signals of the system 'Sidus' | 15 | //| Type=SignalAdvanced | 16 | //| Name=Sidus | 17 | //| ShortName=MA | 18 | //| Class=CSignalSidus | 19 | //| Page= | 20 | //| Parameter=NumberOpenPosition,int,5,Bars number checked to cross | 21 | //| Parameter=Pattern_0,int,80,Model 0 | 22 | //| Parameter=Pattern_1,int,20,Model 1 | 23 | //| Parameter=Pattern_2,int,80,Model 2 | 24 | //+------------------------------------------------------------------+ 25 | // wizard description end 26 | //+------------------------------------------------------------------+ 27 | //| Class CSignalSidus. | 28 | //| Purpose: Class of generator of trade signals based on | 29 | //| the 'Sidus' system. | 30 | //| Is derived from the CExpertSignal class. | 31 | //+------------------------------------------------------------------+ 32 | 33 | class CSignalSidus : public CExpertSignal 34 | { 35 | protected: 36 | CiMA m_ma18; // object-indicator 37 | CiMA m_ma28; // object-indicator 38 | CiMA m_ma5; // object-indicator 39 | CiMA m_ma8; // object-indicator 40 | //--- adjusted parameters 41 | int m_numberOpenPosition; 42 | //--- "weights" of market models (0-100) 43 | int m_pattern_0; // model 0 "5 WMA and 8 WMA cross the 18 EMA and the 28 EMA upward or top down" 80 44 | int m_pattern_1; // model 1 "5 WMA crosses the 8 WMA upward or top down" 10 45 | int m_pattern_2; // model 2 "18 EMA crosses the 28 EMA upward or top down" 80 46 | 47 | public: 48 | CSignalSidus(void); 49 | ~CSignalSidus(void); 50 | //--- methods of setting adjustable parameters 51 | void NumberOpenPosition(int value) { m_numberOpenPosition=value; } 52 | //--- methods of adjusting "weights" of market models 53 | void Pattern_0(int value) { m_pattern_0=value; } 54 | void Pattern_1(int value) { m_pattern_1=value; } 55 | void Pattern_2(int value) { m_pattern_2=value; } 56 | //--- method of verification of settings 57 | virtual bool ValidationSettings(void); 58 | //--- method of creating the indicator and timeseries 59 | virtual bool InitIndicators(CIndicators *indicators); 60 | //--- methods of checking if the market models are formed 61 | virtual int LongCondition(void); 62 | virtual int ShortCondition(void); 63 | protected: 64 | //--- method of initialization of the indicator 65 | bool InitMA(CIndicators *indicators); 66 | }; 67 | 68 | CSignalSidus::CSignalSidus(void) : m_numberOpenPosition(5), 69 | m_pattern_0(80), 70 | m_pattern_1(10), 71 | m_pattern_2(80) 72 | { 73 | //--- initialization of protected data 74 | m_used_series=USE_SERIES_OPEN+USE_SERIES_HIGH+USE_SERIES_LOW+USE_SERIES_CLOSE; 75 | } 76 | //+------------------------------------------------------------------+ 77 | //| Destructor | 78 | //+------------------------------------------------------------------+ 79 | CSignalSidus::~CSignalSidus(void) 80 | { 81 | } 82 | //+------------------------------------------------------------------+ 83 | //| Validation settings protected data. | 84 | //+------------------------------------------------------------------+ 85 | bool CSignalSidus::ValidationSettings(void) 86 | { 87 | //--- validation settings of additional filters 88 | if(!CExpertSignal::ValidationSettings()) 89 | return(false); 90 | 91 | //--- ok 92 | return(true); 93 | } 94 | //+------------------------------------------------------------------+ 95 | //| Create indicators. | 96 | //+------------------------------------------------------------------+ 97 | bool CSignalSidus::InitIndicators(CIndicators *indicators) 98 | { 99 | //--- check pointer 100 | if(indicators==NULL) 101 | return(false); 102 | //--- initialization of indicators and timeseries of additional filters 103 | if(!CExpertSignal::InitIndicators(indicators)) 104 | return(false); 105 | //--- create and initialize MA indicator 106 | if(!InitMA(indicators)) 107 | return(false); 108 | //--- ok 109 | return(true); 110 | } 111 | //+------------------------------------------------------------------+ 112 | //| Initialize MA indicators. | 113 | //+------------------------------------------------------------------+ 114 | bool CSignalSidus::InitMA(CIndicators *indicators) 115 | { 116 | //--- check pointer 117 | if(indicators==NULL) 118 | return(false); 119 | //--- add object to collection 120 | if(!indicators.Add(GetPointer(m_ma18))) 121 | { 122 | printf(__FUNCTION__+": error adding object"); 123 | return(false); 124 | } 125 | //--- initialize object 126 | if(!m_ma18.Create(m_symbol.Name(),m_period,18,0,MODE_EMA,PRICE_WEIGHTED)) 127 | { 128 | printf(__FUNCTION__+": error initializing object"); 129 | return(false); 130 | } 131 | //--- add object to collection 132 | if(!indicators.Add(GetPointer(m_ma28))) 133 | { 134 | printf(__FUNCTION__+": error adding object"); 135 | return(false); 136 | } 137 | //--- initialize object 138 | if(!m_ma28.Create(m_symbol.Name(),m_period,28,0,MODE_EMA,PRICE_WEIGHTED)) 139 | { 140 | printf(__FUNCTION__+": error initializing object"); 141 | return(false); 142 | } 143 | //--- add object to collection 144 | if(!indicators.Add(GetPointer(m_ma5))) 145 | { 146 | printf(__FUNCTION__+": error adding object"); 147 | return(false); 148 | } 149 | //--- initialize object 150 | if(!m_ma5.Create(m_symbol.Name(),m_period,5,0,MODE_LWMA,PRICE_WEIGHTED)) 151 | { 152 | printf(__FUNCTION__+": error initializing object"); 153 | return(false); 154 | } 155 | //--- add object to collection 156 | if(!indicators.Add(GetPointer(m_ma8))) 157 | { 158 | printf(__FUNCTION__+": error adding object"); 159 | return(false); 160 | } 161 | //--- initialize object 162 | if(!m_ma8.Create(m_symbol.Name(),m_period,8,0,MODE_LWMA,PRICE_WEIGHTED)) 163 | { 164 | printf(__FUNCTION__+": error initializing object"); 165 | return(false); 166 | } 167 | //--- ok 168 | return(true); 169 | } 170 | //+------------------------------------------------------------------+ 171 | //| "Voting" that price will grow. | 172 | //+------------------------------------------------------------------+ 173 | int CSignalSidus::LongCondition(void) 174 | { 175 | int result=0; 176 | int idx=StartIndex(); 177 | if(m_ma5.Main(idx)>m_ma8.Main(idx)&&m_ma8.Main(idx)>m_ma18.Main(idx)&&m_ma8.Main(idx)>m_ma28.Main(idx)){ 178 | bool flagCross1=false; 179 | bool flagCross2=false; 180 | for (int i=(idx+1);im_ma8.Main(idx)){ 191 | bool flagCross=false; 192 | for (int i=(idx+1);im_ma28.Main(idx)){ 200 | bool flagCross=false; 201 | for (int i=(idx+1);im_ma18.Main(i)&&m_ma5.Main(i)>m_ma28.Main(i)){ 222 | flagCross1=true; 223 | } 224 | if(m_ma8.Main(i)>m_ma18.Main(i)&&m_ma8.Main(i)>m_ma28.Main(i)){ 225 | flagCross2=true; 226 | }} 227 | if(flagCross1==true&&flagCross2==true){ 228 | result=m_pattern_0; 229 | }} 230 | if(m_ma5.Main(idx)m_ma8.Main(i)){ 234 | flagCross=true; 235 | }} 236 | if(flagCross==true){ 237 | result=m_pattern_1; 238 | }} 239 | if(m_ma18.Main(idx)m_ma28.Main(i)){ 243 | flagCross=true; 244 | }} 245 | if(flagCross==true){ 246 | result=m_pattern_2; 247 | }} 248 | return result; 249 | } 250 | -------------------------------------------------------------------------------- /Creating Indicator based on Modules of Expert Trading Signals/ExpertInd.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| ExpertInd.mqh | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | 10 | #include 11 | //+------------------------------------------------------------------+ 12 | //| | 13 | //+------------------------------------------------------------------+ 14 | class CExpertInd : public CExpert 15 | { 16 | public: 17 | virtual bool RefreshInd(void); 18 | }; 19 | //+------------------------------------------------------------------+ 20 | //| Refreshing data for processing | 21 | //+------------------------------------------------------------------+ 22 | bool CExpertInd::RefreshInd(void) 23 | { 24 | MqlDateTime time; 25 | //--- refresh rates 26 | if(!m_symbol.RefreshRates()) 27 | return(false); 28 | //--- check need processing 29 | TimeToStruct(m_symbol.Time(),time); 30 | if(m_period_flags!=WRONG_VALUE && m_period_flags!=0) 31 | if((m_period_flags&TimeframesFlags(time))==0) 32 | return(false); 33 | m_last_tick_time=time; 34 | //--- refresh indicators 35 | m_indicators.Refresh(); 36 | //--- ok 37 | return(true); 38 | } 39 | -------------------------------------------------------------------------------- /Creating Indicator based on Modules of Expert Trading Signals/IndSignals.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| IndSignals.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | #property indicator_chart_window 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #property indicator_buffers 2 16 | #property indicator_plots 1 17 | #property indicator_type1 DRAW_COLOR_LINE 18 | #property indicator_color1 clrBlack,clrRed,clrLawnGreen 19 | #property indicator_style1 STYLE_SOLID 20 | #property indicator_width1 2 21 | double InBuffer[]; 22 | double ColorBuffer[]; 23 | int bars_calculated=0; 24 | 25 | input int Signal_ThresholdOpen =20; // Signal threshold value to open [0...100] 26 | input int Signal_ThresholdClose =20; // Signal threshold value to close [0...100] 27 | 28 | input int Signal_MACD_PeriodFast =12; // MACD(12,24,9,PRICE_CLOSE) Period of fast EMA 29 | input int Signal_MACD_PeriodSlow =24; // MACD(12,24,9,PRICE_CLOSE) Period of slow EMA 30 | input int Signal_MACD_PeriodSignal =9; // MACD(12,24,9,PRICE_CLOSE) Period of averaging of difference 31 | input ENUM_APPLIED_PRICE Signal_MACD_Applied =PRICE_CLOSE; // MACD(12,24,9,PRICE_CLOSE) Prices series 32 | input double Signal_MACD_Weight =1.0; // MACD(12,24,9,PRICE_CLOSE) Weight [0...1.0] 33 | input int Signal_MA_PeriodMA =12; // Moving Average(12,0,...) Period of averaging 34 | input int Signal_MA_Shift =0; // Moving Average(12,0,...) Time shift 35 | input ENUM_MA_METHOD Signal_MA_Method =MODE_SMA; // Moving Average(12,0,...) Method of averaging 36 | input ENUM_APPLIED_PRICE Signal_MA_Applied =PRICE_CLOSE; // Moving Average(12,0,...) Prices series 37 | input double Signal_MA_Weight =1.0; // Moving Average(12,0,...) Weight [0...1.0] 38 | 39 | CExpertInd ExtExpert; 40 | CSignalMAInd *filter0 = new CSignalMAInd; 41 | CSignalMACDInd *filter1 = new CSignalMACDInd; 42 | 43 | //+------------------------------------------------------------------+ 44 | //| Custom indicator initialization function | 45 | //+------------------------------------------------------------------+ 46 | int OnInit() 47 | { 48 | 49 | //--- Initializing expert 50 | if(!ExtExpert.Init(Symbol(),Period(),true,100)) 51 | { 52 | //--- failed 53 | printf(__FUNCTION__+": error initializing expert"); 54 | ExtExpert.Deinit(); 55 | return(INIT_FAILED); 56 | } 57 | //--- Creating signal 58 | CExpertSignal *signal=new CExpertSignal; 59 | if(signal==NULL) 60 | { 61 | //--- failed 62 | printf(__FUNCTION__+": error creating signal"); 63 | ExtExpert.Deinit(); 64 | return(INIT_FAILED); 65 | } 66 | //--- 67 | ExtExpert.InitSignal(signal); 68 | 69 | filter0.PeriodMA(Signal_MA_PeriodMA); 70 | filter0.Shift(Signal_MA_Shift); 71 | filter0.Method(Signal_MA_Method); 72 | filter0.Applied(Signal_MA_Applied); 73 | 74 | filter1.PeriodFast(Signal_MACD_PeriodFast); 75 | filter1.PeriodSlow(Signal_MACD_PeriodSlow); 76 | filter1.PeriodSignal(Signal_MACD_PeriodSignal); 77 | filter1.Applied(Signal_MACD_Applied); 78 | 79 | signal.AddFilter(filter0); 80 | signal.AddFilter(filter1); 81 | if(!ExtExpert.ValidationSettings()) 82 | { 83 | //--- failed 84 | ExtExpert.Deinit(); 85 | return(INIT_FAILED); 86 | } 87 | //--- Tuning of all necessary indicators 88 | if(!ExtExpert.InitIndicators()) 89 | { 90 | //--- failed 91 | printf(__FUNCTION__+": error initializing indicators"); 92 | ExtExpert.Deinit(); 93 | return(INIT_FAILED); 94 | } 95 | //--- ok 96 | //--- indicator buffers mapping 97 | SetIndexBuffer(0,InBuffer,INDICATOR_DATA); 98 | SetIndexBuffer(1,ColorBuffer,INDICATOR_COLOR_INDEX); 99 | 100 | ArraySetAsSeries(InBuffer,true); 101 | ArraySetAsSeries(ColorBuffer,true); 102 | 103 | 104 | //--- 105 | return(INIT_SUCCEEDED); 106 | } 107 | //+------------------------------------------------------------------+ 108 | //| Custom indicator iteration function | 109 | //+------------------------------------------------------------------+ 110 | int OnCalculate(const int rates_total, 111 | const int prev_calculated, 112 | const datetime &time[], 113 | const double &open[], 114 | const double &high[], 115 | const double &low[], 116 | const double &close[], 117 | const long &tick_volume[], 118 | const long &volume[], 119 | const int &spread[]) 120 | { 121 | int values_to_copy; 122 | int calculated=MathMin(filter0.BarsCalculatedInd(), filter1.BarsCalculatedInd()); 123 | if(calculated<=0) 124 | { 125 | PrintFormat("BarsCalculated() return %d, error code %d",calculated,GetLastError()); 126 | return(0); 127 | } 128 | 129 | if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 130 | { 131 | if(calculated>rates_total) values_to_copy=rates_total; 132 | else values_to_copy=calculated; 133 | } 134 | else 135 | { 136 | values_to_copy=(rates_total-prev_calculated)+1; 137 | } 138 | 139 | bars_calculated=calculated; 140 | 141 | ArraySetAsSeries(open,true); 142 | 143 | ExtExpert.RefreshInd(); 144 | 145 | if(values_to_copy>1) 146 | { 147 | 148 | for (int i=0; i=Signal_ThresholdOpen) 158 | { 159 | ColorBuffer[i]=2; 160 | } 161 | 162 | if(-result>=Signal_ThresholdOpen){ 163 | ColorBuffer[i]=1; 164 | } 165 | } 166 | } 167 | //--- return value of prev_calculated for next call 168 | return(rates_total); 169 | } 170 | //+------------------------------------------------------------------+ 171 | -------------------------------------------------------------------------------- /Creating Indicator based on Modules of Expert Trading Signals/SignalMACDInd.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| SignalMACDInd.mqh | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | 10 | #include 11 | #include 12 | //+------------------------------------------------------------------+ 13 | //| | 14 | //+------------------------------------------------------------------+ 15 | class CSignalMACDInd : public CSignalMACD 16 | { 17 | public: 18 | virtual int BarsCalculatedInd(); 19 | virtual int LongConditionInd(int ind); 20 | virtual int ShortConditionInd(int ind); 21 | 22 | }; 23 | //+------------------------------------------------------------------+ 24 | int CSignalMACDInd:: BarsCalculatedInd(){ 25 | return m_MACD.BarsCalculated(); 26 | } 27 | 28 | //+------------------------------------------------------------------+ 29 | //| "Voting" that price will grow. | 30 | //+------------------------------------------------------------------+ 31 | int CSignalMACDInd::LongConditionInd(int idx) 32 | { 33 | int result=0; 34 | 35 | //--- check direction of the main line 36 | if(DiffMain(idx)>0.0) 37 | { 38 | //--- the main line is directed upwards, and it confirms the possibility of price growth 39 | if(IS_PATTERN_USAGE(0)) 40 | result=m_pattern_0; // "confirming" signal number 0 41 | //--- if the model 1 is used, look for a reverse of the main line 42 | if(IS_PATTERN_USAGE(1) && DiffMain(idx+1)<0.0) 43 | result=m_pattern_1; // signal number 1 44 | //--- if the model 2 is used, look for an intersection of the main and signal line 45 | if(IS_PATTERN_USAGE(2) && State(idx)>0.0 && State(idx+1)<0.0) 46 | result=m_pattern_2; // signal number 2 47 | //--- if the model 3 is used, look for an intersection of the main line and the zero level 48 | if(IS_PATTERN_USAGE(3) && Main(idx)>0.0 && Main(idx+1)<0.0) 49 | result=m_pattern_3; // signal number 3 50 | //--- if the models 4 or 5 are used and the main line turned upwards below the zero level, look for divergences 51 | if((IS_PATTERN_USAGE(4) || IS_PATTERN_USAGE(5)) && Main(idx)<0.0) 52 | { 53 | //--- perform the extended analysis of the oscillator state 54 | ExtState(idx); 55 | //--- if the model 4 is used, look for the "divergence" signal 56 | if(IS_PATTERN_USAGE(4) && CompareMaps(1,1)) // 0000 0001b 57 | result=m_pattern_4; // signal number 4 58 | //--- if the model 5 is used, look for the "double divergence" signal 59 | if(IS_PATTERN_USAGE(5) && CompareMaps(0x11,2)) // 0001 0001b 60 | return(m_pattern_5); // signal number 5 61 | } 62 | } 63 | //--- return the result 64 | return(result); 65 | } 66 | //+------------------------------------------------------------------+ 67 | //| "Voting" that price will fall. | 68 | //+------------------------------------------------------------------+ 69 | int CSignalMACDInd::ShortConditionInd(int idx) 70 | { 71 | int result=0; 72 | 73 | //--- check direction of the main line 74 | if(DiffMain(idx)<0.0) 75 | { 76 | //--- main line is directed downwards, confirming a possibility of falling of price 77 | if(IS_PATTERN_USAGE(0)) 78 | result=m_pattern_0; // "confirming" signal number 0 79 | //--- if the model 1 is used, look for a reverse of the main line 80 | if(IS_PATTERN_USAGE(1) && DiffMain(idx+1)>0.0) 81 | result=m_pattern_1; // signal number 1 82 | //--- if the model 2 is used, look for an intersection of the main and signal line 83 | if(IS_PATTERN_USAGE(2) && State(idx)<0.0 && State(idx+1)>0.0) 84 | result=m_pattern_2; // signal number 2 85 | //--- if the model 3 is used, look for an intersection of the main line and the zero level 86 | if(IS_PATTERN_USAGE(3) && Main(idx)<0.0 && Main(idx+1)>0.0) 87 | result=m_pattern_3; // signal number 3 88 | //--- if the models 4 or 5 are used and the main line turned downwards above the zero level, look for divergences 89 | if((IS_PATTERN_USAGE(4) || IS_PATTERN_USAGE(5)) && Main(idx)>0.0) 90 | { 91 | //--- perform the extended analysis of the oscillator state 92 | ExtState(idx); 93 | //--- if the model 4 is used, look for the "divergence" signal 94 | if(IS_PATTERN_USAGE(4) && CompareMaps(1,1)) // 0000 0001b 95 | result=m_pattern_4; // signal number 4 96 | //--- if the model 5 is used, look for the "double divergence" signal 97 | if(IS_PATTERN_USAGE(5) && CompareMaps(0x11,2)) // 0001 0001b 98 | return(m_pattern_5); // signal number 5 99 | } 100 | } 101 | //--- return the result 102 | return(result); 103 | } 104 | //+------------------------------------------------------------------+ 105 | -------------------------------------------------------------------------------- /Creating Indicator based on Modules of Expert Trading Signals/SignalMAInd.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| SignalMAInd .mqh | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | 10 | #include 11 | //+------------------------------------------------------------------+ 12 | //| | 13 | //+------------------------------------------------------------------+ 14 | class CSignalMAInd : public CSignalMA 15 | { 16 | public: 17 | virtual int BarsCalculatedInd(); 18 | virtual int LongConditionInd(int ind); 19 | virtual int ShortConditionInd(int ind); 20 | }; 21 | //+------------------------------------------------------------------+ 22 | int CSignalMAInd:: BarsCalculatedInd(){ 23 | return m_ma.BarsCalculated(); 24 | } 25 | 26 | //+------------------------------------------------------------------+ 27 | //| "Voting" that price will grow. | 28 | //+------------------------------------------------------------------+ 29 | int CSignalMAInd::LongConditionInd(int idx) 30 | { 31 | 32 | int result=0; 33 | 34 | //--- analyze positional relationship of the close price and the indicator at the first analyzed bar 35 | if(DiffCloseMA(idx)<0.0) 36 | { 37 | //--- the close price is below the indicator 38 | if(IS_PATTERN_USAGE(1) && DiffOpenMA(idx)>0.0 && DiffMA(idx)>0.0) 39 | { 40 | //--- the open price is above the indicator (i.e. there was an intersection), but the indicator is directed upwards 41 | result=m_pattern_1; 42 | //--- consider that this is an unformed "piercing" and suggest to enter the market at the current price 43 | m_base_price=0.0; 44 | } 45 | } 46 | else 47 | { 48 | //--- the close price is above the indicator (the indicator has no objections to buying) 49 | if(IS_PATTERN_USAGE(0)) 50 | result=m_pattern_0; 51 | //--- if the indicator is directed upwards 52 | if(DiffMA(idx)>0.0) 53 | { 54 | if(DiffOpenMA(idx)<0.0) 55 | { 56 | //--- if the model 2 is used 57 | if(IS_PATTERN_USAGE(2)) 58 | { 59 | //--- the open price is below the indicator (i.e. there was an intersection) 60 | result=m_pattern_2; 61 | //--- suggest to enter the market at the "roll back" 62 | m_base_price=m_symbol.NormalizePrice(MA(idx)); 63 | } 64 | } 65 | else 66 | { 67 | //--- if the model 3 is used and the open price is above the indicator 68 | if(IS_PATTERN_USAGE(3) && DiffLowMA(idx)<0.0) 69 | { 70 | //--- the low price is below the indicator 71 | result=m_pattern_3; 72 | //--- consider that this is a formed "piercing" and suggest to enter the market at the current price 73 | m_base_price=0.0; 74 | } 75 | } 76 | } 77 | } 78 | //--- return the result 79 | return(result); 80 | } 81 | //+------------------------------------------------------------------+ 82 | //| "Voting" that price will fall. | 83 | //+------------------------------------------------------------------+ 84 | int CSignalMAInd::ShortConditionInd(int idx) 85 | { 86 | int result=0; 87 | 88 | //--- analyze positional relationship of the close price and the indicator at the first analyzed bar 89 | if(DiffCloseMA(idx)>0.0) 90 | { 91 | //--- the close price is above the indicator 92 | if(IS_PATTERN_USAGE(1) && DiffOpenMA(idx)<0.0 && DiffMA(idx)<0.0) 93 | { 94 | //--- the open price is below the indicator (i.e. there was an intersection), but the indicator is directed downwards 95 | result=m_pattern_1; 96 | //--- consider that this is an unformed "piercing" and suggest to enter the market at the current price 97 | m_base_price=0.0; 98 | } 99 | } 100 | else 101 | { 102 | //--- the close price is below the indicator (the indicator has no objections to buying) 103 | if(IS_PATTERN_USAGE(0)) 104 | result=m_pattern_0; 105 | //--- the indicator is directed downwards 106 | if(DiffMA(idx)<0.0) 107 | { 108 | if(DiffOpenMA(idx)>0.0) 109 | { 110 | //--- if the model 2 is used 111 | if(IS_PATTERN_USAGE(2)) 112 | { 113 | //--- the open price is above the indicator (i.e. there was an intersection) 114 | result=m_pattern_2; 115 | //--- suggest to enter the market at the "roll back" 116 | m_base_price=m_symbol.NormalizePrice(MA(idx)); 117 | } 118 | } 119 | else 120 | { 121 | //--- if the model 3 is used and the open price is below the indicator 122 | if(IS_PATTERN_USAGE(3) && DiffHighMA(idx)>0.0) 123 | { 124 | //--- the high price is above the indicator 125 | result=m_pattern_3; 126 | //--- consider that this is a formed "piercing" and suggest to enter the market at the current price 127 | m_base_price=0.0; 128 | } 129 | } 130 | } 131 | } 132 | //--- return the result 133 | return(result); 134 | } 135 | //+------------------------------------------------------------------+ 136 | -------------------------------------------------------------------------------- /Example of Creating Advisor using OOP/CheckTrade.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| CheckTrade.mqh | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | //+------------------------------------------------------------------+ 10 | //| | 11 | //+------------------------------------------------------------------+ 12 | class CheckTrade 13 | { 14 | private: 15 | 16 | public: 17 | CheckTrade(); 18 | ~CheckTrade(); 19 | int OnCheckTradeInit(double lot); 20 | int OnCheckTradeTick(double lot, double spread); 21 | }; 22 | //+------------------------------------------------------------------+ 23 | //| | 24 | //+------------------------------------------------------------------+ 25 | CheckTrade::CheckTrade() 26 | { 27 | } 28 | //+------------------------------------------------------------------+ 29 | //| | 30 | //+------------------------------------------------------------------+ 31 | CheckTrade::~CheckTrade() 32 | { 33 | } 34 | //+------------------------------------------------------------------+ 35 | int CheckTrade::OnCheckTradeInit(double lot){ 36 | if((ENUM_ACCOUNT_TRADE_MODE)AccountInfoInteger(ACCOUNT_TRADE_MODE)==ACCOUNT_TRADE_MODE_REAL){ 37 | int mb=MessageBox("Run the advisor on a real account?","Message Box",MB_YESNO|MB_ICONQUESTION); 38 | if(mb==IDNO) return(0); 39 | } 40 | if(!TerminalInfoInteger(TERMINAL_CONNECTED)){ 41 | Alert("No connection to the trade server"); 42 | return(0); 43 | }else{ 44 | if(!AccountInfoInteger(ACCOUNT_TRADE_ALLOWED)){ 45 | Alert("Trade for this account is prohibited"); 46 | return(0); 47 | } 48 | } 49 | if(!AccountInfoInteger(ACCOUNT_TRADE_EXPERT)){ 50 | Alert("Trade with the help of experts for the account is prohibited"); 51 | return(0); 52 | } 53 | if(lotSymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX)){ 54 | Alert("Lot is not correct!!!"); 55 | return(0); 56 | } 57 | return(INIT_SUCCEEDED); 58 | 59 | } 60 | 61 | int CheckTrade::OnCheckTradeTick(double lot,double spread){ 62 | if(!TerminalInfoInteger(TERMINAL_CONNECTED)){ 63 | Alert("No connection to the trade server"); 64 | return(0); 65 | } 66 | if (!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED)){ 67 | Alert("Auto Trade Permission Off!"); 68 | return(0); 69 | } 70 | if(!MQLInfoInteger(MQL_TRADE_ALLOWED)){ 71 | Alert("Automatic trading is prohibited in the properties of the expert ",__FILE__); 72 | return(0); 73 | } 74 | if((ENUM_ACCOUNT_STOPOUT_MODE)AccountInfoInteger(ACCOUNT_MARGIN_SO_MODE)==ACCOUNT_STOPOUT_MODE_PERCENT){ 75 | if(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL)!=0&&AccountInfoDouble(ACCOUNT_MARGIN_LEVEL) 76 | <=AccountInfoDouble(ACCOUNT_MARGIN_SO_CALL)){ 77 | Alert("Margin Call!!!"); 78 | return(0); 79 | }} 80 | if((ENUM_ACCOUNT_STOPOUT_MODE)AccountInfoInteger(ACCOUNT_MARGIN_SO_MODE)==ACCOUNT_STOPOUT_MODE_MONEY){ 81 | if(AccountInfoDouble(ACCOUNT_EQUITY)<=AccountInfoDouble(ACCOUNT_MARGIN_SO_CALL)){ 82 | Alert("Margin Call!!!"); 83 | return(0); 84 | }} 85 | if((ENUM_ACCOUNT_STOPOUT_MODE)AccountInfoInteger(ACCOUNT_MARGIN_SO_MODE)==ACCOUNT_STOPOUT_MODE_PERCENT){ 86 | if(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL)!=0&&AccountInfoDouble(ACCOUNT_MARGIN_LEVEL) 87 | <=AccountInfoDouble(ACCOUNT_MARGIN_SO_SO)){ 88 | Alert("Stop Out!!!"); 89 | return(0); 90 | }} 91 | if((ENUM_ACCOUNT_STOPOUT_MODE)AccountInfoInteger(ACCOUNT_MARGIN_SO_MODE)==ACCOUNT_STOPOUT_MODE_MONEY){ 92 | if(AccountInfoDouble(ACCOUNT_EQUITY)<=AccountInfoDouble(ACCOUNT_MARGIN_SO_SO)){ 93 | Alert("Stop Out!!!"); 94 | return(0); 95 | }} 96 | double margin; 97 | MqlTick last_tick; 98 | ResetLastError(); 99 | if(SymbolInfoTick(Symbol(),last_tick)) 100 | { 101 | if(OrderCalcMargin(ORDER_TYPE_BUY,Symbol(),lot,last_tick.ask,margin)) 102 | { 103 | if(margin>AccountInfoDouble(ACCOUNT_MARGIN_FREE)){ 104 | Alert("Not enough money in the account!"); 105 | return(0); 106 | }} 107 | }else{ 108 | Print(GetLastError()); 109 | } 110 | double _spread= 111 | SymbolInfoInteger(Symbol(),SYMBOL_SPREAD)*MathPow(10,-SymbolInfoInteger(Symbol(),SYMBOL_DIGITS))/MathPow(10,-4); 112 | if(_spread>spread){ 113 | Alert("Too big spread!"); 114 | return(0); 115 | } 116 | if((ENUM_SYMBOL_TRADE_MODE)SymbolInfoInteger(Symbol(),SYMBOL_TRADE_MODE)!=SYMBOL_TRADE_MODE_FULL){ 117 | Alert("Restrictions on trading operations!"); 118 | return(0); 119 | } 120 | if(Bars(Symbol(), 0)<100) 121 | { 122 | Alert("In the chart little bars, Expert will not work!!"); 123 | return(0); 124 | } 125 | 126 | return(1); 127 | } 128 | -------------------------------------------------------------------------------- /Example of Creating Advisor using OOP/ExpSidusOOP.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| ExpSidusOOP.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | 10 | #include "CheckTrade.mqh" 11 | #include "Trade.mqh" 12 | #include "Sidus.mqh" 13 | 14 | input double Lot=1; 15 | input int EA_Magic=1000; 16 | input double spreadLevel=10.0; 17 | input double StopLoss=0.01; 18 | input double Profit=0.01; 19 | input int numberBarOpenPosition=5; 20 | input int numberBarStopPosition=5; 21 | 22 | CheckTrade checkTrade; 23 | Trade trade(StopLoss,Profit,Lot); 24 | Sidus sidus(numberBarOpenPosition,numberBarStopPosition); 25 | 26 | bool flagStopLoss=false; 27 | //+------------------------------------------------------------------+ 28 | //| Expert initialization function | 29 | //+------------------------------------------------------------------+ 30 | int OnInit() 31 | { 32 | return(checkTrade.OnCheckTradeInit(Lot)); 33 | } 34 | //+------------------------------------------------------------------+ 35 | //| Expert deinitialization function | 36 | //+------------------------------------------------------------------+ 37 | void OnDeinit(const int reason) 38 | { 39 | } 40 | //+------------------------------------------------------------------+ 41 | //| Expert tick function | 42 | //+------------------------------------------------------------------+ 43 | void OnTick() 44 | { 45 | if(!checkTrade.OnCheckTradeTick(Lot,spreadLevel)){ 46 | return; 47 | } 48 | static datetime last_time; 49 | datetime last_bar_time=(datetime)SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE); 50 | if(last_time!=last_bar_time) 51 | { 52 | last_time=last_bar_time; 53 | }else{ 54 | return; 55 | } 56 | static datetime last_time_daily; 57 | datetime last_bar_time_daily=(datetime)SeriesInfoInteger(Symbol(),PERIOD_D1,SERIES_LASTBAR_DATE); 58 | if(last_time_daily!=last_bar_time_daily) 59 | { 60 | last_time_daily=last_bar_time_daily; 61 | flagStopLoss=false; 62 | } 63 | 64 | if(flagStopLoss==true)return; 65 | 66 | int num=5; 67 | if(numberBarOpenPosition>numberBarStopPosition)num=numberBarOpenPosition; 68 | if(numberBarOpenPosition<=numberBarStopPosition)num=numberBarStopPosition; 69 | MqlRates mrate[]; 70 | ResetLastError(); 71 | if(CopyRates(Symbol(),Period(),0,num,mrate)<0) 72 | { 73 | Print(GetLastError()); 74 | return; 75 | } 76 | 77 | ArraySetAsSeries(mrate,true); 78 | //----------------------------------------------------------------------------- 79 | bool TradeSignalBuy=false; 80 | bool TradeSignalSell=false; 81 | TradeSignalBuy=sidus.OnTradeSignalBuy(); 82 | TradeSignalSell=sidus.OnTradeSignalSell(); 83 | bool TradeSignalBuyStop=false; 84 | bool TradeSignalSellStop=false; 85 | TradeSignalBuyStop=sidus.OnTradeSignalBuyStop(mrate); 86 | TradeSignalSellStop=sidus.OnTradeSignalSellStop(mrate); 87 | trade.Order(TradeSignalBuy,TradeSignalBuyStop,TradeSignalSell,TradeSignalSellStop); 88 | } 89 | //+------------------------------------------------------------------+ 90 | //| Trade function | 91 | //+------------------------------------------------------------------+ 92 | void OnTrade() 93 | { 94 | static int _deals; 95 | ulong _ticket=0; 96 | 97 | if(HistorySelect(0,TimeCurrent())) 98 | { 99 | int i=HistoryDealsTotal()-1; 100 | 101 | if(_deals!=i) { 102 | _deals=i; 103 | } else { return; } 104 | 105 | if((_ticket=HistoryDealGetTicket(i))>0) 106 | { 107 | string _comment=HistoryDealGetString(_ticket,DEAL_COMMENT); 108 | if(StringFind(_comment,"sl",0)!=-1) { 109 | flagStopLoss=true; 110 | } 111 | } 112 | } 113 | } 114 | //-------------------------------------------------- 115 | 116 | -------------------------------------------------------------------------------- /Example of Creating Advisor using OOP/Sidus.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| Sidus.mqh | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | //+------------------------------------------------------------------+ 10 | //| | 11 | //+------------------------------------------------------------------+ 12 | class Sidus 13 | { 14 | private: 15 | int numberBarOpenPosition; 16 | int numberBarStopPosition; 17 | int handleIMA18; 18 | double MA18Buffer[]; 19 | int handleIMA28; 20 | double MA28Buffer[]; 21 | int handleIWMA5; 22 | double WMA5Buffer[]; 23 | int handleIWMA8; 24 | double WMA8Buffer[]; 25 | 26 | public: 27 | Sidus(int BarOpenPosition, int BarStopPosition); 28 | ~Sidus(); 29 | bool OnTradeSignalBuy(); 30 | bool OnTradeSignalBuyStop(MqlRates& mrate[]); 31 | bool OnTradeSignalSell(); 32 | bool OnTradeSignalSellStop(MqlRates& mrate[]); 33 | }; 34 | //+------------------------------------------------------------------+ 35 | Sidus::Sidus(int BarOpenPosition, int BarStopPosition) 36 | { 37 | numberBarOpenPosition=BarOpenPosition; 38 | numberBarStopPosition=BarStopPosition; 39 | handleIMA18=iMA(_Symbol,PERIOD_H1,18,0,MODE_EMA,PRICE_WEIGHTED); 40 | handleIMA28=iMA(_Symbol,PERIOD_H1,28,0,MODE_EMA,PRICE_WEIGHTED); 41 | handleIWMA5=iMA(_Symbol,PERIOD_H1,5,0,MODE_LWMA,PRICE_WEIGHTED); 42 | handleIWMA8=iMA(_Symbol,PERIOD_H1,8,0,MODE_LWMA,PRICE_WEIGHTED); 43 | } 44 | //+------------------------------------------------------------------+ 45 | //| | 46 | //+------------------------------------------------------------------+ 47 | Sidus::~Sidus() 48 | { 49 | } 50 | //+------------------------------------------------------------------+ 51 | bool Sidus::OnTradeSignalBuy(){ 52 | 53 | bool flagBuy=false; 54 | 55 | if(CopyBuffer(handleIMA18,0,0,numberBarOpenPosition,MA18Buffer)<0) 56 | { 57 | return false; 58 | } 59 | 60 | if(CopyBuffer(handleIMA28,0,0,numberBarOpenPosition,MA28Buffer)<0) 61 | { 62 | return false; 63 | } 64 | 65 | if(CopyBuffer(handleIWMA5,0,0,numberBarOpenPosition,WMA5Buffer)<0) 66 | { 67 | return false; 68 | } 69 | 70 | if(CopyBuffer(handleIWMA8,0,0,numberBarOpenPosition,WMA8Buffer)<0) 71 | { 72 | return false; 73 | } 74 | ArraySetAsSeries(MA18Buffer,true); 75 | ArraySetAsSeries(MA28Buffer,true); 76 | ArraySetAsSeries(WMA5Buffer,true); 77 | 78 | bool flagCross1=false; 79 | bool flagCross2=false; 80 | bool flagCross=false; 81 | 82 | if(WMA5Buffer[1]>MA18Buffer[1]&&WMA5Buffer[1]>MA28Buffer[1]&&WMA8Buffer[1]>MA18Buffer[1]&&WMA8Buffer[1]>MA28Buffer[1]){ 83 | for (int i=2;iWMA8Buffer[i]){ 125 | flagCross=true; 126 | } 127 | } 128 | } 129 | 130 | double max=mrate[1].high; 131 | 132 | for (int i=1;imax)max=mrate[i].high; 134 | } 135 | 136 | if(flagCross==true&&mrate[1].high<=max&&mrate[numberBarStopPosition-1].high<=max){ 137 | flagBuyStop=true; 138 | } 139 | 140 | return flagBuyStop; 141 | } 142 | 143 | bool Sidus::OnTradeSignalSell(){ 144 | 145 | bool flagSell=false; 146 | 147 | if(CopyBuffer(handleIMA18,0,0,numberBarOpenPosition,MA18Buffer)<0) 148 | { 149 | return false; 150 | } 151 | 152 | if(CopyBuffer(handleIMA28,0,0,numberBarOpenPosition,MA28Buffer)<0) 153 | { 154 | return false; 155 | } 156 | 157 | if(CopyBuffer(handleIWMA5,0,0,numberBarOpenPosition,WMA5Buffer)<0) 158 | { 159 | return false; 160 | } 161 | 162 | if(CopyBuffer(handleIWMA8,0,0,numberBarOpenPosition,WMA8Buffer)<0) 163 | { 164 | return false; 165 | } 166 | 167 | ArraySetAsSeries(MA18Buffer,true); 168 | ArraySetAsSeries(MA28Buffer,true); 169 | ArraySetAsSeries(WMA5Buffer,true); 170 | ArraySetAsSeries(WMA8Buffer,true); 171 | 172 | bool flagCross1=false; 173 | bool flagCross2=false; 174 | bool flagCross=false; 175 | 176 | if(WMA5Buffer[1]MA18Buffer[i]&&WMA5Buffer[i]>MA28Buffer[i]){ 179 | flagCross1=true; 180 | } 181 | if(WMA8Buffer[i]>MA18Buffer[i]&&WMA8Buffer[i]>MA28Buffer[i]){ 182 | flagCross2=true; 183 | } 184 | } 185 | if(flagCross1==true&&flagCross2==true){ 186 | flagCross=true; 187 | } 188 | } 189 | 190 | if(flagCross==true){ 191 | flagSell=true; 192 | } 193 | return flagSell; 194 | } 195 | 196 | bool Sidus::OnTradeSignalSellStop(MqlRates& mrate[]){ 197 | 198 | bool flagSellStop=false; 199 | 200 | if(CopyBuffer(handleIWMA5,0,0,numberBarStopPosition,WMA5Buffer)<0) 201 | { 202 | return false; 203 | } 204 | 205 | if(CopyBuffer(handleIWMA8,0,0,numberBarStopPosition,WMA8Buffer)<0) 206 | { 207 | return false; 208 | } 209 | 210 | ArraySetAsSeries(WMA5Buffer,true); 211 | ArraySetAsSeries(WMA8Buffer,true); 212 | 213 | bool flagCross=false; 214 | 215 | if(WMA5Buffer[1]>WMA8Buffer[1]){ 216 | for (int i=2;i=min&&mrate[numberBarStopPosition-1].low>=min){ 230 | flagSellStop=true; 231 | } 232 | 233 | return flagSellStop; 234 | } 235 | -------------------------------------------------------------------------------- /Example of Creating Indicator/Impulse keeper +.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| Impulse keeper.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | #property indicator_chart_window 10 | 11 | #property indicator_buffers 4 12 | 13 | double EMA34HBuffer[]; 14 | double EMA34LBuffer[]; 15 | double EMA125Buffer[]; 16 | double PSARBuffer[]; 17 | 18 | int EMA34HHandle; 19 | int EMA34LHandle; 20 | int EMA125Handle; 21 | int PSARHandle; 22 | 23 | int bars_calculated=0; 24 | //+------------------------------------------------------------------+ 25 | //| Custom indicator initialization function | 26 | //+------------------------------------------------------------------+ 27 | int OnInit() 28 | { 29 | //--- indicator buffers mapping 30 | SetIndexBuffer(0,EMA34HBuffer,INDICATOR_CALCULATIONS); 31 | SetIndexBuffer(1,EMA34LBuffer,INDICATOR_CALCULATIONS); 32 | SetIndexBuffer(2,EMA125Buffer,INDICATOR_CALCULATIONS); 33 | SetIndexBuffer(3,PSARBuffer,INDICATOR_CALCULATIONS); 34 | 35 | EMA34HHandle=iMA(NULL,0,34,0,MODE_EMA,PRICE_HIGH); 36 | EMA34LHandle=iMA(NULL,0,34,0,MODE_EMA,PRICE_LOW); 37 | EMA125Handle=iMA(NULL,0,125,0,MODE_EMA,PRICE_CLOSE); 38 | PSARHandle=iSAR(NULL,0,0.02, 0.2); 39 | //--- 40 | return(INIT_SUCCEEDED); 41 | } 42 | //+------------------------------------------------------------------+ 43 | //| Custom indicator iteration function | 44 | //+------------------------------------------------------------------+ 45 | int OnCalculate(const int rates_total, 46 | const int prev_calculated, 47 | const datetime &time[], 48 | const double &open[], 49 | const double &high[], 50 | const double &low[], 51 | const double &close[], 52 | const long &tick_volume[], 53 | const long &volume[], 54 | const int &spread[]) 55 | { 56 | //--- 57 | int values_to_copy; 58 | int start; 59 | int calculated=BarsCalculated(EMA34HHandle); 60 | if(calculated<=0) 61 | { 62 | return(0); 63 | } 64 | if(prev_calculated==0 || calculated!=bars_calculated) 65 | { 66 | start=1; 67 | if(calculated>rates_total) values_to_copy=rates_total; 68 | else values_to_copy=calculated; 69 | } 70 | else 71 | { 72 | start=rates_total-1; 73 | values_to_copy=1; 74 | } 75 | 76 | if(!FillArrayFromMABuffer(EMA34HBuffer,0,EMA34HHandle,values_to_copy)) return(0); 77 | if(!FillArrayFromMABuffer(EMA34LBuffer,0,EMA34LHandle,values_to_copy)) return(0); 78 | if(!FillArrayFromMABuffer(EMA125Buffer,0,EMA125Handle,values_to_copy)) return(0); 79 | if(!FillArrayFromPSARBuffer(PSARBuffer,PSARHandle,values_to_copy)) return(0); 80 | 81 | for(int i=start;iopen[i-1]&&close[i-1]>EMA34HBuffer[i-1]&&close[i-1]>EMA34LBuffer[i-1] 84 | &&low[i-1]>EMA125Buffer[i-1]&&low[i-1]>PSARBuffer[i-1]&&EMA125Buffer[i-1]EMA34LBuffer[i-1] 100 | &&EMA125Buffer[i-1]>EMA34HBuffer[i-1]){ 101 | 102 | if(!ObjectCreate(0,"Sell"+i,OBJ_ARROW,0,time[i-1],low[i-1])) 103 | { 104 | return(false); 105 | } 106 | ObjectSetInteger(0,"Sell"+i,OBJPROP_COLOR,clrRed); 107 | ObjectSetInteger(0,"Sell"+i,OBJPROP_ARROWCODE,234); 108 | ObjectSetInteger(0,"Sell"+i,OBJPROP_WIDTH,2); 109 | ObjectSetInteger(0,"Sell"+i,OBJPROP_ANCHOR,ANCHOR_LOWER); 110 | ObjectSetInteger(0,"Sell"+i,OBJPROP_HIDDEN,true); 111 | ObjectSetString(0,"Sell"+i,OBJPROP_TOOLTIP,""+close[i-1]); 112 | } 113 | } 114 | bars_calculated=calculated; 115 | //--- return value of prev_calculated for next call 116 | return(rates_total); 117 | } 118 | 119 | void OnDeinit(const int reason){ 120 | ObjectsDeleteAll(0,-1,-1); 121 | } 122 | 123 | 124 | //+------------------------------------------------------------------+ 125 | bool FillArrayFromPSARBuffer( 126 | double &sar_buffer[], // Parabolic SAR indicator's value buffer 127 | int ind_handle, // iSAR indicator's handle 128 | int amount // number of values to be copied 129 | ) 130 | { 131 | ResetLastError(); 132 | if(CopyBuffer(ind_handle,0,0,amount,sar_buffer)<0) 133 | { 134 | return(false); 135 | } 136 | return(true); 137 | } 138 | //+------------------------------------------------------------------+ 139 | bool FillArrayFromMABuffer( 140 | double &values[], // indicator's buffer of Moving Average values 141 | int shift, // shift 142 | int ind_handle, // iMA indicator's handle 143 | int amount // number of values to be copied 144 | ) 145 | { 146 | ResetLastError(); 147 | if(CopyBuffer(ind_handle,0,-shift,amount,values)<0) 148 | { 149 | return(false); 150 | } 151 | return(true); 152 | } 153 | -------------------------------------------------------------------------------- /Example of Creating Indicator/Impulse keeper.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| Impulse keeper.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | #property indicator_chart_window 10 | 11 | #property indicator_buffers 8 12 | #property indicator_plots 2 13 | #property indicator_color1 clrGreen, clrBlack 14 | #property indicator_type1 DRAW_COLOR_ARROW 15 | #property indicator_color2 clrRed, clrBlack 16 | #property indicator_type2 DRAW_COLOR_ARROW 17 | 18 | double IKBuyBuffer[]; 19 | double ColorIKBuyBuffer[]; 20 | double IKSellBuffer[]; 21 | double ColorIKSellBuffer[]; 22 | double EMA34HBuffer[]; 23 | double EMA34LBuffer[]; 24 | double EMA125Buffer[]; 25 | double PSARBuffer[]; 26 | 27 | int EMA34HHandle; 28 | int EMA34LHandle; 29 | int EMA125Handle; 30 | int PSARHandle; 31 | 32 | int bars_calculated=0; 33 | //+------------------------------------------------------------------+ 34 | //| Custom indicator initialization function | 35 | //+------------------------------------------------------------------+ 36 | int OnInit() 37 | { 38 | PlotIndexSetInteger(0,PLOT_ARROW,233); 39 | PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0); 40 | PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-10); 41 | 42 | PlotIndexSetInteger(1,PLOT_ARROW,234); 43 | PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0); 44 | PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,10); 45 | //--- indicator buffers mapping 46 | SetIndexBuffer(0,IKBuyBuffer,INDICATOR_DATA); 47 | SetIndexBuffer(1,ColorIKBuyBuffer,INDICATOR_COLOR_INDEX); 48 | 49 | SetIndexBuffer(2,IKSellBuffer,INDICATOR_DATA); 50 | SetIndexBuffer(3,ColorIKSellBuffer,INDICATOR_COLOR_INDEX); 51 | 52 | SetIndexBuffer(4,EMA34HBuffer,INDICATOR_CALCULATIONS); 53 | SetIndexBuffer(5,EMA34LBuffer,INDICATOR_CALCULATIONS); 54 | SetIndexBuffer(6,EMA125Buffer,INDICATOR_CALCULATIONS); 55 | SetIndexBuffer(7,PSARBuffer,INDICATOR_CALCULATIONS); 56 | 57 | EMA34HHandle=iMA(NULL,0,34,0,MODE_EMA,PRICE_HIGH); 58 | EMA34LHandle=iMA(NULL,0,34,0,MODE_EMA,PRICE_LOW); 59 | EMA125Handle=iMA(NULL,0,125,0,MODE_EMA,PRICE_CLOSE); 60 | PSARHandle=iSAR(NULL,0,0.02, 0.2); 61 | 62 | //--- 63 | return(INIT_SUCCEEDED); 64 | } 65 | //+------------------------------------------------------------------+ 66 | //| Custom indicator iteration function | 67 | //+------------------------------------------------------------------+ 68 | int OnCalculate(const int rates_total, 69 | const int prev_calculated, 70 | const datetime &time[], 71 | const double &open[], 72 | const double &high[], 73 | const double &low[], 74 | const double &close[], 75 | const long &tick_volume[], 76 | const long &volume[], 77 | const int &spread[]) 78 | { 79 | //--- 80 | int values_to_copy; 81 | int start; 82 | int calculated=BarsCalculated(EMA34HHandle); 83 | if(calculated<=0) 84 | { 85 | return(0); 86 | } 87 | if(prev_calculated==0 || calculated!=bars_calculated) 88 | { 89 | start=1; 90 | if(calculated>rates_total) values_to_copy=rates_total; 91 | else values_to_copy=calculated; 92 | } 93 | else 94 | { 95 | start=rates_total-1; 96 | values_to_copy=1; 97 | } 98 | 99 | if(!FillArrayFromMABuffer(EMA34HBuffer,0,EMA34HHandle,values_to_copy)) return(0); 100 | 101 | if(!FillArrayFromMABuffer(EMA34LBuffer,0,EMA34LHandle,values_to_copy)) return(0); 102 | 103 | if(!FillArrayFromMABuffer(EMA125Buffer,0,EMA125Handle,values_to_copy)) return(0); 104 | 105 | if(!FillArrayFromPSARBuffer(PSARBuffer,PSARHandle,values_to_copy)) return(0); 106 | 107 | for(int i=start;iopen[i-1]&&close[i-1]>EMA34HBuffer[i-1]&&close[i-1]>EMA34LBuffer[i-1] 116 | &&low[i-1]>EMA125Buffer[i-1]&&low[i-1]>PSARBuffer[i-1]&&EMA125Buffer[i-1]EMA34LBuffer[i-1] 124 | &&EMA125Buffer[i-1]>EMA34HBuffer[i-1]){ 125 | IKSellBuffer[i-1]=low[i-1]; 126 | ColorIKSellBuffer[i-1]=0; 127 | } 128 | } 129 | 130 | Print(calculated+" "+bars_calculated); 131 | 132 | bars_calculated=calculated; 133 | //--- return value of prev_calculated for next call 134 | return(rates_total); 135 | } 136 | 137 | //+------------------------------------------------------------------+ 138 | bool FillArrayFromPSARBuffer( 139 | double &sar_buffer[], // Parabolic SAR indicator's value buffer 140 | int ind_handle, // iSAR indicator's handle 141 | int amount // number of values to be copied 142 | ) 143 | { 144 | ResetLastError(); 145 | if(CopyBuffer(ind_handle,0,0,amount,sar_buffer)<0) 146 | { 147 | return(false); 148 | } 149 | return(true); 150 | } 151 | //+------------------------------------------------------------------+ 152 | bool FillArrayFromMABuffer( 153 | double &values[], // indicator's buffer of Moving Average values 154 | int shift, // shift 155 | int ind_handle, // iMA indicator's handle 156 | int amount // number of values to be copied 157 | ) 158 | { 159 | ResetLastError(); 160 | if(CopyBuffer(ind_handle,0,-shift,amount,values)<0) 161 | { 162 | return(false); 163 | } 164 | return(true); 165 | } 166 | -------------------------------------------------------------------------------- /Genetic Algorithms/part1/ExpertInd.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| ExpertInd.mqh | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | 10 | #include 11 | //+------------------------------------------------------------------+ 12 | //| | 13 | //+------------------------------------------------------------------+ 14 | class CExpertInd : public CExpert 15 | { 16 | public: 17 | virtual bool RefreshInd(void); 18 | }; 19 | //+------------------------------------------------------------------+ 20 | //| Refreshing data for processing | 21 | //+------------------------------------------------------------------+ 22 | bool CExpertInd::RefreshInd(void) 23 | { 24 | MqlDateTime time; 25 | //--- refresh rates 26 | if(!m_symbol.RefreshRates()) 27 | return(false); 28 | //--- check need processing 29 | TimeToStruct(m_symbol.Time(),time); 30 | if(m_period_flags!=WRONG_VALUE && m_period_flags!=0) 31 | if((m_period_flags&TimeframesFlags(time))==0) 32 | return(false); 33 | m_last_tick_time=time; 34 | //--- refresh indicators 35 | m_indicators.Refresh(); 36 | //--- ok 37 | return(true); 38 | } 39 | -------------------------------------------------------------------------------- /Genetic Algorithms/part1/GenScript.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| GenScript.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | #include 10 | #include 11 | 12 | double ReplicationPortion_E = 100.0; 13 | double NMutationPortion_E = 10.0; 14 | double ArtificialMutation_E = 10.0; 15 | double GenoMergingPortion_E = 20.0; 16 | double CrossingOverPortion_E = 20.0; 17 | //--- 18 | double ReplicationOffset_E = 0.5; 19 | double NMutationProbability_E= 5.0; 20 | 21 | //--- inputs for main signal 22 | input int Signal_ThresholdOpen =20; // Signal threshold value to open [0...100] 23 | input int Signal_ThresholdClose =20; // Signal threshold value to close [0...100] 24 | input double Signal_PriceLevel =0.0; // Price level to execute a deal 25 | input double Signal_StopLevel =50.0; // Stop Loss level (in points) 26 | input double Signal_TakeLevel =50.0; // Take Profit level (in points) 27 | input int Signal_Expiration =4; // Expiration of pending orders (in bars) 28 | input int Signal_MA_PeriodMA =12; // Moving Average(12,0,...) Period of averaging 29 | input int Signal_MA_Shift =0; // Moving Average(12,0,...) Time shift 30 | input ENUM_MA_METHOD Signal_MA_Method =MODE_SMA; // Moving Average(12,0,...) Method of averaging 31 | input ENUM_APPLIED_PRICE Signal_MA_Applied =PRICE_CLOSE; // Moving Average(12,0,...) Prices series 32 | input double Signal_MA_Weight =1.0; // Moving Average(12,0,...) Weight [0...1.0] 33 | input int Signal_MACD_PeriodFast =12; // MACD(12,24,9,PRICE_CLOSE) Period of fast EMA 34 | input int Signal_MACD_PeriodSlow =24; // MACD(12,24,9,PRICE_CLOSE) Period of slow EMA 35 | input int Signal_MACD_PeriodSignal=9; // MACD(12,24,9,PRICE_CLOSE) Period of averaging of difference 36 | input ENUM_APPLIED_PRICE Signal_MACD_Applied =PRICE_CLOSE; // MACD(12,24,9,PRICE_CLOSE) Prices series 37 | input double Signal_MACD_Weight =1.0; // MACD(12,24,9,PRICE_CLOSE) Weight [0...1.0] 38 | 39 | //+------------------------------------------------------------------+ 40 | //| Script program start function | 41 | //+------------------------------------------------------------------+ 42 | void OnStart() 43 | { 44 | //--- 45 | ChromosomeCount = 10; 46 | GeneCount = 2; 47 | Epoch = 50; 48 | //--- 49 | RangeMinimum = 0.0; 50 | RangeMaximum = 1.0; 51 | Precision = 0.1; 52 | OptimizeMethod = 2; 53 | ArrayResize(Chromosome,GeneCount+1); 54 | ArrayInitialize(Chromosome,0); 55 | 56 | int time_start=(int)GetTickCount(),time_end=0; 57 | //--------------------------------------------------------------------- 58 | 59 | UGA 60 | ( 61 | ReplicationPortion_E, 62 | NMutationPortion_E, 63 | ArtificialMutation_E, 64 | GenoMergingPortion_E, 65 | CrossingOverPortion_E, 66 | //--- 67 | ReplicationOffset_E, 68 | NMutationProbability_E 69 | ); 70 | //---------------------------------- 71 | time_end=(int)GetTickCount(); 72 | //---------------------------------- 73 | Print(time_end-time_start); 74 | //---------------------------------- 75 | 76 | } 77 | //+------------------------------------------------------------------+ -------------------------------------------------------------------------------- /Genetic Algorithms/part1/IndSignals.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| IndSignals.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | #property indicator_chart_window 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #property indicator_buffers 2 16 | #property indicator_plots 1 17 | #property indicator_type1 DRAW_COLOR_LINE 18 | #property indicator_color1 clrBlack,clrRed,clrLawnGreen 19 | #property indicator_style1 STYLE_SOLID 20 | #property indicator_width1 2 21 | 22 | double InBuffer[]; 23 | double ColorBuffer[]; 24 | int bars_calculated=0; 25 | 26 | input int Signal_ThresholdOpen =20; // Signal threshold value to open [0...100] 27 | input int Signal_ThresholdClose =20; // Signal threshold value to close [0...100] 28 | 29 | input int Signal_MACD_PeriodFast =12; // MACD(12,24,9,PRICE_CLOSE) Period of fast EMA 30 | input int Signal_MACD_PeriodSlow =24; // MACD(12,24,9,PRICE_CLOSE) Period of slow EMA 31 | input int Signal_MACD_PeriodSignal =9; // MACD(12,24,9,PRICE_CLOSE) Period of averaging of difference 32 | input ENUM_APPLIED_PRICE Signal_MACD_Applied =PRICE_CLOSE; // MACD(12,24,9,PRICE_CLOSE) Prices series 33 | input double Signal_MACD_Weight =1.0; // MACD(12,24,9,PRICE_CLOSE) Weight [0...1.0] 34 | input int Signal_MA_PeriodMA =12; // Moving Average(12,0,...) Period of averaging 35 | input int Signal_MA_Shift =0; // Moving Average(12,0,...) Time shift 36 | input ENUM_MA_METHOD Signal_MA_Method =MODE_SMA; // Moving Average(12,0,...) Method of averaging 37 | input ENUM_APPLIED_PRICE Signal_MA_Applied =PRICE_CLOSE; // Moving Average(12,0,...) Prices series 38 | input double Signal_MA_Weight =1.0; // Moving Average(12,0,...) Weight [0...1.0] 39 | 40 | CExpertInd ExtExpert; 41 | CSignalMAInd *filter0 = new CSignalMAInd; 42 | CSignalMACDInd *filter1 = new CSignalMACDInd; 43 | 44 | //+------------------------------------------------------------------+ 45 | //| Custom indicator initialization function | 46 | //+------------------------------------------------------------------+ 47 | int OnInit() 48 | { 49 | 50 | //--- Initializing expert 51 | if(!ExtExpert.Init(Symbol(),Period(),true,100)) 52 | { 53 | //--- failed 54 | printf(__FUNCTION__+": error initializing expert"); 55 | ExtExpert.Deinit(); 56 | return(INIT_FAILED); 57 | } 58 | //--- Creating signal 59 | CExpertSignal *signal=new CExpertSignal; 60 | if(signal==NULL) 61 | { 62 | //--- failed 63 | printf(__FUNCTION__+": error creating signal"); 64 | ExtExpert.Deinit(); 65 | return(INIT_FAILED); 66 | } 67 | //--- 68 | ExtExpert.InitSignal(signal); 69 | 70 | filter0.PeriodMA(Signal_MA_PeriodMA); 71 | filter0.Shift(Signal_MA_Shift); 72 | filter0.Method(Signal_MA_Method); 73 | filter0.Applied(Signal_MA_Applied); 74 | 75 | filter1.PeriodFast(Signal_MACD_PeriodFast); 76 | filter1.PeriodSlow(Signal_MACD_PeriodSlow); 77 | filter1.PeriodSignal(Signal_MACD_PeriodSignal); 78 | filter1.Applied(Signal_MACD_Applied); 79 | 80 | signal.AddFilter(filter0); 81 | signal.AddFilter(filter1); 82 | if(!ExtExpert.ValidationSettings()) 83 | { 84 | //--- failed 85 | ExtExpert.Deinit(); 86 | return(INIT_FAILED); 87 | } 88 | //--- Tuning of all necessary indicators 89 | if(!ExtExpert.InitIndicators()) 90 | { 91 | //--- failed 92 | printf(__FUNCTION__+": error initializing indicators"); 93 | ExtExpert.Deinit(); 94 | return(INIT_FAILED); 95 | } 96 | //--- ok 97 | //--- indicator buffers mapping 98 | SetIndexBuffer(0,InBuffer,INDICATOR_DATA); 99 | SetIndexBuffer(1,ColorBuffer,INDICATOR_COLOR_INDEX); 100 | 101 | ArraySetAsSeries(InBuffer,true); 102 | ArraySetAsSeries(ColorBuffer,true); 103 | 104 | 105 | //--- 106 | return(INIT_SUCCEEDED); 107 | } 108 | //+------------------------------------------------------------------+ 109 | //| Custom indicator iteration function | 110 | //+------------------------------------------------------------------+ 111 | int OnCalculate(const int rates_total, 112 | const int prev_calculated, 113 | const datetime &time[], 114 | const double &open[], 115 | const double &high[], 116 | const double &low[], 117 | const double &close[], 118 | const long &tick_volume[], 119 | const long &volume[], 120 | const int &spread[]) 121 | { 122 | int values_to_copy; 123 | int calculated=MathMin(filter0.BarsCalculatedInd(), filter1.BarsCalculatedInd()); 124 | if(calculated<=0) 125 | { 126 | PrintFormat("BarsCalculated() returns %d, error code %d",calculated,GetLastError()); 127 | return(0); 128 | } 129 | 130 | if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 131 | { 132 | 133 | if(calculated>rates_total) values_to_copy=rates_total; 134 | else values_to_copy=calculated; 135 | } 136 | else 137 | { 138 | 139 | values_to_copy=(rates_total-prev_calculated)+1; 140 | } 141 | 142 | bars_calculated=calculated; 143 | 144 | ArraySetAsSeries(open,true); 145 | 146 | if(values_to_copy>1) 147 | { 148 | 149 | ExtExpert.RefreshInd(); 150 | 151 | bool flagBuy=false; 152 | bool flagSell=false; 153 | double priceBuy=0; 154 | double priceStopBuy=0; 155 | double profitBuy=0; 156 | double profitTotalBuy=0; 157 | int countProfitBuyPlus=0; 158 | int countProfitBuyMinus=0; 159 | double priceSell=0; 160 | double priceStopSell=0; 161 | double profitSell=0; 162 | double profitTotalSell=0; 163 | int countProfitSellPlus=0; 164 | int countProfitSellMinus=0; 165 | double sp=0.0002; 166 | 167 | for (int i=0; i=Signal_ThresholdOpen) 177 | { 178 | ColorBuffer[i]=2; 179 | if(flagSell==true){ 180 | flagSell=false; 181 | priceStopSell=InBuffer[i]; 182 | profitSell=(priceStopSell-priceSell-sp)*10000; 183 | if(profitSell>0){countProfitSellPlus++;}else{countProfitSellMinus++;} 184 | profitTotalSell=profitTotalSell+profitSell; 185 | } 186 | if(flagBuy==false){ 187 | flagBuy=true; 188 | priceBuy=InBuffer[i]; 189 | } 190 | } 191 | 192 | if(-result>=Signal_ThresholdOpen){ 193 | ColorBuffer[i]=1; 194 | if(flagBuy==true){ 195 | flagBuy=false; 196 | priceStopBuy=InBuffer[i]; 197 | profitBuy=(priceStopBuy-priceBuy-sp)*10000; 198 | if(profitBuy>0){countProfitBuyPlus++;}else{countProfitBuyMinus++;} 199 | profitTotalBuy=profitTotalBuy+profitBuy; 200 | } 201 | if(flagSell==false){ 202 | priceSell=InBuffer[i]; 203 | flagSell=true; 204 | } 205 | } 206 | } 207 | //Print(" ProfitBuy ", profitTotalBuy," countProfitBuyPlus ",countProfitBuyPlus," countProfitBuyMinus ",countProfitBuyMinus); 208 | //Print(" ProfitSell ", profitTotalSell," countProfitSellPlus ",countProfitSellPlus," countProfitSellMinus ",countProfitSellMinus); 209 | 210 | } 211 | //--- return value of prev_calculated for next call 212 | return(rates_total); 213 | } 214 | //+------------------------------------------------------------------+ -------------------------------------------------------------------------------- /Genetic Algorithms/part1/MAMACDFitness.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| MAMACDFitness.mqh | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | 7 | void FitnessFunction(int chromos) 8 | { 9 | 10 | double _MACD_Weight=0.0; 11 | double _MA_Weight=0.0; 12 | double sum=0.0; 13 | int cnt=1; 14 | 15 | while (cnt<=GeneCount) 16 | { 17 | _MACD_Weight=Colony[cnt][chromos]; 18 | cnt++; 19 | _MA_Weight=Colony[cnt][chromos]; 20 | cnt++; 21 | 22 | int handleInd; 23 | double BufferInd[]; 24 | double BufferColorInd[]; 25 | 26 | 27 | handleInd=iCustom(NULL,0,"IndSignals", 28 | Signal_ThresholdOpen, 29 | Signal_ThresholdClose, 30 | Signal_MACD_PeriodFast, 31 | Signal_MACD_PeriodSlow, 32 | Signal_MACD_PeriodSignal, 33 | Signal_MACD_Applied, 34 | _MACD_Weight, 35 | Signal_MA_PeriodMA, 36 | Signal_MA_Shift, 37 | Signal_MA_Method, 38 | Signal_MA_Applied, 39 | _MA_Weight 40 | ); 41 | 42 | ResetLastError(); 43 | 44 | int size=5000; 45 | 46 | if(CopyBuffer(handleInd,0,0,size,BufferInd)<0) 47 | { 48 | PrintFormat("Error code %d",GetLastError()); 49 | 50 | } 51 | 52 | if(CopyBuffer(handleInd,1,0,size,BufferColorInd)<0) 53 | { 54 | PrintFormat("Error code %d",GetLastError()); 55 | 56 | } 57 | ArraySetAsSeries(BufferInd,true); 58 | ArraySetAsSeries(BufferColorInd,true); 59 | 60 | bool flagBuy=false; 61 | bool flagSell=false; 62 | double priceBuy=0; 63 | double priceStopBuy=0; 64 | double profitBuy=0; 65 | double profitTotalBuy=0; 66 | int countProfitBuyPlus=0; 67 | int countProfitBuyMinus=0; 68 | double priceSell=0; 69 | double priceStopSell=0; 70 | double profitSell=0; 71 | double profitTotalSell=0; 72 | int countProfitSellPlus=0; 73 | int countProfitSellMinus=0; 74 | double sp=0.0002; 75 | 76 | for (int i=0; i0){countProfitSellPlus++;}else{countProfitSellMinus++;} 85 | profitTotalSell=profitTotalSell+profitSell; 86 | } 87 | if(flagBuy==false){ 88 | flagBuy=true; 89 | priceBuy=BufferInd[i]; 90 | } 91 | } 92 | 93 | if(BufferColorInd[i]==1){ 94 | if(flagBuy==true){ 95 | flagBuy=false; 96 | priceStopBuy=BufferInd[i]; 97 | profitBuy=(priceStopBuy-priceBuy-sp)*10000; 98 | if(profitBuy>0){countProfitBuyPlus++;}else{countProfitBuyMinus++;} 99 | profitTotalBuy=profitTotalBuy+profitBuy; 100 | } 101 | if(flagSell==false){ 102 | priceSell=BufferInd[i]; 103 | flagSell=true; 104 | } 105 | } 106 | } 107 | //Print(" ProfitBuy ", profitTotalBuy," countProfitBuyPlus ",countProfitBuyPlus," countProfitBuyMinus ",countProfitBuyMinus); 108 | //Print(" ProfitSell ", profitTotalSell," countProfitSellPlus ",countProfitSellPlus," countProfitSellMinus ",countProfitSellMinus); 109 | 110 | sum = profitTotalBuy + profitTotalSell; 111 | } 112 | AmountStartsFF++; 113 | Colony[0][chromos]=sum; 114 | } 115 | 116 | void ServiceFunction() 117 | { 118 | 119 | double _MACD_Weight=0.0; 120 | double _MA_Weight=0.0; 121 | int cnt=1; 122 | 123 | while (cnt<=GeneCount) 124 | { 125 | _MACD_Weight=Chromosome[cnt]; 126 | cnt++; 127 | _MA_Weight=Chromosome[cnt]; 128 | cnt++; 129 | } 130 | Print("Fitness func =",Chromosome[0],"\n", 131 | "The resulting argument values:","\n", 132 | "_MACD_Weight =",Chromosome[1],"\n", 133 | "_MA_Weight =",Chromosome[2],"\n" 134 | ); 135 | 136 | } 137 | //——————————————————————————————————————————————————————————————————— -------------------------------------------------------------------------------- /Genetic Algorithms/part1/SignalMACDInd.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| SignalMACDInd.mqh | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | 10 | #include 11 | #include 12 | //+------------------------------------------------------------------+ 13 | //| | 14 | //+------------------------------------------------------------------+ 15 | class CSignalMACDInd : public CSignalMACD 16 | { 17 | public: 18 | virtual int BarsCalculatedInd(); 19 | virtual int LongConditionInd(int ind); 20 | virtual int ShortConditionInd(int ind); 21 | 22 | }; 23 | //+------------------------------------------------------------------+ 24 | int CSignalMACDInd:: BarsCalculatedInd(){ 25 | return m_MACD.BarsCalculated(); 26 | } 27 | 28 | //+------------------------------------------------------------------+ 29 | //| "Voting" that price will grow. | 30 | //+------------------------------------------------------------------+ 31 | int CSignalMACDInd::LongConditionInd(int idx) 32 | { 33 | int result=0; 34 | 35 | //--- check direction of the main line 36 | if(DiffMain(idx)>0.0) 37 | { 38 | //--- the main line is directed upwards, and it confirms the possibility of price growth 39 | if(IS_PATTERN_USAGE(0)) 40 | result=m_pattern_0; // "confirming" signal number 0 41 | //--- if the model 1 is used, look for a reverse of the main line 42 | if(IS_PATTERN_USAGE(1) && DiffMain(idx+1)<0.0) 43 | result=m_pattern_1; // signal number 1 44 | //--- if the model 2 is used, look for an intersection of the main and signal line 45 | if(IS_PATTERN_USAGE(2) && State(idx)>0.0 && State(idx+1)<0.0) 46 | result=m_pattern_2; // signal number 2 47 | //--- if the model 3 is used, look for an intersection of the main line and the zero level 48 | if(IS_PATTERN_USAGE(3) && Main(idx)>0.0 && Main(idx+1)<0.0) 49 | result=m_pattern_3; // signal number 3 50 | //--- if the models 4 or 5 are used and the main line turned upwards below the zero level, look for divergences 51 | if((IS_PATTERN_USAGE(4) || IS_PATTERN_USAGE(5)) && Main(idx)<0.0) 52 | { 53 | //--- perform the extended analysis of the oscillator state 54 | ExtState(idx); 55 | //--- if the model 4 is used, look for the "divergence" signal 56 | if(IS_PATTERN_USAGE(4) && CompareMaps(1,1)) // 0000 0001b 57 | result=m_pattern_4; // signal number 4 58 | //--- if the model 5 is used, look for the "double divergence" signal 59 | if(IS_PATTERN_USAGE(5) && CompareMaps(0x11,2)) // 0001 0001b 60 | return(m_pattern_5); // signal number 5 61 | } 62 | } 63 | //--- return the result 64 | return(result); 65 | } 66 | //+------------------------------------------------------------------+ 67 | //| "Voting" that price will fall. | 68 | //+------------------------------------------------------------------+ 69 | int CSignalMACDInd::ShortConditionInd(int idx) 70 | { 71 | int result=0; 72 | 73 | //--- check direction of the main line 74 | if(DiffMain(idx)<0.0) 75 | { 76 | //--- main line is directed downwards, confirming a possibility of falling of price 77 | if(IS_PATTERN_USAGE(0)) 78 | result=m_pattern_0; // "confirming" signal number 0 79 | //--- if the model 1 is used, look for a reverse of the main line 80 | if(IS_PATTERN_USAGE(1) && DiffMain(idx+1)>0.0) 81 | result=m_pattern_1; // signal number 1 82 | //--- if the model 2 is used, look for an intersection of the main and signal line 83 | if(IS_PATTERN_USAGE(2) && State(idx)<0.0 && State(idx+1)>0.0) 84 | result=m_pattern_2; // signal number 2 85 | //--- if the model 3 is used, look for an intersection of the main line and the zero level 86 | if(IS_PATTERN_USAGE(3) && Main(idx)<0.0 && Main(idx+1)>0.0) 87 | result=m_pattern_3; // signal number 3 88 | //--- if the models 4 or 5 are used and the main line turned downwards above the zero level, look for divergences 89 | if((IS_PATTERN_USAGE(4) || IS_PATTERN_USAGE(5)) && Main(idx)>0.0) 90 | { 91 | //--- perform the extended analysis of the oscillator state 92 | ExtState(idx); 93 | //--- if the model 4 is used, look for the "divergence" signal 94 | if(IS_PATTERN_USAGE(4) && CompareMaps(1,1)) // 0000 0001b 95 | result=m_pattern_4; // signal number 4 96 | //--- if the model 5 is used, look for the "double divergence" signal 97 | if(IS_PATTERN_USAGE(5) && CompareMaps(0x11,2)) // 0001 0001b 98 | return(m_pattern_5); // signal number 5 99 | } 100 | } 101 | //--- return the result 102 | return(result); 103 | } 104 | //+------------------------------------------------------------------+ 105 | -------------------------------------------------------------------------------- /Genetic Algorithms/part1/SignalMAInd.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| SignalMAInd .mqh | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | 10 | #include 11 | //+------------------------------------------------------------------+ 12 | //| | 13 | //+------------------------------------------------------------------+ 14 | class CSignalMAInd : public CSignalMA 15 | { 16 | public: 17 | virtual int BarsCalculatedInd(); 18 | virtual int LongConditionInd(int ind); 19 | virtual int ShortConditionInd(int ind); 20 | }; 21 | //+------------------------------------------------------------------+ 22 | int CSignalMAInd:: BarsCalculatedInd(){ 23 | return m_ma.BarsCalculated(); 24 | } 25 | 26 | //+------------------------------------------------------------------+ 27 | //| "Voting" that price will grow. | 28 | //+------------------------------------------------------------------+ 29 | int CSignalMAInd::LongConditionInd(int idx) 30 | { 31 | 32 | int result=0; 33 | 34 | //--- analyze positional relationship of the close price and the indicator at the first analyzed bar 35 | if(DiffCloseMA(idx)<0.0) 36 | { 37 | //--- the close price is below the indicator 38 | if(IS_PATTERN_USAGE(1) && DiffOpenMA(idx)>0.0 && DiffMA(idx)>0.0) 39 | { 40 | //--- the open price is above the indicator (i.e. there was an intersection), but the indicator is directed upwards 41 | result=m_pattern_1; 42 | //--- consider that this is an unformed "piercing" and suggest to enter the market at the current price 43 | m_base_price=0.0; 44 | } 45 | } 46 | else 47 | { 48 | //--- the close price is above the indicator (the indicator has no objections to buying) 49 | if(IS_PATTERN_USAGE(0)) 50 | result=m_pattern_0; 51 | //--- if the indicator is directed upwards 52 | if(DiffMA(idx)>0.0) 53 | { 54 | if(DiffOpenMA(idx)<0.0) 55 | { 56 | //--- if the model 2 is used 57 | if(IS_PATTERN_USAGE(2)) 58 | { 59 | //--- the open price is below the indicator (i.e. there was an intersection) 60 | result=m_pattern_2; 61 | //--- suggest to enter the market at the "roll back" 62 | m_base_price=m_symbol.NormalizePrice(MA(idx)); 63 | } 64 | } 65 | else 66 | { 67 | //--- if the model 3 is used and the open price is above the indicator 68 | if(IS_PATTERN_USAGE(3) && DiffLowMA(idx)<0.0) 69 | { 70 | //--- the low price is below the indicator 71 | result=m_pattern_3; 72 | //--- consider that this is a formed "piercing" and suggest to enter the market at the current price 73 | m_base_price=0.0; 74 | } 75 | } 76 | } 77 | } 78 | //--- return the result 79 | return(result); 80 | } 81 | //+------------------------------------------------------------------+ 82 | //| "Voting" that price will fall. | 83 | //+------------------------------------------------------------------+ 84 | int CSignalMAInd::ShortConditionInd(int idx) 85 | { 86 | int result=0; 87 | 88 | //--- analyze positional relationship of the close price and the indicator at the first analyzed bar 89 | if(DiffCloseMA(idx)>0.0) 90 | { 91 | //--- the close price is above the indicator 92 | if(IS_PATTERN_USAGE(1) && DiffOpenMA(idx)<0.0 && DiffMA(idx)<0.0) 93 | { 94 | //--- the open price is below the indicator (i.e. there was an intersection), but the indicator is directed downwards 95 | result=m_pattern_1; 96 | //--- consider that this is an unformed "piercing" and suggest to enter the market at the current price 97 | m_base_price=0.0; 98 | } 99 | } 100 | else 101 | { 102 | //--- the close price is below the indicator (the indicator has no objections to buying) 103 | if(IS_PATTERN_USAGE(0)) 104 | result=m_pattern_0; 105 | //--- the indicator is directed downwards 106 | if(DiffMA(idx)<0.0) 107 | { 108 | if(DiffOpenMA(idx)>0.0) 109 | { 110 | //--- if the model 2 is used 111 | if(IS_PATTERN_USAGE(2)) 112 | { 113 | //--- the open price is above the indicator (i.e. there was an intersection) 114 | result=m_pattern_2; 115 | //--- suggest to enter the market at the "roll back" 116 | m_base_price=m_symbol.NormalizePrice(MA(idx)); 117 | } 118 | } 119 | else 120 | { 121 | //--- if the model 3 is used and the open price is below the indicator 122 | if(IS_PATTERN_USAGE(3) && DiffHighMA(idx)>0.0) 123 | { 124 | //--- the high price is above the indicator 125 | result=m_pattern_3; 126 | //--- consider that this is a formed "piercing" and suggest to enter the market at the current price 127 | m_base_price=0.0; 128 | } 129 | } 130 | } 131 | } 132 | //--- return the result 133 | return(result); 134 | } 135 | //+------------------------------------------------------------------+ 136 | -------------------------------------------------------------------------------- /Genetic Algorithms/part2/GenScript.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| GenScript.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | #include 10 | #include 11 | 12 | double ReplicationPortion_E = 100.0; 13 | double NMutationPortion_E = 10.0; 14 | double ArtificialMutation_E = 10.0; 15 | double GenoMergingPortion_E = 20.0; 16 | double CrossingOverPortion_E = 20.0; 17 | //--- 18 | double ReplicationOffset_E = 0.5; 19 | double NMutationProbability_E= 5.0; 20 | 21 | //--- inputs for main signal 22 | input int Signal_ThresholdOpen =20; // Signal threshold value to open [0...100] 23 | input int Signal_ThresholdClose =20; // Signal threshold value to close [0...100] 24 | input double Signal_PriceLevel =0.0; // Price level to execute a deal 25 | input double Signal_StopLevel =50.0; // Stop Loss level (in points) 26 | input double Signal_TakeLevel =50.0; // Take Profit level (in points) 27 | input int Signal_Expiration =4; // Expiration of pending orders (in bars) 28 | input int Signal_MA_PeriodMA =12; // Moving Average(12,0,...) Period of averaging 29 | input int Signal_MA_Shift =0; // Moving Average(12,0,...) Time shift 30 | input ENUM_MA_METHOD Signal_MA_Method =MODE_SMA; // Moving Average(12,0,...) Method of averaging 31 | input ENUM_APPLIED_PRICE Signal_MA_Applied =PRICE_CLOSE; // Moving Average(12,0,...) Prices series 32 | input double Signal_MA_Weight =1.0; // Moving Average(12,0,...) Weight [0...1.0] 33 | input int Signal_MACD_PeriodFast =12; // MACD(12,24,9,PRICE_CLOSE) Period of fast EMA 34 | input int Signal_MACD_PeriodSlow =24; // MACD(12,24,9,PRICE_CLOSE) Period of slow EMA 35 | input int Signal_MACD_PeriodSignal=9; // MACD(12,24,9,PRICE_CLOSE) Period of averaging of difference 36 | input ENUM_APPLIED_PRICE Signal_MACD_Applied =PRICE_CLOSE; // MACD(12,24,9,PRICE_CLOSE) Prices series 37 | input double Signal_MACD_Weight =1.0; // MACD(12,24,9,PRICE_CLOSE) Weight [0...1.0] 38 | 39 | //+------------------------------------------------------------------+ 40 | //| Script program start function | 41 | //+------------------------------------------------------------------+ 42 | void OnStart() 43 | { 44 | //--- 45 | ChromosomeCount = 10; 46 | GeneCount = 2; 47 | Epoch = 50; 48 | //--- 49 | RangeMinimum = 0.0; 50 | RangeMaximum = 1.0; 51 | Precision = 0.1; 52 | OptimizeMethod = 2; 53 | ArrayResize(Chromosome,GeneCount+1); 54 | ArrayInitialize(Chromosome,0); 55 | 56 | int time_start=(int)GetTickCount(),time_end=0; 57 | //--------------------------------------------------------------------- 58 | 59 | UGA 60 | ( 61 | ReplicationPortion_E, 62 | NMutationPortion_E, 63 | ArtificialMutation_E, 64 | GenoMergingPortion_E, 65 | CrossingOverPortion_E, 66 | //--- 67 | ReplicationOffset_E, 68 | NMutationProbability_E 69 | ); 70 | //---------------------------------- 71 | time_end=(int)GetTickCount(); 72 | //---------------------------------- 73 | Print(time_end-time_start); 74 | //---------------------------------- 75 | 76 | } 77 | //+------------------------------------------------------------------+ -------------------------------------------------------------------------------- /Genetic Algorithms/part2/MAMACDFitness.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| MAMACDFitness.mqh | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | 7 | void FitnessFunction(int chromos) 8 | { 9 | 10 | double _MACD_Weight=0.0; 11 | double _MA_Weight=0.0; 12 | double sum=0.0; 13 | int cnt=1; 14 | 15 | while (cnt<=GeneCount) 16 | { 17 | _MACD_Weight=Colony[cnt][chromos]; 18 | cnt++; 19 | _MA_Weight=Colony[cnt][chromos]; 20 | cnt++; 21 | 22 | int handleInd; 23 | double BufferInd[]; 24 | double BufferColorInd[]; 25 | 26 | 27 | handleInd=iCustom(NULL,0,"IndSignalsLow", 28 | Signal_ThresholdOpen, 29 | Signal_ThresholdClose, 30 | Signal_MACD_PeriodFast, 31 | Signal_MACD_PeriodSlow, 32 | Signal_MACD_PeriodSignal, 33 | Signal_MACD_Applied, 34 | _MACD_Weight, 35 | Signal_MA_PeriodMA, 36 | Signal_MA_Shift, 37 | Signal_MA_Method, 38 | Signal_MA_Applied, 39 | _MA_Weight 40 | ); 41 | 42 | ResetLastError(); 43 | 44 | int size=5000; 45 | 46 | if(CopyBuffer(handleInd,0,0,size,BufferInd)<0) 47 | { 48 | PrintFormat("Error code %d",GetLastError()); 49 | 50 | } 51 | 52 | if(CopyBuffer(handleInd,1,0,size,BufferColorInd)<0) 53 | { 54 | PrintFormat("Error code %d",GetLastError()); 55 | 56 | } 57 | ArraySetAsSeries(BufferInd,true); 58 | ArraySetAsSeries(BufferColorInd,true); 59 | 60 | bool flagBuy=false; 61 | bool flagSell=false; 62 | double priceBuy=0; 63 | double priceStopBuy=0; 64 | double profitBuy=0; 65 | double profitTotalBuy=0; 66 | int countProfitBuyPlus=0; 67 | int countProfitBuyMinus=0; 68 | double priceSell=0; 69 | double priceStopSell=0; 70 | double profitSell=0; 71 | double profitTotalSell=0; 72 | int countProfitSellPlus=0; 73 | int countProfitSellMinus=0; 74 | double sp=0.0002; 75 | 76 | for (int i=0; i0){countProfitSellPlus++;}else{countProfitSellMinus++;} 85 | profitTotalSell=profitTotalSell+profitSell; 86 | } 87 | if(flagBuy==false){ 88 | flagBuy=true; 89 | priceBuy=BufferInd[i]; 90 | } 91 | } 92 | 93 | if(BufferColorInd[i]==1){ 94 | if(flagBuy==true){ 95 | flagBuy=false; 96 | priceStopBuy=BufferInd[i]; 97 | profitBuy=(priceStopBuy-priceBuy-sp)*10000; 98 | if(profitBuy>0){countProfitBuyPlus++;}else{countProfitBuyMinus++;} 99 | profitTotalBuy=profitTotalBuy+profitBuy; 100 | } 101 | if(flagSell==false){ 102 | priceSell=BufferInd[i]; 103 | flagSell=true; 104 | } 105 | } 106 | } 107 | //Print(" ProfitBuy ", profitTotalBuy," countProfitBuyPlus ",countProfitBuyPlus," countProfitBuyMinus ",countProfitBuyMinus); 108 | //Print(" ProfitSell ", profitTotalSell," countProfitSellPlus ",countProfitSellPlus," countProfitSellMinus ",countProfitSellMinus); 109 | 110 | sum = profitTotalBuy + profitTotalSell; 111 | } 112 | AmountStartsFF++; 113 | Colony[0][chromos]=sum; 114 | } 115 | 116 | void ServiceFunction() 117 | { 118 | 119 | double _MACD_Weight=0.0; 120 | double _MA_Weight=0.0; 121 | int cnt=1; 122 | 123 | while (cnt<=GeneCount) 124 | { 125 | _MACD_Weight=Chromosome[cnt]; 126 | cnt++; 127 | _MA_Weight=Chromosome[cnt]; 128 | cnt++; 129 | } 130 | Print("Fitness func =",Chromosome[0],"\n", 131 | "The resulting argument values:","\n", 132 | "_MACD_Weight =",Chromosome[1],"\n", 133 | "_MA_Weight =",Chromosome[2],"\n" 134 | ); 135 | 136 | } 137 | //——————————————————————————————————————————————————————————————————— -------------------------------------------------------------------------------- /Genetic Algorithms/part2/SignalMAIndLow.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| SignalMAIndLow.mqh | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | //+------------------------------------------------------------------+ 10 | //| | 11 | //+------------------------------------------------------------------+ 12 | #include 13 | class CSignalMAIndLow : public CSignalMA 14 | { 15 | public: 16 | virtual int BarsCalculatedInd(); 17 | virtual int LongConditionInd(int ind, int amount, double close, double open, double low); 18 | virtual int ShortConditionInd(int ind, int amount, double close, double open, double high); 19 | }; 20 | 21 | //+------------------------------------------------------------------+ 22 | //| Refresh indicators. | 23 | //+------------------------------------------------------------------+ 24 | int CSignalMAIndLow:: BarsCalculatedInd(){ 25 | m_ma.Refresh(); 26 | int bars = m_ma.BarsCalculated(); 27 | return bars; 28 | } 29 | //+------------------------------------------------------------------+ 30 | //| "Voting" that price will grow. | 31 | //+------------------------------------------------------------------+ 32 | int CSignalMAIndLow::LongConditionInd(int idx, int amount, double close, double open, double low) 33 | { 34 | int handle=m_ma.Handle(); 35 | double iMABuffer[]; 36 | if(CopyBuffer(handle,0,0,amount,iMABuffer)<0) 37 | { 38 | PrintFormat("Failed to copy data from iMA indicator, error code %d",GetLastError()); 39 | 40 | return(-1); 41 | } 42 | ArraySetAsSeries(iMABuffer,true); 43 | 44 | int result=0; 45 | 46 | double DiffCloseMA = close - iMABuffer[idx]; 47 | double DiffOpenMA = open - iMABuffer[idx]; 48 | double DiffMA = iMABuffer[idx] - iMABuffer[idx+1]; 49 | double DiffLowMA = low - iMABuffer[idx]; 50 | //--- analyze positional relationship of the close price and the indicator at the first analyzed bar 51 | if(DiffCloseMA<0.0) 52 | { 53 | //--- the close price is below the indicator 54 | if(IS_PATTERN_USAGE(1) && DiffOpenMA>0.0 && DiffMA>0.0) 55 | { 56 | //--- the open price is above the indicator (i.e. there was an intersection), but the indicator is directed upwards 57 | result=m_pattern_1; 58 | //--- consider that this is an unformed "piercing" and suggest to enter the market at the current price 59 | m_base_price=0.0; 60 | } 61 | } 62 | else 63 | { 64 | //--- the close price is above the indicator (the indicator has no objections to buying) 65 | if(IS_PATTERN_USAGE(0)) 66 | result=m_pattern_0; 67 | //--- if the indicator is directed upwards 68 | if(DiffMA>0.0) 69 | { 70 | if(DiffOpenMA<0.0) 71 | { 72 | //--- if the model 2 is used 73 | if(IS_PATTERN_USAGE(2)) 74 | { 75 | //--- the open price is below the indicator (i.e. there was an intersection) 76 | result=m_pattern_2; 77 | //--- suggest to enter the market at the "roll back" 78 | m_base_price=m_symbol.NormalizePrice(iMABuffer[idx]); 79 | } 80 | } 81 | else 82 | { 83 | //--- if the model 3 is used and the open price is above the indicator 84 | if(IS_PATTERN_USAGE(3) && DiffLowMA<0.0) 85 | { 86 | //--- the low price is below the indicator 87 | result=m_pattern_3; 88 | //--- consider that this is a formed "piercing" and suggest to enter the market at the current price 89 | m_base_price=0.0; 90 | } 91 | } 92 | } 93 | } 94 | //--- return the result 95 | return(result); 96 | } 97 | //+------------------------------------------------------------------+ 98 | //| "Voting" that price will fall. | 99 | //+------------------------------------------------------------------+ 100 | int CSignalMAIndLow::ShortConditionInd(int idx, int amount, double close, double open, double high) 101 | { 102 | int handle=m_ma.Handle(); 103 | double iMABuffer[]; 104 | if(CopyBuffer(handle,0,0,amount,iMABuffer)<0) 105 | { 106 | PrintFormat("Failed to copy data from iMA indicator, error code %d",GetLastError()); 107 | 108 | return(-1); 109 | } 110 | ArraySetAsSeries(iMABuffer,true); 111 | 112 | int result=0; 113 | 114 | double DiffCloseMA = close - iMABuffer[idx]; 115 | double DiffOpenMA = open - iMABuffer[idx]; 116 | double DiffMA = iMABuffer[idx] - iMABuffer[idx+1]; 117 | double DiffHighMA = high - iMABuffer[idx]; 118 | 119 | //--- analyze positional relationship of the close price and the indicator at the first analyzed bar 120 | if(DiffCloseMA>0.0) 121 | { 122 | //--- the close price is above the indicator 123 | if(IS_PATTERN_USAGE(1) && DiffOpenMA<0.0 && DiffMA<0.0) 124 | { 125 | //--- the open price is below the indicator (i.e. there was an intersection), but the indicator is directed downwards 126 | result=m_pattern_1; 127 | //--- consider that this is an unformed "piercing" and suggest to enter the market at the current price 128 | m_base_price=0.0; 129 | } 130 | } 131 | else 132 | { 133 | //--- the close price is below the indicator (the indicator has no objections to buying) 134 | if(IS_PATTERN_USAGE(0)) 135 | result=m_pattern_0; 136 | //--- the indicator is directed downwards 137 | if(DiffMA<0.0) 138 | { 139 | if(DiffOpenMA>0.0) 140 | { 141 | //--- if the model 2 is used 142 | if(IS_PATTERN_USAGE(2)) 143 | { 144 | //--- the open price is above the indicator (i.e. there was an intersection) 145 | result=m_pattern_2; 146 | //--- suggest to enter the market at the "roll back" 147 | m_base_price=m_symbol.NormalizePrice(iMABuffer[idx]); 148 | } 149 | } 150 | else 151 | { 152 | //--- if the model 3 is used and the open price is below the indicator 153 | if(IS_PATTERN_USAGE(3) && DiffHighMA>0.0) 154 | { 155 | //--- the high price is above the indicator 156 | result=m_pattern_3; 157 | //--- consider that this is a formed "piercing" and suggest to enter the market at the current price 158 | m_base_price=0.0; 159 | } 160 | } 161 | } 162 | } 163 | //--- return the result 164 | return(result); 165 | } 166 | //+------------------------------------------------------------------+ 167 | -------------------------------------------------------------------------------- /Graphic Objects/Impulse keeper.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| Impulse keeper.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | #property indicator_chart_window 10 | 11 | #property indicator_buffers 4 12 | 13 | double EMA34HBuffer[]; 14 | double EMA34LBuffer[]; 15 | double EMA125Buffer[]; 16 | double PSARBuffer[]; 17 | 18 | int EMA34HHandle; 19 | int EMA34LHandle; 20 | int EMA125Handle; 21 | int PSARHandle; 22 | 23 | int bars_calculated=0; 24 | //+------------------------------------------------------------------+ 25 | //| Custom indicator initialization function | 26 | //+------------------------------------------------------------------+ 27 | int OnInit() 28 | { 29 | //--- indicator buffers mapping 30 | SetIndexBuffer(0,EMA34HBuffer,INDICATOR_CALCULATIONS); 31 | SetIndexBuffer(1,EMA34LBuffer,INDICATOR_CALCULATIONS); 32 | SetIndexBuffer(2,EMA125Buffer,INDICATOR_CALCULATIONS); 33 | SetIndexBuffer(3,PSARBuffer,INDICATOR_CALCULATIONS); 34 | 35 | EMA34HHandle=iMA(NULL,0,34,0,MODE_EMA,PRICE_HIGH); 36 | EMA34LHandle=iMA(NULL,0,34,0,MODE_EMA,PRICE_LOW); 37 | EMA125Handle=iMA(NULL,0,125,0,MODE_EMA,PRICE_CLOSE); 38 | PSARHandle=iSAR(NULL,0,0.02, 0.2); 39 | //--- 40 | return(INIT_SUCCEEDED); 41 | } 42 | //+------------------------------------------------------------------+ 43 | //| Custom indicator iteration function | 44 | //+------------------------------------------------------------------+ 45 | int OnCalculate(const int rates_total, 46 | const int prev_calculated, 47 | const datetime &time[], 48 | const double &open[], 49 | const double &high[], 50 | const double &low[], 51 | const double &close[], 52 | const long &tick_volume[], 53 | const long &volume[], 54 | const int &spread[]) 55 | { 56 | //--- 57 | int values_to_copy; 58 | int start; 59 | int calculated=BarsCalculated(EMA34HHandle); 60 | if(calculated<=0) 61 | { 62 | return(0); 63 | } 64 | if(prev_calculated==0 || calculated!=bars_calculated) 65 | { 66 | start=1; 67 | if(calculated>rates_total) values_to_copy=rates_total; 68 | else values_to_copy=calculated; 69 | } 70 | else 71 | { 72 | start=rates_total-1; 73 | values_to_copy=1; 74 | } 75 | 76 | if(!FillArrayFromMABuffer(EMA34HBuffer,0,EMA34HHandle,values_to_copy)) return(0); 77 | if(!FillArrayFromMABuffer(EMA34LBuffer,0,EMA34LHandle,values_to_copy)) return(0); 78 | if(!FillArrayFromMABuffer(EMA125Buffer,0,EMA125Handle,values_to_copy)) return(0); 79 | if(!FillArrayFromPSARBuffer(PSARBuffer,PSARHandle,values_to_copy)) return(0); 80 | 81 | for(int i=start;iopen[i-1]&&close[i-1]>EMA34HBuffer[i-1]&&close[i-1]>EMA34LBuffer[i-1] 84 | &&low[i-1]>EMA125Buffer[i-1]&&low[i-1]>PSARBuffer[i-1]&&EMA125Buffer[i-1]EMA34LBuffer[i-1] 115 | &&EMA125Buffer[i-1]>EMA34HBuffer[i-1]){ 116 | 117 | if(!ObjectCreate(0,"Sell"+i,OBJ_ARROW,0,time[i-1],low[i-1])) 118 | { 119 | return(false); 120 | } 121 | ObjectSetInteger(0,"Sell"+i,OBJPROP_COLOR,clrRed); 122 | ObjectSetInteger(0,"Sell"+i,OBJPROP_ARROWCODE,234); 123 | ObjectSetInteger(0,"Sell"+i,OBJPROP_WIDTH,2); 124 | ObjectSetInteger(0,"Sell"+i,OBJPROP_ANCHOR,ANCHOR_LOWER); 125 | ObjectSetInteger(0,"Sell"+i,OBJPROP_HIDDEN,true); 126 | ObjectSetString(0,"Sell"+i,OBJPROP_TOOLTIP,""+close[i-1]); 127 | 128 | if(!ObjectCreate(0,"Sell1"+i,OBJ_ARROW,1,time[i-1],low[i-1])) 129 | { 130 | return(false); 131 | } 132 | 133 | ObjectSetInteger(0,"Sell1"+i,OBJPROP_COLOR,clrRed); 134 | ObjectSetInteger(0,"Sell1"+i,OBJPROP_ARROWCODE,234); 135 | ObjectSetInteger(0,"Sell1"+i,OBJPROP_WIDTH,2); 136 | ObjectSetInteger(0,"Sell1"+i,OBJPROP_ANCHOR,ANCHOR_LOWER); 137 | ObjectSetInteger(0,"Sell1"+i,OBJPROP_HIDDEN,true); 138 | ObjectSetString(0,"Sell1"+i,OBJPROP_TOOLTIP,close[i-1]); 139 | } 140 | } 141 | 142 | bars_calculated=calculated; 143 | //--- return value of prev_calculated for next call 144 | return(rates_total); 145 | } 146 | 147 | void OnDeinit(const int reason){ 148 | ObjectsDeleteAll(0,-1,-1); 149 | } 150 | 151 | 152 | //+------------------------------------------------------------------+ 153 | bool FillArrayFromPSARBuffer( 154 | double &sar_buffer[], // Parabolic SAR indicator's value buffer 155 | int ind_handle, // iSAR indicator's handle 156 | int amount // number of values to be copied 157 | ) 158 | { 159 | ResetLastError(); 160 | if(CopyBuffer(ind_handle,0,0,amount,sar_buffer)<0) 161 | { 162 | return(false); 163 | } 164 | return(true); 165 | } 166 | //+------------------------------------------------------------------+ 167 | bool FillArrayFromMABuffer( 168 | double &values[], // indicator's buffer of Moving Average values 169 | int shift, // shift 170 | int ind_handle, // iMA indicator's handle 171 | int amount // number of values to be copied 172 | ) 173 | { 174 | ResetLastError(); 175 | if(CopyBuffer(ind_handle,0,-shift,amount,values)<0) 176 | { 177 | return(false); 178 | } 179 | return(true); 180 | } 181 | -------------------------------------------------------------------------------- /Graphic Objects/OBJPROP_ANGLE.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| OBJPROP_ANGLE.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | #property indicator_chart_window 10 | //+------------------------------------------------------------------+ 11 | //| Custom indicator initialization function | 12 | //+------------------------------------------------------------------+ 13 | int OnInit() 14 | { 15 | //--- indicator buffers mapping 16 | 17 | //--- 18 | return(INIT_SUCCEEDED); 19 | } 20 | //+------------------------------------------------------------------+ 21 | //| Custom indicator iteration function | 22 | //+------------------------------------------------------------------+ 23 | int OnCalculate(const int rates_total, 24 | const int prev_calculated, 25 | const datetime &time[], 26 | const double &open[], 27 | const double &high[], 28 | const double &low[], 29 | const double &close[], 30 | const long &tick_volume[], 31 | const long &volume[], 32 | const int &spread[]) 33 | { 34 | //--- 35 | ArraySetAsSeries(time, true); 36 | ArraySetAsSeries(high, true); 37 | ArraySetAsSeries(low, true); 38 | ArraySetAsSeries(close, true); 39 | ObjectDelete(0,"Line"); 40 | ObjectDelete(0,"Price"); 41 | if(!ObjectCreate(0,"Line",OBJ_VLINE,0,time[1],close[1])) 42 | { 43 | return(false); 44 | } 45 | ObjectSetInteger(0,"Line",OBJPROP_COLOR,clrBlue); 46 | ObjectSetInteger(0,"Line",OBJPROP_WIDTH,1); 47 | ObjectSetString(0,"Line",OBJPROP_TOOLTIP,close[1]); 48 | 49 | if(!ObjectCreate(0,"Price",OBJ_TEXT,0,time[3],high[1])) 50 | { 51 | return(false); 52 | } 53 | ObjectSetString(0,"Price",OBJPROP_TEXT,close[1]); 54 | ObjectSetInteger(0,"Price",OBJPROP_COLOR,clrBlack); 55 | ObjectSetDouble(0,"Price",OBJPROP_ANGLE,90); 56 | ObjectSetString(0,"Price",OBJPROP_TOOLTIP,close[1]); 57 | 58 | //--- return value of prev_calculated for next call 59 | return(rates_total); 60 | } 61 | //+------------------------------------------------------------------+ 62 | void OnDeinit(const int reason){ 63 | ObjectsDeleteAll(0,-1,-1); 64 | } 65 | 66 | -------------------------------------------------------------------------------- /Graphic Objects/OBJPROP_PRICE.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| OBJPROP_PRICE.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | #property indicator_chart_window 10 | //+------------------------------------------------------------------+ 11 | //| Custom indicator initialization function | 12 | //+------------------------------------------------------------------+ 13 | int OnInit() 14 | { 15 | //--- indicator buffers mapping 16 | 17 | //--- 18 | return(INIT_SUCCEEDED); 19 | } 20 | //+------------------------------------------------------------------+ 21 | //| Custom indicator iteration function | 22 | //+------------------------------------------------------------------+ 23 | int OnCalculate(const int rates_total, 24 | const int prev_calculated, 25 | const datetime &time[], 26 | const double &open[], 27 | const double &high[], 28 | const double &low[], 29 | const double &close[], 30 | const long &tick_volume[], 31 | const long &volume[], 32 | const int &spread[]) 33 | { 34 | //--- 35 | ArraySetAsSeries(time, true); 36 | ArraySetAsSeries(high, true); 37 | ArraySetAsSeries(low, true); 38 | ArraySetAsSeries(close, true); 39 | ObjectDelete(0,"Price"); 40 | if(!ObjectCreate(0,"Price",OBJ_HLINE,0,time[1],close[1])) 41 | { 42 | return(false); 43 | } 44 | ObjectSetInteger(0,"Price",OBJPROP_COLOR,clrGreen); 45 | ObjectSetInteger(0,"Price",OBJPROP_WIDTH,1); 46 | ObjectSetString(0,"Price",OBJPROP_TOOLTIP,close[1]); 47 | if (open[1]>close[1]) 48 | ObjectSetDouble(0,"Price",OBJPROP_PRICE,low[1]); 49 | if (open[1] 10 | //--- indicator settings 11 | #property indicator_separate_window 12 | #property indicator_buffers 4 13 | #property indicator_plots 2 14 | #property indicator_type1 DRAW_HISTOGRAM 15 | #property indicator_type2 DRAW_LINE 16 | #property indicator_color1 Silver 17 | #property indicator_color2 Red 18 | #property indicator_width1 2 19 | #property indicator_width2 1 20 | #property indicator_label1 "MACD" 21 | #property indicator_label2 "Signal" 22 | //--- input parameters 23 | input int InpFastEMA=12; // Fast EMA period 24 | input int InpSlowEMA=26; // Slow EMA period 25 | input int InpSignalSMA=9; // Signal SMA period 26 | input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price 27 | //--- indicator buffers 28 | double ExtMacdBuffer[]; 29 | double ExtSignalBuffer[]; 30 | double ExtFastMaBuffer[]; 31 | double ExtSlowMaBuffer[]; 32 | //--- MA handles 33 | int ExtFastMaHandle; 34 | int ExtSlowMaHandle; 35 | //+------------------------------------------------------------------+ 36 | //| Custom indicator initialization function | 37 | //+------------------------------------------------------------------+ 38 | void OnInit() 39 | { 40 | //--- indicator buffers mapping 41 | SetIndexBuffer(0,ExtMacdBuffer,INDICATOR_DATA); 42 | SetIndexBuffer(1,ExtSignalBuffer,INDICATOR_DATA); 43 | SetIndexBuffer(2,ExtFastMaBuffer,INDICATOR_CALCULATIONS); 44 | SetIndexBuffer(3,ExtSlowMaBuffer,INDICATOR_CALCULATIONS); 45 | //--- sets first bar from what index will be drawn 46 | PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpSignalSMA-1); 47 | //--- name for Dindicator subwindow label 48 | IndicatorSetString(INDICATOR_SHORTNAME,"MACD("+string(InpFastEMA)+","+string(InpSlowEMA)+","+string(InpSignalSMA)+")"); 49 | //--- get MA handles 50 | 51 | // ExtFastMaHandle=iMA(NULL,0,InpFastEMA,0,MODE_EMA,InpAppliedPrice); 52 | // ExtSlowMaHandle=iMA(NULL,0,InpSlowEMA,0,MODE_EMA,InpAppliedPrice); 53 | // ExtFastMaHandle=iCustom(NULL,0,"Examples\\Custom Moving Average", InpFastEMA,0,MODE_EMA,InpAppliedPrice); 54 | // ExtSlowMaHandle=iCustom(NULL,0,"Examples\\Custom Moving Average", InpSlowEMA,0,MODE_EMA,InpAppliedPrice); 55 | 56 | MqlParam params[]; 57 | ArrayResize(params,5); 58 | params[0].type =TYPE_STRING; 59 | params[0].string_value="Examples\\Custom Moving Average"; 60 | //--- set ma_period 61 | params[1].type =TYPE_INT; 62 | params[1].integer_value=InpFastEMA; 63 | //--- set ma_shift 64 | params[2].type =TYPE_INT; 65 | params[2].integer_value=0; 66 | //--- set ma_method 67 | params[3].type =TYPE_INT; 68 | params[3].integer_value=MODE_EMA; 69 | //--- set applied_price 70 | params[4].type =TYPE_INT; 71 | params[4].integer_value=InpAppliedPrice; 72 | 73 | ExtFastMaHandle=IndicatorCreate(NULL,NULL,IND_CUSTOM,4,params); 74 | params[1].integer_value=InpSlowEMA; 75 | ExtSlowMaHandle=IndicatorCreate(NULL,NULL,IND_CUSTOM,4,params); 76 | 77 | 78 | //--- initialization done 79 | } 80 | //+------------------------------------------------------------------+ 81 | //| Moving Averages Convergence/Divergence | 82 | //+------------------------------------------------------------------+ 83 | int OnCalculate(const int rates_total, 84 | const int prev_calculated, 85 | const datetime &time[], 86 | const double &open[], 87 | const double &high[], 88 | const double &low[], 89 | const double &close[], 90 | const long &tick_volume[], 91 | const long &volume[], 92 | const int &spread[]) 93 | { 94 | //--- check for data 95 | if(rates_totalrates_total || prev_calculated<0) to_copy=rates_total; 113 | else 114 | { 115 | to_copy=rates_total-prev_calculated; 116 | if(prev_calculated>0) to_copy++; 117 | } 118 | //--- get Fast EMA buffer 119 | if(IsStopped()) return(0); //Checking for stop flag 120 | if(CopyBuffer(ExtFastMaHandle,0,0,to_copy,ExtFastMaBuffer)<=0) 121 | { 122 | Print("Getting fast EMA is failed! Error",GetLastError()); 123 | return(0); 124 | } 125 | //--- get SlowSMA buffer 126 | if(IsStopped()) return(0); //Checking for stop flag 127 | if(CopyBuffer(ExtSlowMaHandle,0,0,to_copy,ExtSlowMaBuffer)<=0) 128 | { 129 | Print("Getting slow SMA is failed! Error",GetLastError()); 130 | return(0); 131 | } 132 | //--- 133 | int limit; 134 | if(prev_calculated==0) 135 | limit=0; 136 | else limit=prev_calculated-1; 137 | //--- calculate MACD 138 | for(int i=limit;i 10 | 11 | #property indicator_separate_window 12 | //#property indicator_chart_window 13 | #property indicator_buffers 7 14 | #property indicator_plots 3 15 | #property indicator_type1 DRAW_COLOR_LINE 16 | #property indicator_color1 LightSeaGreen, clrBlue, clrLightBlue, clrRed, clrLightPink 17 | #property indicator_style1 STYLE_SOLID 18 | #property indicator_width1 2 19 | #property indicator_type2 DRAW_LINE 20 | #property indicator_color2 YellowGreen 21 | #property indicator_style2 STYLE_DOT 22 | #property indicator_width2 1 23 | #property indicator_type3 DRAW_LINE 24 | #property indicator_color3 Wheat 25 | #property indicator_style3 STYLE_DOT 26 | #property indicator_width3 1 27 | #property indicator_label1 "ADX" 28 | #property indicator_label2 "+DI" 29 | #property indicator_label3 "-DI" 30 | 31 | //--- input parameters 32 | input int InpPeriodADX=14; // Period 33 | //---- buffers 34 | double ExtADXBuffer[]; 35 | double ExtPDIBuffer[]; 36 | double ExtNDIBuffer[]; 37 | double ExtPDBuffer[]; 38 | double ExtNDBuffer[]; 39 | double ExtTmpBuffer[]; 40 | //Color buffer 41 | double ExtColorsBuffer[]; 42 | //--- global variables 43 | int ExtADXPeriod; 44 | //+------------------------------------------------------------------+ 45 | //| Custom indicator initialization function | 46 | //+------------------------------------------------------------------+ 47 | void OnInit() 48 | { 49 | //--- check for input parameters 50 | if(InpPeriodADX>=100 || InpPeriodADX<=0) 51 | { 52 | ExtADXPeriod=14; 53 | printf("Incorrect value for input variable Period_ADX=%d. Indicator will use value=%d for calculations.",InpPeriodADX,ExtADXPeriod); 54 | } 55 | else ExtADXPeriod=InpPeriodADX; 56 | //---- indicator buffers 57 | SetIndexBuffer(0,ExtADXBuffer); 58 | SetIndexBuffer(1,ExtColorsBuffer,INDICATOR_COLOR_INDEX); 59 | SetIndexBuffer(2,ExtPDIBuffer); 60 | SetIndexBuffer(3,ExtNDIBuffer); 61 | SetIndexBuffer(4,ExtPDBuffer,INDICATOR_CALCULATIONS); 62 | SetIndexBuffer(5,ExtNDBuffer,INDICATOR_CALCULATIONS); 63 | SetIndexBuffer(6,ExtTmpBuffer,INDICATOR_CALCULATIONS); 64 | 65 | //--- indicator digits 66 | IndicatorSetInteger(INDICATOR_DIGITS,2); 67 | //--- set draw begin 68 | PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtADXPeriod<<1); 69 | PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtADXPeriod); 70 | PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtADXPeriod); 71 | //--- indicator short name 72 | string short_name="ADX("+string(ExtADXPeriod)+")"; 73 | IndicatorSetString(INDICATOR_SHORTNAME,short_name); 74 | //--- change 1-st index label 75 | PlotIndexSetString(0,PLOT_LABEL,short_name); 76 | //---- end of initialization function 77 | } 78 | //+------------------------------------------------------------------+ 79 | //| Custom indicator iteration function | 80 | //+------------------------------------------------------------------+ 81 | int OnCalculate(const int rates_total, 82 | const int prev_calculated, 83 | const datetime &time[], 84 | const double &open[], 85 | const double &high[], 86 | const double &low[], 87 | const double &close[], 88 | const long &tick_volume[], 89 | const long &volume[], 90 | const int &spread[]) 91 | { 92 | //--- checking for bars count 93 | if(rates_total1) start=prev_calculated-1; 98 | else 99 | { 100 | start=1; 101 | ExtPDIBuffer[0]=0.0; 102 | ExtNDIBuffer[0]=0.0; 103 | ExtADXBuffer[0]=0.0; 104 | } 105 | //--- main cycle 106 | for(int i=start;idTmpN) dTmpN=0.0; 120 | else 121 | { 122 | if(dTmpPExtNDIBuffer[i]&&ExtADXBuffer[i]>ExtADXBuffer[i-1]){ 157 | ExtColorsBuffer[i]=1; 158 | } 159 | if(ExtPDIBuffer[i]>ExtNDIBuffer[i]&&ExtADXBuffer[i]ExtADXBuffer[i-1]){ 163 | ExtColorsBuffer[i]=3; 164 | } 165 | if(ExtPDIBuffer[i]0?diff:0); 88 | SumN+=(diff<0?-diff:0); 89 | } 90 | //--- calculate first visible value 91 | ExtPosBuffer[ExtPeriodRSI]=SumP/ExtPeriodRSI; 92 | ExtNegBuffer[ExtPeriodRSI]=SumN/ExtPeriodRSI; 93 | if(ExtNegBuffer[ExtPeriodRSI]!=0.0) 94 | ExtRSIBuffer[ExtPeriodRSI]=100.0-(100.0/(1.0+ExtPosBuffer[ExtPeriodRSI]/ExtNegBuffer[ExtPeriodRSI])); 95 | else 96 | { 97 | if(ExtPosBuffer[ExtPeriodRSI]!=0.0) 98 | ExtRSIBuffer[ExtPeriodRSI]=100.0; 99 | else 100 | ExtRSIBuffer[ExtPeriodRSI]=50.0; 101 | } 102 | //--- prepare the position value for main calculation 103 | pos=ExtPeriodRSI+1; 104 | } 105 | //--- the main loop of calculations 106 | for(i=pos;i0.0?diff:0.0))/ExtPeriodRSI; 110 | ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(ExtPeriodRSI-1)+(diff<0.0?-diff:0.0))/ExtPeriodRSI; 111 | if(ExtNegBuffer[i]!=0.0) 112 | ExtRSIBuffer[i]=100.0-100.0/(1+ExtPosBuffer[i]/ExtNegBuffer[i]); 113 | else 114 | { 115 | if(ExtPosBuffer[i]!=0.0) 116 | ExtRSIBuffer[i]=100.0; 117 | else 118 | ExtRSIBuffer[i]=50.0; 119 | } 120 | } 121 | //--- OnCalculate done. Return new prev_calculated. 122 | return(rates_total); 123 | } 124 | //+------------------------------------------------------------------+ 125 | -------------------------------------------------------------------------------- /Money Management and Expert Evaluation/ExpSidusOOP+.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| ExpSidusOOP.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | 10 | #include "CheckTrade.mqh" 11 | #include "Trade.mqh" 12 | #include "Sidus.mqh" 13 | 14 | input double Lot=1; 15 | input double spreadLevel=5.0; 16 | input double StopLoss=0.055; 17 | input double Profit=0.007; 18 | input int numberBarOpenPosition=7; 19 | input int numberBarStopPosition=2; 20 | 21 | input double TrailingStop=0.002; 22 | 23 | CheckTrade checkTrade; 24 | Trade trade(StopLoss,Profit,Lot, TrailingStop); 25 | Sidus sidus(numberBarOpenPosition,numberBarStopPosition); 26 | 27 | bool flagStopLoss=false; 28 | 29 | //+------------------------------------------------------------------+ 30 | //| Expert initialization function | 31 | //+------------------------------------------------------------------+ 32 | int OnInit() 33 | { 34 | return(checkTrade.OnCheckTradeInit(Lot)); 35 | } 36 | 37 | //+------------------------------------------------------------------+ 38 | //| Expert tick function | 39 | //+------------------------------------------------------------------+ 40 | void OnTick() 41 | { 42 | 43 | if(!checkTrade.OnCheckTradeTick(Lot,spreadLevel)){ 44 | return; 45 | } 46 | static datetime last_time; 47 | datetime last_bar_time=(datetime)SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE); 48 | if(last_time!=last_bar_time) 49 | { 50 | last_time=last_bar_time; 51 | }else{ 52 | return; 53 | } 54 | 55 | /* 56 | static datetime last_time_daily; 57 | datetime last_bar_time_daily=(datetime)SeriesInfoInteger(Symbol(),PERIOD_D1,SERIES_LASTBAR_DATE); 58 | if(last_time_daily!=last_bar_time_daily) 59 | { 60 | last_time_daily=last_bar_time_daily; 61 | flagStopLoss=false; 62 | } 63 | 64 | if(flagStopLoss==true)return; 65 | */ 66 | 67 | MqlRates mrate[]; 68 | ResetLastError(); 69 | if(CopyRates(Symbol(),Period(),0,numberBarStopPosition,mrate)<0) 70 | { 71 | Print(GetLastError()); 72 | return; 73 | } 74 | 75 | ArraySetAsSeries(mrate,true); 76 | 77 | //----------------------------------------------------------------------------- 78 | 79 | bool TradeSignalBuy=false; 80 | bool TradeSignalSell=false; 81 | 82 | 83 | TradeSignalBuy=sidus.OnTradeSignalBuy(); 84 | TradeSignalSell=sidus.OnTradeSignalSell(); 85 | 86 | bool TradeSignalBuyStop=false; 87 | bool TradeSignalSellStop=false; 88 | 89 | //TradeSignalBuyStop=sidus.OnTradeSignalBuyStop(mrate); 90 | //TradeSignalSellStop=sidus.OnTradeSignalSellStop(mrate); 91 | 92 | trade.Order(TradeSignalBuy,TradeSignalBuyStop,TradeSignalSell,TradeSignalSellStop); 93 | trade.Trailing(); 94 | 95 | } 96 | //+------------------------------------------------------------------+ 97 | //| Trade function | 98 | //+------------------------------------------------------------------+ 99 | void OnTrade() 100 | { 101 | static int _deals; 102 | ulong _ticket=0; 103 | 104 | if(HistorySelect(0,TimeCurrent())) 105 | { 106 | int i=HistoryDealsTotal()-1; 107 | 108 | if(_deals!=i) { 109 | _deals=i; 110 | } else { return; } 111 | 112 | if((_ticket=HistoryDealGetTicket(i))>0) 113 | { 114 | string _comment=HistoryDealGetString(_ticket,DEAL_COMMENT); 115 | if(StringFind(_comment,"sl",0)!=-1) { 116 | flagStopLoss=true; 117 | } 118 | 119 | } 120 | } 121 | } 122 | 123 | //------------------------------------------------------------------------ 124 | //| Expert deinitialization function | 125 | //+------------------------------------------------------------------+ 126 | void OnDeinit(const int reason) 127 | { 128 | 129 | } 130 | -------------------------------------------------------------------------------- /Money Management and Expert Evaluation/ExpSidusOOP.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| ExpSidusOOP.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | 10 | #include "CheckTrade.mqh" 11 | #include "Trade.mqh" 12 | #include "Sidus.mqh" 13 | 14 | input double Lot=1; 15 | input int EA_Magic=1000; 16 | input double spreadLevel=10.0; 17 | input double StopLoss=0.01; 18 | input double Profit=0.01; 19 | input int numberBarOpenPosition=5; 20 | input int numberBarStopPosition=5; 21 | 22 | CheckTrade checkTrade; 23 | Trade trade(StopLoss,Profit,Lot); 24 | Sidus sidus(numberBarOpenPosition,numberBarStopPosition); 25 | 26 | bool flagStopLoss=false; 27 | //+------------------------------------------------------------------+ 28 | //| Expert initialization function | 29 | //+------------------------------------------------------------------+ 30 | int OnInit() 31 | { 32 | return(checkTrade.OnCheckTradeInit(Lot)); 33 | } 34 | //+------------------------------------------------------------------+ 35 | //| Expert deinitialization function | 36 | //+------------------------------------------------------------------+ 37 | void OnDeinit(const int reason){ 38 | if(HistorySelect(0,TimeCurrent())) 39 | { 40 | int n=HistoryDealsTotal(); 41 | for(int i=0;inumberBarStopPosition)num=numberBarOpenPosition; 76 | if(numberBarOpenPosition<=numberBarStopPosition)num=numberBarStopPosition; 77 | MqlRates mrate[]; 78 | ResetLastError(); 79 | if(CopyRates(Symbol(),Period(),0,num,mrate)<0) 80 | { 81 | Print(GetLastError()); 82 | return; 83 | } 84 | 85 | ArraySetAsSeries(mrate,true); 86 | //----------------------------------------------------------------------------- 87 | bool TradeSignalBuy=false; 88 | bool TradeSignalSell=false; 89 | TradeSignalBuy=sidus.OnTradeSignalBuy(); 90 | TradeSignalSell=sidus.OnTradeSignalSell(); 91 | bool TradeSignalBuyStop=false; 92 | bool TradeSignalSellStop=false; 93 | TradeSignalBuyStop=sidus.OnTradeSignalBuyStop(mrate); 94 | TradeSignalSellStop=sidus.OnTradeSignalSellStop(mrate); 95 | trade.Order(TradeSignalBuy,TradeSignalBuyStop,TradeSignalSell,TradeSignalSellStop); 96 | } 97 | //+------------------------------------------------------------------+ 98 | //| Trade function | 99 | //+------------------------------------------------------------------+ 100 | void OnTrade() 101 | { 102 | static int _deals; 103 | ulong _ticket=0; 104 | 105 | if(HistorySelect(0,TimeCurrent())) 106 | { 107 | int i=HistoryDealsTotal()-1; 108 | 109 | if(_deals!=i) { 110 | _deals=i; 111 | } else { return; } 112 | 113 | if((_ticket=HistoryDealGetTicket(i))>0) 114 | { 115 | string _comment=HistoryDealGetString(_ticket,DEAL_COMMENT); 116 | if(StringFind(_comment,"sl",0)!=-1) { 117 | flagStopLoss=true; 118 | } 119 | } 120 | } 121 | } 122 | //-------------------------------------------------- 123 | 124 | -------------------------------------------------------------------------------- /Object Oriented Approach/CImpulsekeeper+.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| CImpulsekeeper.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | #property indicator_chart_window 10 | 11 | #include 12 | 13 | CiMA MA34H; 14 | CiMA MA34L; 15 | CiMA MA125; 16 | CiSAR SAR; 17 | 18 | int bars_calculated=0; 19 | 20 | //+------------------------------------------------------------------+ 21 | //| Custom indicator initialization function | 22 | //+------------------------------------------------------------------+ 23 | int OnInit() 24 | { 25 | MA34H.Create(_Symbol,PERIOD_CURRENT,34,0,MODE_EMA,PRICE_HIGH); 26 | MA34L.Create(_Symbol,PERIOD_CURRENT,34,0,MODE_EMA,PRICE_LOW); 27 | MA125.Create(_Symbol,PERIOD_CURRENT,125,0,MODE_EMA,PRICE_CLOSE); 28 | SAR.Create(_Symbol,PERIOD_CURRENT,0.02, 0.2); 29 | //--- 30 | return(INIT_SUCCEEDED); 31 | } 32 | //+------------------------------------------------------------------+ 33 | //| Custom indicator iteration function | 34 | //+------------------------------------------------------------------+ 35 | int OnCalculate(const int rates_total, 36 | const int prev_calculated, 37 | const datetime &time[], 38 | const double &open[], 39 | const double &high[], 40 | const double &low[], 41 | const double &close[], 42 | const long &tick_volume[], 43 | const long &volume[], 44 | const int &spread[]) 45 | { 46 | //--- 47 | int start; 48 | int calculated=MA34H.BarsCalculated(); 49 | if(calculated<=0) 50 | { 51 | return(0); 52 | } 53 | if(prev_calculated==0 || calculated!=bars_calculated) 54 | { 55 | start=rates_total-1; 56 | } 57 | else 58 | { 59 | start=1; 60 | } 61 | 62 | IKSignal iks(start,time,open,high,low,close); 63 | 64 | //Print(MA34H.BufferSize()); 65 | 66 | MA34H.BufferResize(rates_total); 67 | MA34L.BufferResize(rates_total); 68 | MA125.BufferResize(rates_total); 69 | SAR.BufferResize(rates_total); 70 | 71 | MA34H.Refresh(); 72 | MA34L.Refresh(); 73 | MA125.Refresh(); 74 | SAR.Refresh(); 75 | 76 | if(!iks.draw(MA34H, MA34L, MA125, SAR)){ 77 | return(false); 78 | } 79 | bars_calculated=calculated; 80 | //--- return value of prev_calculated for next call 81 | return(rates_total); 82 | } 83 | 84 | //+------------------------------------------------------------------+ 85 | void OnDeinit(const int reason){ 86 | ObjectsDeleteAll(0,-1,-1); 87 | } -------------------------------------------------------------------------------- /Object Oriented Approach/CImpulsekeeper.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| CImpulsekeeper.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | #property indicator_chart_window 10 | 11 | #include 12 | 13 | CiMA MA34H; 14 | CiMA MA34L; 15 | CiMA MA125; 16 | CiSAR SAR; 17 | 18 | int bars_calculated=0; 19 | 20 | //+------------------------------------------------------------------+ 21 | //| Custom indicator initialization function | 22 | //+------------------------------------------------------------------+ 23 | int OnInit() 24 | { 25 | MA34H.Create(_Symbol,PERIOD_CURRENT,34,0,MODE_EMA,PRICE_HIGH); 26 | MA34L.Create(_Symbol,PERIOD_CURRENT,34,0,MODE_EMA,PRICE_LOW); 27 | MA125.Create(_Symbol,PERIOD_CURRENT,125,0,MODE_EMA,PRICE_CLOSE); 28 | SAR.Create(_Symbol,PERIOD_CURRENT,0.02, 0.2); 29 | //--- 30 | return(INIT_SUCCEEDED); 31 | } 32 | //+------------------------------------------------------------------+ 33 | //| Custom indicator iteration function | 34 | //+------------------------------------------------------------------+ 35 | int OnCalculate(const int rates_total, 36 | const int prev_calculated, 37 | const datetime &time[], 38 | const double &open[], 39 | const double &high[], 40 | const double &low[], 41 | const double &close[], 42 | const long &tick_volume[], 43 | const long &volume[], 44 | const int &spread[]) 45 | { 46 | //--- 47 | int start; 48 | int calculated=MA34H.BarsCalculated(); 49 | if(calculated<=0) 50 | { 51 | return(0); 52 | } 53 | if(prev_calculated==0 || calculated!=bars_calculated) 54 | { 55 | start=rates_total-1; 56 | } 57 | else 58 | { 59 | start=1; 60 | } 61 | ArraySetAsSeries(time, true); 62 | ArraySetAsSeries(high, true); 63 | ArraySetAsSeries(low, true); 64 | ArraySetAsSeries(open, true); 65 | ArraySetAsSeries(close, true); 66 | 67 | //Print(MA34H.BufferSize()); 68 | 69 | MA34H.BufferResize(rates_total); 70 | MA34L.BufferResize(rates_total); 71 | MA125.BufferResize(rates_total); 72 | SAR.BufferResize(rates_total); 73 | 74 | MA34H.Refresh(); 75 | MA34L.Refresh(); 76 | MA125.Refresh(); 77 | SAR.Refresh(); 78 | 79 | for(int i=start;i>=1;i--) 80 | { 81 | if(close[i]>open[i]&&close[i]>MA34H.Main(i)&&close[i]>MA34L.Main(i) 82 | &&low[i]>MA125.Main(i)&&low[i]>SAR.Main(i)&&MA125.Main(i)MA34L.Main(i) 97 | &&MA125.Main(i)>MA34H.Main(i)){ 98 | if(!ObjectCreate(0,"Sell"+i,OBJ_ARROW,0,time[i],low[i])) 99 | { 100 | return(false); 101 | } 102 | ObjectSetInteger(0,"Sell"+i,OBJPROP_COLOR,clrRed); 103 | ObjectSetInteger(0,"Sell"+i,OBJPROP_ARROWCODE,234); 104 | ObjectSetInteger(0,"Sell"+i,OBJPROP_WIDTH,2); 105 | ObjectSetInteger(0,"Sell"+i,OBJPROP_ANCHOR,ANCHOR_LOWER); 106 | ObjectSetInteger(0,"Sell"+i,OBJPROP_HIDDEN,true); 107 | ObjectSetString(0,"Sell"+i,OBJPROP_TOOLTIP,close[i]); 108 | } 109 | } 110 | ChartRedraw(0); 111 | bars_calculated=calculated; 112 | //--- return value of prev_calculated for next call 113 | return(rates_total); 114 | } 115 | //+------------------------------------------------------------------+ 116 | void OnDeinit(const int reason){ 117 | ObjectsDeleteAll(0,-1,-1); 118 | } -------------------------------------------------------------------------------- /Object Oriented Approach/IKSignal.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| IKSignal.mqh | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | #include 10 | //+------------------------------------------------------------------+ 11 | //| | 12 | //+------------------------------------------------------------------+ 13 | class IKSignal 14 | { 15 | private: 16 | int _start; 17 | datetime _time[]; 18 | double _open[]; 19 | double _high[]; 20 | double _low[]; 21 | double _close[]; 22 | 23 | public: 24 | IKSignal( 25 | int start, 26 | const datetime &time[], 27 | const double &open[], 28 | const double &high[], 29 | const double &low[], 30 | const double &close[] 31 | ); 32 | bool draw(CiMA &MA34H, CiMA &MA34L, CiMA &MA125, CiSAR &SAR); 33 | ~IKSignal(); 34 | }; 35 | //+------------------------------------------------------------------+ 36 | //| | 37 | //+------------------------------------------------------------------+ 38 | IKSignal::IKSignal(int start, 39 | const datetime &time[], 40 | const double &open[], 41 | const double &high[], 42 | const double &low[], 43 | const double &close[] 44 | ) 45 | { 46 | _start=start; 47 | if(ArraySize(time)>0) 48 | { 49 | ArrayResize(_time,ArraySize(time)); 50 | ArrayCopy(_time, time); 51 | } 52 | if(ArraySize(open)>0) 53 | { 54 | ArrayResize(_open,ArraySize(open)); 55 | ArrayCopy(_open, open); 56 | } 57 | if(ArraySize(high)>0) 58 | { 59 | ArrayResize(_high,ArraySize(high)); 60 | ArrayCopy(_high, high); 61 | } 62 | if(ArraySize(low)>0) 63 | { 64 | ArrayResize(_low,ArraySize(low)); 65 | ArrayCopy(_low, low); 66 | } 67 | if(ArraySize(close)>0) 68 | { 69 | ArrayResize(_close,ArraySize(close)); 70 | ArrayCopy(_close, close); 71 | } 72 | ArraySetAsSeries(_time, true); 73 | ArraySetAsSeries(_high, true); 74 | ArraySetAsSeries(_low, true); 75 | ArraySetAsSeries(_open, true); 76 | ArraySetAsSeries(_close, true); 77 | } 78 | //+------------------------------------------------------------------+ 79 | //| | 80 | //+------------------------------------------------------------------+ 81 | IKSignal::~IKSignal() 82 | { 83 | } 84 | //+------------------------------------------------------------------+ 85 | bool IKSignal::draw(CiMA &MA34H, CiMA &MA34L, CiMA &MA125, CiSAR &SAR){ 86 | for(int i=_start;i>=1;i--) 87 | { if(_close[i]>_open[i]&&_close[i]>MA34H.Main(i)&&_close[i]>MA34L.Main(i) 88 | &&_low[i]>MA125.Main(i)&&_low[i]>SAR.Main(i)&&MA125.Main(i)MA34L.Main(i)&&MA125.Main(i)>MA34H.Main(i)){ 102 | if(!ObjectCreate(0,"Sell"+i,OBJ_ARROW,0,_time[i],_low[i])) 103 | { 104 | return(false); 105 | } 106 | ObjectSetInteger(0,"Sell"+i,OBJPROP_COLOR,clrRed); 107 | ObjectSetInteger(0,"Sell"+i,OBJPROP_ARROWCODE,234); 108 | ObjectSetInteger(0,"Sell"+i,OBJPROP_WIDTH,2); 109 | ObjectSetInteger(0,"Sell"+i,OBJPROP_ANCHOR,ANCHOR_LOWER); 110 | ObjectSetInteger(0,"Sell"+i,OBJPROP_HIDDEN,true); 111 | ObjectSetString(0,"Sell"+i,OBJPROP_TOOLTIP,_close[i]); 112 | } 113 | } 114 | ChartRedraw(0); 115 | return(true); 116 | } 117 | -------------------------------------------------------------------------------- /OnChartEvent/Impulse keeper.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| Impulse keeper.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | #property indicator_chart_window 10 | #property indicator_buffers 4 11 | double EMA34HBuffer[]; 12 | double EMA34LBuffer[]; 13 | double EMA125Buffer[]; 14 | double PSARBuffer[]; 15 | int EMA34HHandle; 16 | int EMA34LHandle; 17 | int EMA125Handle; 18 | int PSARHandle; 19 | 20 | int bars_calculated=0; 21 | //+------------------------------------------------------------------+ 22 | //| Custom indicator initialization function | 23 | //+------------------------------------------------------------------+ 24 | int OnInit() 25 | { 26 | //--- indicator buffers mapping 27 | SetIndexBuffer(0,EMA34HBuffer,INDICATOR_CALCULATIONS); 28 | SetIndexBuffer(1,EMA34LBuffer,INDICATOR_CALCULATIONS); 29 | SetIndexBuffer(2,EMA125Buffer,INDICATOR_CALCULATIONS); 30 | SetIndexBuffer(3,PSARBuffer,INDICATOR_CALCULATIONS); 31 | EMA34HHandle=iMA(NULL,0,34,0,MODE_EMA,PRICE_HIGH); 32 | EMA34LHandle=iMA(NULL,0,34,0,MODE_EMA,PRICE_LOW); 33 | EMA125Handle=iMA(NULL,0,125,0,MODE_EMA,PRICE_CLOSE); 34 | PSARHandle=iSAR(NULL,0,0.02, 0.2); 35 | //--- 36 | return(INIT_SUCCEEDED); 37 | } 38 | //+------------------------------------------------------------------+ 39 | //| Custom indicator iteration function | 40 | //+------------------------------------------------------------------+ 41 | int OnCalculate(const int rates_total, 42 | const int prev_calculated, 43 | const datetime &time[], 44 | const double &open[], 45 | const double &high[], 46 | const double &low[], 47 | const double &close[], 48 | const long &tick_volume[], 49 | const long &volume[], 50 | const int &spread[]) 51 | { 52 | //--- 53 | int values_to_copy; 54 | int start; 55 | int calculated=BarsCalculated(EMA34HHandle); 56 | if(calculated<=0) 57 | { 58 | return(0); 59 | } 60 | if(prev_calculated==0 || calculated!=bars_calculated) 61 | { 62 | start=rates_total-1; 63 | if(calculated>rates_total) values_to_copy=rates_total; 64 | else values_to_copy=calculated; 65 | } 66 | else 67 | { 68 | start=1; 69 | values_to_copy=1; 70 | } 71 | if(!FillArrayFromMABuffer(EMA34HBuffer,0,EMA34HHandle,values_to_copy)) return(0); 72 | if(!FillArrayFromMABuffer(EMA34LBuffer,0,EMA34LHandle,values_to_copy)) return(0); 73 | if(!FillArrayFromMABuffer(EMA125Buffer,0,EMA125Handle,values_to_copy)) return(0); 74 | if(!FillArrayFromPSARBuffer(PSARBuffer,PSARHandle,values_to_copy)) return(0); 75 | 76 | ArraySetAsSeries(time, true); 77 | ArraySetAsSeries(high, true); 78 | ArraySetAsSeries(low, true); 79 | ArraySetAsSeries(open, true); 80 | ArraySetAsSeries(close, true); 81 | ArraySetAsSeries(EMA34HBuffer, true); 82 | ArraySetAsSeries(EMA34LBuffer, true); 83 | ArraySetAsSeries(EMA125Buffer, true); 84 | ArraySetAsSeries(PSARBuffer, true); 85 | 86 | for(int i=start;i>=1;i--) 87 | { 88 | if(close[i]>open[i]&&close[i]>EMA34HBuffer[i]&&close[i]>EMA34LBuffer[i] 89 | &&low[i]>EMA125Buffer[i]&&low[i]>PSARBuffer[i]&&EMA125Buffer[i]EMA34LBuffer[i] 105 | &&EMA125Buffer[i]>EMA34HBuffer[i]){ 106 | 107 | if(!ObjectCreate(0,"Sell"+i,OBJ_ARROW,0,time[i],low[i])) 108 | { 109 | return(false); 110 | } 111 | ObjectSetInteger(0,"Sell"+i,OBJPROP_COLOR,clrRed); 112 | ObjectSetInteger(0,"Sell"+i,OBJPROP_ARROWCODE,234); 113 | ObjectSetInteger(0,"Sell"+i,OBJPROP_WIDTH,2); 114 | ObjectSetInteger(0,"Sell"+i,OBJPROP_ANCHOR,ANCHOR_LOWER); 115 | ObjectSetInteger(0,"Sell"+i,OBJPROP_HIDDEN,true); 116 | ObjectSetString(0,"Sell"+i,OBJPROP_TOOLTIP,close[i]); 117 | } 118 | } 119 | bars_calculated=calculated; 120 | //--- return value of prev_calculated for next call 121 | return(rates_total); 122 | } 123 | //+------------------------------------------------------------------+ 124 | bool FillArrayFromPSARBuffer( 125 | double &sar_buffer[], // Parabolic SAR indicator's value buffer 126 | int ind_handle, // iSAR indicator's handle 127 | int amount // number of values to be copied 128 | ) 129 | { 130 | 131 | ResetLastError(); 132 | if(CopyBuffer(ind_handle,0,0,amount,sar_buffer)<0) 133 | { 134 | return(false); 135 | } 136 | return(true); 137 | } 138 | //+------------------------------------------------------------------+ 139 | bool FillArrayFromMABuffer( 140 | double &values[], // indicator's buffer of Moving Average values 141 | int shift, // shift 142 | int ind_handle, // iMA indicator's handle 143 | int amount // number of values to be copied 144 | ) 145 | { 146 | ResetLastError(); 147 | if(CopyBuffer(ind_handle,0,-shift,amount,values)<0) 148 | { 149 | return(false); 150 | } 151 | return(true); 152 | } 153 | //------------------------------------------------------------------- 154 | void OnDeinit(const int reason){ 155 | ObjectsDeleteAll(0,-1,-1); 156 | } 157 | //+------------------------------------------------------------------+ 158 | //| ChartEvent function | 159 | //+------------------------------------------------------------------+ 160 | void OnChartEvent(const int id, 161 | const long &lparam, 162 | const double &dparam, 163 | const string &sparam) 164 | { 165 | //--- 166 | if(id==CHARTEVENT_OBJECT_CLICK){ 167 | 168 | if(StringFind(sparam,"Sell",0)!=-1){ 169 | int pos=StringToInteger(StringSubstr(sparam,4)); 170 | Alert("EMA34 High: ", EMA34HBuffer[pos]," , EMA34 Low: ", 171 | EMA34LBuffer[pos], " , EMA125: ", EMA125Buffer[pos], " , PSAR: ", PSARBuffer[pos] ); 172 | } 173 | 174 | if(StringFind(sparam,"Buy",0)!=-1){ 175 | int pos=StringToInteger(StringSubstr(sparam,3)); 176 | Alert("EMA34 High: ", EMA34HBuffer[pos]," , EMA34 Low: ", 177 | EMA34LBuffer[pos], " , EMA125: ", EMA125Buffer[pos], " , PSAR: ", PSARBuffer[pos] ); 178 | } 179 | } 180 | } 181 | //+------------------------------------------------------------------+ 182 | -------------------------------------------------------------------------------- /OnInit/Custom Moving Average.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| Custom Moving Average.mq5 | 3 | //| Copyright 2009-2017, MetaQuotes Software Corp. | 4 | //| http://www.mql5.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "2009-2017, MetaQuotes Software Corp." 7 | #property link "http://www.mql5.com" 8 | 9 | //--- indicator settings 10 | #property indicator_chart_window 11 | #property indicator_buffers 1 12 | #property indicator_plots 1 13 | #property indicator_type1 DRAW_LINE 14 | //#property indicator_type1 DRAW_ARROW 15 | #property indicator_color1 Red 16 | //--- input parameters 17 | input int InpMAPeriod=13; // Period 18 | input int InpMAShift=0; // Shift 19 | input ENUM_MA_METHOD InpMAMethod=MODE_SMMA; // Method 20 | //--- indicator buffers 21 | double ExtLineBuffer[]; 22 | //+------------------------------------------------------------------+ 23 | //| simple moving average | 24 | //+------------------------------------------------------------------+ 25 | void CalculateSimpleMA(int rates_total,int prev_calculated,int begin,const double &price[]) 26 | { 27 | int i,limit; 28 | //--- first calculation or number of bars was changed 29 | if(prev_calculated==0)// first calculation 30 | { 31 | limit=InpMAPeriod+begin; 32 | //--- set empty value for first limit bars 33 | for(i=0;i 10 | //--- indicator settings 11 | #property indicator_separate_window 12 | #property indicator_buffers 4 13 | #property indicator_plots 2 14 | #property indicator_type1 DRAW_HISTOGRAM 15 | #property indicator_type2 DRAW_LINE 16 | //#property indicator_color1 Silver 17 | //#property indicator_color2 Red 18 | #property indicator_width1 2 19 | #property indicator_width2 1 20 | //#property indicator_label1 "MACD" 21 | //#property indicator_label2 "Signal" 22 | //--- input parameters 23 | input int InpFastEMA=12; // Fast EMA period 24 | input int InpSlowEMA=26; // Slow EMA period 25 | input int InpSignalSMA=9; // Signal SMA period 26 | input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price 27 | //--- indicator buffers 28 | double ExtMacdBuffer[]; 29 | double ExtSignalBuffer[]; 30 | double ExtFastMaBuffer[]; 31 | double ExtSlowMaBuffer[]; 32 | //--- MA handles 33 | int ExtFastMaHandle; 34 | int ExtSlowMaHandle; 35 | //------------------------------------ 36 | input string symbol=" "; // Symbol 37 | string name=symbol; 38 | //+------------------------------------------------------------------+ 39 | //| Custom indicator initialization function | 40 | //+------------------------------------------------------------------+ 41 | void OnInit() 42 | { 43 | PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,1); 44 | PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,Silver); 45 | PlotIndexSetInteger(1,PLOT_COLOR_INDEXES,1); 46 | PlotIndexSetInteger(1,PLOT_LINE_COLOR,0,Red); 47 | 48 | PlotIndexSetString(0, PLOT_LABEL, "MACD"); 49 | PlotIndexSetString(1, PLOT_LABEL, "Signal"); 50 | 51 | 52 | //--- indicator buffers mapping 53 | SetIndexBuffer(0,ExtMacdBuffer,INDICATOR_DATA); 54 | SetIndexBuffer(1,ExtSignalBuffer,INDICATOR_DATA); 55 | SetIndexBuffer(2,ExtFastMaBuffer,INDICATOR_CALCULATIONS); 56 | SetIndexBuffer(3,ExtSlowMaBuffer,INDICATOR_CALCULATIONS); 57 | //--- sets first bar from what index will be drawn 58 | PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpSignalSMA-1); 59 | //--- name for Dindicator subwindow label 60 | IndicatorSetString(INDICATOR_SHORTNAME,"MACD("+string(InpFastEMA)+","+string(InpSlowEMA)+","+string(InpSignalSMA)+")"); 61 | 62 | //--- get MA handles 63 | 64 | //--- delete the left and right spaces 65 | StringTrimRight(name); 66 | StringTrimLeft(name); 67 | //--- if after this the length of the string name is zero 68 | if(StringLen(name)==0) 69 | { 70 | //--- take a symbol from the chart on which the indicator is running 71 | name=_Symbol; 72 | } 73 | 74 | ExtFastMaHandle=iMA(name,0,InpFastEMA,0,MODE_EMA,InpAppliedPrice); 75 | ExtSlowMaHandle=iMA(name,0,InpSlowEMA,0,MODE_EMA,InpAppliedPrice); 76 | 77 | //--- initialization done 78 | } 79 | //+------------------------------------------------------------------+ 80 | //| Moving Averages Convergence/Divergence | 81 | //+------------------------------------------------------------------+ 82 | int OnCalculate(const int rates_total, 83 | const int prev_calculated, 84 | const datetime &time[], 85 | const double &open[], 86 | const double &high[], 87 | const double &low[], 88 | const double &close[], 89 | const long &tick_volume[], 90 | const long &volume[], 91 | const int &spread[]) 92 | { 93 | //--- check for data 94 | if(rates_totalrates_total || prev_calculated<0) to_copy=rates_total; 112 | else 113 | { 114 | to_copy=rates_total-prev_calculated; 115 | if(prev_calculated>0) to_copy++; 116 | } 117 | //--- get Fast EMA buffer 118 | if(IsStopped()) return(0); //Checking for stop flag 119 | if(CopyBuffer(ExtFastMaHandle,0,0,to_copy,ExtFastMaBuffer)<=0) 120 | { 121 | Print("Getting fast EMA is failed! Error",GetLastError()); 122 | return(0); 123 | } 124 | //--- get SlowSMA buffer 125 | if(IsStopped()) return(0); //Checking for stop flag 126 | if(CopyBuffer(ExtSlowMaHandle,0,0,to_copy,ExtSlowMaBuffer)<=0) 127 | { 128 | Print("Getting slow SMA is failed! Error",GetLastError()); 129 | return(0); 130 | } 131 | //--- 132 | int limit; 133 | if(prev_calculated==0) 134 | limit=0; 135 | else limit=prev_calculated-1; 136 | //--- calculate MACD 137 | for(int i=limit;i0?diff:0); 104 | SumN+=(diff<0?-diff:0); 105 | } 106 | //--- calculate first visible value 107 | ExtPosBuffer[ExtPeriodRSI]=SumP/ExtPeriodRSI; 108 | ExtNegBuffer[ExtPeriodRSI]=SumN/ExtPeriodRSI; 109 | if(ExtNegBuffer[ExtPeriodRSI]!=0.0) 110 | ExtRSIBuffer[ExtPeriodRSI]=100.0-(100.0/(1.0+ExtPosBuffer[ExtPeriodRSI]/ExtNegBuffer[ExtPeriodRSI])); 111 | else 112 | { 113 | if(ExtPosBuffer[ExtPeriodRSI]!=0.0) 114 | ExtRSIBuffer[ExtPeriodRSI]=100.0; 115 | else 116 | ExtRSIBuffer[ExtPeriodRSI]=50.0; 117 | } 118 | //--- prepare the position value for main calculation 119 | pos=ExtPeriodRSI+1; 120 | } 121 | //--- the main loop of calculations 122 | for(i=pos;i0.0?diff:0.0))/ExtPeriodRSI; 126 | ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(ExtPeriodRSI-1)+(diff<0.0?-diff:0.0))/ExtPeriodRSI; 127 | if(ExtNegBuffer[i]!=0.0) 128 | ExtRSIBuffer[i]=100.0-100.0/(1+ExtPosBuffer[i]/ExtNegBuffer[i]); 129 | else 130 | { 131 | if(ExtPosBuffer[i]!=0.0) 132 | ExtRSIBuffer[i]=100.0; 133 | else 134 | ExtRSIBuffer[i]=50.0; 135 | } 136 | } 137 | //--- OnCalculate done. Return new prev_calculated. 138 | return(rates_total); 139 | } 140 | //+------------------------------------------------------------------+ 141 | -------------------------------------------------------------------------------- /PlaySound/Impulse keeper.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| Impulse keeper.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | #property indicator_chart_window 10 | 11 | #property indicator_buffers 4 12 | 13 | double EMA34HBuffer[]; 14 | double EMA34LBuffer[]; 15 | double EMA125Buffer[]; 16 | double PSARBuffer[]; 17 | 18 | int EMA34HHandle; 19 | int EMA34LHandle; 20 | int EMA125Handle; 21 | int PSARHandle; 22 | 23 | int bars_calculated=0; 24 | 25 | int countBuy=0; 26 | int countSell=0; 27 | 28 | //+------------------------------------------------------------------+ 29 | //| Custom indicator initialization function | 30 | //+------------------------------------------------------------------+ 31 | int OnInit() 32 | { 33 | //--- indicator buffers mapping 34 | SetIndexBuffer(0,EMA34HBuffer,INDICATOR_CALCULATIONS); 35 | SetIndexBuffer(1,EMA34LBuffer,INDICATOR_CALCULATIONS); 36 | SetIndexBuffer(2,EMA125Buffer,INDICATOR_CALCULATIONS); 37 | SetIndexBuffer(3,PSARBuffer,INDICATOR_CALCULATIONS); 38 | 39 | EMA34HHandle=iMA(NULL,0,34,0,MODE_EMA,PRICE_HIGH); 40 | EMA34LHandle=iMA(NULL,0,34,0,MODE_EMA,PRICE_LOW); 41 | EMA125Handle=iMA(NULL,0,125,0,MODE_EMA,PRICE_CLOSE); 42 | PSARHandle=iSAR(NULL,0,0.02, 0.2); 43 | //--- 44 | return(INIT_SUCCEEDED); 45 | } 46 | //+------------------------------------------------------------------+ 47 | //| Custom indicator iteration function | 48 | //+------------------------------------------------------------------+ 49 | int OnCalculate(const int rates_total, 50 | const int prev_calculated, 51 | const datetime &time[], 52 | const double &open[], 53 | const double &high[], 54 | const double &low[], 55 | const double &close[], 56 | const long &tick_volume[], 57 | const long &volume[], 58 | const int &spread[]) 59 | { 60 | //--- 61 | int values_to_copy; 62 | int start; 63 | int calculated=BarsCalculated(EMA34HHandle); 64 | if(calculated<=0) 65 | { 66 | return(0); 67 | } 68 | if(prev_calculated==0 || calculated!=bars_calculated) 69 | { 70 | start=1; 71 | if(calculated>rates_total) values_to_copy=rates_total; 72 | else values_to_copy=calculated; 73 | } 74 | else 75 | { 76 | start=rates_total-1; 77 | values_to_copy=1; 78 | } 79 | 80 | if(!FillArrayFromMABuffer(EMA34HBuffer,0,EMA34HHandle,values_to_copy)) return(0); 81 | if(!FillArrayFromMABuffer(EMA34LBuffer,0,EMA34LHandle,values_to_copy)) return(0); 82 | if(!FillArrayFromMABuffer(EMA125Buffer,0,EMA125Handle,values_to_copy)) return(0); 83 | if(!FillArrayFromPSARBuffer(PSARBuffer,PSARHandle,values_to_copy)) return(0); 84 | 85 | for(int i=start;iopen[i-1]&&close[i-1]>EMA34HBuffer[i-1]&&close[i-1]>EMA34LBuffer[i-1] 88 | &&low[i-1]>EMA125Buffer[i-1]&&low[i-1]>PSARBuffer[i-1]&&EMA125Buffer[i-1]open[i-1]&&close[i-1]>EMA34HBuffer[i-1]&&close[i-1]>EMA34LBuffer[i-1] 107 | &&low[i-1]>EMA125Buffer[i-1]&&low[i-1]>PSARBuffer[i-1]&&EMA125Buffer[i-1]EMA34LBuffer[i-1] 121 | &&EMA125Buffer[i-1]>EMA34HBuffer[i-1]){ 122 | 123 | countSell++; 124 | if (countSell==1){ 125 | PlaySound("alert2.wav"); 126 | Print("alert"); 127 | } 128 | }else{ 129 | countSell=0; 130 | } 131 | } 132 | 133 | 134 | 135 | if(close[i-1]EMA34LBuffer[i-1] 137 | &&EMA125Buffer[i-1]>EMA34HBuffer[i-1]){ 138 | 139 | if(!ObjectCreate(0,"Sell"+i,OBJ_ARROW,0,time[i-1],low[i-1])) 140 | { 141 | return(false); 142 | } 143 | ObjectSetInteger(0,"Sell"+i,OBJPROP_COLOR,clrRed); 144 | ObjectSetInteger(0,"Sell"+i,OBJPROP_ARROWCODE,234); 145 | ObjectSetInteger(0,"Sell"+i,OBJPROP_WIDTH,2); 146 | ObjectSetInteger(0,"Sell"+i,OBJPROP_ANCHOR,ANCHOR_LOWER); 147 | ObjectSetInteger(0,"Sell"+i,OBJPROP_HIDDEN,true); 148 | ObjectSetString(0,"Sell"+i,OBJPROP_TOOLTIP,""+close[i-1]); 149 | } 150 | } 151 | 152 | bars_calculated=calculated; 153 | //--- return value of prev_calculated for next call 154 | return(rates_total); 155 | } 156 | 157 | void OnDeinit(const int reason){ 158 | ObjectsDeleteAll(0,-1,-1); 159 | } 160 | 161 | 162 | //+------------------------------------------------------------------+ 163 | bool FillArrayFromPSARBuffer( 164 | double &sar_buffer[], // Parabolic SAR indicator's value buffer 165 | int ind_handle, // iSAR indicator's handle 166 | int amount // number of values to be copied 167 | ) 168 | { 169 | ResetLastError(); 170 | if(CopyBuffer(ind_handle,0,0,amount,sar_buffer)<0) 171 | { 172 | return(false); 173 | } 174 | return(true); 175 | } 176 | //+------------------------------------------------------------------+ 177 | bool FillArrayFromMABuffer( 178 | double &values[], // indicator's buffer of Moving Average values 179 | int shift, // shift 180 | int ind_handle, // iMA indicator's handle 181 | int amount // number of values to be copied 182 | ) 183 | { 184 | ResetLastError(); 185 | if(CopyBuffer(ind_handle,0,-shift,amount,values)<0) 186 | { 187 | return(false); 188 | } 189 | return(true); 190 | } 191 | -------------------------------------------------------------------------------- /Python.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novts/MetaTrader-5-Creating-Trading-Robots-and-Indicators-with-MQL5/93aa922e31eb13319ad63cc3e825b64706772771/Python.zip -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MetaTrader 5: Creating Trading Robots and Indicators with MQL5 2 | -------------------------------------------------------------------------------- /Testing Experts/ExpSidusOOP.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| ExpSidusOOP.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | 10 | #include "CheckTrade.mqh" 11 | #include "Trade.mqh" 12 | #include "Sidus.mqh" 13 | 14 | input double Lot=1; 15 | input int EA_Magic=1000; 16 | input double spreadLevel=10.0; 17 | input double StopLoss=0.01; 18 | input double Profit=0.01; 19 | input int numberBarOpenPosition=5; 20 | input int numberBarStopPosition=5; 21 | 22 | CheckTrade checkTrade; 23 | Trade trade(StopLoss,Profit,Lot); 24 | Sidus sidus(numberBarOpenPosition,numberBarStopPosition); 25 | 26 | bool flagStopLoss=false; 27 | 28 | int handleProfit; 29 | double ProfitBuffer[]; 30 | //+------------------------------------------------------------------+ 31 | //| Expert initialization function | 32 | //+------------------------------------------------------------------+ 33 | int OnInit() 34 | { 35 | handleProfit=iCustom(_Symbol,PERIOD_H1,"Profit",5,0.0001,1); 36 | return(checkTrade.OnCheckTradeInit(Lot)); 37 | } 38 | //+------------------------------------------------------------------+ 39 | //| Expert deinitialization function | 40 | //+------------------------------------------------------------------+ 41 | void OnDeinit(const int reason) 42 | { 43 | } 44 | //+------------------------------------------------------------------+ 45 | //| Expert tick function | 46 | //+------------------------------------------------------------------+ 47 | void OnTick() 48 | { 49 | if(!checkTrade.OnCheckTradeTick(Lot,spreadLevel)){ 50 | return; 51 | } 52 | static datetime last_time; 53 | datetime last_bar_time=(datetime)SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE); 54 | if(last_time!=last_bar_time) 55 | { 56 | last_time=last_bar_time; 57 | }else{ 58 | return; 59 | } 60 | static datetime last_time_daily; 61 | datetime last_bar_time_daily=(datetime)SeriesInfoInteger(Symbol(),PERIOD_D1,SERIES_LASTBAR_DATE); 62 | if(last_time_daily!=last_bar_time_daily) 63 | { 64 | last_time_daily=last_bar_time_daily; 65 | flagStopLoss=false; 66 | } 67 | 68 | if(flagStopLoss==true)return; 69 | 70 | int num=5; 71 | if(numberBarOpenPosition>numberBarStopPosition)num=numberBarOpenPosition; 72 | if(numberBarOpenPosition<=numberBarStopPosition)num=numberBarStopPosition; 73 | MqlRates mrate[]; 74 | ResetLastError(); 75 | if(CopyRates(Symbol(),Period(),0,num,mrate)<0) 76 | { 77 | Print(GetLastError()); 78 | return; 79 | } 80 | 81 | ArraySetAsSeries(mrate,true); 82 | //----------------------------------------------------------------------------- 83 | bool TradeSignalBuy=false; 84 | bool TradeSignalSell=false; 85 | TradeSignalBuy=sidus.OnTradeSignalBuy(); 86 | TradeSignalSell=sidus.OnTradeSignalSell(); 87 | bool TradeSignalBuyStop=false; 88 | bool TradeSignalSellStop=false; 89 | TradeSignalBuyStop=sidus.OnTradeSignalBuyStop(mrate); 90 | TradeSignalSellStop=sidus.OnTradeSignalSellStop(mrate); 91 | trade.Order(TradeSignalBuy,TradeSignalBuyStop,TradeSignalSell,TradeSignalSellStop); 92 | } 93 | //+------------------------------------------------------------------+ 94 | //| Trade function | 95 | //+------------------------------------------------------------------+ 96 | void OnTrade() 97 | { 98 | static int _deals; 99 | ulong _ticket=0; 100 | 101 | if(HistorySelect(0,TimeCurrent())) 102 | { 103 | int i=HistoryDealsTotal()-1; 104 | 105 | if(_deals!=i) { 106 | _deals=i; 107 | } else { return; } 108 | 109 | if((_ticket=HistoryDealGetTicket(i))>0) 110 | { 111 | string _comment=HistoryDealGetString(_ticket,DEAL_COMMENT); 112 | if(StringFind(_comment,"sl",0)!=-1) { 113 | flagStopLoss=true; 114 | } 115 | if(HistoryDealGetDouble(_ticket, DEAL_PROFIT)!=0.0){ 116 | if(!ObjectCreate(0,"Deal"+_ticket,OBJ_TEXT,0, 117 | HistoryDealGetInteger(_ticket,DEAL_TIME),HistoryDealGetDouble(_ticket,DEAL_PRICE))) 118 | { 119 | return; 120 | } 121 | ObjectSetString(0,"Deal"+_ticket,OBJPROP_TEXT,"Profit: "+HistoryDealGetDouble(_ticket, DEAL_PROFIT)); 122 | ObjectSetDouble(0,"Deal"+_ticket,OBJPROP_ANGLE,90.0); 123 | ObjectSetInteger(0,"Deal"+_ticket,OBJPROP_COLOR,clrYellow); 124 | }}}} 125 | 126 | //-------------------------------------------------- 127 | 128 | -------------------------------------------------------------------------------- /Testing Experts/IndTrade.mq5: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| IndTrade.mq5 | 3 | //| Copyright 2018, NOVTS | 4 | //| http://novts.com | 5 | //+------------------------------------------------------------------+ 6 | #property copyright "Copyright 2018, NOVTS" 7 | #property link "http://novts.com" 8 | #property version "1.00" 9 | #property indicator_chart_window 10 | 11 | #property indicator_buffers 2 12 | #property indicator_plots 1 13 | #property indicator_type1 DRAW_COLOR_LINE 14 | #property indicator_color1 clrBlack,clrRed,clrBlueViolet 15 | #property indicator_style1 STYLE_SOLID 16 | #property indicator_width1 2 17 | 18 | input int ma_period=5; 19 | input double delta=0.0001; 20 | input int shift_delta=1; 21 | int ma_shift=0; // Shift 22 | ENUM_MA_METHOD ma_method=MODE_EMA; // Smoothing type 23 | ENUM_APPLIED_PRICE applied_price=PRICE_WEIGHTED; // Price type 24 | 25 | string symbol=_Symbol; // Symbol 26 | ENUM_TIMEFRAMES period=PERIOD_CURRENT; // Timeframe 27 | 28 | double InBuffer[]; 29 | double ColorBuffer[]; 30 | int handleMA; 31 | string name=symbol; 32 | string short_name; 33 | int bars_calculated=0; 34 | //+------------------------------------------------------------------+ 35 | //| Custom indicator initialization function | 36 | //+------------------------------------------------------------------+ 37 | int OnInit() 38 | { 39 | //--- indicator buffers mapping 40 | SetIndexBuffer(0,InBuffer,INDICATOR_DATA); 41 | SetIndexBuffer(1,ColorBuffer,INDICATOR_COLOR_INDEX); 42 | name=symbol; 43 | StringTrimRight(name); 44 | StringTrimLeft(name); 45 | if(StringLen(name)==0) 46 | { 47 | name=_Symbol; 48 | } 49 | handleMA=iMA(name,period,ma_period,ma_shift,ma_method,applied_price); 50 | short_name="Profit"; 51 | IndicatorSetString(INDICATOR_SHORTNAME,short_name); 52 | //--- 53 | return(INIT_SUCCEEDED); 54 | } 55 | //+------------------------------------------------------------------+ 56 | //| Custom indicator iteration function | 57 | //+------------------------------------------------------------------+ 58 | int OnCalculate(const int rates_total, 59 | const int prev_calculated, 60 | const datetime &time[], 61 | const double &open[], 62 | const double &high[], 63 | const double &low[], 64 | const double &close[], 65 | const long &tick_volume[], 66 | const long &volume[], 67 | const int &spread[]) 68 | { 69 | int values_to_copy; 70 | int calculated=BarsCalculated(handleMA); 71 | if(calculated<=0) 72 | { 73 | PrintFormat("BarsCalculated() вернул %d, код ошибки %d",calculated,GetLastError()); 74 | return(0); 75 | } 76 | if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 77 | { 78 | if(calculated>rates_total) values_to_copy=rates_total; 79 | else values_to_copy=calculated; 80 | } 81 | else 82 | { 83 | values_to_copy=(rates_total-prev_calculated)+1; 84 | } 85 | 86 | if(!FillArrayFromBufferMA(InBuffer,ma_shift,handleMA,values_to_copy)) return(0); 87 | 88 | bars_calculated=calculated; 89 | if(values_to_copy>1){ 90 | 91 | bool flagSell=false; 92 | double priceSellOpen; 93 | double priceSellStop; 94 | bool flagBuy=false; 95 | double priceBuyOpen; 96 | double priceBuyStop; 97 | 98 | int size=values_to_copy; 99 | for (int i=shift_delta; i<(size-1); i++){ 100 | 101 | ColorBuffer[i]=0; 102 | 103 | if((InBuffer[i-shift_delta]-InBuffer[i])>delta){ 104 | ColorBuffer[i]=1; 105 | if(flagSell==false){ 106 | priceSellOpen=open[i]; 107 | if(!ObjectCreate(0,"Sell"+i,OBJ_ARROW,0,time[i],low[i])) 108 | { 109 | return(false); 110 | } 111 | ObjectSetInteger(0,"Sell"+i,OBJPROP_COLOR,clrRed); 112 | ObjectSetInteger(0,"Sell"+i,OBJPROP_ARROWCODE,234); 113 | ObjectSetInteger(0,"Sell"+i,OBJPROP_WIDTH,1); 114 | ObjectSetInteger(0,"Sell"+i,OBJPROP_ANCHOR,ANCHOR_LOWER); 115 | ObjectSetInteger(0,"Sell"+i,OBJPROP_HIDDEN,true); 116 | ObjectSetString(0,"Sell"+i,OBJPROP_TOOLTIP,""+close[i]); 117 | 118 | } 119 | flagSell=true; 120 | }else{ 121 | if(flagSell==true){ 122 | priceSellStop=open[i]; 123 | double profit=priceSellOpen-priceSellStop; 124 | if(profit>spread[i]/MathPow(10,Digits())){ 125 | if(!ObjectCreate(0,"SellStop"+i,OBJ_TEXT,0,time[i],low[i])) 126 | { 127 | return(false); 128 | } 129 | ObjectSetString(0,"SellStop"+i,OBJPROP_TEXT,"Profit: "+DoubleToString(profit*MathPow(10,Digits()),1)); 130 | ObjectSetDouble(0,"SellStop"+i,OBJPROP_ANGLE,90.0); 131 | ObjectSetInteger(0,"SellStop"+i,OBJPROP_COLOR,clrRed); 132 | } 133 | flagSell=false; 134 | } 135 | } 136 | 137 | if((InBuffer[i]-InBuffer[i-shift_delta])>delta){ 138 | ColorBuffer[i]=2; 139 | if(flagBuy==false){ 140 | priceBuyOpen=open[i]; 141 | if(!ObjectCreate(0,"Buy"+i,OBJ_ARROW,0,time[i],high[i])) 142 | { 143 | return(false); 144 | } 145 | 146 | ObjectSetInteger(0,"Buy"+i,OBJPROP_COLOR,clrGreen); 147 | ObjectSetInteger(0,"Buy"+i,OBJPROP_ARROWCODE,233); 148 | ObjectSetInteger(0,"Buy"+i,OBJPROP_WIDTH,1); 149 | ObjectSetInteger(0,"Buy"+i,OBJPROP_ANCHOR,ANCHOR_UPPER); 150 | ObjectSetInteger(0,"Buy"+i,OBJPROP_HIDDEN,true); 151 | ObjectSetString(0,"Buy"+i,OBJPROP_TOOLTIP,""+close[i]); 152 | } 153 | flagBuy=true; 154 | }else{ 155 | if(flagBuy==true){ 156 | priceBuyStop=open[i]; 157 | double profit=priceBuyStop-priceBuyOpen; 158 | if(profit>spread[i]/MathPow(10,Digits())){ 159 | if(!ObjectCreate(0,"BuyStop"+i,OBJ_TEXT,0,time[i],high[i])) 160 | { 161 | return(false); 162 | } 163 | ObjectSetString(0,"BuyStop"+i,OBJPROP_TEXT,"Profit: "+DoubleToString(profit*MathPow(10,Digits()),1)); 164 | ObjectSetDouble(0,"BuyStop"+i,OBJPROP_ANGLE,90.0); 165 | ObjectSetInteger(0,"BuyStop"+i,OBJPROP_COLOR,clrBlueViolet); 166 | } 167 | flagBuy=false; 168 | }}}} 169 | return(rates_total); 170 | } 171 | //+------------------------------------------------------------------+ 172 | bool FillArrayFromBufferMA(double &values[], 173 | int shift, 174 | int ind_handle, 175 | int amount 176 | ) 177 | { 178 | ResetLastError(); 179 | if(CopyBuffer(ind_handle,0,-shift,amount,values)<0) 180 | { 181 | PrintFormat("Failed to copy data from iMA indicator, error code %d", GetLastError()); 182 | return(false); 183 | } 184 | return(true); 185 | } 186 | void OnDeinit(const int reason){ 187 | ObjectsDeleteAll(0,-1,-1); 188 | } 189 | --------------------------------------------------------------------------------