Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: example of ATR (average true range) #315

Open
margaretkennedy opened this issue Sep 25, 2024 · 0 comments
Open

Question: example of ATR (average true range) #315

margaretkennedy opened this issue Sep 25, 2024 · 0 comments
Assignees

Comments

@margaretkennedy
Copy link
Collaborator

Does Deephaven have a built-in ATR? No, but you can accomplish with code like:

from deephaven import time_table, agg, updateby
from deephaven.calendar import calendar

cal = nyse_cal = calendar("USNYSE_EXAMPLE")

trades = (
    time_table("PT00:00:01", "2024-09-01T00:00 ET")
    .update([
        "Date = formatDate(Timestamp, 'ET')", 
        "Sym = ii%2 == 0 ? `COS` : `SIN`", 
        "Price = ii%2 == 0 ? cos(0.000001*ii) : sin(0.0000001*ii)",
        ])
    .where("cal.isBusinessDay(Date)")
    )

# See https://www.investopedia.com/terms/a/atr.asp

data = (
    trades.agg_by([
        agg.first("Open=Price"),
        agg.last("Close=Price"),
        agg.max_("High=Price"),
        agg.min_("Low=Price")
    ], by=["Date", "Sym"])
)

data = (
    data
    .update("PriorBusDay = cal.plusBusinessDays(Date, -1)")
    .natural_join(data, on=["PriorBusDay=Date", "Sym"], joins="ClosePrior=Close")
    .where("!isNull(ClosePrior)")
    .update("TR = max(High-Low, abs(High-ClosePrior), abs(Low-ClosePrior))")
    .update_by([updateby.rolling_avg_tick("ATR=TR", rev_ticks=14)], by=["Sym"])
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant