Gann's Price & Time Forecast For ThinkOrSwim

Picard

Member
Gann's Price & Time Forecast [Square The Range]
Time & Price 1.jpg

Ruby:
# Gann's Price & Time Forecast  [Square The Range] For ThinkOrSwim
# @Picard 8/23
declare upper;

input numMonths1 = 5;
input VScale = 0.5;
input HScale = 5.0;
input GridSize = 50;

input Show1      = yes;
input lineWeight = 5;

def numdays  = 21;

def numBars1  = numdays * numMonths1;
def barNum    = if IsNaN( close ) then Double.NaN else BarNumber();
def lastBar   = HighestAll( barNum );
def startBar1 = if lastBar <= numBars1 then 1 else lastBar - numBars1;
def bar = BarNumber();

def hData1    = If( barNum < startBar1, Double.NaN, high );
def lData1    = If( barNum < startBar1, Double.NaN, low );

def a_price   = HighestAll(high);
def b_price   = LowestAll(low);
def barnumber = BarNumber();
def c_price   = if high == a_price then barnumber else Double.NaN;
def d_price   = if low == b_price then barnumber else Double.NaN;
rec highnumber    = CompoundValue(1, if IsNaN(c_price) then highnumber[1] else c_price, c_price);
def highnumberall = HighestAll(highnumber);
rec lownumber     = CompoundValue(1, if IsNaN(d_price) then lownumber[1] else d_price, d_price);
def lownumberall  = LowestAll(lownumber);

def months1   = Round( ( lastBar - startBar1 + 1 ) / 21, 0 );
plot HighestHigh1 = If( Show1, HighestAll( hData1 ), Double.NaN );
plot LowestLow1   = If( Show1, LowestAll( lData1 ),  Double.NaN );

# ===============================================================
#                Chart Bubbles
# ===============================================================
def BeginBarH = if barNum >= startBar1 and HighestAll(HighestHigh1) == high
                then  BarNumber() else BeginBarH[1];
def BeginBarL = if barNum >= startBar1 and HighestAll(LowestLow1) == low
                then  BarNumber() else  BeginBarL[1];
def BeginBar  = HighestAll(Min(BeginBarH, BeginBarL));

# ===============================================================
#                Plotting Section
# ===============================================================

#===================[ Define Plot Appearences ]=====================
DefineGlobalColor( "H1", Color.CYAN);
DefineGlobalColor( "L1", Color.PINK);

HighestHigh1.SetPaintingStrategy( PaintingStrategy.DASHES );
HighestHigh1.SetLineWeight( lineWeight );
HighestHigh1.AssignValueColor( GlobalColor( "H1" ) );
HighestHigh1.HideBubble();

LowestLow1.SetPaintingStrategy( PaintingStrategy.DASHES );
LowestLow1.SetLineWeight( lineWeight );
LowestLow1.AssignValueColor( GlobalColor( "L1" ) );
LowestLow1.HideBubble();

AddChartBubble( Show1 and barNum == BeginBarH, HighestHigh1, "$" + HighestHigh1 +  " @ " + GetMonth() +  "/" + GetDayOfMonth(GetYYYYMMDD()) +  "/" + AsPrice(GetYear()), GlobalColor( "H1" ), yes  );

AddChartBubble( Show1 and barNum == BeginBarL, LowestLow1, "$" + LowestLow1 +  " @ " + GetMonth() +  "/" + GetDayOfMonth(GetYYYYMMDD()) +  "/" + AsPrice(GetYear()), GlobalColor( "L1" ), no  );


def begindate  = if BarNumber() == BeginBar then GetYYYYMMDD() else begindate[1];
def beginclose = if BarNumber() == BeginBar then close else beginclose[1];
def enddate    = if BarNumber() == BeginBar + GridSize then GetYYYYMMDD() else enddate[1];

def BeginPrice  = HighestAll(Min(HighestHigh1, LowestLow1));


def HighDate = if BarNumber() == BeginBarH then GetYYYYMMDD() else HighDate[1];
def LowDate = if BarNumber() == BeginBarL then GetYYYYMMDD() else LowDate[1];

# ===============================================================
#                Calculate Price & Time Forecast
# ===============================================================

#  AddLabel ( boolean visible, Any text, CustomColor color);

# Next High in
def NH = (HighestHigh1 / 10) * 30.437;

# Next Low in
def NL = (LowestLow1 / 10) * 30.437;

