Exp_AnalysisOnBarsJFatlCandle_vol_ReOpen

Author: Copyright © 2018, Nikolay Kositsin
0 Views
0 Downloads
0 Favorites
Exp_AnalysisOnBarsJFatlCandle_vol_ReOpen
ÿþ//+------------------------------------------------------------------+

//|                     Exp_AnalysisOnBarsJFatlCandle_vol_ReOpen.mq5 |

//|                               Copyright © 2018, Nikolay Kositsin | 

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

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

#property copyright "Copyright © 2018, Nikolay Kositsin"

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

#property version   "1.00"

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

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

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

#include <TradeAlgorithms.mqh>

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

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

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

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

  {

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

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

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

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

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

  }; */

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

//| 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 uint   PriceStep=300;       //H03 F5=>2>9 A5B:8 2 ?C=:B0E 4;O 4>;82:8

input uint   PosTotal=10;         //:>;8G5AB2> 4>;82>G=KE A45;>:

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

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

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

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

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

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

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

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

input uint JLength=15;                                 //3;C18=0 JMA A3;06820=8O                   

input int JPhase=100;                                  //?0@0<5B@ JMA A3;06820=8O,

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

input ENUM_APPLIED_VOLUME VolumeType=VOLUME_TICK;      //>1JQ<            

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

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

//---- 1JO2;5=85 F5;KE ?5@5<5==KE 4;O E@0=5=8O ?5@8>40 3@0D8:0 2 A5:C=40E 

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 AnalysisOnBarsJFatlCandle_vol

   InpInd_Handle=iCustom(Symbol(),InpInd_Timeframe,"AnalysisOnBarsJFatlCandle_vol",JLength,JPhase,0,VolumeType);

   if(InpInd_Handle==INVALID_HANDLE)

     {

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

      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);



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

   min_rates_total=39+30;

   min_rates_total+=int(3+SignalBar);

//----

   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

   static bool ReBUY_Open=false,ReSELL_Open=false;

//---- 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;

   static string RePosComment="";



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

//| ?@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;

      ReBUY_Open=false;

      ReSELL_Open=false;

      Recount=false;

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

      double Value[2];



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

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



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

      if(Value[1]==2)

        {

         if(BuyPosOpen && Value[0]<2) 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(Value[1]==0)

        {

         if(SellPosOpen && Value[0]>0) SELL_Open=true;

         if(BuyPosClose) BUY_Close=true;

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

        }



      //---- >;CG5=85 A83=0;>2 4;O 4>;82>:

      if(PositionSelect(Symbol())) //@>25@:0 =0 =0 =0;8G85 >B:@KB>9 ?>78F88

        {

         //---- >;CG8< G8A;> 4>;82>: 2 ?>78F88

         string PosComment=PositionGetString(POSITION_COMMENT); //:><<5=B0@89 : ?>78F88

         int OrderTotal;

         double OpenPrice,LastLot;

         if(PosComment=="")

           {

            OrderTotal=0; //G8A;> 4>;82>: 2 ?>78F88

            OpenPrice=PositionGetDouble(POSITION_PRICE_OPEN); //&5=0 >B:@KB8O ?>A;54=59 4>;82:8

            LastLot=PositionGetDouble(POSITION_VOLUME); // 07<5@ ;>B0 ?>A;54=59 4>;82:8

           }

         else

           {

            OrderTotal=GetTotalFromStringComment(PosComment); //G8A;> 4>;82>: 2 ?>78F88

            OpenPrice=GetPriceFromStringComment(PosComment); //&5=0 >B:@KB8O ?>A;54=59 4>;82:8

            LastLot=GetLotFromStringComment(PosComment); // 07<5@ ;>B0 ?>A;54=59 4>;82:8

           }



         if(OrderTotal<int(PosTotal)) //?@>25@:0 G8A;0 4>;82>: =0 ;8<8B G8A;0 4>;82>:

           {

            ENUM_POSITION_TYPE PosType=ENUM_POSITION_TYPE(PositionGetInteger(POSITION_TYPE));



            if(PosType==POSITION_TYPE_SELL)

              {

               double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);

               double point=SymbolInfoDouble(Symbol(),SYMBOL_POINT);

               if(!Bid || !point) {Recount=true; return;} //=5B 40==KE 4;O 40;L=59H53> @0AGQB0

               int len=int((OpenPrice-Bid)/point); //@0AAB>O=85 2 ?C=:B0E <564C B5:CI59 F5=>9 8 F5=>9 >B:@KB8O

               if(len>int(PriceStep)) //?@>25@:0 =0 <8=8<0;L=>5 @0AAB>O=85 <564C 4>;82:0<8

                 {

                  ReSELL_Open=true;

                  //---- =D>@<0F8O > =>2>9 4>;82:5 4;O :><<5=B0@8O : ?>78F88

                  int NewOrderTotal=OrderTotal+1;

                  if(NewOrderTotal<10) RePosComment="  "+string(NewOrderTotal);

                  else if(NewOrderTotal<100) RePosComment=" "+string(NewOrderTotal);

                  else if(NewOrderTotal<1000) RePosComment=string(NewOrderTotal);

                  RePosComment=RePosComment+"/"+DoubleToString(Bid,_Digits);

                 }

              }



            if(PosType==POSITION_TYPE_BUY)

              {

               double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);

               double point=SymbolInfoDouble(Symbol(),SYMBOL_POINT);

               if(!Ask || !point) {Recount=true; return;}  //=5B 40==KE 4;O 40;L=59H53> @0AGQB0

               int len=int((Ask-OpenPrice)/point); //@0AAB>O=85 2 ?C=:B0E <564C B5:CI59 F5=>9 8 F5=>9 >B:@KB8O

               if(len>int(PriceStep)) //?@>25@:0 =0 <8=8<0;L=>5 @0AAB>O=85 <564C 4>;82:0<8

                 {

                  ReBUY_Open=true;

                  //---- =D>@<0F8O > =>2>9 4>;82:5 4;O :><<5=B0@8O : ?>78F88

                  int NewOrderTotal=OrderTotal+1;

                  if(NewOrderTotal<10) RePosComment="  "+string(NewOrderTotal);

                  else if(NewOrderTotal<100) RePosComment=" "+string(NewOrderTotal);

                  else if(NewOrderTotal<1000) RePosComment=string(NewOrderTotal);

                  RePosComment=RePosComment+"/"+DoubleToString(Ask,_Digits);

                 }

              }

           }

        }

     }



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

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

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

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

   BuyPositionClose(BUY_Close,Symbol(),Deviation_);



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

   SellPositionClose(SELL_Close,Symbol(),Deviation_);



