Check if a bool variable was true on previous bar

 

Hi.

I've been coding on tradingview now i'm trying to convert a simple code to an EA.


the trading view part i'm trying to copy is:

//-------------------------------------------------- BuySignal
buySignalStage1         = close<lower
buySignalStage2         = buySignalStage1[1] and close>close[1] and close>lower
buySignalStage3         = buySignalStage2[2] and close[1]<close[2] and low[1]>low[3]

but when trying to use [1] on a variable i'm getting:

'[' - array required


I've also tried offsetting every function like this in mt4 but it dosn't give me the same resault as on tradingview:
Here buySignalStage3 never gets true:

 //--- input parameters
   myBBHigh1  = iBands(Symbol(),PERIOD_CURRENT,iBands_period,iBand_deviation,0,PRICE_CLOSE,MODE_UPPER,1);
   myBBHigh2  = iBands(Symbol(),PERIOD_CURRENT,iBands_period,iBand_deviation,0,PRICE_CLOSE,MODE_UPPER,2);
   myBBHigh3  = iBands(Symbol(),PERIOD_CURRENT,iBands_period,iBand_deviation,0,PRICE_CLOSE,MODE_UPPER,3);
   myBBLow1   = iBands(Symbol(),PERIOD_CURRENT,iBands_period,iBand_deviation,0,PRICE_CLOSE,MODE_LOWER,1);
   myBBLow2   = iBands(Symbol(),PERIOD_CURRENT,iBands_period,iBand_deviation,0,PRICE_CLOSE,MODE_LOWER,2);
   myBBLow3   = iBands(Symbol(),PERIOD_CURRENT,iBands_period,iBand_deviation,0,PRICE_CLOSE,MODE_LOWER,3);
   myClose1   = iClose(Symbol(),PERIOD_CURRENT,1);
   myClose2   = iClose(Symbol(),PERIOD_CURRENT,2);
   myClose3   = iClose(Symbol(),PERIOD_CURRENT,3);
   high3    = iHigh(Symbol(),PERIOD_CURRENT,3);
   low3     = iLow(Symbol(),PERIOD_CURRENT,3);
   
   bool     buySignalStage1      = myClose1<myBBLow1;
   bool     buySignalStage2      = myClose2<myBBLow2  && myClose1>myClose2;
   bool     buySignalStage3      = myClose3<myBBLow3  && myClose2>myClose3 && myClose2>myBBLow2 && myClose1<myClose2 && myBBLow1>myBBLow3;


so my question is how do I check if my bool function was true on the previous bar?

 
In future please post in the correct section
I will move this topic to the MQL4 and Metatrader 4 section.
 
mattehalen: the trading view part i'm trying to copy is:
//-------------------------------------------------- BuySignal
buySignalStage1         = close<lower
buySignalStage2         = buySignalStage1[1] and close>close[1] and close>lower
buySignalStage3         = buySignalStage2[2] and close[1]<close[2] and low[1]>low[3]


You have to understand what Trading View means. Then translate it to MT4. In MT4 there is no "close" or "lower" etc.

In TV close means the current bid/current close price. close[1] means previous. buySignalStage1[1] means that variable value as it was on the previous candle. You must know MT4.

   bool     buySignalStage1      = myClose1<myBBLow1;

This is bogus; not Close[0] < iBands(… 0) buySignalStage1[1] would then be Close[1] < iBands(… 1)

Do not post code that will not compile. Show us your attempt (using the CODE button) and state the nature of your problem.
          No free help 2017.04.21

Or pay someone. Top of every page is the link Freelance.
          Hiring to write script - General - MQL5 programming forum 2018.05.12

 
William Roeder:


You have to understand what Trading View means. Then translate it to MT4. In MT4 there is no "close" or "lower" etc.

In TV close means the current bid/current close price. close[1] means previous. buySignalStage1[1] means that variable value as it was on the previous candle. You must know MT4.

This is bogus; not Close[0] < iBands(… 0) buySignalStage1[1] would then be Close[1] < iBands(… 1)

Do not post code that will not compile. Show us your attempt (using the CODE button) and state the nature of your problem.
          No free help 2017.04.21

Or pay someone. Top of every page is the link Freelance.
          Hiring to write script - General - MQL5 programming forum 2018.05.12

OK to clarify. What i'm looking for is how to I check I my buySignal1 was true on previous bar. In TV I can use [1] to get that. Buy in MT4 I get the error.
I think I need to send my 
buySignal1 to an Array so it logs all those values or i'm I wrong? this might be called buffering values as in indicators? Or is there a simple way like in TV.

 
buySignalStage1         = close<lower <-> close_current<lower_current
buySignalStage2         = buySignalStage1[1] and close>close[1] and close>lower <-> close_1_bar_ago<lower_1_bar_ago && close_current>close_1_bar_ago && close_current>lower_current
buySignalStage3         = buySignalStage2[2] and close[1]<close[2] and low[1]>low[3] <-> the buySignalStage2's formula 2 bars ago && close_1_bar_ago < close_2_bar_ago && low_1_bar_ago>low_3_bar_ago

buySignalStage2 formula applied 2 bars ago would be something like this :

close_1+2_bar_ago<lower_1+2_bar_ago && close_current+2>close_1+2_bar_ago && close_current+2>lower_current+2

... so with your code ... 

   bool     buySignalStage1      = iClose(Symbol(),PERIOD_CURRENT,0)<iBands(Symbol(),PERIOD_CURRENT,iBands_period,iBand_deviation,0,PRICE_CLOSE,MODE_LOWER,0); <--- it's current bar
   bool     buySignalStage2      = myClose1<myBBLow1  && iClose(Symbol(),PERIOD_CURRENT,0)>myClose1 && iClose(Symbol(),PERIOD_CURRENT,0)> iLow(Symbol(),PERIOD_CURRENT,0);

with : 

myClose1<myBBLow1 = buySignalStage1 1 bar ago

etc etc ... 

You could indeed save the value at each new bar in an array, but it's not recommended