I'm starting to understand the different sections of the code a little better. It appears to me that the "script sfull {********}" section is a subscript that allows separate calculations for each symbol. But I don't understand what value is being returned by this subscript. Is it the end result of all calculations within the subscript?
With that assumption made, I attempted to simplify the code so that it would use only the calculations from my original. It is now working in that it plots the same value as the original, but only for the symbol being charted. Changing the symbol1 and symbol2 inputs has no change in the plots.
Why is that and how do I change it to use the symbol1 and symbol2 inputs?
Thanks for all your help.
Code:#...
How do I reference indicator calculations from a different symbol? I want to create an alert when both the "/ES" and "SPX" fire a signal on the same bar. The indicator that I need calculations from is the StochasticFull.
Ruby:# # TD Ameritrade IP Company, Inc. (c) 2008-2021 # declare lower; script sfull { input over_bought = 80; input over_sold = 20; input KPeriod = 10; input DPeriod = 10; input symbol = "/ES"; def priceH = high(symbol); def priceL = low(symbol); def priceC = close(symbol); input slowing_period = 3; input averageType = AverageType.SIMPLE; input showBreakoutSignals = {"No", default "On FullK", "On FullD", "On FullK & FullD"}; def lowest_k = Lowest(priceL, KPeriod); def c1 = priceC - lowest_k; def c2 = Highest(priceH, KPeriod) - lowest_k; def FastK = if c2 != 0 then c1 / c2 * 100 else 0; plot FullK = MovingAverage(averageType, FastK, slowing_period); plot FullD = MovingAverage(averageType, FullK, DPeriod); } input symbol1 = "/ES"; input symbol2 = "SPX"; def fs1 = sfull(symbol = symbol1); def fS2 = sfull(symbol = symbol2); def fs1ext = if IsNaN(fs1) then fs1ext[1] else fs1; def fs2ext = if IsNaN(fS2) then fs2ext[1] else fS2; plot fs1ext_ = fs1ext; fs1ext_.SetDefaultColor(Color.CYAN); plot fs2ext_ = fs2ext; fs2ext_.SetDefaultColor(Color.MAGENTA); plot overbought = 80; plot oversold = 20; input arrow = {default Each, Together}; input lineweight = 2; plot uBoth = if arrow == arrow.Together and (fs1 crosses below overbought and fS2 crosses below overbought) then overbought else Double.NaN; plot dBoth = if arrow == arrow.Together and (fs1 crosses above oversold and fS2 crosses above oversold) then oversold else Double.NaN; uBoth.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN); dBoth.SetPaintingStrategy(PaintingStrategy.ARROW_UP); uBoth.SetLineWeight(lineweight); dBoth.SetLineWeight(lineweight); plot us1 = if arrow == arrow.Each and fs1 crosses below overbought then overbought else Double.NaN; plot ds1 = if arrow == arrow.Each and fs1 crosses above oversold then oversold else Double.NaN; us1.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN); ds1.SetPaintingStrategy(PaintingStrategy.ARROW_UP); us1.SetDefaultColor(Color.CYAN); ds1.SetDefaultColor(Color.CYAN); us1.SetLineWeight(lineweight); ds1.SetLineWeight(lineweight); plot us2 = if arrow == arrow.Each and fS2 crosses below overbought then overbought else Double.NaN; plot dS2 = if arrow == arrow.Each and fS2 crosses above oversold then oversold else Double.NaN; us2.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN); dS2.SetPaintingStrategy(PaintingStrategy.ARROW_UP); us2.SetDefaultColor(Color.MAGENTA); dS2.SetDefaultColor(Color.MAGENTA); us2.SetLineWeight(lineweight); dS2.SetLineWeight(lineweight);
SleepyZ - Thank you very much for answering my question, but this kind of thinkScript programming is way beyond my skill set. I need to do further calculations on the StochasticFull before the result gets plotted and compared for each symbol, and I can't comprehend where these additional calculations might go in your script. Perhaps you can help me:
def step1 = Max(-100, Min(100, (StochasticFull(KPeriod = 17, slowing_period = 5, averageType = AverageType.EXPONENTIAL))) - 50) / 50.01;
rec step2 = CompoundValue(1, if IsNaN(0.5 * (Log((1 + step1) / (1 - step1)) + step2[1])) then step2[1] else 0.5 * (Log((1 + step1) / (1 - step1)) + step2[1]), 0);
plot SignalLine = if IsNaN(close) then Double.NaN else step2;
plot SignalLineBuy = if SignalLine < -1.2 and Sign (step2 - step2[1]) > Sign (step2[1] - step2[2]) and !IsNaN(close) then step2[1] else Double.NaN;
plot SignalLineSell = if SignalLine > 1.2 and Sign (step2 - step2[1]) < Sign (step2[1] - step2[2]) and !IsNaN(close) then step2[1] else Double.NaN;
Ruby:# # TD Ameritrade IP Company, Inc. (c) 2008-2021 # declare lower; script sfull { input over_bought = 80; input over_sold = 20; input KPeriod = 17; input DPeriod = 5; input symbol = "/ES"; def priceH = high(symbol); def priceL = low(symbol); def priceC = close(symbol); input slowing_period = 3; input averageType = AverageType.exPONENTIAL; input showBreakoutSignals = {"No", default "On FullK", "On FullD", "On FullK & FullD"}; def lowest_k = Lowest(priceL, KPeriod); def c1 = priceC - lowest_k; def c2 = Highest(priceH, KPeriod) - lowest_k; def FastK = if c2 != 0 then c1 / c2 * 100 else 0; plot FullK = MovingAverage(averageType, FastK, slowing_period); plot FullD = MovingAverage(averageType, FullK, DPeriod); def step1 = Max(-100, Min(100, (StochasticFull(KPeriod = 17, slowing_period = 5, averageType = AverageType.EXPONENTIAL))) - 50) / 50.01; rec step2 = CompoundValue(1, if IsNaN(0.5 * (Log((1 + step1) / (1 - step1)) + step2[1])) then step2[1] else 0.5 * (Log((1 + step1) / (1 - step1)) + step2[1]), 0); plot SignalLine = if IsNaN(close) then Double.NaN else step2; plot SignalLineBuy = if SignalLine < -1.2 and Sign (step2 - step2[1]) > Sign (step2[1] - step2[2]) and !IsNaN(close) then step2[1] else Double.NaN; plot SignalLineSell = if SignalLine > 1.2 and Sign (step2 - step2[1]) < Sign (step2[1] - step2[2]) and !IsNaN(close) then step2[1] else Double.NaN; } input symbol1 = "/ES"; input symbol2 = "SPX"; def fs1 = sfull(symbol = symbol1); def fS2 = sfull(symbol = symbol2); def fs1ext = if IsNaN(fs1) then fs1ext[1] else fs1; def fs2ext = if IsNaN(fS2) then fs2ext[1] else fS2; plot fs1ext_ = fs1ext; fs1ext_.SetDefaultColor(Color.CYAN); plot fs2ext_ = fs2ext; fs2ext_.SetDefaultColor(Color.MAGENTA); plot overbought = 80; plot oversold = 20; input fullk_arrows = {default Each, Together, None}; input lineweight = 2; plot uBoth = if fullk_arrows == fullk_arrows.Together and (fs1 crosses below overbought and fS2 crosses below overbought) then overbought else Double.NaN; plot dBoth = if fullk_arrows == fullk_arrows.Together and (fs1 crosses above oversold and fS2 crosses above oversold) then oversold else Double.NaN; uBoth.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN); dBoth.SetPaintingStrategy(PaintingStrategy.ARROW_UP); uBoth.SetLineWeight(lineweight); dBoth.SetLineWeight(lineweight); plot us1 = if fullk_arrows == fullk_arrows.Each and fs1 crosses below overbought then overbought else Double.NaN; plot ds1 = if fullk_arrows == fullk_arrows.Each and fs1 crosses above oversold then oversold else Double.NaN; us1.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN); ds1.SetPaintingStrategy(PaintingStrategy.ARROW_UP); us1.SetDefaultColor(Color.CYAN); ds1.SetDefaultColor(Color.CYAN); us1.SetLineWeight(lineweight); ds1.SetLineWeight(lineweight); plot us2 = if fullk_arrows == fullk_arrows.Each and fS2 crosses below overbought then overbought else Double.NaN; plot dS2 = if fullk_arrows == fullk_arrows.Each and fS2 crosses above oversold then oversold else Double.NaN; us2.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN); dS2.SetPaintingStrategy(PaintingStrategy.ARROW_UP); us2.SetDefaultColor(Color.MAGENTA); dS2.SetDefaultColor(Color.MAGENTA); us2.SetLineWeight(lineweight); dS2.SetLineWeight(lineweight); input signal_arrows = yes; def signal1buy = sfull(symbol = symbol1).SignalLineBuy; def signal2buy = sfull(symbol = symbol2).SignalLineBuy; plot buy = if signal_arrows and signal1buy and signal2buy then oversold else double.nan; buy.setpaintingStrategy(paintingStrategy.ARROW_UP); buy.setlineWeight(lineweight); def signal1sell = sfull(symbol = symbol1).SignalLineSell; def signal2sell = sfull(symbol = symbol2).SignalLineSell; plot sell = if signal_arrows and signal1sell and signal2sell then overbought else double.nan; sell.setpaintingStrategy(paintingStrategy.ARROW_down); sell.setlineWeight(lineweight);
Not that I am aware.I'm still not getting this code to do what I want. My biggest hurdle is not being able to figure out what the different parts of the code are doing. So I thought I'd phrase my question in a different way:
Is it possible to take the expression
"(StochasticFull(KPeriod = 17, slowing_period = 5, averageType = AverageType.EXPONENTIAL))"
and assign in to do the calculation on a specific symbol? If I can do this I should be able to plot what I want.
Ruby:# # TD Ameritrade IP Company, Inc. (c) 2008-2021 # declare lower; script sfull { input over_bought = 80; input over_sold = 20; input KPeriod = 17; input DPeriod = 5; input symbol = "/ES"; def priceH = high(symbol); def priceL = low(symbol); def priceC = close(symbol); input slowing_period = 3; input averageType = AverageType.EXPONENTIAL; input showBreakoutSignals = {"No", default "On FullK", "On FullD", "On FullK & FullD"}; def lowest_k = Lowest(priceL, KPeriod); def c1 = priceC - lowest_k; def c2 = Highest(priceH, KPeriod) - lowest_k; def FastK = if c2 != 0 then c1 / c2 * 100 else 0; plot FullK = MovingAverage(averageType, FastK, slowing_period); plot FullD = MovingAverage(averageType, FullK, DPeriod); def step1 = Max(-100, Min(100, FullK) - 50) / 50.01; rec step2 = CompoundValue(1, if IsNaN(0.5 * (Log((1 + step1) / (1 - step1)) + step2[1])) then step2[1] else 0.5 * (Log((1 + step1) / (1 - step1)) + step2[1]), 0); } input symbol1 = "/ES"; input symbol2 = "SPX"; input lineweight = 2; plot fs1 = Round(sfull(symbol = symbol1)); plot fS2 = Round(sfull(symbol = symbol2)); plot overbought = 80; plot oversold = 20; input step = {default combined, symbol1, symbol2, None}; #symbol1-------------------------------------------- def step1 = Max(-100, Min(100, fs1) - 50) / 50.01; def step2 = CompoundValue(1, if IsNaN(0.5 * (Log((1 + step1) / (1 - step1)) + step2[1])) then step2[1] else 0.5 * (Log((1 + step1) / (1 - step1)) + step2[1]), 0); def SignalLine = if IsNaN(close) then Double.NaN else step2; plot SignalLineBuy = if SignalLine < -1.2 and Sign (step2 - step2[1]) > Sign (step2[1] - step2[2]) and !IsNaN(close) then oversold else Double.NaN; def SignalLineSell = if SignalLine > 1.2 and Sign (step2 - step2[1]) < Sign (step2[1] - step2[2]) and !IsNaN(close) then overbought else Double.NaN; #symbol2-------------------------------------------- def step1a = Max(-100, Min(100, fS2) - 50) / 50.01; def step2a = CompoundValue(1, if IsNaN(0.5 * (Log((1 + step1a) / (1 - step1a)) + step2a[1])) then step2a[1] else 0.5 * (Log((1 + step1a) / (1 - step1a)) + step2a[1]), 0); def SignalLinea = if IsNaN(close) then Double.NaN else step2a; def SignalLineBuya = if SignalLinea < -1.2 and Sign (step2a - step2a[1]) > Sign (step2a[1] - step2a[2]) and !IsNaN(close) then oversold else Double.NaN; def SignalLineSella = if SignalLinea > 1.2 and Sign (step2a - step2a[1]) < Sign (step2a[1] - step2a[2]) and !IsNaN(close) then overbought else Double.NaN; #Both when Concur---------------------------------- def combinedbuy = if step==step.combined and SignalLineBuy == SignalLineBuya then 1 else 0; def combinedsell = if step==step.combined and SignalLineSell == SignalLineSella then 1 else 0; #Plots---------------------------------------------- plot buy = if step == step.combined then combinedbuy else if step == step.symbol1 then signallinebuy else if step == step.symbol2 then signallinebuya else if step == step.none then double.nan else Double.NaN; buy.SetPaintingStrategy(PaintingStrategy.ARROW_UP); buy.setdefaultColor(color.cyan); buy.SetLineWeight(lineweight); def sell = if step == step.combined then combinedsell else if step == step.symbol1 then signallinesell else if step == step.symbol2 then signallinesella else if step == step.none then double.nan else Double.NaN; plot signalsell = if sell then overbought else Double.NaN; signalsell.SetPaintingStrategy(PaintingStrategy.ARROW_down); signalsell.SetdefaultColor(color.magenta); signalsell.setlineWeight(lineweight);
#
# TD Ameritrade IP Company, Inc. (c) 2008-2021
#
declare lower;
script sfull {
input symbol = "/ES";
def step1 = Max(-100, Min(100, (StochasticFull(KPeriod = 17, slowing_period = 5, averageType = AverageType.EXPONENTIAL))) - 50) / 50.01;
rec step2 = CompoundValue(1, if IsNaN(0.5 * (Log((1 + step1) / (1 - step1)) + step2[1])) then step2[1] else 0.5 * (Log((1 + step1) / (1 - step1)) + step2[1]), 0);
plot SignalLine = if IsNaN(close) then Double.NaN else step2;
}
input symbol1 = "AAPL";
input symbol2 = "IBM";
def fs1 = sfull(symbol = symbol1);
def fS2 = sfull(symbol = symbol2);
def fs1ext = if IsNaN(fs1) then fs1ext[1] else fs1;
def fs2ext = if IsNaN(fS2) then fs2ext[1] else fS2;
plot fs1ext_ = fs1ext;
fs1ext_.SetDefaultColor(Color.CYAN);
plot fs2ext_ = fs2ext;
fs2ext_.SetDefaultColor(Color.MAGENTA);
Your code above is not able to use selectable symbols. Your code uses within it, high, low and close prices. The standard TOS code you used does not allow modification of those to be selectable symbols.I'm starting to understand the different sections of the code a little better. It appears to me that the "script sfull {********}" section is a subscript that allows separate calculations for each symbol. But I don't understand what value is being returned by this subscript. Is it the end result of all calculations within the subscript?
With that assumption made, I attempted to simplify the code so that it would use only the calculations from my original. It is now working in that it plots the same value as the original, but only for the symbol being charted. Changing the symbol1 and symbol2 inputs has no change in the plots.
Why is that and how do I change it to use the symbol1 and symbol2 inputs?
Thanks for all your help.
Code:# # TD Ameritrade IP Company, Inc. (c) 2008-2021 # declare lower; script sfull { input symbol = "/ES"; def step1 = Max(-100, Min(100, (StochasticFull(KPeriod = 17, slowing_period = 5, averageType = AverageType.EXPONENTIAL))) - 50) / 50.01; rec step2 = CompoundValue(1, if IsNaN(0.5 * (Log((1 + step1) / (1 - step1)) + step2[1])) then step2[1] else 0.5 * (Log((1 + step1) / (1 - step1)) + step2[1]), 0); plot SignalLine = if IsNaN(close) then Double.NaN else step2; } input symbol1 = "AAPL"; input symbol2 = "IBM"; def fs1 = sfull(symbol = symbol1); def fS2 = sfull(symbol = symbol2); def fs1ext = if IsNaN(fs1) then fs1ext[1] else fs1; def fs2ext = if IsNaN(fS2) then fs2ext[1] else fS2; plot fs1ext_ = fs1ext; fs1ext_.SetDefaultColor(Color.CYAN); plot fs2ext_ = fs2ext; fs2ext_.SetDefaultColor(Color.MAGENTA);
Ruby:declare lower; script sfull { input over_bought = 80; input over_sold = 20; input KPeriod = 17; input DPeriod = 5; input symbol = "/ES"; def priceH = high(symbol); def priceL = low(symbol); def priceC = close(symbol); input slowing_period = 5; input averageType = AverageType.EXPONENTIAL; input showBreakoutSignals = {"No", default "On FullK", "On FullD", "On FullK & FullD"}; def lowest_k = Lowest(priceL, KPeriod); def c1 = priceC - lowest_k; def c2 = Highest(priceH, KPeriod) - lowest_k; def FastK = if c2 != 0 then c1 / c2 * 100 else 0; def FullK = MovingAverage(averageType, FastK, slowing_period); def FullD = MovingAverage(averageType, FullK, DPeriod); def step1 = Max(-100, Min(100, (FullK)) - 50) / 50.01; rec step2 = CompoundValue(1, if IsNaN(0.5 * (Log((1 + step1) / (1 - step1)) + step2[1])) then step2[1] else 0.5 * (Log((1 + step1) / (1 - step1)) + step2[1]), 0); plot SignalLine = if IsNaN(close) then Double.NaN else step2; plot SignalLineBuy = if SignalLine < -1.2 and Sign (step2 - step2[1]) > Sign (step2[1] - step2[2]) and !IsNaN(close) then step2[1] else Double.NaN; plot SignalLineSell = if SignalLine > 1.2 and Sign (step2 - step2[1]) < Sign (step2[1] - step2[2]) and !IsNaN(close) then step2[1] else Double.NaN; } input symbol1 = "/ES"; input symbol2 = "SPX"; def fs1 = sfull(symbol = symbol1).signalline; def fS2 = sfull(symbol = symbol2).signalline; def fs1b = sfull(symbol = symbol1).SignalLineBuy; def fS2b = sfull(symbol = symbol2).SignalLineBuy; def fs1s = sfull(symbol = symbol1).SignalLineSell; def fS2s = sfull(symbol = symbol2).SignalLineSell; def fs1ext = if IsNaN(fs1) then fs1ext[1] else fs1; def fs2ext = if IsNaN(fS2) then fs2ext[1] else fS2; #Plots---------------------------------------------- input step = {default combined, symbol1, symbol2, None}; input lineweight = 2; plot fs1ext_ = if step == step.symbol2 then Double.NaN else fs1ext; fs1ext_.SetDefaultColor(Color.CYAN); plot fs2ext_ = if step == step.symbol1 then Double.NaN else fs2ext; fs2ext_.SetDefaultColor(Color.MAGENTA); def buy = if step == step.combined then fs1b and fS2b else if step == step.symbol1 then fs1b else if step == step.symbol2 then fS2b else if step == step.None then Double.NaN else Double.NaN; plot buyline = if step == step.combined then Min(fs1b, fS2b) else buy; buyline.SetPaintingStrategy(PaintingStrategy.ARROW_UP); buyline.SetDefaultColor(Color.CYAN); buyline.SetLineWeight(lineweight); buyline.HideBubble(); def sell = if step == step.combined then fs1s and fS2s else if step == step.symbol1 then fs1s else if step == step.symbol2 then fS2s else if step == step.None then Double.NaN else Double.NaN; plot sellline = if step == step.combined then Min(fs1s, fS2s) else sell; sellline.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN); sellline.SetDefaultColor(Color.MAGENTA); sellline.SetLineWeight(lineweight); sellline.HideBubble();
Yes! This is more in line with what I was looking for. Thank you very much. One glitch that I hadn't thought about is that the SPX oscillator plot is not going to be accurate during the first 17 bar KPeriod lookback of each day due to the absence of overnight data. This is a big problem, as my intent is to run this on 15 minute and hourly charts, which means that it won't plot at all on the hourly and less than half a day on the 15 minute. TradeStation would allow me to work around this by specifying custom trading sessions for the ES to match the regular trading session of the SPX. Does TOS allow something similar either through a setting or through thinkScript?
Thanks for all your help.
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
C | Code referencing other stocks not working properly | Questions | 2 | |
![]() |
referencing FPL() in a strategy (to set a daily $ cap) | Questions | 1 | |
M | Referencing Floating P/L (FPL()) in scans? | Questions | 2 | |
S | Referencing a custom study in Scan Hacker | Questions | 1 | |
M | Referencing FullK and FullD from Stochastic functions | Questions | 2 |
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.