//---- >;8205< ;>=3

   ReBuyPositionOpen_X(ReBUY_Open,Symbol(),UpSignalTime,MM,MMMode,Deviation_,StopLoss_,TakeProfit_,RePosComment);



//---- >;8205< H>@B

   ReSellPositionOpen_X(ReSELL_Open,Symbol(),DnSignalTime,MM,MMMode,Deviation_,StopLoss_,TakeProfit_,RePosComment);



//---- 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_);

//----

  }

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

//|  >;CG5=85 G8A;0 4>;82>: 87 :><5=B0 : >B:@KB>9 ?>78F88           |

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

int GetTotalFromStringComment(string comment)

  {

//----

   return(int(StringToInteger(StringSubstr(comment,0,3))));

//----

  }

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

//|  >;CG5=85 ?>A;54=59 F5=K >B:@KB8O 87 :><5=B0 : >B:@KB>9 ?>78F88 |

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

double GetPriceFromStringComment(string comment)

  {

//----

   return(StringToDouble(StringSubstr(comment,4,7)));

//----

  }

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

//|  >;CG5=85 ?>A;54=53> >1JQ<0 87 :><5=B0 : >B:@KB>9 ?>78F88       |

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

double GetLotFromStringComment(string comment)

  {

//----

   return(StringToDouble(StringSubstr(comment,12,-1)));

//----

  }

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

//|  >;CG5=85 B>@3>2>3> @57C;LB0B0 ?>A;54=59 70:@KB>9 ?>78F88       |

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

double GetLastClosePosRezalt(const string PosSymbol)

  {

//---- 0?@>A8< 2AN B>@3>2CN 8AB>@8N ?> AGQBC

   if(!HistorySelect(0,TimeCurrent())) return(0.0);



//---- ?@545;8< @07<5@ A?8A:0 A45;>:

   uint deals=HistoryDealsTotal();



//--- B5?5@L >1@01>B05< :064CN A45;:C 

   for(int pos=int(deals)-1; pos>=0; pos--)

     {

      //---- =094Q< B8:5B ?>A;54=59 70:@KB>9 A45;:8

      ulong deal_ticket=HistoryDealGetTicket(pos);

      //---- >?@545;8< A8<2>; A45;:8

      string symbol=HistoryDealGetString(deal_ticket,DEAL_SYMBOL);

      if(symbol!=PosSymbol) continue;

      //---- >?@545;8< ?@>D8B ?>A;54=59 70:@KB>9 A45;:8

      double last_profit=HistoryDealGetDouble(deal_ticket,DEAL_PROFIT);

      double last_volume=HistoryDealGetDouble(deal_ticket,DEAL_VOLUME);

      if(last_profit>=0) return(last_volume);

      else return(-last_volume);

     }

//----

   return(0.0);

  }

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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---