# AddLabel ( yes, "High Price = $" + HighestHigh1, Color.BLUE);
# AddLabel ( yes, "Low Price = $" + LowestLow1, Color.RED);

AddLabel ( yes, "Next High in = " + NH + " Days", Color.BLUE);
AddLabel ( yes, "Next Low in = " + NL + " Days", Color.RED);
 

Attachments

  • Gann_PriceAndTimeSTUDY.txt
    3.8 KB · Views: 233
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

I'm trying to upgrade my code to be adaptable to any timeframe and I saw some code in this forum that I'm working with, but I need some help. Its at
https://usethinkscript.com/threads/mega-moving-average-for-thinkorswim.15858/post-129691

I'm not sure how to adapt this for my use, but I've given it a try. Here is what I have so far.
I want to code this for intraday all the way up to yearly timeframe. Does anyone have a suggestion? How can I automatically detect and code for changing timeframes.

Code:
# Automatically detect the chart's time frame
def TimeFrame = GetAggregationPeriod();

def priceO = open(period = TimeFrame);
def priceH = high(period = TimeFrame);
def priceL = low(period = TimeFrame);
def priceC = close(period = TimeFrame);

Plot Line = PriceO;
Line.Hide();

AddLabel(yes, "TimeFrame is: " + If timeFrame < AggregationPeriod.Day Then "Intraday" else If timeFrame == AggregationPeriod.Day Then "Daily" else If timeFrame == AggregationPeriod.Week Then "Weekly" else If timeFrame == AggregationPeriod.Month Then "Monthly" else "Yearly"  , Color.Dark_Red);

def Choice;
switch (TimeFrame){
case "Day":
    Choice = GetDay();
case "Week":
    Choice = GetWeek();
case "Month":
    Choice = GetMonth();
case "Year":
    Choice = GetYear();
}
 
Last edited:
I'm trying to upgrade my code to be adaptable to any timeframe and I saw some code in this forum that I'm working with, but I need some help. Its at
https://usethinkscript.com/threads/mega-moving-average-for-thinkorswim.15858/post-129691

I'm not sure how to adapt this for my use, but I've given it a try. Here is what I have so far.
I want to code this for intraday all the way up to yearly timeframe. Does anyone have a suggestion? How can I automatically detect and code for changing timeframes.

Code:
# Automatically detect the chart's time frame
def TimeFrame = GetAggregationPeriod();

def Choice;
switch (TimeFrame){
case "Day":
    Choice = GetDay();
case "Week":
    Choice = GetWeek();
case "Month":
    Choice = GetMonth();
case "Year":
    Choice = GetYear();
}

what are you trying to do?

it seems switch is causing an error with the agg variable.

GetAggregationPeriod() returns a number in milliseconds, a huge number. if you divide it by 60000, you have minutes.

getday(), getweek(),... return integers , 1 to 365, 52, 12,...

so if the formulas worked, they will never be equal.


here is something to determine what the chart time is
https://usethinkscript.com/threads/...icator-for-thinkorswim.1129/page-4#post-69106
 
My goal is to try to code the Square Of Nine on an intraday chart the way Optuma's SO9 tool displays the turn times. If someone knows of a ThinkScript code that can do this, please let me know.

SO9 on NFKX.jpg
 
Gann's Price & Time Forecast [Square The Range]
View attachment 19476
Ruby:
# Gann's Price & Time Forecast  [Square The Range] For ThinkOrSwim
# @Picard 8/23
declare upper;

input numMonths1 = 5;
input VScale = 0.5;
input HScale = 5.0;
input GridSize = 50;

input Show1      = yes;
input lineWeight = 5;

def numdays  = 21;

def numBars1  = numdays * numMonths1;
def barNum    = if IsNaN( close ) then Double.NaN else BarNumber();
def lastBar   = HighestAll( barNum );
def startBar1 = if lastBar <= numBars1 then 1 else lastBar - numBars1;
def bar = BarNumber();

def hData1    = If( barNum < startBar1, Double.NaN, high );
def lData1    = If( barNum < startBar1, Double.NaN, low );

def a_price   = HighestAll(high);
def b_price   = LowestAll(low);
def barnumber = BarNumber();
def c_price   = if high == a_price then barnumber else Double.NaN;
def d_price   = if low == b_price then barnumber else Double.NaN;
rec highnumber    = CompoundValue(1, if IsNaN(c_price) then highnumber[1] else c_price, c_price);
def highnumberall = HighestAll(highnumber);
rec lownumber     = CompoundValue(1, if IsNaN(d_price) then lownumber[1] else d_price, d_price);
def lownumberall  = LowestAll(lownumber);

