MorningFlat_V3_v1

Author: Scriptong
1 Views
0 Downloads
0 Favorites
MorningFlat_V3_v1
ÿþ//+-------------------------------------------------------------------------------------+

//|                                                                  MorningFlat_V3.mq4 |

//|                                                                           Scriptong |

//|                                                                                     |

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

#property copyright "Scriptong"

#property link      "http://advancetools.net/"

#property strict



// > 2B>@>9 25@A88 4>102;5=> >B>1@065=85 H8@8=K :0=0;0

//  B@5BL59 25@A88 4>102;5=0 2>7<>6=>ABL C:070=8O <8=CB



#property indicator_chart_window

#property indicator_buffers 4

#property indicator_plots   4



//--- plot g_arrfUp

#property indicator_color1  clrTurquoise

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot g_arrfDn

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot g_arrfTargetUp

#property indicator_color3  clrGoldenrod

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot g_arrfTargetDn

#property indicator_color4  clrSilver

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1





input int      i_nStartHour      = 0;                                   // Day start hour

input int      i_nStartMinute    = 15;                                  // Day start minute

input int      i_nEndHour        = 8;                                   // Day end hour

input int      i_nEndMinute      = 45;                                  // Day end minute

input double   i_fTargetLevel    = 161.8;                               // Target level, %

input color    i_clrUpColor      = clrTurquoise;                        // Color of upper limit

input color    i_clrDnColor      = clrRed;                              // Color of lower limit

input color    i_clrTargetUp     = clrGoldenrod;                        // Upper target line color

input color    i_clrTargetDn     = clrSilver;                           // Lower target line color



#define PREFIX             "LAB_"

#define SECONDS_PER_DAY    datetime(60 * 60 * 24)



//---- buffers

double g_arrfUp[];

double g_arrfDn[];

double g_arrfTargetUp[];

double g_arrfTargetDn[];

bool Activate = false;

datetime dtLastDay;

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

//| Custom indicator initialization function                                            |

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

int OnInit()

{

//---- indicators

   if(Period()>PERIOD_H1)

   {

      Print("=48:0B>@ @01>B05B =0 B09<D@59<0E <5=LH5 H4!");

      return INIT_SUCCEEDED;

   }



   if(i_nStartHour<0 || i_nEndHour<0 || i_nStartHour>23 || i_nEndHour>23 || 

      i_nStartHour>i_nEndHour || (i_nStartHour==i_nEndHour && i_nStartMinute>=i_nEndMinute))

     {

      Alert("=0G5=8O i_nStartHour 8 i_nEndHour 4>;6=K ;560BL 2 480?07>=5 >B 0 4> 24 8 ",

              "i_nStartHour < i_nEndHour.");

      return INIT_FAILED;

     }



   if(i_nStartMinute<0 || i_nEndMinute<0 || i_nStartMinute>59 || i_nEndMinute>59)

     {

      Alert("=0G5=8O i_nStartHour 8 i_nEndHour 4>;6=K ;560BL 2 480?07>=5 >B 0 4> 24 8 ",

              "i_nStartHour < i_nEndHour.");

      return INIT_FAILED;

     }

     

   if (!BuffersBind())

      return INIT_FAILED;





   Activate = true;

//----

   return INIT_SUCCEEDED;

}

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

//| Custom indicator deinitialization function                                          |

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

void OnDeinit(const int reason)

{

   ObjectsDeleteAll(0, PREFIX);

}

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

//| &5=>20O <5B:0 A> 7=0G5=85< C@>2=O                                                   |

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

void DrawLabel(datetime TimeL, double Price, color Col, string sSuffix)

{

   string sName = PREFIX + DoubleToString(TimeL,0) + sSuffix;



   if (ObjectCreate(0, sName, OBJ_ARROW_LEFT_PRICE, 0, TimeL, Price))

   {

      ObjectSetInteger(0, sName, OBJPROP_COLOR, Col);

      ObjectSetString(0, sName, OBJPROP_TOOLTIP, "\n");

   }

}

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

//| >4?8AL H8@8=K :0=0;0                                                               |

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

void DrawText(datetime TimeL, double Price, string St, color Col)

