Conditions based on the candle type of the final candle of the trading day.

[Deleted]  

Hi

To accuratly work out my pivot points I need to log the candle type of the final candle that closes at 2200 on the 1min chart. My EA is not attached to the 1 min chart though.

I am new to coding but keen to learn and I have this:

//2200 time to work out the pivot points
double Pivot(string up_dn){int time = StrToTime("22:00")-(Period()*60),shift;
   if(TimeCurrent()<=time){
      shift = iBarShift(Symbol(),Period(),iTime(Symbol(),1440,1)+(22*3600)-(Period()*60),false);
   }else{
      shift = iBarShift(Symbol(),Period(),iTime(Symbol(),1440,0)+(22*3600)-(Period()*60),false);
   }
   int    count    = 1440/Period();
   double spr      = MarketInfo(Symbol(),MODE_SPREAD)*Point*mno;
   //working out the days highest and lowest prices (PDayHigh and PDayLow)
   double PDayHigh = iHigh(Symbol(),Period(),iHighest(Symbol(),Period(),MODE_HIGH,count,shift))+spr;//day highest plus spread
   double PDayLow  = iLow (Symbol(),Period(),iLowest (Symbol(),Period(),MODE_LOW ,count,shift));//day lowest spread not used

//Now here we can working out the day closing price (Dayclose) by checking the details of the last candle of the pivot point day on the 1min chart
int Dayclose, Ten_o_clock_postman(){int time; string ten = "22:00";// :))

   if(TimeCurrent()>=StrToTime(ten)){
      time = StrToTime(ten)-60;
   }else{
      time = StrToTime(ten)-(1439*60);
   }
   // here we determine if we have a bull candle
   if(iClose(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,time,false))>//last candle close
      iOpen (Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,time,false)))// last candle open
      
     {Dayclose = iHigh(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,time,false))-spr;}//because this candle is bull, Dayclose= high(of this candle) - spread
     
    // here we determine if we have a bear candle  
   if(iClose(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,time,false))<//last candle close
      iOpen (Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,time,false)))// last candle open
      
      {Dayclose = iLow(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,time,false))+spr;}//because this candle is bear, Dayclose= low(of this candle) + spread
      
      else{
      // here we determine if the candle is neutral (close=open)
      Dayclose = (iClose(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,time,false));}//if the candle is neutral or doiji then it it just that candle close

   double Pivot    = (PDayHigh + PDayLow + Dayclose) / 3;
   double Range = PDayHigh - PDayLow;
   pivot[0] = Pivot;//pivot point 
   pivot[1] = PDayHigh - PDayLow;
   pivot[2] = 2 * pivot[0] - PDayLow;                     // R1
   pivot[3] = pivot[0] + Range;                           // R2
   pivot[4] = pivot[2] + Range;                           // R3
   pivot[5] = 2 * pivot[0] - PDayHigh;                    // S1
   pivot[6] = pivot[0] - Range;                           // S2
   pivot[7] = pivot[5] - Range;                           // S3

Unfortunately I cant get it to compile as it comes up with the unbalanced parenthesis error.

Can anyone help with this one?

Thanks

Antony

Ickyrus
893
Ickyrus  

Please dont take offence - just amusing my idiot brain.

