Re: [amibroker] Is it possible to assign to an array the previous value?

 

Hello,

Thank you very much for your e-mail.

First and foremost:
please read http://www.amibroker.com/guide/h_understandafl.html
Reading this is essential to understand array processing.

You have got self-reference (recursion) in your code and you are not getting
the result you want because you forgot/did not know that Ref() as any other
array function processes entire array AT ONCE.

If you formula has to "keep" some old value, the proper array-wise way to do this
is to use ValueWhen function


PreviousValuePlaceholder = 0; // any value other than values assigned below
MySignal = IIF( Condition, 1, IIF( Condition2, -1, PreviousValuePlaceholder ) );
MySignal = ValueWhen( MySignal != PreviousValuePlaceholder, MySignal );


Of course it can be be shortened to single line:

MySignal = ValueWhen( Condition1 OR Condition2, IIF( Condition1, 1, -1 ) );

but I wanted to show general idea. Compact codes are nice but usually skip some "mental indirection" levels.

Best regards,
Tomasz Janeczko
amibroker.com

On 2016-01-22 10:16, dpellon@gmail.com [amibroker] wrote:
Hello,

Let's imagine i have two very different ema crossovers and i want to make sure that i don't get conflicting values so i first check the buy and if not then i check sell, if not either then i want previous value of signal.

Why is not getting the array the previous value?




MySignal[499]=0;

Condition1=EMA(C,10) > EMA(C,20);
Condition2=EMA(C,20) < EMA(C,40);

MySignal=IIf(Condition1, 1, IIf(Condition2, -1, Ref(MySignal,-1)));

Buy = MySignal==1;
Sell=  MySignal==-1;


Thanks and Best regards,





__._,_.___

Posted by: Tomasz Janeczko <groups@amibroker.com>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (4)

Check out the automatic photo album with 1 photo(s) from this topic.
dhgfaibh.png

**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/


.

__,_._,___


EmoticonEmoticon