{

   string Name = PREFIX + "WIDTH_" + DoubleToString(TimeL, 0);



   if (ObjectCreate(0, Name, OBJ_TEXT, 0, TimeL, Price))

   {

      ObjectSetString(0, Name, OBJPROP_TEXT, St);

      ObjectSetInteger(0, Name, OBJPROP_FONTSIZE, 10);

      ObjectSetString(0, Name, OBJPROP_FONT, "Arial");

      ObjectSetInteger(0, Name, OBJPROP_COLOR, Col);

      ObjectSetString(0, Name, OBJPROP_TOOLTIP, "Height of channel");

      return;

   }

   

   ObjectSetString(0, Name, OBJPROP_TEXT, St);  

}

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

//| Custom indicator iteration function                                                 |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime& time[],

                const double& open[],

                const double& high[],

                const double& low[],

                const double& close[],

                const long& tick_volume[],

                const long& volume[],

                const int& spread[])

{

//----

   if(!Activate)

      return 0;



   dtLastDay = 0;





   int nTotal = 0;

   for(int i = GetRecalcIndex(nTotal, rates_total, prev_calculated); i >= 0; i--)

   {

      datetime dtTime = iTime(NULL, PERIOD_CURRENT, i);

      MqlDateTime stTime; 

      TimeToStruct(dtTime, stTime);

      if (stTime.hour <= i_nEndHour && (stTime.min <= i_nEndMinute || stTime.hour != i_nEndHour))

         continue;

         

      datetime dtBeginDay = dtTime / SECONDS_PER_DAY * SECONDS_PER_DAY;

      datetime dtNextDay = dtBeginDay + SECONDS_PER_DAY;

      

      //  MB>B 45=L C65 >B>1@065=K C@>2=8

      if (dtLastDay >= dtBeginDay)                                                                      

         continue;                                        



      // 0@, A>>B25BA2CNI89 =0G0;C ACB>: ?;NA A<5I5=85 2 G0A0E 8 <8=CB0E

      int nStartBar = iBarShift(NULL, PERIOD_CURRENT, dtBeginDay + i_nStartHour * 3600 + i_nStartMinute * 60);



      // 0@, A>>B25BAB2CNI89 ?>A;54=5<C 10@C "CB@5==53> D;MB0"

      int nFinishBar = iBarShift(NULL, PERIOD_CURRENT, dtBeginDay + i_nEndHour * 3600 + i_nEndMinute * 60) + 1;



      int ind1 = iLowest(NULL, PERIOD_CURRENT, MODE_LOW, nStartBar - nFinishBar + 1, nFinishBar);

      int ind2 = iHighest(NULL, PERIOD_CURRENT, MODE_HIGH, nStartBar - nFinishBar + 1, nFinishBar);



      if (ind1 >= 0 && ind2 >= 0)

      {

         double LowV = iLow(NULL, PERIOD_CURRENT, ind1);   // 86=OO 3@0=8F0 :0=0;0

         double HighV = iHigh(NULL, PERIOD_CURRENT, ind2); // 5@E=OO 3@0=8F0 :0=0;0

         double TargetU = (HighV - LowV) * (i_fTargetLevel - 100.0) / 100.0 + HighV;       // 5@E=OO F5;L

         double TargetD = LowV - (HighV - LowV) * (i_fTargetLevel - 100.0) / 100.0;         // 86=OO F5;L

         

         // 0=0; "CB@5==53> D;MB0"

         for(int j = nStartBar; j >= nFinishBar; j--)

         {

            g_arrfUp[j] = HighV;

            g_arrfDn[j] = LowV;

         }

         // -----------------------



         // 68405<K5 F5;8 ?@8 ?@>1>5 D;MB0  

         for(int j = nFinishBar; j >= 0; j--)

         {

            if(!(iTime(NULL, PERIOD_CURRENT, j) < dtNextDay)) 

               break;

         

            g_arrfTargetUp[j] = TargetU;

            g_arrfTargetDn[j] = TargetD;

         }

         // -------------------  



         int nBar = iBarShift(NULL, 0, dtBeginDay + i_nStartHour * 3600 + i_nStartMinute * 60);

         datetime dtBarTime = iTime(NULL, PERIOD_CURRENT, nBar);

         DrawLabel(dtBarTime, HighV, i_clrUpColor, "_U");     // 5@E=OO F5=>20ON <5B:0 :0=0;0

         DrawLabel(dtBarTime, LowV, i_clrDnColor, "_D");      // 86=OO F5=>20O <5B:0 :0=0;0

                                                          // 5@E=OO F5=>20O <5B:0 F5;8

         DrawLabel(iTime(NULL, PERIOD_CURRENT, nFinishBar), TargetU, i_clrTargetUp, "_U");

         // 86=OO F5=>20O <5B:0 F5;8

         DrawLabel(iTime(NULL, PERIOD_CURRENT, nFinishBar), TargetD, i_clrTargetDn, "_D");

         DrawText((iTime(NULL, PERIOD_CURRENT, nFinishBar) - dtBarTime) / 2 + dtBarTime, HighV,    // B>1@065=85 2KA>BK :0=0;0

                  DoubleToString(MathRound((HighV-LowV)/Point()),0),clrRed);

         dtLastDay = dtBeginDay;      // B<5G05<, GB> 2 MB>B 45=L C@>2=8 C65 1K;8 >B>1@065=K

      }

   }



   ChartRedraw();

//----

   return(rates_total);

}

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

