Assigning Multiple Variables Inside Loop


Category:
0
0

Hi Pete, Fold loops in TOS can be a pain to work with, but here’s what I’m dealing with.

Problem: Looking to assign multiple variables upon successful condition within for loop

Example Scan: The below scan finds a day in the past year that had 5x the daily average volume

def average_daily_volume = Average(data = VOLUME, length = 251)[1];

def bars_ago = fold n =1 to 251 with high_volume_day = double.nan while isnan(high_volume_day) do if volume[n] >= (average_daily_volume * 5) then n else double.nan;

The intent was to take bars_ago and create new variables:

bars_ago_high = high[bars_ago];
bars_ago_volume = high[bars_ago];

The problem is that I cannot use “bars_ago” in the [ ] because it is a double and not an integer, unable to convert.

Another solution is to assign multiple variables in the loop, but since I’m defining “bars_ago”,  I do not have the luxury of assigning other variables.

Currently my solution is to run the same loop various times for the amount of variables I have – clearly not efficient.

Thanks!

Marked as spam
Posted by (Questions: 7, Answers: 9)
Asked on August 22, 2020 10:58 am
576 views
0
Private answer

Sorry but I do not have a solution for you. Thinkorswim barely supports looping as it is. And as you discovered their "fold" looping structure does not allow working with multiple variables within the loop. Details here straight from Thinkorswim developers:

https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/fold

I find their loop structure so restrictive that I simply refuse to use it. I find other ways, which are not very elegant or efficient. But I find a way to get the job down without using that worthless fold structure at all. It takes quite some time working in this environment before the mind is able to think in those dimensions.

Marked as spam
Posted by (Questions: 37, Answers: 4091)
Answered on August 22, 2020 11:09 am
0
A very restrictive system that forces creativity. Thanks for confirming my findings
( at August 22, 2020 11:30 am)