Dealing with static datetime function - page 2

 
Luandre Ezra: hmm becoming a little bit confuse here. what do you mean by calling it again on the same tick? In my code, did this means calling it again?
if(NewBar() && HAGreenCandleCurrentPeriod(1))
   Alert("New Bar & Green Candle");
if(NewBar() && !HAGreenCandleCurrentPeriod(1))
   Alert("New Bar & Red Candle");

Those are two separate functions calls. You fixed it in #4.

 

Ok so far I understand that it is better to use a variable than a function for dealing with the static datetime function. I'm adding a new condition for opening a position. my code is like this:

void OnTick()
  {

   static datetime preTime=0,curTime;
   bool newBar=preTime!=(curTime=iTime(Symbol(),Period(),0));

   if(!CheckIfOpenOrdersByMagicNumber(MagicNumber))
        {
         if((newBar &&
            (HAGreenCandleHigherPeriod() && HAGreenCandleCurrentPeriod(1)) &&
            (HAGreenCandleCurrentPeriod(1) > EMACurrentTimeframe(1)) &&
            (FuncMACDCrossOver(1) == 1 && (MACDFast(1) < 0 && MACDSlow(1) < 0)))
           {   
            preTime=curTime;    
            Print("it's buy");
           }

         if((newBar &&
            (!HAGreenCandleHigherPeriod()   && !HAGreenCandleCurrentPeriod(1)) &&
            (!HAGreenCandleCurrentPeriod(1) < EMACurrentTimeframe(1)) &&
            (FuncMACDCrossOver(1) == 2 && (MACDFast(1) > 0 && MACDSlow(1) > 0)))
           {
            preTime=curTime;
            Print("it's sell");
           }
        }
     

that code always return the it's sell and never says about buy. I assume it only works only on this line of code. From the previous code into this code there's not much different except checking if there's a open order by magic number and few new condition.

 

First of all, this does not detect a new bar.

   static datetime preTime=0,curTime;
   bool newBar=preTime!=(curTime=iTime(Symbol(),Period(),0));

This does

   static datetime preTime=0,curTime;
   bool newBar=false;
   curTime=Time[0];
   if(preTime!=curTime)
     {
      preTime=curTime;
      newBar=true;
     }
 

I'm confused.

          if((newBar &&
            (HAGreenCandleHigherPeriod() && HAGreenCandleCurrentPeriod(1)) &&  //This seems to be checking if 2 bools are true
            (HAGreenCandleCurrentPeriod(1) > EMACurrentTimeframe(1)) &&        //This is checking if a bool is larger than a double??????
            (FuncMACDCrossOver(1) == 1 && (MACDFast(1) < 0 && MACDSlow(1) < 0)))
 

The code from Ernst (on previous page) is working but I don’t know why it’s not working after I add new condition. I will try to use your code and see if it works on my code.

          if((newBar &&
            (HAGreenCandleHigherPeriod() && HAGreenCandleCurrentPeriod(1)) &&  //This seems to be checking if 2 bools are true
            (HAGreenCandleCurrentPeriod(1) > EMACurrentTimeframe(1)) &&        //This is checking if a bool is larger than a double??????
            (FuncMACDCrossOver(1) == 1 && (MACDFast(1) < 0 && MACDSlow(1) < 0)))
I just realize it that I’m comparing the bool with double. That line should tell me wether the price is above the ema line. I think I’m to focus on that static datetime that I use the bool and not the price. Let me try your code if it works in my code.
 
Luandre Ezra:

The code from Ernst (on previous page) is working but I don’t know why it’s not working after I add new condition. I will try to use your code and see if it works on my code.

I just realize it that I’m comparing the bool with double. That line should tell me wether the price is above the ema line. I think I’m to focus on that static datetime that I use the bool and not the price. Let me try your code if it works in my code.

Hi already change the code and it works now. Thank you for your assist Keith