//| !2O7K20=85 1CD5@>2 8=48:0B>@0 A <0AA820<8                                                                                                                                                         |

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

bool BuffersBind()

{

   string sName = MQLInfoString(MQL_PROGRAM_NAME);



   if (

       !SetIndexBuffer(0, g_arrfUp, INDICATOR_DATA) || 

       !SetIndexBuffer(1, g_arrfDn, INDICATOR_DATA) ||

       !SetIndexBuffer(2, g_arrfTargetUp, INDICATOR_DATA) ||

       !SetIndexBuffer(3, g_arrfTargetDn, INDICATOR_DATA)

      )

   {

      Alert(sName, ": error associating arrays with indicator buffers. Error !", GetLastError());

      return false;

   }

   

   ArraySetAsSeries(g_arrfUp, true);

   ArraySetAsSeries(g_arrfDn, true);

   ArraySetAsSeries(g_arrfTargetUp, true);

   ArraySetAsSeries(g_arrfTargetDn, true);



   for (int i = 0; i < 4; i++)   

      PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_LINE);



   IndicatorSetString(INDICATOR_LEVELTEXT, 0, "Upper channel limit");

   IndicatorSetString(INDICATOR_LEVELTEXT, 1, "Lower channel limit");

   IndicatorSetString(INDICATOR_LEVELTEXT, 2, "Upper target price");

   IndicatorSetString(INDICATOR_LEVELTEXT, 3, "Lower target price");      

   

   IndicatorSetString(INDICATOR_SHORTNAME, "Morning Flat");

   return (true);

}

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

//| =8F80;870F8O 2A5E 8=48:0B>@=KE 1CD5@>2                                                                                                                                                           |

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

void BuffersInitializeAll()

{

   ArrayInitialize(g_arrfUp, EMPTY_VALUE);     

   ArrayInitialize(g_arrfDn, EMPTY_VALUE);     

   ArrayInitialize(g_arrfTargetUp, EMPTY_VALUE);     

   ArrayInitialize(g_arrfTargetDn, EMPTY_VALUE);

}

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

//| ?@545;5=85 8=45:A0 10@0, A :>B>@>3> =5>1E>48<> ?@>872>48BL ?5@5@0AG5B                                                                                                                            |

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

int GetRecalcIndex(int& total, const int ratesTotal, const int prevCalculated)

{

   // ?@545;5=85 ?5@2>3> 10@0 8AB>@88, =0 :>B>@>< 1C4CB 4>ABC?=K 045:20B=K5 7=0G5=8O 8=48:0B>@0

   total = ratesTotal - 1;                                                                         

                                                   

   // 5@2>5 >B>1@065=85 8=48:0B>@0 8;8 ?@>87>H;0 ?>4:0G:0 40==KE, B. 5. =0 ?@54K4CI5< B8:5 10@>2 1K;> =5 =0 >48= 10@ <5=LH5, :0: ?@8 =>@<0;L=>< @0728B88 8AB>@88, 0 =0 420 8;8 1>;55 10@>2 <5=LH5

   if (prevCalculated < ratesTotal - 1)                     

   {       

      ObjectsDeleteAll(0, PREFIX);

      BuffersInitializeAll();

      return total;

   }

   

   // >@<0;L=>5 @0728B85 8AB>@88. >;8G5AB2> 10@>2 B5:CI53> B8:0 >B;8G05BAO >B :>;8G5AB20 10@>2 ?@54K4CI53> B8:0 =5 1>;LH5, G5< =0 >48= 10@

   return (MathMin(ratesTotal - prevCalculated, total));                            

}

Comments