Need help with simple code.

fungus12

Member
Hi. I'm trying to get this simple idea coded and I'm not sure what I'm doing wrong. The indicator is the IFT_Stoch that comes with thinkorswim. What I want to have happen is an arrow appear whenever the IFT and the Stoch are equal. Here's the code:

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2012-2022
#

declare lower;

input price = close;
input length = 30;
input slowingLength = 5;
input over_bought = 60;
input over_sold = 30;


def rainbow = reference RainbowAverage(price = price, averageType = AverageType.WEIGHTED);
plot Stochastic = Sum(rainbow - Lowest(rainbow, length), slowingLength) / (Sum(Highest(rainbow, length) - Lowest(rainbow, length), slowingLength) + 0.0001) * 100;

def normStochRainbow = 0.1 * (Stochastic - 50);
plot IFT = 100 / (1 + Exp(-2 * normStochRainbow));
plot OverBought = over_bought;
plot OverSold = over_sold;

Stochastic.SetDefaultColor(GetColor(1));
IFT.SetDefaultColor(GetColor(2));
OverBought.SetDefaultColor(GetColor(5));
OverSold.SetDefaultColor(GetColor(5));

#my code ------------------------------
plot down = if Stochastic >= IFT then IFT else Double.NaN;
down.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

The issue is that it seems to be working when the IFT and Stoch are above the overbought line but not when they're below that. No idea why that's happening. Any help would be appreciated.
 
Solution
Try something like this. I didn't test it though.

Ruby:
plot down = if Stochastic > overbought and Stochastic <= IFT then IFT else Double.NaN;
down.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
plot up   = if Stochastic < OverSold and Stochastic >= IFT then IFT else Double.NaN;
up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Try something like this. I didn't test it though.

Ruby:
plot down = if Stochastic > overbought and Stochastic <= IFT then IFT else Double.NaN;
down.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
plot up   = if Stochastic < OverSold and Stochastic >= IFT then IFT else Double.NaN;
up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
 
Solution

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

Hi. I'm trying to get this simple idea coded and I'm not sure what I'm doing wrong. The indicator is the IFT_Stoch that comes with thinkorswim. What I want to have happen is an arrow appear whenever the IFT and the Stoch are equal. Here's the code:

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2012-2022
#

declare lower;

input price = close;
input length = 30;
input slowingLength = 5;
input over_bought = 60;
input over_sold = 30;


def rainbow = reference RainbowAverage(price = price, averageType = AverageType.WEIGHTED);
plot Stochastic = Sum(rainbow - Lowest(rainbow, length), slowingLength) / (Sum(Highest(rainbow, length) - Lowest(rainbow, length), slowingLength) + 0.0001) * 100;

def normStochRainbow = 0.1 * (Stochastic - 50);
plot IFT = 100 / (1 + Exp(-2 * normStochRainbow));
plot OverBought = over_bought;
plot OverSold = over_sold;

Stochastic.SetDefaultColor(GetColor(1));
IFT.SetDefaultColor(GetColor(2));
OverBought.SetDefaultColor(GetColor(5));
OverSold.SetDefaultColor(GetColor(5));

#my code ------------------------------
plot down = if Stochastic >= IFT then IFT else Double.NaN;
down.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

The issue is that it seems to be working when the IFT and Stoch are above the overbought line but not when they're below that. No idea why that's happening. Any help would be appreciated.

2 variables are rarely the same. so to check when they are equal may miss some desired conditions.
might be better to check the difference of the 2, and compare that to a small number or %.
could check for a crossing, but that might not happen.

Code:
#replace a and b with desired formules.
def a = ...
def b = ...
input near_percent = 0.4;
def diff = absvalue(a-b);
def per = (100 * diff/a);
def near = per <= near_percent;
plot down = if near then high else double.nan;
down.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#
 
Try something like this. I didn't test it though.

Ruby:
plot down = if Stochastic > overbought and Stochastic <= IFT then IFT else Double.NaN;
down.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
plot up   = if Stochastic < OverSold and Stochastic >= IFT then IFT else Double.NaN;
up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
That didn't quite work. It's now just creating arrows whenever both are above OS or OB, rather than only when they're equal. However, what's interesting is that when they ARE equal, arrows are not displayed in both areas. So maybe doing the inverse of that code would fix it?

EDIT: yep that seemed to do it. Changing the equal-to operators in both lines did the trick. Thanks a lot!

2 variables are rarely the same. so to check when they are equal may miss some desired conditions.
might be better to check the difference of the 2, and compare that to a small number or %.
could check for a crossing, but that might not happen.

Code:
#replace a and b with desired formules.
def a = ...
def b = ...
input near_percent = 0.4;
def diff = absvalue(a-b);
def per = (100 * diff/a);
def near = per <= near_percent;
plot down = if near then high else double.nan;
down.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#
I'm not sure I follow. The two variables are the same often enough so I feel like it's fine to have that as my parameter. For instance, in the code I listed in my original post, there were several instances of them being equal showcased by an arrow. But the issue was that for some reason this would only work when they were equal in the Overbought area. For some reason when they became equal in the Oversold area, the arrows were just spammed throughout all bars. So the code I listed beforehand.
In regard to the code you posted, it seems like it just checks for it to be "close enough".

EDIT: problem solved. Here's the working code to those interested:

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2012-2022
#

declare lower;

input price = close;
input length = 30;
input slowingLength = 5;
input over_bought = 60;
input over_sold = 30;


def rainbow = reference RainbowAverage(price = price, averageType = AverageType.WEIGHTED);
plot Stochastic = Sum(rainbow - Lowest(rainbow, length), slowingLength) / (Sum(Highest(rainbow, length) - Lowest(rainbow, length), slowingLength) + 0.0001) * 100;

def normStochRainbow = 0.1 * (Stochastic - 50);
plot IFT = 100 / (1 + Exp(-2 * normStochRainbow));
plot OverBought = over_bought;
plot OverSold = over_sold;

Stochastic.SetDefaultColor(GetColor(1));
IFT.SetDefaultColor(GetColor(2));
OverBought.SetDefaultColor(GetColor(5));
OverSold.SetDefaultColor(GetColor(5));

#my code ------------------------------
plot down = if Stochastic > overbought and Stochastic >= IFT then IFT else Double.NaN;
down.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
plot up   = if Stochastic < OverSold and Stochastic <= IFT then IFT else Double.NaN;
up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
501 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