juliacandlestick-charttechnical-indicator

Julia add indicator to candlestick chart


I'm new to Julia and I'm trying to add a technical indicator (let it be a simple moving average) to my candlestick chart. How should I do that?

using Plots, MarketData, TimeSeries
gr()

ta = yahoo(:GOOG, YahooOpt(period1 = now() - Month(1)))
display(plot(ta, seriestype= :candlestick))
readline()

Solution

  • The general answer to your question is that Plots uses the bang (!) naming convention to naming functions which mutate an existing plot object. Therefore if you want to add something to another plot, you should call plot! (or scatter!, bar!, etc.) after your first plot call.

    In your case, the high level solution would therefore be:

    plot(ta, st = :candlestick)
    plot!(my_indicator_data)
    

    Now you're saying you want a moving average, so here's an example:

    julia> plot(ta, st = :candlestick; xlabel = "Trading Day", ylabel = "Price", 
                xrot = 45, bottom_margin = 4Plots.mm)
    
    julia> using RollingFunctions
    
    julia> plot!(runmean(values((ta.High .+ ta.Low) ./ 2), 5)) # using mid-price
    

    which will give you

    enter image description here

    you might want to double check that this is right, it looks to me like the average line is offset by one day, which might be down to some trickery TimeSeries is doing to the xticks.