//2200 time to work out the pivot points
double Pivot(_)
   { <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Hello whats this doing here!!!!
   ()-(()),shift;
   if()
   {
      shift = iBarShift((),(),((),)+()-(()),);
   }
   else
   {
      shift = iBarShift((),(),((),0,0)+()-(()),);
   }
   int    count    = 1440/Period();
   double spr      = MarketInfo((),);
   //working out the days highest and lowest prices (PDayHigh and PDayLow)
   double PDayHigh = iHigh((),(),((),(),,,))//day highest plus spread
   double PDayLow  = iLow (,(),iLowest ((),(),,,));//day lowest spread not used

//Now here we can working out the day closing price (Dayclose) by checking the details of the last candle of the pivot point day on the 1min chart
int Dayclose, Ten_o_clock_postman()  <<<<<<<<<<<<<<Compiler sees this as new function
   {

   if(()())
   {      
   }
   else
   {      
   }
   // here we determine if we have a bull candle
   if(iClose((),,((),,,))>iOpen ((),,((),,,)))// last candle open      
     {
     Dayclose = iHigh((),,iBarShift((),,,));
     }//because this candle is bull, Dayclose= high(of this candle) - spread
     
    // here we determine if we have a bear candle  
   if(iClose(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,time,false))< iOpen (Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,time,false)))// last candle open
      {
      Dayclose = iLow(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,time,false))+spr;
      }//because this candle is bear, Dayclose= low(of this candle) + spread
      else
      {
      // here we determine if the candle is neutral (close=open)
      Dayclose = (iClose(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,time,false));
      }//if the candle is neutral or doiji then it it just that candle close

   double Pivot    = (PDayHigh + PDayLow + Dayclose) / 3;
   double Range = PDayHigh - PDayLow;
   pivot[0] = Pivot;//pivot point 
   pivot[1] = PDayHigh - PDayLow;
   pivot[2] = 2 * pivot[0] - PDayLow;                     // R1
   pivot[3] = pivot[0] + Range;                           // R2
   pivot[4] = pivot[2] + Range;                           // R3
   pivot[5] = 2 * pivot[0] - PDayHigh;                    // S1
   pivot[6] = pivot[0] - Range;                           // S2
   pivot[7] = pivot[5] - Range;                           // S3
If your code is saved you can do a Save As to make another copy to be safe and hack the code apart to look for these kind of problems the alternative is to have a coding style that makes things clear.
[Deleted]  

Hi

Dont worry no offence taken lol, I am new to coding as you can probably see. Would you be prepared to show me what is should look like, so I can learn where I went wrong?

Thanks

Antony

Ickyrus
893
Ickyrus  

Are you trying to do something like this

---------

I am going to blab a bit some you may know sorry but what I am talking about is struture.

Starting code from the begining int start() { return(0) } is a function where { always begins the code block (mentally I suppose a code block caputers an idea) and } ends the code block so if you are writing code and you see the need of { then immediatly type its opposite } and comment it saying what it is the end of example// end for

The same would apply to open bracket () close bracket without the comment.

When starting keep { and } on lines all by themselves. Others are used to

if( A==B ){
  //intent code to show it belongs to if
}//end if( A=B ) unindent to show end of conditional code block

my preference is

if ( A==B )
  {  
   //intent code to show it belongs to if
  }//end if( A=B ) 
  //parenthese line up so you can work out later when code editing which end belongs to what begin
Z=( A+B )/2 ; //code lines up with if to show next executed line after the if statement.

Note the use of spaces and not spaces to make the conditions clear ( (a==b) && (b==c) )

finally use functions to unclutter code and keep code flow clear.

Now that is off my mind lets look at the code snipet you have posted.

