I added some weighted MA calculations and the chart looks beautiful and this is my final version of chart See if u like it ... http://tos.mx/TipPyQ2
Last edited by a moderator:
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
##ASPA TRADER Projection Pivots for HA Candles
declare Once_Per_Bar;
input n = 21;
input showLines = no;
input showValues = no;
input showBarNumbers = no;
input period = 50;
input candleSmoothing = {default Valcu, Vervoort};
input movingAverageType = {default TEMA, Exponential, Weighted, Hull, Variable, SIMPLE};
def openMA;
def closeMA;
def highMA;
def lowMA;
switch (movingAverageType) {
case Simple:
openMA = compoundValue(1, Average(open, period), open);
closeMA = compoundValue(1, Average(close, period), close);
highMA = compoundValue(1, Average(high, period), high);
lowMA = compoundValue(1, Average(low, period), low);
case Exponential:
openMA = compoundValue(1, ExpAverage(open, period), open);
closeMA = compoundValue(1, ExpAverage(close, period), close);
highMA = compoundValue(1, ExpAverage(high, period), high);
lowMA = compoundValue(1, ExpAverage(low, period), low);
case Weighted:
openMA = compoundValue(1, WMA(open, period), open);
closeMA = compoundValue(1, WMA(close, period), close);
highMA = compoundValue(1, WMA(high, period), high);
lowMA = compoundValue(1, WMA(low, period), low);
Case Hull:
openMA = compoundValue(1, HullMovingAvg(open, period), open);
closeMA = compoundValue(1, HullMovingAvg(close, period), close);
highMA = compoundValue(1, HullMovingAvg(high, period), high);
lowMA = compoundValue(1, HullMovingAvg(low, period), low);
case variable:
openMA = compoundValue(1, VariableMA(open, period), open);
closeMA = compoundValue(1, VariableMA(close, period), close);
highMA = compoundValue(1, VariableMA(high, period), high);
lowMA = compoundValue(1, VariableMA(low, period), low);
case TEMA:
openMA = compoundValue(1, TEMA(open, period), open);
closeMA = compoundValue(1, TEMA(close, period), close);
highMA = compoundValue(1, TEMA(high, period), high);
lowMA = compoundValue(1, TEMA(low, period), low);
}
def h = highMA;
def l = lowMA;
def bar = barNumber();
def PH;
def PL;
def hh = fold i = 1 to n + 1
with p = 1
while p
do h > getValue(h, -i);
PH = if (bar > n and
h == highest(h, n) and
hh)
then h
else double.NaN;
def ll = fold j = 1 to n + 1
with q = 1
while q
do l < getValue(low, -j);
PL = if (bar > n and
l == lowest(l, n) and
ll)
then l
else double.NaN;
def PHBar = if !isNaN(PH)
then bar
else PHBar[1];
def PLBar = if !isNaN(PL)
then bar
else PLBar[1];
def PHL = if !isNaN(PH)
then PH
else PHL[1];
def priorPHBar = if PHL != PHL[1]
then PHBar[1]
else priorPHBar[1];
def PLL = if !isNaN(PL)
then PL
else PLL[1];
def priorPLBar = if PLL != PLL[1]
then PLBar[1]
else priorPLBar[1];
def HighPivots = bar >= highestAll(priorPHBar);
def LowPivots = bar >= highestAll(priorPLBar);
def FirstRpoint = if HighPivots
then bar - PHBar
else 0;
def PriorRpoint = if HighPivots
then bar - PriorPHBar
else 0;
def RSlope = (getvalue(PH, FirstRpoint) - getvalue(PH, PriorRpoint))
/ (PHBar - PriorPHBar);
def FirstSpoint = if LowPivots
then bar - PLBar
else 0;
def PriorSpoint = if LowPivots
then bar - PriorPLBar
else 0;
def SSlope = (getvalue(PL, FirstSpoint) - getvalue(PL, PriorSpoint))
/ (PLBar - PriorPLBar);
def RExtend = if bar == highestall(PHBar)
then 1
else RExtend[1];
def SExtend = if bar == highestall(PLBar)
then 1
else SExtend[1];
plot pivotHigh = if HighPivots
then PH
else double.NaN;
pivotHigh.SetDefaultColor(GetColor(1));
pivotHigh.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
pivotHigh.setHiding(!showValues);
plot pivotHighLine = if PHL > 0 and
HighPivots
then PHL
else double.NaN;
pivotHighLine.SetPaintingStrategy(PaintingStrategy.DASHES);
pivotHighLine.setHiding(!showLines);
plot RLine = pivotHigh;
RLine.enableApproximation();
RLine.SetDefaultColor(GetColor(7));
RLine.SetStyle(Curve.FIRM);
plot RExtension = if RExtend
then (bar - PHBar) * RSlope + PHL
else double.NaN;
RExtension.SetStyle(Curve.FIRM);
RExtension.SetDefaultColor(GetColor(7));
plot pivotLow = if LowPivots
then PL
else double.NaN;
pivotLow.setDefaultColor(GetColor(4));
pivotLow.setPaintingStrategy(PaintingStrategy.VALUES_BELOW);
pivotLow.setHiding(!showValues);
plot pivotLowLine = if PLL > 0 and
LowPivots
then PLL
else double.NaN;
pivotLowLine.SetPaintingStrategy(PaintingStrategy.DASHES);
pivotLowLine.setHiding(!showLines);
plot SupportLine = pivotLow;
SupportLine.enableApproximation();
SupportLine.SetDefaultColor(GetColor(7));
SUpportLine.SetStyle(Curve.FIRM);
plot SupportExtension = if SExtend
then (bar - PLBar) * SSlope + PLL
else double.NaN;
SupportExtension.SetDefaultColor(GetColor(7));
SupportExtension.SetStyle(Curve.FIRM);
plot BN = bar;
BN.SetDefaultColor(GetColor(0));
BN.setHiding(!showBarNumbers);
BN.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
plot PivotDot = if !isNaN(pivotHigh)
then pivotHigh
else if !isNaN(pivotLow)
then pivotLow
else double.NaN;
pivotDot.SetDefaultColor(GetColor(7));
# End Code Projection Pivots
## ProjectionPivots_v03_JQ
## 03.04.2019
## Original Code and Concept by Mobius:
# V01.08.2012 Projection Pivots
# mobius
# Notes:
# 03.04.2019 added linits on extensions
# 03.05.2019 adjusted limits on extensions by adding user input upper and lower extenion percent limits
#declare Once_Per_Bar;
## Inputs
input period = 50;
input n = 21;
input showLines = yes;
input showValues = no;
input showBarNumbers = no;
input ExtensionLengthBars = 20; # added to control length of Entension
input UpperExtensionPercentLimit = 5;
input LowerExtensionPercentLimit = 5;
input DisplayLabel = no; ## JQ 7.8.2018 added
addlabel (DisplayLabel, "Projection Pivots n:" + n + " " , color.WHITE); ## JQ 7.8.2018 added
# Universal Header _v030429019 _JQ
# code from various sources including Mobius, NoLongerNube and others
# Comment out unnecessary portions to preserve tos memory and enhance speed
# Universal Definitions using Padawan variable naming convention (JQ) v03.04.2019
# iData Definitions
input movingAverageType = {default TEMA, Exponential, Weighted, Hull, Variable, SIMPLE};
def openMA;
def closeMA;
def highMA;
def lowMA;
switch (movingAverageType) {
case Simple:
openMA = compoundValue(1, Average(open, period), open);
closeMA = compoundValue(1, Average(close, period), close);
highMA = compoundValue(1, Average(high, period), high);
lowMA = compoundValue(1, Average(low, period), low);
case Exponential:
openMA = compoundValue(1, ExpAverage(open, period), open);
closeMA = compoundValue(1, ExpAverage(close, period), close);
highMA = compoundValue(1, ExpAverage(high, period), high);
lowMA = compoundValue(1, ExpAverage(low, period), low);
case Weighted:
openMA = compoundValue(1, WMA(open, period), open);
closeMA = compoundValue(1, WMA(close, period), close);
highMA = compoundValue(1, WMA(high, period), high);
lowMA = compoundValue(1, WMA(low, period), low);
Case Hull:
openMA = compoundValue(1, HullMovingAvg(open, period), open);
closeMA = compoundValue(1, HullMovingAvg(close, period), close);
highMA = compoundValue(1, HullMovingAvg(high, period), high);
lowMA = compoundValue(1, HullMovingAvg(low, period), low);
case variable:
openMA = compoundValue(1, VariableMA(open, period), open);
closeMA = compoundValue(1, VariableMA(close, period), close);
highMA = compoundValue(1, VariableMA(high, period), high);
lowMA = compoundValue(1, VariableMA(low, period), low);
case TEMA:
openMA = compoundValue(1, TEMA(open, period), open);
closeMA = compoundValue(1, TEMA(close, period), close);
highMA = compoundValue(1, TEMA(high, period), high);
lowMA = compoundValue(1, TEMA(low, period), low);
}
def vHigh = highMA; # creates the variable vHigh. Use of the variable reduce data calls to tos iData server
# def initHigh = CompoundValue(1, highMA, highMA); # creates and initialized variable for High
def vLow = lowMA;
# def initLow = CompoundValue(1, lowMA, lowMA);
def vOpen = openMA;
# def initOpen = CompoundValue(1, openMA, openMA);
def vClose = closeMA;
# def initClose = CompoundValue(1, closeMA, closeMA);
def vVolume = volume;
# def initVolume = CompoundValue(1, volume, volume);
def nan = Double.NaN;
# Bar Time & Date
def bn = BarNumber();
def currentBar = HighestAll(if !IsNaN(vHigh) then bn else nan);
# def Today = GetDay() ==GetLastDay();
# def time = GetTime();
# def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());
# def globeX_v2 = if time crosses below RegularTradingEnd(GetYYYYMMDD()) then bn else GlobeX[1];
# def RTS = RegularTradingStart(GetYYYYMMDD());
# def RTE = RegularTradingEnd(GetYYYYMMDD());
# def RTH = GetTime() > RegularTradingStart(GetYYYYMMDD());
# def RTH_v2 = if time crosses above RegularTradingStart(GetYYYYMMDD()) then bn else RTH[1];
# bars that start and end the sessions #(borrowed from nube)
# def rthStartBar = CompoundValue(1,
# if !IsNaN(vClose)
# && time crosses above RegularTradingStart(GetYYYYMMDD())
# then bn
# else rthStartBar[1], 0);
# def rthEndBar = CompoundValue(1,
# if !IsNaN(vClose)
# && time crosses above RegularTradingEnd(GetYYYYMMDD())
# then bn
# else rthEndBar[1], 1);
# def globexStartBar = CompoundValue(1,
# if !IsNaN(vClose)
# && time crosses below RegularTradingEnd(GetYYYYMMDD())
# then bn
# else globexStartBar[1], 1);
# def rthSession = if bn crosses above rthStartBar #+ barsExtendedBeyondSession
# then 1
# else if bn crosses above rthEndBar #+ barsExtendedBeyondSession
# then 0
# else rthSession[1];
# Bubble Locations
def x_AxisLastExpansionBar = BarNumber() == HighestAll(BarNumber()); #corrected 11.12.2018 (JQ)
# syntax: addChartBubble(x_AxisLastExpansionBar, y-axis coordinate," text", Color.LIME); #verified 12.25.2018 (JQ)
def PH;
def PL;
def hh = fold i = 1 to n + 1
with p = 1
while p
do vHigh > getValue(vHigh, -i);
PH = if (bn > n and
vHigh == highest(vHigh, n) and
hh)
then vHigh
else double.NaN;
def ll = fold j = 1 to n + 1
with q = 1
while q
do vLow < getValue(lowMA, -j);
PL = if (bn > n and
vLow == lowest(vLow, n) and
ll)
then vLow
else double.NaN;
def PHBar = if !isNaN(PH)
then bn
else PHBar[1];
def PLBar = if !isNaN(PL)
then bn
else PLBar[1];
def PHL = if !isNaN(PH)
then PH
else PHL[1];
def priorPHBar = if PHL != PHL[1]
then PHBar[1]
else priorPHBar[1];
def PLL = if !isNaN(PL)
then PL
else PLL[1];
def priorPLBar = if PLL != PLL[1]
then PLBar[1]
else priorPLBar[1];
def HighPivots = bn >= highestAll(priorPHBar);
def LowPivots = bn >= highestAll(priorPLBar);
def FirstRpoint = if HighPivots
then bn - PHBar
else 0;
def PriorRpoint = if HighPivots
then bn - PriorPHBar
else 0;
def RSlope = (getvalue(PH, FirstRpoint) - getvalue(PH, PriorRpoint))
/ (PHBar - PriorPHBar);
def FirstSpoint = if LowPivots
then bn - PLBar
else 0;
def PriorSpoint = if LowPivots
then bn - PriorPLBar
else 0;
def SSlope = (getvalue(PL, FirstSpoint) - getvalue(PL, PriorSpoint))
/ (PLBar - PriorPLBar);
def RExtend = if bn == highestall(PHBar)
then 1
else RExtend[1];
def SExtend = if bn == highestall(PLBar)
then 1
else SExtend[1];
plot pivotHigh = if HighPivots
then PH
else double.NaN;
pivotHigh.SetDefaultColor(GetColor(1));
pivotHigh.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
pivotHigh.setHiding(!showValues);
plot pivotHighLine = if PHL > 0 and
HighPivots
then PHL
else double.NaN;
pivotHighLine.SetPaintingStrategy(PaintingStrategy.DASHES); # Mobius original was DASHES
pivotHighLine.setDefaultColor(color.uptick); ## JQ 7.8.2018 added
pivotHighLine.setHiding(!showLines);
plot RLine = pivotHigh;
RLine.enableApproximation();
RLine.SetDefaultColor(Color.LIGHT_GRAY);
RLine.SetStyle(Curve.Short_DASH);
# Added code to limit resistance estension line (JQ 03.04.2019)
def calc_ResistanceExtension = if RExtend
then (bn - PHBar) * RSlope + PHL
else double.NaN;
plot line_ResistanceExtension = if bn <= (Currentbar + ExtensionLengthBars)
and calc_ResistanceExtension[1] >= (lowestall(vLow) * (1-(lowerExtensionPercentLimit/100)))
and calc_ResistanceExtension[1] <= (Highestall(vHigh) * (1 + (upperExtensionPercentLimit/100)))
then calc_ResistanceExtension else double.nan;
line_ResistanceExtension.SetStyle(Curve.Short_DASH);
line_ResistanceExtension.SetDefaultColor(color.LIGHT_GRAY); #was 7
line_ResistanceExtension.setLineWeight(1);
# Low Plots
plot pivotLow = if LowPivots
then PL
else double.NaN;
pivotLow.setDefaultColor(GetColor(4));
pivotLow.setPaintingStrategy(PaintingStrategy.VALUES_BELOW);
pivotLow.setHiding(!showValues);
plot pivotLowLine = if PLL > 0 and
LowPivots
then PLL
else double.NaN;
pivotLowLine.SetPaintingStrategy(PaintingStrategy.DASHES); # Mobius original was DASHES
pivotLowLine.setDefaultColor(color.DOWNTICK);# # JQ 7.8.2018 added
pivotLowLine.setHiding(!showLines);
plot SupportLine = pivotLow;
SupportLine.enableApproximation();
SupportLine.SetDefaultColor(color.LIGHT_GRAY);
SUpportLine.SetStyle(Curve.Short_DASH);
# Added code to limit support estension line (JQ 03.04.2019)
def calc_SupportExtension = if SExtend
then (bn - PLBar) * SSlope + PLL
else double.NaN;
plot line_SupportExtension = if bn <= (Currentbar + ExtensionLengthBars)
and calc_SupportExtension[1] >= (lowestall(vLow) * (1-(lowerExtensionPercentLimit/100)))
and calc_SupportExtension[1] <= (Highestall(vHigh) * (1 + (upperExtensionPercentLimit/100)))
then calc_supportExtension else double.nan;
line_SupportExtension.SetDefaultColor(color.LIGHT_GRAY); #was 7
line_SupportExtension.SetStyle(Curve.Short_DASH);
line_SupportExtension.setLineWeight(1);
plot BarNumbersBelow = bn;
BarNumbersBelow.SetDefaultColor(GetColor(0));
BarNumbersBelow.setHiding(!showBarNumbers);
BarNumbersBelow.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
plot PivotDot = if !isNaN(pivotHigh)
then pivotHigh
else if !isNaN(pivotLow)
then pivotLow
else double.NaN;
pivotDot.SetDefaultColor(GetColor(7));
pivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
pivotDot.SetLineWeight(3);
# End Code
I love this! When I try this with USD or NVDL, it's pretty much exactly where I would want the entries and exits (if maybe slightly delayed at times). And the curves are extremely aesthetically pleasing.I added some weighted MA calculations and the chart looks beautiful and this is my final version of chart See if u like it ... http://tos.mx/TipPyQ2
#
# HMA Long-Only Trading Strategy
#
input price = close;
input length = 20;
input displace = 0;
input averageType = AverageType.HULL;
def HMA = MovingAverage(averageType, price, length)[-displace];
# Entry condition: HMA crosses above its previous value
AddOrder(OrderType.BUY_TO_OPEN,
HMA crosses above HMA[1],
tickcolor = GetColor(0),
arrowcolor = GetColor(0),
name = "HMA_Long_Entry");
# Exit condition: HMA crosses below its previous value
AddOrder(OrderType.SELL_TO_CLOSE,
HMA crosses below HMA[1],
tickcolor = GetColor(1),
arrowcolor = GetColor(1),
name = "HMA_Long_Exit");
# Plot HMA for reference
plot HMALine = HMA;
HMALine.SetDefaultColor(GetColor(2));
# Add labels for visual confirmation
AddChartBubble(HMA crosses above HMA[1], low, "Buy", Color.GREEN, yes);
AddChartBubble(HMA crosses below HMA[1], high, "Sell", Color.RED, yes);
# Define alert conditions
Alert(HMA crosses above HMA[1], "HMA Long Entry", Alert.BAR, Sound.Ding);
Alert(HMA crosses below HMA[1], "HMA Long Exit", Alert.BAR, Sound.Ding);
I love this! When I try this with USD or NVDL, it's pretty much exactly where I would want the entries and exits (if maybe slightly delayed at times). And the curves are extremely aesthetically pleasing.
One of the things that I love about @AspaTrader 's chart is how and when the curves change from purple to yellow. When I try to take the HullMovingAvg study (what seems like it's the source of the curve) and turn it into a strategy (below), the trades seem to be delayed relative to where the original curve changes color.
To simplify my question further: how do I take the transitions from purple to yellow and turn them into a strategy?
I'd be super grateful for any insights.
Many thanks!
Code:# # HMA Long-Only Trading Strategy # input price = close; input length = 20; input displace = 0; input averageType = AverageType.HULL; def HMA = MovingAverage(averageType, price, length)[-displace]; # Entry condition: HMA crosses above its previous value AddOrder(OrderType.BUY_TO_OPEN, HMA crosses above HMA[1], tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "HMA_Long_Entry"); # Exit condition: HMA crosses below its previous value AddOrder(OrderType.SELL_TO_CLOSE, HMA crosses below HMA[1], tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "HMA_Long_Exit"); # Plot HMA for reference plot HMALine = HMA; HMALine.SetDefaultColor(GetColor(2)); # Add labels for visual confirmation AddChartBubble(HMA crosses above HMA[1], low, "Buy", Color.GREEN, yes); AddChartBubble(HMA crosses below HMA[1], high, "Sell", Color.RED, yes); # Define alert conditions Alert(HMA crosses above HMA[1], "HMA Long Entry", Alert.BAR, Sound.Ding); Alert(HMA crosses below HMA[1], "HMA Long Exit", Alert.BAR, Sound.Ding);
Thank you for this information, it's extremely helpful!Yes, there is a delay.
Backtesting in the ToS app, requires the candle to close before placing entries and exits.
The ToS backtesting does not provide for the inter-candle actions that are seen in live trading.
Thank you for this information, it's extremely helpful!
I have a followup newbie question. I want to be able to save the original chart with all the studies from the top of this thread. I'd like to be able to open up TOS and recall that chart at any point. How do I save it for future use?
I tried saving as a study set, but when I go to the chart area of TOS and pull up that study set, it overlays on top of the default chart instead of replacing, so there are two sets of candles. Thank you.
Start a new thread and receive assistance from our community.
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.
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.