Reference previous value in a loop

Hi,

I have a scenario where there is some processing and changes happening at each bar (Options Strategy). I have employed the use of loops for the same since it helps me track each array value on an individual basis as well.

The challenge that I am facing is this - Some values of the array are essentially to take its previous value before going into processing and I am using the following code for it

Within the for loop with i as the variable:

variable = variable[i-1];

This works perfectly well for most of the scenarios, however, in some cases (Mostly when there is a change in day) - the value changes. I tried using exploration to understand the same.

If the exploration is done for a single day, it works fine, however if the exploration is done for a couple of days the same problem arises (the previous variable value gets changed - in fact the way amibroker handles the previous day is also changed).

Would like some input on how I can go about this.

To give an example, one variable I am tracking is "change" - this essentially counts the number of changes the strategy is keeping. For day 1 - this value is 3, now when I go to day 2, it should have continued from "3" but it has a value of "2".

Please let me know if there is any additional information required.

Regards
Siddhant Kankani

@siddhantkankani

Loops are rarely required with AFL. But here are a couple of things that might help...

// in your loop
variable[i] = variable[i-1];

// Non Looping
varB = Ref(VarA, -1);


1 Like

Thanks @snoopy.pa30

The reason I am using loops is because of the dynamic nature of the strategy.

The instrument to be traded and the quantity can change every 15mins, hence the need to check multiple conditions / values. I thought using looping would be the best method, although I'm sure there might be easier methods like you said.

Within a loop, how do I ensure that variable values do not change? I mean is there anything wrong with:

variable[i] = variable[i-1];

It seems to work for most part except a few scenarios. So in a test of 200 odd weeks, around 30 weeks, it gives me some issues. Unable to get my head around it.

Regards
Siddhant kankani

@siddhantkankani, to get further help, as a minimum1), you need to post the entire formula.

1) Please, review steps 2 and 3 of the linked topic

@siddhantkankani,

As I showed in my previous post, I used a different variable name, so you can choose when to use each, Var A or VarB.

As @beppe said, you need to post code. If you don't want to reveal your "secret" money making functions, you will have to make a minimal version that replicates the problem.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.