double Pivot(string up_dn){int time = StrToTime("22:00")-(Period()*60),shift; 

or restyleling

double Pivot(string up_dn)
  {
   int time = StrToTime("22:00")-(Period()*60) ;
   int shift; //While combining the creation of variable names on a single line is easy 
              //it makes them difficult to see when back checking for their type.
   // more code
  }end function Pivot(string up_dn)

now at this point I cant tell what the purpose of

int Dayclose, Ten_o_clock_postman(){int time; string ten = "22:00";// :))

is doing in the middle of the code

[Deleted]  

Hi

Thanks for the reply.

I use that idicator for manual trading but for the true pivots you need to take into account the spread on the highest of the day and the day close.

For the day close I need my EA to check the very last candle of the trading day on the 1min chart, if it is bear, then its low of the candle plus spread, for a bull its high minus spread, and for neutral it is just the close.

The high and low of the day is easy to sort but the close is proving a little tricky for my coding abilities lol as my EA is attached to a few timeframes.

double Pivot    = (PDayHigh + PDayLow + Dayclose) / 3;

The Dayclose int he above line is the result of the last candle of the day in the 1 min chart as explained above.

Thanks

Antony

[Deleted]  

Hi

I have gone through the code and I seem to be getting there lol, the only issue I am getting now is the "unbalanced left parenthesis error", I have gone through the code but cant seem to solve it, can anyone help?

The error is in this code:

double Pivot(string up_dn){int time = StrToTime("22:00")-(Period()*60),shift;
   if(TimeCurrent()<=time){
      shift = iBarShift(Symbol(),Period(),iTime(Symbol(),1440,1)+(22*3600)-(Period()*60),false);
   }
   else{
      shift = iBarShift(Symbol(),Period(),iTime(Symbol(),1440,0)+(22*3600)-(Period()*60),false);
   }
   int    count    = 1440/Period();
   double spr      = MarketInfo(Symbol(),MODE_SPREAD)*Point*mno;
   double PDayHigh = iHigh(Symbol(),Period(),iHighest(Symbol(),Period(),MODE_HIGH,count,shift))+spr;
   double PDayLow  = iLow (Symbol(),Period(),iLowest (Symbol(),Period(),MODE_LOW ,count,shift));


int Dayclose; {int times; string ten = "22:00";// :))

   if(TimeCurrent()>=StrToTime(ten)){
      times = StrToTime(ten)-60;
   }
   else{
      times = StrToTime(ten)-(1439*60);
   }
   // here we determine if we have a bull candle
   if(iClose(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,times,false))>//last candle close
      iOpen (Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,times,false)))// last candle open
      
     {Dayclose = iHigh(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,times,false))-spr;}//because this candle is bull, Dayclose= high(of this candle) - spread
     
    // here we determine if we have a bear candle  
   if(iClose(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,times,false))<//last candle close
      iOpen (Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,times,false)))// last candle open
      
      {Dayclose = iLow(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,times,false))+spr;}//because this candle is bear, Dayclose= low(of this candle) + spread
      
      else{
      // here we determine if the candle is neutral (close=open)
      Dayclose = (iClose(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,times,false));}//if the candle is neutral or doiji then it it just that candle close

   double Pivot    = (PDayHigh + PDayLow + Dayclose) / 3;
   double Range = PDayHigh - PDayLow;
   pivot[0] = Pivot;//pivot point 
   pivot[1] = PDayHigh - PDayLow;
   pivot[2] = 2 * pivot[0] - PDayLow;                     // R1
   pivot[3] = pivot[0] + Range;                           // R2
   pivot[4] = pivot[2] + Range;                           // R3
   pivot[5] = 2 * pivot[0] - PDayHigh;                    // S1
   pivot[6] = pivot[0] - Range;                           // S2
   pivot[7] = pivot[5] - Range;                           // S3 

   if(DrawPivots){
      SetArrow(4,Red,       pivot[2]," R1 ");
      SetArrow(4,Blue,      pivot[5]," S1 ");
      SetArrow(4,Red,       pivot[3]," R2 ");
      SetArrow(4,Blue,      pivot[6]," S2 ");
      SetArrow(4,Red,       pivot[4]," R3 ");
      SetArrow(4,Blue,      pivot[7]," S3 ");            
      SetArrow(4,Gray  ,    pivot[0]," Pivot ");
   }
   if(up_dn == "lo"){
      return(Pivot - Range);
   }   
   if(up_dn == "hi"){
      return(Pivot + Range);
   }
   
   return(Pivot); 
   }
}

Thanks

Antony

Ickyrus
893
Ickyrus  
//+------------------------------------------------------------------+
//|                                                     TestBed.mq4  |
//|                                                           Marcus |
//|                                                                  |
//+------------------------------------------------------------------+
extern int StartHr = 22 ;

double pivot[7] ; // Array declared outside of start means variable can be used in any function declared after it
double spr ;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   spr= MarketInfo( Symbol(), MODE_SPREAD )*Point ; 
   // Was -  MarketInfo(Symbol(),MODE_SPREAD)*Point*mno - don't know what mno was for so took it out
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

int start()
  {
   int shift = LastPeriodBar( Period(), StartHr ) ; //find the bar just before the start hour 
   int count = PERIOD_D1/Period() ;

   //working out the days highest and lowest prices (PDayHigh and PDayLow)
   int H_Bar = iHighest( Symbol(), Period(), MODE_HIGH, count, shift ) ; //looks backwards for count bars starting at shift
   int L_Bar = iLowest ( Symbol(), Period(), MODE_LOW, count, shift ) ;
   double PDayHigh = iHigh( Symbol(), Period(), H_Bar )+spr;  //day highest plus spread
   double PDayLow  = iLow ( Symbol(), Period(), L_Bar );       //day lowest spread not used

//Now here we can working out the day closing price (Dayclose) by checking the details of the last candle of the pivot point day on the 1min chart
   double Dayclose=DayClose(PERIOD_M1, StartHr) ;
   double Pivot    = (PDayHigh + PDayLow + Dayclose) / 3;
   double Range = PDayHigh - PDayLow;
   pivot[0] = Pivot;//pivot point 
   pivot[1] = PDayHigh - PDayLow;
   pivot[2] = 2 * pivot[0] - PDayLow;                     // R1
   pivot[3] = pivot[0] + Range;                           // R2
   pivot[4] = pivot[2] + Range;                           // R3
   pivot[5] = 2 * pivot[0] - PDayHigh;                    // S1
   pivot[6] = pivot[0] - Range;                           // S2
   pivot[7] = pivot[5] - Range;                           // S3
   
   return(0) ;
  }
  
