Indicator Help: Market Statistics: Infamous J Perl’s Framework for ThinkorSwim (TOS)

Hey,

There’s a few indicators that I’m having trouble coding in ThinkorSwim (TOS) with Thinkscripter, although I think I have figured out parts of the indicators, however I’m new to coding in Thinkscripter, so there may be errors in my code.

Trading with Market Statistics Indicators:
  • Skew: (Intraday VWAP – POC (point of control from intraday volume profile) / Standard Deviation, with the Histogram plotted
  • Percentage Ratio of Skew to 1 Standard Deviation: ratio of the skew to the size of ONE standard Deviation. 1 StDev of the current VWAP standard Deviations....which is the distance between the bands, represented as a (%) percent
  • Each of the formulas with Adlabels on the chart
Daily Trading Statistics:
  • Position Sizing:
  • Frog SD: Standard Deviation of the Average Daily Range, represented in ($) Dollars
  • With Adlabel(s) on the chart
Here’s some links to J Perl’s Trading with Market Statistics on the following thread(s) for context to what I’m referring to:

If anyone is interested in the concepts: Links to all relevant J Perl’s popular/infamous market statistics methodology for the reasoning behind my coding requests, it’s basically a more advanced market/volume profile trading framework:
Specifically on each of the indicators, I do have some of the code figured out, here’s what I have so far with each of the indicators.

Trading with Market Statistics Indicators:
  • Skew: (Intraday VWAP – POC (point of control from intraday volume profile) / Standard Deviation, with histogram plotted on the chart
  • Percentage Ratio of Skew to 1 Standard Deviation: ratio of the skew to the size of ONE standard Deviation. 1 StDev of the current VWAP standard Deviations....which is the distance between the bands, represented as a (%) percent
  • Each of the formulas with Adlabels on the chart
The details of what I have figured out so far with Trading with Market Statistics is down below.

Skew:
Concept:
(mean - mode) / standard deviation

Equivalent Skew Equation:
Skew Definition= (Intraday VWAP (volume weighted average price) – POC (point of control from the intraday volume profile))/Standard Deviation

Here’s the Code I have for the Skew: I think it’s right, it might look ugly but I think that the math is at least right, and I got the Adlabel to pull up on the chart and I double checked the math with standard dev calculators online, it might be good to double check the math

Here’s the code for the Skew:

def RTH = GetTime() >= RegularTradingStart(GetYYYYMMDD()) and
GetTime() <= RegularTradingEnd(GetYYYYMMDD());
def n = if RTH and !RTH[1]
then 1
else if RTH
then n[1] + 1
else n[1];
def Avg = (fold i = 0 to n
with s
do s + getValue(close, i)) / n;
def VWAP_ = (fold ii = 0 to n
with ss
do ss + getValue(vwap, ii)) / n;
#StartScript;

def yyyymmdd = GetYYYYMMDD();
def day_number = DaysFromDate(First(yyyymmdd)) + GetDayOfWeek(First(yyyymmdd));


def FirstBar = SecondsFromTime(0945) >= 0 and SecondsFromTime(0945) < 60 or SecondsFromTime(1000) >= 0 and SecondsFromTime(1000) < 60 or SecondsFromTime(1015) >= 0 and SecondsFromTime(1015) < 60 or SecondsFromTime(1030) >= 0 and SecondsFromTime(1030) < 60 or SecondsFromTime(1045) >= 0 and SecondsFromTime(1045) < 60 or SecondsFromTime(1100) >= 0 and SecondsFromTime(1100) < 60 or SecondsFromTime(1115) >= 0 and SecondsFromTime(1115) < 60 or SecondsFromTime(1130) >= 0 and SecondsFromTime(1130) < 60 or SecondsFromTime(1145) >= 0 and SecondsFromTime(1145) < 60 or SecondsFromTime(1200) >= 0 and SecondsFromTime(1200) < 60 or SecondsFromTime(1215) >= 0 and SecondsFromTime(1215) < 60 or SecondsFromTime(1230) >= 0 and SecondsFromTime(1230) < 60 or SecondsFromTime(1245) >= 0 and SecondsFromTime(1245) < 60 or SecondsFromTime(1300) >= 0 and SecondsFromTime(1300) < 60 or SecondsFromTime(1315) >= 0 and SecondsFromTime(1315) < 60 or SecondsFromTime(1330) >= 0 and SecondsFromTime(1330) < 60 or SecondsFromTime(1345) >= 0 and SecondsFromTime(1345) < 60 or SecondsFromTime(1400) >= 0 and SecondsFromTime(1400) < 60 or SecondsFromTime(1415) >= 0 and SecondsFromTime(1415) < 60 or SecondsFromTime(1430) >= 0 and SecondsFromTime(1430) < 60 or SecondsFromTime(1445) >= 0 and SecondsFromTime(1445) < 60 or SecondsFromTime(1500) >= 0 and SecondsFromTime(1500) < 60 or SecondsFromTime(1515) >= 0 and SecondsFromTime(1515) < 60 or SecondsFromTime(1530) >= 0 and SecondsFromTime(1530) < 60 or SecondsFromTime(1545) >= 0 and SecondsFromTime(1545) < 60 or SecondsFromTime(1600) >= 0 and SecondsFromTime(1600) < 60;


def cond = firstbar;
profile VP = VolumeProfile(pricePerRow = PricePerRow.TICKSIZE, startNewProfile = cond, onExpansion = no);
VP.Show(color = Color.BLACK, "volume poc color" = Color.BLUE);

plot VPOC = VP.GetPointOfControl();
VPOC.SetPaintingStrategy(PaintingStrategy.DASHES);
VPOC.SetDefaultColor(GetColor(9));

#EndScript

def StDev = Sqrt((fold iii = 0 to n
with sss = 0
do sss + Sqr(Avg - getValue(close, iii))) / n);

def Skew=(VWAP_ - VPOC)/StDev;



Adlabel
AddLabel(yes, " Skew= " + (Skew), if Skew > .1 then color.lime else if Skew < .1 then color.pink else color.yellow);


The code may look a little messy but I think I have it correct, if you would like to give an alternative code that’s more accurate for the definition of skew above, then please double check that I have it right

I would like this plotted as its own indicator, to see the context of the skew throughout the trading day, such as a histogram.

Plot Histogram:
I would prefer to have a histogram to plot the skew values, this gives context to the skew values as to where it was, and gives some clues as to where it might be going.


Histogram:
Here’s an example of what the histogram might look like:

Notice in the Histogram Example:
  • color.green for positive skew values, above + 0.1
  • Color.red for negative skew values, below – 0.1
  • Color.yellow for neutral/symmetrical skew values, such as between + 0.1 to – 0.1
  • Horizontal Axis: displays the timeframe, time of day
  • Vertical Axis: Skew values of the zero line, with Standard Deviations up to 2 or 3 SD
Example Code Above to plot the histogram like it’s shown above:

declare lower;

input price = close;
input length = 30;
input ZavgLength = 30;

Although instead of length, I need upticks of the skew and downticks of the skew, kinda like this, not really sure:

Uptick.DefineColor("Positive", Color.UPTICK);
Uptick.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Downtick.DefineColor("Negative", Color.DOWNTICK);
Downtick.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
data.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
data.SetLineWeight(3);
ZeroLine.SetDefaultColor(GetColor(5));

Uptick.SetDefaultColor(Color.UPTICK);
Uptick.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Downtick.SetDefaultColor(Color.DOWNTICK);
Downtick.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);


#Initialize values
def oneSD = stdev(price,length);
def avgClose = simpleMovingAvg(price,length);
def ofoneSD = oneSD*price[1];
def Zscorevalue = ((price-avgClose)/oneSD);
def avgZv = average(Zscorevalue,20);

#Compute and plot Z-Score
plot Zscore = ((price-avgClose)/oneSD);
Zscore.setPaintingStrategy(paintingStrategy.HISTOGRAM);
Zscore.setLineWeight(2);
Zscore.assignValueColor(if Zscore > 0 then color.green else color.red);

plot avgZscore = average(Zscorevalue,ZavgLength);
avgZscore.setPaintingStrategy(paintingStrategy.LINE);

#This is an optional plot that will display the momentum of the Z-Score average
#plot momZAvg = (avgZv-avgZv[5]);

#Plot zero line and extreme bands
plot zero = 0;
plot two = 2;
plot negtwo = -2;
zero.setDefaultColor(color.black);

Summary: basically, I want the skew formula turned into a histogram like above just with the skew z-score plotted instead of a price z-score.

Percentage Ratio of Skew to 1 Standard Deviation of the VWAP:
is the ratio of the skew to the size of ONE standard Deviation. I put that there as a possible way to monitor how the skew may be stacking up to current volatility, i.e. the current VWAP standard Deviations....which is the distance between the bands (1 StDev of the VWAP). This should be a better measurement that a fixed number, as it is tied to volatility. The higher the %, the smaller the skew, the less likely a trend is in place.

With an Adlabel
Example: Adlabel (Input 1, Input 2, Input 3)
Inputs:
  • Input 1: determines when the label will be displayed.
  • If you want the label to always be displayed then substitute "yes" for input 1.
  • I need it always displayed such as, AddLabel (yes, input 2, input 3)
  • Input 2: determines what the label will display.
  • Text, “ % Ratio/StDev= “
  • The label can display multiple bits of information by utilizing the "+" symbol.
  • Represented as a %
  • Input 3: determines what color the label will be. "If … then" statements may also be used to determine the label's color based on differing conditions.
  • Color.white for the Adlabel
Example: Addlabel(yes, “ % Ratio/StDev= “ + AsPercent(%Ratio), color.white;

Daily Trading Statistics:
  • Position Sizing:
  • Frog SD: Standard Deviation of the Average Daily Range, represented in ($) Dollars
  • With Adlabel(s) on the chart
This is what I have figured out with the Thinkscript code so far.

Intraday Frog Box StDev Conceptual Definition:
The purpose: of the indicator is to discern price action which is part of a directional move rather than price action that is noise. Once price has progressed further than one frog box, it is reasonable to expect that the stock has begun a trend in the direction of the initial move.

Formula: 1 Standard Deviation of Average Daily Range (30 days “length”)

AvgDailyRange(30) / StDev(30), not sure if this formula describes the above formula

Concept: this can be used to find the standard deviation of the average daily range, this can be used to calibrate the position sizing of your average stock that is daytraded based on the StDev

Coding the indicator:

I need to code the Average Daily Range, not the Average True Range, because I need to filter out the overnight gaps into the calculator, but as far as I can tell the formula for average daily range is messy in Thinkscript, here’s a couple of variations I’ve come up with, not sure which one works better for what I’m trying to achieve:

def length = 30;

def dayHigh = DailyHighLow(AggregationPeriod.DAY, length, 30, no).dailyhigh;
def dayLow = DailyHighLow(AggregationPeriod.DAY, length, 30, no).DailyLow;

def ADR_high = (dayHigh + dayHigh[-1] + dayHigh[-2] + dayHigh[-3] + dayHigh[-4]+ dayHigh[-5]+ dayHigh[-6]+ dayHigh[-7]+ dayHigh[-8]+ dayHigh[-9]+ dayHigh[-10]+ dayHigh[-11]+ dayHigh[-12]+ dayHigh[-13]+ dayHigh[-14]+ dayHigh[-15]+ dayHigh[-16]+ dayHigh[-17]+ dayHigh[-18]+ dayHigh[-19]+ dayHigh[-20]+ dayHigh[-21]+ dayHigh[-22]+ dayHigh[-23]+ dayHigh[-24]+ dayHigh[-25]+ dayHigh[-26]+ dayHigh[-27]+ dayHigh[-28]+ dayHigh[-29]+ dayHigh[-30]) / 30;

def ADR_low = (dayLow + dayLow[-1] + dayLow[-2] + dayLow[-3] + dayLow[-4])+ dayLow[-5]+ dayLow[-6]+ dayLow[-7]+ dayLow[-8]+ dayLow[-9]+ dayLow[-10]+ dayLow[-11]+ dayLow[-12]+ dayLow[-13]+ dayLow[-14]+ dayLow[-15]+ dayLow[-16]+ dayLow[-17]+ dayLow[-18]+ dayLow[-19]+ dayLow[-20]+ dayLow[-21]+ dayLow[-22]+ dayLow[-23]+ dayLow[-24]+ dayLow[-25]+ dayLow[-26]+ dayLow[-27]+ dayLow[-28]+ dayLow[-29]+ dayLow[-30] / 30;

Next Average Daily Range Definition:
def ADR=(ADR_high - ADR_low);

Next: I’m not sure if this part is right, because I need 1 standard deviation of the average daily range, not sure if this is the correct way to code this

input numDevDn = -1.0;
input numDevUp = 1.0;
input displace = 0;
input price=close;
def RTH = (RegularTradingEnd(GetYYYYMMDD()) - RegularTradingStart(GetYYYYMMDD())) / AggregationPeriod.DAY;
def n = if RTH and !RTH[1]
then 1
else if RTH
then n[1] + 1
else n[1];
def Avg = (fold i = 0 to n
with s
do s + getValue(close, i)) / n;

def StDev = Sqrt((fold iii = 0 to n
with sss = 0
do sss + Sqr(Avg - getValue(close, iii))) / n);


def Frog_SD=ADR/StDev(length);

However I’m not sure if this formula is doing it for the last 30 days of the average daily range and 1 standard deviation of that range

plot data=Frog_SD;
plot UpperBand = data + numDevUp * StDev;
plot LowerBand = data + numDevDn * StDev;

UpperBand.setDefaultColor(getColor(2));

With an Adlabel:
Example: Adlabel (Input 1, Input 2, Input 3)
Inputs:
  • Input 1: determines when the label will be displayed.
  • If you want the label to always be displayed then substitute "yes" for input 1.
  • I need it always displayed such as, AddLabel (yes, input 2, input 3)
  • Input 2: determines what the label will display.
  • Text, “ Frog SD= “
  • The label can display multiple bits of information by utilizing the "+" symbol.
  • Represented in ($) Dollar Amount
  • Input 3: determines what color the label will be. "If … then" statements may also be used to determine the label's color based on differing conditions.
  • Color.white for the Adlabel
Example:
AddLabel(yes, " Frog SD= " + AsDollars(Upperband-data), color.white);

However, I’m having trouble putting the elements of the code together, I get some errors in Thinkscript, if someone could help this would be great, thanks

Summary:
I need the following indicators to be coded in ThinkorSwim (TOS) with Thinkscripter:

Trading with Market Statistics Indicators:
  • Skew: (Intraday VWAP – POC (point of control from intraday volume profile) / Standard Deviation
  • Percentage Ratio of Skew to 1 Standard Deviation: ratio of the skew to the size of ONE standard Deviation. 1 StDev of the current VWAP standard Deviations....which is the distance between the bands, represented as a (%) percent
  • Each of the formulas with Adlabels on the chart
Daily Trading Statistics:
  • Position Sizing:
  • Frog SD: Standard Deviation of the Average Daily Range, represented in ($) Dollars
  • With Adlabel(s) on the chart
Thanks for taking a look,
 
Last edited by a moderator:

Prison Mike

Member
@socialintelligence99 quite a little write up you did. I was working on a “skew” indicator myself but was having issues. The link was super helpful to bring clarity. Wish the videos still worked.

As @john3 mentioned any luck?
 
Last edited:

Similar threads

Top