Skip to content

Commit

Permalink
Fix candle managment bug in update!
Browse files Browse the repository at this point in the history
  • Loading branch information
g-gundam committed Sep 13, 2024
1 parent a3e42a5 commit 6f3e63a
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ function update!(chart::Chart, c::Candle)
if chart.ts != floor(c.ts, chart.tf)
# timeframe boundary case
chart.ts = floor(c.ts, chart.tf)
chart.candle = merge_candle!(missing, c)
chart.candle = Candle(c.ts, c.o, c.h, c.l, c.c, c.v) # clone tf boundary candle to start fresh candle aggregation
push_new_candle!(chart, c)
else
# normal case
chart.candle = merge_candle!(chart.candle, c)
update_last_candle!(chart, c)
update_last_candle!(chart, chart.candle)
end
end

Expand Down Expand Up @@ -215,3 +215,29 @@ function chart(name, tf; indicators::Vector=[], visuals::Vector{<:Dict}=Vector{D
candle = missing
return Chart(;name, tf, df, indicators, visuals, ts, candle)
end

""" random_candles(tf, n; start)
This belongs somewhere in ~/hak/trading, but it can live here for now.
"""
function random_candles(tf, n;
start = round(now(), tf),
open = 1000.00,
volatility = -0.05:0.01:0.05)
start
res = []
for i in 0:n-1
t = start + (i * tf)
v = rand(volatility, 3)
close = open + (open * v[1])
high = open + (open * abs(v[2]))
low = close - (close * abs(v[3]))
# @info "vals" open high low close
c = Candle(t, open, high, low, close, (100 * v[1]) + 100)
push!(res, c)
open = close
end
res
end

export random_candles

0 comments on commit 6f3e63a

Please sign in to comment.