/////function section  

int LastPeriodBar( int period, int hr )
  {     
   int shift, yesterday, today ;
   int time=StrToTime(IntToStr(hr)+":00")-(period*60); // On 1min chart time = 21:59 on 5mim chart time =21:55 on 1hr chart time= 21:00
   if (TimeCurrent()<=time)                            //if the brokers server time is less than or equal to time worked out on the first line
     {
      yesterday=iTime(Symbol(),1440,1) ;//server midnight so just yesterdays date in seconds 
      yesterday=yesterday+(hr*3600) ;  //add hours as seconds - note all time is in seconds
      yesterday=yesterday-period*60 ;  //take off time period of last candle
      shift = iBarShift(Symbol(),period,yesterday,false); //ie find candle position just before 22:00 
     }
   else
     {
      today = iTime(Symbol(),1440,0) ;
      today = today + (hr*3600) ;
      today = today - (period*60) ;
      shift = iBarShift(Symbol(),period,today,false); //ie just past 22:00 so use todays candle to mark end of last 24hr
     }
  return(shift) ;   
  }// end function LastPeriodBar
/////////////

double DayClose(int period, int Hr)
   {
    double Dayclose ; //local function variable   
    int shift ;
    
    shift=LastPeriodBar( period, Hr ) ;
    if( iClose(Symbol(), period, shift) > iOpen(Symbol(), period, shift) )// Close strictly greater than Open candle is bullish
      { 
       Dayclose = iHigh(Symbol(), period, shift)-spr; //because this candle is bull, Dayclose= high(of this candle) - spread
      }     
   // here we determine if we have a bear candle  
   if( iClose(Symbol(), period, shift) < iOpen(Symbol(), period, shift) )// Close strictly less than Open candle is bearish
      {
       Dayclose = iLow(Symbol(), period, shift)+spr; //because this candle is bear, Dayclose= low(of this candle) + spread
      }
   else
      { // here we determine if the candle is neutral (close=open)
       Dayclose = iClose(Symbol(), period, shift ); //if the candle is neutral or doiji then it it just that candle close
      }
   return(Dayclose) ;
  }
    
//////////////

string IntToStr(int myInt ) // Cant find a MT4 function inttostr so this will have to do.
  {
   return( myInt ) ;
  }//end function IntToStr
/////////////  


/*  Notes
looking at the definition of the function iHighest
int iHighest( string symbol, int timeframe, int type, int count=WHOLE_ARRAY, int start=0) 
Returns the shift of the maximum value over a specific number of periods depending on type. 
Parameters:
symbol   -   Symbol the data of which should be used to calculate indicator. NULL means the current symbol. 
timeframe   -   Timeframe. It can be any of Timeframe enumeration values. 0 means the current chart timeframe. 
type   -   Series array identifier. It can be any of the Series array identifier enumeration values. 
count   -   Number of periods (in direction from the start bar to the back one) on which the calculation is carried out. 
start   -   Shift showing the bar, relative to the current bar, that the data should be taken from.

varable "mno" mentioned in code but no idea what it is for
*/ 

Hopefully this will explain what is happening - (Took me long enough!)

not too sure what happens on H4 charts and will probably fail on weekly monthly charts.

[Deleted]  

Hi

Thanks for that :) I will have a look through that, as this is part of my EA that has many other functions, I will have to make a few adjustments? ie start() function is used elsewhere?

Thanks

Antony

Ickyrus
893
Ickyrus  
tonyjms2005:

Hi

