Duplicate topic ! Was: Why Ref does not seem to work in IIf

Hi All,
I am quite new to Amibroker AFLs. I am learning looking at simple code.
I have recently written a piece of buy-sell code.
What I am trying to do is to set a HoldBuy condition for each subsequent bar after the Buy is true. If in the next bar Buy is not true, then I need to set HoldBuy to Ref(Holdbuy,-1). I have Sell (Exit Buy) which I will trigger only when it is still a HoldBuy. This helps me to avoid certain false buy-sell signals. However, the false_part of IIf statement does not work. So Holdbuy==1 only when Buy is true, at all subsequent bars holdbuy remains 0. I want it to set to 1 until the Sell(ExitBuy) happens.
The code is as follows:

//Initialize Holdbuy

Buy= <My buy condition>
Holdbuy= IIf(Buy,Buy,Ref(HoldBuy,-1);

Thanks in advance for any help.

Sorry its Holdbuy= IIf(Buy,Buy,Ref(HoldBuy,-1));
Please help

@krish67 I am on my phone but looking at your code think that you need one more closing parenthesis at the end of your HoldBuy line.

I’m not certain what you are trying to achieve but it may help you to review “state” vs “impulse” signals and then the following AmiBroker functions,



@krish67 I doubt that there is anything wrong with the “Ref” function. I see you have corrected the parenthesis but you haven’t shown us your actual signal generating code and I can elaborate on my post above now.

Impulse signals occur on a single bar when some condition becomes True, for example when a moving average crosses over, then reverts to being False on the next bar.

Another example, these are impulse signals for a trading system that enters a long position when using the good old MACD crossover signal.
BuySignal = Cross( MACD(), Signal() );
SellSignal = Cross( Signal(), MACD() );

BuySignal and SellSignal are both Impulse signals that are True for the single bar when the respective crossings take place, False otherwise.

State or level signals indicate the state, typically for many bars in sequence. In the example above, converting to state signals

Buy = Flip(BuySignal,SellSignal);
Sell = Flip(SellSignal,BuySignal);

Buy is now True for the entire period from the entry to the exit.

So below is an example of what your code may be generating.

// sample entry rules
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );
Short = Cover = 0;

Holdbuy = 0;
Holdbuy= IIf( Buy, Buy, Ref(HoldBuy, -1));

//  what you may really want 
Condition1 = Flip(Buy, Sell);

// To illustrate my point let's use an Exploration
dynamic_color = IIf(Condition1, colorYellow, colorDefault );
dynamic_color2 = IIf(Buy, colorlime, colorDefault );
dynamic_color3 = IIf(Sell, colorRose, colorDefault );

Filter = 1; // show all bars
AddColumn( C, "Close" );
AddColumn( Holdbuy, "Holdbuy", 1.0 );
AddColumn( Buy, "Buy", 1.0, colorBlack, dynamic_color2 );
AddColumn( Sell, "Sell", 1.0, colorBlack, dynamic_color3 );
AddColumn( Condition1, "Condition1", 1.0, colorBlack, dynamic_color );

And what is the above code producing,

Much of that is covered by Howard Bandy I think in his books which I highly recommend to any AmiBroker new user. I hope that helps.

Why don’t you search the forum prior to creating duplicate topics like this???