def months1   = Round( ( lastBar - startBar1 + 1 ) / 21, 0 );
plot HighestHigh1 = If( Show1, HighestAll( hData1 ), Double.NaN );
plot LowestLow1   = If( Show1, LowestAll( lData1 ),  Double.NaN );

# ===============================================================
#                Chart Bubbles
# ===============================================================
def BeginBarH = if barNum >= startBar1 and HighestAll(HighestHigh1) == high
                then  BarNumber() else BeginBarH[1];
def BeginBarL = if barNum >= startBar1 and HighestAll(LowestLow1) == low
                then  BarNumber() else  BeginBarL[1];
def BeginBar  = HighestAll(Min(BeginBarH, BeginBarL));

# ===============================================================
#                Plotting Section
# ===============================================================

#===================[ Define Plot Appearences ]=====================
DefineGlobalColor( "H1", Color.CYAN);
DefineGlobalColor( "L1", Color.PINK);

HighestHigh1.SetPaintingStrategy( PaintingStrategy.DASHES );
HighestHigh1.SetLineWeight( lineWeight );
HighestHigh1.AssignValueColor( GlobalColor( "H1" ) );
HighestHigh1.HideBubble();

LowestLow1.SetPaintingStrategy( PaintingStrategy.DASHES );
LowestLow1.SetLineWeight( lineWeight );
LowestLow1.AssignValueColor( GlobalColor( "L1" ) );
LowestLow1.HideBubble();

AddChartBubble( Show1 and barNum == BeginBarH, HighestHigh1, "$" + HighestHigh1 +  " @ " + GetMonth() +  "/" + GetDayOfMonth(GetYYYYMMDD()) +  "/" + AsPrice(GetYear()), GlobalColor( "H1" ), yes  );

AddChartBubble( Show1 and barNum == BeginBarL, LowestLow1, "$" + LowestLow1 +  " @ " + GetMonth() +  "/" + GetDayOfMonth(GetYYYYMMDD()) +  "/" + AsPrice(GetYear()), GlobalColor( "L1" ), no  );


def begindate  = if BarNumber() == BeginBar then GetYYYYMMDD() else begindate[1];
def beginclose = if BarNumber() == BeginBar then close else beginclose[1];
def enddate    = if BarNumber() == BeginBar + GridSize then GetYYYYMMDD() else enddate[1];

def BeginPrice  = HighestAll(Min(HighestHigh1, LowestLow1));


def HighDate = if BarNumber() == BeginBarH then GetYYYYMMDD() else HighDate[1];
def LowDate = if BarNumber() == BeginBarL then GetYYYYMMDD() else LowDate[1];

# ===============================================================
#                Calculate Price & Time Forecast
# ===============================================================

#  AddLabel ( boolean visible, Any text, CustomColor color);

# Next High in
def NH = (HighestHigh1 / 10) * 30.437;

# Next Low in
def NL = (LowestLow1 / 10) * 30.437;

# AddLabel ( yes, "High Price = $" + HighestHigh1, Color.BLUE);
# AddLabel ( yes, "Low Price = $" + LowestLow1, Color.RED);

AddLabel ( yes, "Next High in = " + NH + " Days", Color.BLUE);
AddLabel ( yes, "Next Low in = " + NL + " Days", Color.RED);
Is there a way to get the previous value of the bubbles and make that as a label. Thanks
 
Gann's Price & Time Forecast [Square The Range]
View attachment 19476
Ruby:
# Gann's Price & Time Forecast  [Square The Range] For ThinkOrSwim
# @Picard 8/23
declare upper;

input numMonths1 = 5;
input VScale = 0.5;
input HScale = 5.0;
input GridSize = 50;

input Show1      = yes;
input lineWeight = 5;

def numdays  = 21;

def numBars1  = numdays * numMonths1;
def barNum    = if IsNaN( close ) then Double.NaN else BarNumber();
def lastBar   = HighestAll( barNum );
def startBar1 = if lastBar <= numBars1 then 1 else lastBar - numBars1;
def bar = BarNumber();

def hData1    = If( barNum < startBar1, Double.NaN, high );
def lData1    = If( barNum < startBar1, Double.NaN, low );