Thanks for that :) I will have a look through that, as this is part of my EA that has many other functions, I will have to make a few adjustments? ie start() function is used elsewhere?

Thanks

Antony


"as this is part of my EA that has many other functions"

That is why it is so difficult to help you!

[Deleted]  

Hi

Thanks for your help, I really do appreciate it, it has tied up some loose ends, so I wanted to test what I had learnt and so came up with this as well and it seems to have worked :)

Here take a look, but I have found another problem, instead of using times I think it will be better to use pivots days like in the pivot point indicator, here is my code first:

double close, Pivot(string up_dn){int time = StrToTime("21:00") -(Period()*60),shift;
if(TimeCurrent()<=time){
      shift = iBarShift(Symbol(),Period(),iTime(Symbol(),1440,1)+(22*3600)-(Period()*60),false);
   }else{
      shift = iBarShift(Symbol(),Period(),iTime(Symbol(),1440,0)+(22*3600)-(Period()*60),false);
   }
   int    count    = 1440/Period();
   double spr      = MarketInfo(Symbol(),MODE_SPREAD)*Point;
   double PDayHigh = iHigh(Symbol(),Period(),iHighest(Symbol(),Period(),MODE_HIGH,count,shift))+spr;
   double PDayLow  = iLow (Symbol(),Period(),iLowest (Symbol(),Period(),MODE_LOW ,count,shift));
   int Times;
   if(Ten_o_clock_postman()>0){close = iLow(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,Times,false))+ spr;}
   if(Ten_o_clock_postman()<0){close  = iHigh(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,Times,false))- spr;}
   else{
   close = iClose(Symbol(),Period(),shift);}
   double Pivot    = (PDayHigh + PDayLow + close) / 3;    // Pivot point
   double Range = PDayHigh - PDayLow;
   pivot[0] = (PDayHigh + PDayLow + close) / 3;
   pivot[1] = PDayHigh - PDayLow;
   pivot[2] = 2 * pivot[0] - PDayLow;                     // R1
   pivot[3] = pivot[0] + Range;                           // R2
   pivot[4] = pivot[2] + Range;                           // R3
   pivot[5] = 2 * pivot[0] - PDayHigh;                    // S1
   pivot[6] = pivot[0] - Range;                           // S2
   pivot[7] = pivot[5] - Range;                           // S3

   if(DrawPivots){
      SetArrow(4,Red,       pivot[2]," R1 ");
      SetArrow(4,Blue,      pivot[5]," S1 ");
      SetArrow(4,Red,       pivot[3]," R2 ");
      SetArrow(4,Blue,      pivot[6]," S2 ");
      SetArrow(4,Red,       pivot[4]," R3 ");
      SetArrow(4,Blue,      pivot[7]," S3 ");            
      SetArrow(4,Gray  ,    pivot[0]," Pivot ");
   }
   if(up_dn == "lo"){
      return(Pivot - Range);
   }   
   if(up_dn == "hi"){
      return(Pivot + Range);
   }
   return(Pivot); 
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int Ten_o_clock_postman(){int Times; string ten = "22:00";// :))
   if(TimeCurrent()>=StrToTime(ten)){
      Times = StrToTime(ten)-60;
   }else{
      Times = StrToTime(ten)-(1439*60);
   }
   if(iClose(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,Times,false))>
      iOpen (Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,Times,false))){return( 1);}
   if(iClose(Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,Times,false))<
      iOpen (Symbol(),PERIOD_M1,iBarShift(Symbol(),PERIOD_M1,Times,false))){return(-1);}      
   return(0);
}

Could I use this instead of the 2200 times?:

int PivotDay( datetime BarTime, datetime ShiftHrs )
{
   int PDay = TimeDayOfWeek( BarTime + ShiftHrs * 3600 );

      if( PDay == 0 ) PDay = 1;      // Count Sunday as Monday
      if( PDay == 6 ) PDay = 5;      // Count Saturday as Friday

   return( PDay );
}

Thanks

Antony

Ickyrus
893
Ickyrus  

The first snipit of code should not even compile - too many Pivot variables.

Whatever it is you are trying to do seems to revolve around three key values - PDayHigh, PDayLow, Dayclose. I sugest you turn the getting of these values into functions. I already turned Dayclose into a function for you and this function will work for any time period (execpt H4 upwards) and can be made independent of the chart you attach it to.