NexusFi: Find Your Edge


Home Menu

 





ThinkScript help with HMA alert message


Discussion in ThinkOrSwim

Updated
    1. trending_up 2,470 views
    2. thumb_up 3 thanks given
    3. group 2 followers
    1. forum 2 posts
    2. attach_file 1 attachments




 
Search this Thread
  #1 (permalink)
zentrader618
ny, ny/us
 
Posts: 3 since Aug 2019
Thanks Given: 1
Thanks Received: 0

Good morning forum,
Below is a script that I added an alert that is triggering a buy & sell message at the same time whenever I open a chart which to me doesn't make sense...I thought that the condition would only look at the last met condition and trigger a buy or sell message.

code:

declare upper;

input price = CLOSE;
input HMA_Length = 8;
input lookback = 2;
input labels=no;

plot HMA = HullMovingAvg(price = price, length = HMA_Length);

def delta = HMA[1] - HMA[lookback + 1];
def delta_per_bar = delta / lookback;

def next_bar = HMA[1] + delta_per_bar;

def concavity = if HMA > next_bar then 1 else -1;

plot turning_point = if concavity[1] != concavity then HMA else double.nan;

HMA.AssignValueColor(Color = if concavity[1] == -1 then
if HMA > HMA[1] then color.black else color.blue else
if HMA < HMA[1] then color.yellow else color.orange);

HMA.SetLineWeight(4);

turning_point.SetLineWeight(4);
turning_point.SetPaintingStrategy(paintingStrategy = PaintingStrategy.POINTS);
turning_point.SetDefaultColor(color.white);

plot MA_Max = if HMA[-1] < HMA and HMA > HMA[1] then HMA else Double.NaN;
MA_Max.SetDefaultColor(Color.WHITE);
MA_Max.SetPaintingStrategy(PaintingStrategy.SQUARES);
MA_Max.SetLineWeight(3);

plot MA_Min = if HMA[-1] > HMA and HMA < HMA[1] then HMA else Double.Nan;
MA_Min.SetDefaultColor(Color.WHITE);
MA_Min.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
MA_Min.SetLineWeight(3);

plot sell = if turning_point and concavity == -1 then high else double.nan;
sell.SetDefaultColor(Color.blue);
sell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
sell.SetLineWeight(1);

plot buy = if turning_point and concavity == 1 then low else double.nan;
buy.SetDefaultColor(Color.orange);
buy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
buy.SetLineWeight(1);

def ROC = HMA - next_bar;

addLabel(yes and labels, concat("ROC: " , ROC), color = if concavity < 0 then if ROC[1] > ROC then Color.blue else color.black else if ROC[1] < ROC then color.orange else color.red);

def arrow_plots = high-low;
def plothigh = high + arrow_plots * 0.3;
def plotlow = low - arrow_plots * 0.3;

plot arrowUP = if MA_Min then plotlow else double.nan;
ArrowUP.setpaintingStrategy(PaintingStrategy.ARROW_UP);
ArrowUP.SetLineWeight(3);
ArrowUP.SetDefaultColor(Color.ORANGE);

plot arrowdown = if MA_Max then plothigh else double.nan;
ArrowDown.setpaintingStrategy(PaintingStrategy.ARROW_Down);
ArrowDown.SetLineWeight(3);
ArrowDown.SetDefaultColor(Color.BLUE);

def condition1 = if isnan(HMA[-1] > HMA and HMA < HMA[1]) then 1 else 0;
def condition2 = if isnan(HMA[-1] < HMA and HMA > HMA[1]) then 1 else 0;


Alert(condition1, "MA Min Buy", Alert.Bar, Sound.Chimes);
Alert(condition2, "MA Max Sell", Alert.Bar, Sound.Chimes);


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Trump Media to sell instant access to market-moving soci …
Traders Hideout
Second Night of US Strikes Crushes Iran June 15 to 3.6% …
Prediction Markets & Event Contracts
$500M Riding on World Cup 2026: France/Spain Co-Favored …
Prediction Markets & Event Contracts
Memorandum Watch: How the 60-Day MOU Framework Makes May …
Prediction Markets & Event Contracts
June 15 Peace Odds Surge From 3.6% to 12.25% After Trump …
Prediction Markets & Event Contracts
 
  #2 (permalink)
ivoTrades
Europe
 
Posts: 30 since Aug 2018
Thanks Given: 20
Thanks Received: 47


zentrader618 View Post
def condition1 = if isnan(HMA[-1] > HMA and HMA < HMA[1]) then 1 else 0;
def condition2 = if isnan(HMA[-1] < HMA and HMA > HMA[1]) then 1 else 0;

Looks like the bug is that condition1 and condition2 are always true. I haven't programmed in ThinkScript what does HMA[-1] mean in this context?

I assume HMA is the current value, same as HMA[0] and HMA[1] is the previous value, is HMA[-1] the value in the next time period? If that's the case the next time period does not yet exist at the moment you load the chart so isnan always returns true, which results in the bug you're experiencing.


Reply With Quote
  #3 (permalink)
zentrader618
ny, ny/us
 
Posts: 3 since Aug 2019
Thanks Given: 1
Thanks Received: 0


Hi there ivoTrades,

You are correct in that HMA[-1] is the next bar. Thanks for the comment about isnan. I will do more research on how to correct that.


Attached Thumbnails
Click image for larger version

Name:	image_333.png
Views:	264
Size:	20.7 KB
ID:	306531  
Reply With Quote




Last Updated on November 15, 2020


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts