Reputation: 1
I'm currently converting a ThinkScript indicator to python, however, I've run into this piece of code and I'm kinda confused on how it works:
input rollingPeriodMinutes = 60;
def factor = (SecondsFromTime(Market_Open_Time) / (60 * rollingPeriodMinutes) / 100);
def rolloverTime = if factor == Round(factor) then 1 else 0;
rec H1 = compoundValue(1, if !rolloverTime then if high > H1[1] then high else H1[1] else high, high);
rec H = compoundValue(1, if rolloverTime then H1[1] else H[1], high);
I can't really understand what is stored at the end in the variable "H". Can you help me understand? Any help is really appraciated!! Thanks
Upvotes: 0
Views: 1062
Reputation: 8739
input rollingPeriodMinutes = 60;
def
ines) and sets a variable, rollingPeriodMinutes
, to a default value of 60
. The input
declaration indicates that the user will be able to alter this value in the thinkorswim settings for this script.def factor = (SecondsFromTime(Market_Open_Time) / (60 * rollingPeriodMinutes) / 100);
factor
to a calculated value. This uses the rollingPeriodMinutes
value, above, as well as the SecondsFromTime
function and a Market_Open_Time
variable that must have been set elsewhere in the script.def rolloverTime = if factor == Round(factor) then 1 else 0;
rolloverTime
to a boolean based on the if
statement. This uses the factor
variable above (1
is true and 0
is false in thinkscript).rec H1 = compoundValue(1, if !rolloverTime then if high > H1[1] then high else H1[1] else high, high);
rec H = compoundValue(1, if rolloverTime then H1[1] else H[1], high);
rec
is actually the same as def
and has been obsoleted. Previously, it specifically declared a recursive variable; now one would just use def
regardless. See the notes below for more information.
CompoundValue
is an easy statement in thinkscript, but complicated to understand from the Learning Center reference.
In short, the declarations for H
and H1
are saying 'going back 1 bar: if no data is present, then use the if
statement to determine a value; else if data is present, then use the high
value.
Broken out, the algorithm for H1
(where high
is a reserved word for the high price for a given bar) could look like:
let numBarsBack = 1
if (data is present for the bar at numBarsBack) then
if (!rolloverTime == true) then
if high > (H1 value one bar previous) then H1 = high
else H1 = (H1 value one bar previous)
else H1 = high // thinkscript sometimes requires a "default" in `if` statements, even if there's no 3rd possible value
else (if rolloverTime == true) then H1 = high
else (if data is not present for the bar at numBarsBack) then H1 = high
*** See my complete description of how CompoundValue
works in thinkscript at the SO question "Understanding & Converting ThinkScripts CompoundValue Function".***
Notes:
SecondsFromTime
, according to the current thinkscript Learning Center reference looks like:SecondsFromTime ( int fromTime);
Description
Returns the number of seconds from the specified time (24-hour clock notation) in the EST timezone. Note that this function always returns zero when chart's aggregation period is greater than or equal to 1 day.
Input parameters
Parameter Default value Description
fromTime - Defines time from which seconds are counted, in the HHMM format, 24-hour clock notation.
rec
says this:rec
Notice: this is an article about an obsolete thinkScript® notation. Although rec variables are still supported by thinkScript®, they can be completely replaced by def.
Syntax
rec
Description
Enables you to reference a historical value of a variable that you are calculating in the study or strategy itself. Rec is short for "recursion".
Example
rec C = C[1] + volume; plot CumulativeVolume = C;
This example plots the cumulative volume starting from the beginning of the time period.
and, finally:
Remember that thinkscript code is executed for every bar in a selected period. Ie, if you're looking at 10 days with a daily period, there will be a bar for each of the 10 days; and the script will run a loop, repeating the code for each of those 10 bars. As a result, the variables will have appropriate values for each bar.
Although the OP is wanting to convert a script to Python, if someone comes here interested in how thinkscript works, there are tricks to keep a value constant for an entire script (though this section of code does not include examples for that). For information on how to do this in thinkscript, see my answer to SO question "thinkscript - How to create a variable that retains its value".
Upvotes: 0