def a_price   = HighestAll(high);
def b_price   = LowestAll(low);
def barnumber = BarNumber();
def c_price   = if high == a_price then barnumber else Double.NaN;
def d_price   = if low == b_price then barnumber else Double.NaN;
rec highnumber    = CompoundValue(1, if IsNaN(c_price) then highnumber[1] else c_price, c_price);
def highnumberall = HighestAll(highnumber);
rec lownumber     = CompoundValue(1, if IsNaN(d_price) then lownumber[1] else d_price, d_price);
def lownumberall  = LowestAll(lownumber);

def months1   = Round( ( lastBar - startBar1 + 1 ) / 21, 0 );
plot HighestHigh1 = If( Show1, HighestAll( hData1 ), Double.NaN );
plot LowestLow1   = If( Show1, LowestAll( lData1 ),  Double.NaN );

# ===============================================================
#                Chart Bubbles
# ===============================================================
def BeginBarH = if barNum >= startBar1 and HighestAll(HighestHigh1) == high
                then  BarNumber() else BeginBarH[1];
def BeginBarL = if barNum >= startBar1 and HighestAll(LowestLow1) == low
                then  BarNumber() else  BeginBarL[1];
def BeginBar  = HighestAll(Min(BeginBarH, BeginBarL));

# ===============================================================
#                Plotting Section
# ===============================================================

#===================[ Define Plot Appearences ]=====================
DefineGlobalColor( "H1", Color.CYAN);
DefineGlobalColor( "L1", Color.PINK);

HighestHigh1.SetPaintingStrategy( PaintingStrategy.DASHES );
HighestHigh1.SetLineWeight( lineWeight );
HighestHigh1.AssignValueColor( GlobalColor( "H1" ) );
HighestHigh1.HideBubble();

LowestLow1.SetPaintingStrategy( PaintingStrategy.DASHES );
LowestLow1.SetLineWeight( lineWeight );
LowestLow1.AssignValueColor( GlobalColor( "L1" ) );
LowestLow1.HideBubble();

AddChartBubble( Show1 and barNum == BeginBarH, HighestHigh1, "$" + HighestHigh1 +  " @ " + GetMonth() +  "/" + GetDayOfMonth(GetYYYYMMDD()) +  "/" + AsPrice(GetYear()), GlobalColor( "H1" ), yes  );

AddChartBubble( Show1 and barNum == BeginBarL, LowestLow1, "$" + LowestLow1 +  " @ " + GetMonth() +  "/" + GetDayOfMonth(GetYYYYMMDD()) +  "/" + AsPrice(GetYear()), GlobalColor( "L1" ), no  );


def begindate  = if BarNumber() == BeginBar then GetYYYYMMDD() else begindate[1];
def beginclose = if BarNumber() == BeginBar then close else beginclose[1];
def enddate    = if BarNumber() == BeginBar + GridSize then GetYYYYMMDD() else enddate[1];

def BeginPrice  = HighestAll(Min(HighestHigh1, LowestLow1));


def HighDate = if BarNumber() == BeginBarH then GetYYYYMMDD() else HighDate[1];
def LowDate = if BarNumber() == BeginBarL then GetYYYYMMDD() else LowDate[1];

# ===============================================================
#                Calculate Price & Time Forecast
# ===============================================================

#  AddLabel ( boolean visible, Any text, CustomColor color);

# Next High in
def NH = (HighestHigh1 / 10) * 30.437;

# Next Low in
def NL = (LowestLow1 / 10) * 30.437;

# AddLabel ( yes, "High Price = $" + HighestHigh1, Color.BLUE);
# AddLabel ( yes, "Low Price = $" + LowestLow1, Color.RED);

AddLabel ( yes, "Next High in = " + NH + " Days", Color.BLUE);
AddLabel ( yes, "Next Low in = " + NL + " Days", Color.RED);
Gann's Price & Time Forecast [Square The Range]
View attachment 19476
Ruby:
# Gann's Price & Time Forecast  [Square The Range] For ThinkOrSwim
# @Picard 8/23
declare upper;

input numMonths1 = 5;
input VScale = 0.5;
input HScale = 5.0;
input GridSize = 50;

input Show1      = yes;
input lineWeight = 5;

def numdays  = 21;

