Summing Up Non-Contiguous Cyclic Bar Values in Loop


0
0

Anyway of adding  up/ summing up  non-continuous or non-contiguous  bar values?

I wanted to sum up  close[16] + close [31]  + close[46] , etc. for 50 times, not just  3.

Tried

def TF=15;

def CumSum = fold i = 1 to Length with Sum15=close[1] do Sum + close[i*TF+1];

to

def CumSum = fold i = 1 to Length with Sum15=close[1] do

{j=TF*i+1;

Sum + close[i*TF+1];}

to

def sumTF= if SecondsfromTime(0000)- Floor(SecondsfromTime(0000)/TF*60*1000) =< 60,000;

then close else double.nan;

to

def sumTF= if RegularTradingEnd(GetTime())- Floor(RegularTradingEnd (GetTime())

/TF*60*1000) =<60000; # of milliseconds  1 minute, the chart it is on.

then close else double.nan;  (Works but no plot with or w/o double.nan,say 0)

 

 

 

RESOLVED
Marked as spam
Posted by (Questions: 13, Answers: 18)
Asked on March 4, 2018 4:12 pm
1182 views
0
Private answer

I hate doing loops in Thinkscript. Their fold method of looping is completely lame and very difficult to apply. Makes my brain hurt to even look at that stupid thing. (not holding anything back here). Yet I have used it to build some really cool stuff.

Be sure to check the example sma listed here: http://toslc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/fold.html

Take note of the use of the GetValue() function. Something I did not see used in your examples. This is essential in getting things working correctly. Read more about the GetValue() function here: http://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Others/GetValue.html

Sorry to say I am not very motivated to work out a solution using fold in the free user forum.

So, I will offer some thoughts on how to work without it. Everything in thinkscript is an array. All the data and variables exist as arrays. This occurs because the platform itself runs a loop through the chart and executes each line of a script once pre bar. So we already have a loop in place, one that we can leverage when we understand how to operate within it.

If I wanted to take the sum of a select set of bars on the chart. I would build a true/false statement that marks each of the bars I want to include in the set. Then I would use TotalSum() or Sum() function depending on whether I wanted to compute the cumulative value for the entire chart of just for a select number of previous bars. (the TotalSum() and Sum() functions actually perform their own loop, to look back and compute values based on previous bars)

Hope it helps. Good luck.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on March 4, 2018 5:01 pm
0
Private answer

I used

def cum= cum[1]+1;

def boolarr= if (cum/TF)- Floor(cum/TF)==0 then 1 else 0;

plot SMA=(fold n = 1 to TF*Length with s15 do + (boolarr[n]*close[n]*volume[n])/Length )

this for projecting a  TF=15  , or 15 min Simple Moving Avg onto a 1 min Chart.  It worked great. Thanks

Marked as spam
Posted by (Questions: 13, Answers: 18)
Answered on March 11, 2018 1:40 pm
0

So does this allow you to bypass the secondary time frame restriction on Study Alerts and Conditional Orders? Trying to guess why you go through all this trouble unless you were in a place where you could not use:

def sma = Average(close(period = AggregationPeriod.FIFTEEN_MIN), Length);

( at March 11, 2018 2:39 pm)