Stream: helpdesk (published)

Topic: Seeking API Design Advice for Setting Defaults


view this post on Zulip G Gundam (Dec 14 2024 at 19:16):

I have a constructor for a mutable Chart struct that is used like this.

using OnlineTechnicalIndicators
using TechnicalIndicatorCharts

golden_cross_chart = Chart(
    "AAPL", Week(1);
    indicators = [
        SMA{Float64}(;period=50),         # Setup indicators
        SMA{Float64}(;period=200)
    ],
    visuals = [
        Dict(
            :label_name => "SMA 50",      # Describe how to draw indicators
            :line_color => "#E072A4",
            :line_width => 2
        ),
        Dict(
            :label_name => "SMA 200",
            :line_color => "#3D3B8E",
            :line_width => 5
        )
    ]
)

There's a 1-to-1 correspondence between the values in indicators and the values in visuals. This is for my unreleased library TechnicalIndicatorCharts.jl.

What if I don't care about colors?

If I don't care how it looks, and I just want to use the defaults, this is what I currently have to do.

golden_cross_chart = Chart(
    "AAPL", Week(1);
    indicators = [
        SMA{Float64}(;period=50),         # Setup indicators
        SMA{Float64}(;period=200)
    ],
    visuals = [
        Dict(),                           # Not the prettiest, but tolerable.
        Dict()
    ]
)

It can get worse.

That might not be so bad, but what if I added Bollinger Bands which is drawn with 3 lines instead of just one like SMA.

golden_cross_chart = Chart(
    "AAPL", Week(1);
    indicators = [
        SMA{Float64}(;period=50),         # Setup indicators
        SMA{Float64}(;period=200),
        BB{Float64}(;period=20)
    ],
    visuals = [
        Dict(),                           # Not the prettiest
        Dict(),
        Dict(
            :upper => Dict(),             # Now it's getting ugly.
            :central => Dict(),
            :lower => Dict()              # All this typing just for defaults?!
        )
    ]
)

view this post on Zulip G Gundam (Dec 14 2024 at 19:17):

How should I do this?

I want to be able to declare defaults in the visuals vector with way less typing. How could this be accomplished?


Last updated: Dec 28 2024 at 04:38 UTC