def numBars1  = numdays * numMonths1;
def barNum    = if IsNaN( close ) then Double.NaN else BarNumber();
def lastBar   = HighestAll( barNum );
def startBar1 = if lastBar <= numBars1 then 1 else lastBar - numBars1;
def bar = BarNumber();

def hData1    = If( barNum < startBar1, Double.NaN, high );
def lData1    = If( barNum < startBar1, Double.NaN, low );

def a_price   = HighestAll(high);
def b_price   = LowestAll(low);
def barnumber = BarNumber();
def c_price   = if high == a_price then barnumber else Double.NaN;
def d_price   = if low == b_price then barnumber else Double.NaN;
rec highnumber    = CompoundValue(1, if IsNaN(c_price) then highnumber[1] else c_price, c_price);
def highnumberall = HighestAll(highnumber);
rec lownumber     = CompoundValue(1, if IsNaN(d_price) then lownumber[1] else d_price, d_price);
def lownumberall  = LowestAll(lownumber);

def months1   = Round( ( lastBar - startBar1 + 1 ) / 21, 0 );
plot HighestHigh1 = If( Show1, HighestAll( hData1 ), Double.NaN );
plot LowestLow1   = If( Show1, LowestAll( lData1 ),  Double.NaN );

# ===============================================================
#                Chart Bubbles
# ===============================================================
def BeginBarH = if barNum >= startBar1 and HighestAll(HighestHigh1) == high
                then  BarNumber() else BeginBarH[1];
def BeginBarL = if barNum >= startBar1 and HighestAll(LowestLow1) == low
                then  BarNumber() else  BeginBarL[1];
def BeginBar  = HighestAll(Min(BeginBarH, BeginBarL));

# ===============================================================
#                Plotting Section
# ===============================================================

#===================[ Define Plot Appearences ]=====================
DefineGlobalColor( "H1", Color.CYAN);
DefineGlobalColor( "L1", Color.PINK);

HighestHigh1.SetPaintingStrategy( PaintingStrategy.DASHES );
HighestHigh1.SetLineWeight( lineWeight );
HighestHigh1.AssignValueColor( GlobalColor( "H1" ) );
HighestHigh1.HideBubble();

LowestLow1.SetPaintingStrategy( PaintingStrategy.DASHES );
LowestLow1.SetLineWeight( lineWeight );
LowestLow1.AssignValueColor( GlobalColor( "L1" ) );
LowestLow1.HideBubble();

AddChartBubble( Show1 and barNum == BeginBarH, HighestHigh1, "$" + HighestHigh1 +  " @ " + GetMonth() +  "/" + GetDayOfMonth(GetYYYYMMDD()) +  "/" + AsPrice(GetYear()), GlobalColor( "H1" ), yes  );

AddChartBubble( Show1 and barNum == BeginBarL, LowestLow1, "$" + LowestLow1 +  " @ " + GetMonth() +  "/" + GetDayOfMonth(GetYYYYMMDD()) +  "/" + AsPrice(GetYear()), GlobalColor( "L1" ), no  );


def begindate  = if BarNumber() == BeginBar then GetYYYYMMDD() else begindate[1];
def beginclose = if BarNumber() == BeginBar then close else beginclose[1];
def enddate    = if BarNumber() == BeginBar + GridSize then GetYYYYMMDD() else enddate[1];

def BeginPrice  = HighestAll(Min(HighestHigh1, LowestLow1));


def HighDate = if BarNumber() == BeginBarH then GetYYYYMMDD() else HighDate[1];
def LowDate = if BarNumber() == BeginBarL then GetYYYYMMDD() else LowDate[1];

# ===============================================================
#                Calculate Price & Time Forecast
# ===============================================================

#  AddLabel ( boolean visible, Any text, CustomColor color);

# Next High in
def NH = (HighestHigh1 / 10) * 30.437;

# Next Low in
def NL = (LowestLow1 / 10) * 30.437;

# AddLabel ( yes, "High Price = $" + HighestHigh1, Color.BLUE);
# AddLabel ( yes, "Low Price = $" + LowestLow1, Color.RED);

AddLabel ( yes, "Next High in = " + NH + " Days", Color.BLUE);
AddLabel ( yes, "Next Low in = " + NL + " Days", Color.RED);
Can you please explain why did you inlcude "/ 10) * 30.437" in the following? why did you use 10 an 30.437?
# Next High in
def NH = (HighestHigh1 / 10) * 30.437;

# Next Low in
def NL = (LowestLow1 / 10) * 30.437;
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
291 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top