Exp_Iin_MA_Signal

Author: Copyright © 2018, Nikolay Kositsin
Price Data Components
0 Views
0 Downloads
0 Favorites
Exp_Iin_MA_Signal
ÿþ//+------------------------------------------------------------------+

//|                                            Exp_Iin_MA_Signal.mq5 |

//|                               Copyright © 2018, Nikolay Kositsin | 

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

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

#property copyright "Copyright © 2018, Nikolay Kositsin"

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

#property version   "1.10"

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

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

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

#include <TradeAlgorithms.mqh>

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

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

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

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

  {

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

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

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

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

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

  }; */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

input uint              FastMAPeriod=10;

input  ENUM_MA_METHOD   FastMAType=MODE_EMA;

input ENUM_APPLIED_PRICE FastMAPrice=PRICE_CLOSE;

input uint              SlowMAPeriod=22;

input  ENUM_MA_METHOD   SlowMAType=MODE_SMA;

input ENUM_APPLIED_PRICE SlowMAPrice=PRICE_CLOSE;

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

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



int TimeShiftSec;

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

int InpInd_Handle;

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

int min_rates_total;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

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

   InpInd_Handle=iCustom(Symbol(),InpInd_Timeframe,"Iin_MA_Signal",FastMAPeriod,FastMAType,FastMAPrice,SlowMAPeriod,SlowMAType,SlowMAPrice);

   if(InpInd_Handle==INVALID_HANDLE)

     {

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

      return(INIT_FAILED);

     }



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

   TimeShiftSec=PeriodSeconds(InpInd_Timeframe);



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

   int AtrPeriod=10;

   min_rates_total=int(MathMax(MathMax(FastMAPeriod,SlowMAPeriod)+3,AtrPeriod));

   min_rates_total+=int(3+SignalBar);

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

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//----

   GlobalVariableDel_(Symbol());

//----

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

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

   if(BarsCalculated(InpInd_Handle)<min_rates_total) return;



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

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



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

   double DnValue[1],UpValue[1];

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

   static bool Recount=true;

   static bool BUY_Open=false,BUY_Close=false;

   static bool SELL_Open=false,SELL_Close=false;

   static datetime UpSignalTime,DnSignalTime;

   static CIsNewBar NB;

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

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

     {

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

      BUY_Open=false;

      SELL_Open=false;

      BUY_Close=false;

      SELL_Close=false;

      Recount=false;



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

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

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



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

      if(UpValue[0] && UpValue[0]!=EMPTY_VALUE)

        {

         if(BuyPosOpen) BUY_Open=true;

         if(SellPosClose)SELL_Close=true;

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

        }



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

      if(DnValue[0] && DnValue[0]!=EMPTY_VALUE)

        {

         if(SellPosOpen) SELL_Open=true;

         if(BuyPosClose) BUY_Close=true;

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

        }



      //---- >8A: ?>A;54=53> =0?@02;5=8O B>@3>2;8 4;O ?>;CG5=8O A83=0;>2 4;O 70:@K20=8O ?>78F89

      //if(!MQL5InfoInteger(MQL5_TESTING) && !MQL5InfoInteger(MQL5_OPTIMIZATION)) //5A;8 @568< B>@3>2;8 2 B5AB5@5 "@>872>;L=0O 7045@6:0" 

      if(((BuyPosOpen && BuyPosClose) || (SellPosOpen && SellPosClose)) && (!BUY_Close && !SELL_Close))

        {

         int Bars_=Bars(Symbol(),InpInd_Timeframe);



         for(int bar=int(SignalBar+1); bar<Bars_; bar++)

           {

            if(SellPosClose)

              {

               if(CopyBuffer(InpInd_Handle,1,bar,1,UpValue)<=0) {Recount=true; return;}

               if(UpValue[0]!=0 && UpValue[0]!=EMPTY_VALUE)

                 {

                  SELL_Close=true;

                  break;

                 }

              }



            if(BuyPosClose)

              {

               if(CopyBuffer(InpInd_Handle,0,bar,1,DnValue)<=0) {Recount=true; return;}

               if(DnValue[0]!=0 && DnValue[0]!=EMPTY_VALUE)

                 {

                  BUY_Close=true;

                  break;

                 }

              }

           }

        }

     }

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

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

   BuyPositionClose(BUY_Close,Symbol(),Deviation_);



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

   SellPositionClose(SELL_Close,Symbol(),Deviation_);



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

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



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

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

//----

  }

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

Comments