Responding to multiple replies. Thanks.
From Candles
export Candle, CandleFull, CandleJpy, CandleFullJpy, hcavg, hlavg, lcavg,
movement, ohlcavg, hlcavg, show, candles, csv
candletest.js
below is long but all is here.
module CandlesTest
using Test, Dates, Printf
include("../src/core/Prices.jl")
using .Prices
include("../src/core/Candles.jl")
using .Candles
include("../src/core/TimeFrames.jl")
using .TimeFrames
dt = now(UTC)
dt_s = string(dt)
price = 1.00000
price_s = "1.00000"
s1 = TimeFrames.S1
s5 = TimeFrames.S5
s10 = TimeFrames.S10
s15 = TimeFrames.S15
s30 = TimeFrames.S30
m1 = TimeFrames.M1
m2 = TimeFrames.M2
m4 = TimeFrames.M4
m5 = TimeFrames.M5
m10 = TimeFrames.M10
m15 = TimeFrames.M15
m30 = TimeFrames.M30
h1 = TimeFrames.H1
h2 = TimeFrames.H2
h3 = TimeFrames.H3
h4 = TimeFrames.H4
h6 = TimeFrames.H6
h8 = TimeFrames.H8
h12 = TimeFrames.H12
dt_candles_start = DateTime(2017, 1, 1, 21, 0)
@testset "Testing All Candles" begin
@testset "Testing Candle" begin
candle = Candle(dt, price, price, price, price)
candle_s = Candle(dt_s, price_s, price_s, price_s, price_s)
candle_jpy = CandleJpy(dt,100.000,100.000,100.000,100.000)
@test candle.datetime == dt
@test candle.open == price
@test candle.high == price
@test candle.low == price
@test candle.close == price
@test candle_s.datetime == dt
@test candle_s.open == price
@test candle_s.high == price
@test candle_s.low == price
@test candle_s.close == price
@test hcavg(candle) == 1.0
@test hlavg(candle) == 1.0
@test lcavg(candle) == 1.0
@test movement(candle) == 0.0
@test ohlcavg(candle) == 1.0
@test hlcavg(candle) == 1.0
@test string(candle) == "$(candle.datetime) o:1.00000 h:1.00000 l:1.00000 c:1.00000"
@test csv(candle) == "$(candle.datetime),1.00000,1.00000,1.00000,1.00000"
@test csv(candle_jpy) == "$(candle.datetime),100.000,100.000,100.000,100.000"
end
@testset "Testing CandleFull" begin
candle = CandleFull(dt, price, price, price, price, price, price, price, price, 100)
candle_s = CandleFull(dt_s, price_s, price_s, price_s, price_s,
price_s, price_s, price_s, price_s, "100")
candle_jpy = CandleFullJpy(dt,100.000,100.000,100.000,100.000,
100.000,100.000,100.000,100.000, 100)
@test candle.datetime == dt
@test candle.bidopen == price
@test candle.bidhigh == price
@test candle.bidlow == price
@test candle.bidclose == price
@test candle.askopen == price
@test candle.askhigh == price
@test candle.asklow == price
@test candle.askclose == price
@test candle.volume == 100
@test candle_s.datetime == dt
@test candle_s.bidopen == price
@test candle_s.bidhigh == price
@test candle_s.bidlow == price
@test candle_s.bidclose == price
@test candle_s.askopen == price
@test candle_s.askhigh == price
@test candle_s.asklow == price
@test candle_s.askclose == price
@test candle_s.volume == 100
@test hcavg(candle) == 1.0
@test hlavg(candle) == 1.0
@test lcavg(candle) == 1.0
@test movement(candle) == 0.0
@test ohlcavg(candle) == 1.0
@test hlcavg(candle) == 1.0
@test string(candle) == "$(candle.datetime) bid o:1.00000 h:1.00000 l:1.00000 c:1.00000" *
" ask o:1.00000 h:1.00000 l:1.00000 c:1.00000 v:100"
@test csv(candle) == "$(candle.datetime),1.00000,1.00000,1.00000,1.00000," *
"1.00000,1.00000,1.00000,1.00000,100"
@test csv(candle_jpy) == "$(candle.datetime),100.000,100.000,100.000,100.000," *
"100.000,100.000,100.000,100.000,100"
end
@testset "Converting candles" begin
# We have to create a lot of M1 candles in order to have 50 tests per timeframe.
# I think 50 tests provides a sufficient data set to confirm problems and fix them.
# If something were to require larger data set it is always available to adjust
# the first(m1_candles, ??) for any testset.
vsize = 72000
m1_candles = Vector{Candle}(undef, vsize)
startt = now(UTC)
# println("Creating $vsize m1 candles at: $startt")
for i in 1:vsize
ct = dt_candles_start + Minute(i)
open = 1.0 + (i * 0.00001)
high = open + 0.00001
low = open - 0.00001
close = high
m1_candles[i] = Candle(Dates.format(ct, ISODateTimeFormat), @sprintf("%0.5f", open),
@sprintf("%0.5f", high), @sprintf("%0.5f", low), @sprintf("%0.5f", close))
end
endt = now(UTC)
println("printing from m1_candles")
println(m1_candles[1])
println(m1_candles[1000])
# println("Finished creating $vsize m1 candles at: $(endt)")
# println("Total time creating $vsize m1 candles: $(endt - startt)")
# In normal usage we test for high >= low as they can be equal
# But due to specific creation of candles and their properties.
# For the purpose of these test the high > low should always be true.
@testset "Testing m1m5 candles conversion" begin
tfduration = m5.duration
tfcandles = Candles.candles(first(m1_candles, 250), m5)
for index in 2:(length(tfcandles))
candle = tfcandles[index]
dt = (dt_candles_start + ((index - 1) * tfduration))
@test candle.datetime == floor(dt, tfduration)
@test candle.open == round((1.0 + ((index - 1) * tfduration.value * 0.00001)), digits=5)
@test candle.low == round((candle.open - 0.00001), digits=5)
@test candle.high == candle.close
@test (candle.high > candle.low) == true
end
end
@testset "Testing m1m10 candles conversion" begin
tfduration = m10.duration
tfcandles = Candles.candles(first(m1_candles, 500), m10)
for index in 2:(length(tfcandles))
candle = tfcandles[index]
dt = (dt_candles_start + ((index - 1) * tfduration))
@test candle.datetime == floor(dt, tfduration)
@test candle.open == round((1.0 + ((index - 1) * tfduration.value * 0.00001)), digits=5)
@test candle.low == round((candle.open - 0.00001), digits=5)
@test candle.high == candle.close
@test (candle.high > candle.low) == true
end
end
@testset "Testing m1m15 candles conversion" begin
tfduration = m15.duration
tfcandles = Candles.candles(first(m1_candles, 750), m15)
for index in 2:(length(tfcandles))
candle = tfcandles[index]
dt = (dt_candles_start + ((index - 1) * tfduration))
@test candle.datetime == floor(dt, tfduration)
@test candle.open == round((1.0 + ((index - 1) * tfduration.value * 0.00001)), digits=5)
@test candle.low == round((candle.open - 0.00001), digits=5)
@test candle.high == candle.close
@test (candle.high > candle.low) == true
end
end
@testset "Testing m1m30 candles conversion" begin
tfduration = m30.duration
tfcandles = Candles.candles(first(m1_candles, 1500), m30)
for index in 2:(length(tfcandles))
candle = tfcandles[index]
dt = (dt_candles_start + ((index - 1) * tfduration))
@test candle.datetime == floor(dt, tfduration)
@test candle.open == round((1.0 + ((index - 1) * tfduration.value * 0.00001)), digits=5)
@test candle.low == round((candle.open - 0.00001), digits=5)
@test candle.high == candle.close
@test (candle.high > candle.low) == true
end
end
@testset "Testing m1h1 candles conversion" begin
tfduration = h1.duration
tfcandles = Candles.candles(first(m1_candles, 3000), h1)
for index in 2:(length(tfcandles))
candle = tfcandles[index]
dt = (dt_candles_start + ((index - 1) * tfduration))
@test candle.datetime == floor(dt, tfduration)
minutes = (index - 1) * Minute(tfduration).value
@test candle.open == round((1.0 + (minutes * 0.00001)), digits=5)
@test candle.low == round((candle.open - 0.00001), digits=5)
@test candle.high == candle.close
@test (candle.high > candle.low) == true
end
end
@testset "Testing m1h2 candles conversion" begin
tfduration = h2.duration
tfcandles = Candles.candles(first(m1_candles, 6000), h2)
offset = Minute(tfduration).value - (tfcandles[2].open * 100000 - 100000)
for index in 2:(length(tfcandles))
candle = tfcandles[index]
dt = (dt_candles_start + ((index - 1) * tfduration))
@test candle.datetime == floor(dt, tfduration)
minutes = ((index-1) * Minute(tfduration).value) - offset
@test candle.open == round((1.0 + (minutes * 0.00001)), digits=5)
@test candle.low == round((candle.open - 0.00001), digits=5)
@test candle.high == candle.close
@test (candle.high > candle.low) == true
end
end
@testset "Testing m1h3 candles conversion" begin
tfduration = h3.duration
tfcandles = Candles.candles(first(m1_candles, 9000), h3)
offset = Minute(tfduration).value - (tfcandles[2].open * 100000 - 100000)
for index in 2:(length(tfcandles))
candle = tfcandles[index]
dt = (dt_candles_start + ((index - 1) * tfduration))
@test candle.datetime == floor(dt, tfduration)
minutes = ((index-1) * Minute(tfduration).value) - offset
@test candle.open == round((1.0 + (minutes * 0.00001)), digits=5)
@test candle.low == round((candle.open - 0.00001), digits=5)
@test candle.high == candle.close
@test (candle.high > candle.low) == true
end
end
@testset "Testing m1h4 candles conversion" begin
tfduration = h4.duration
tfcandles = Candles.candles(first(m1_candles, 12000), h4)
offset = Minute(tfduration).value - (tfcandles[2].open * 100000 - 100000)
for index in 2:(length(tfcandles))
candle = tfcandles[index]
dt = (dt_candles_start + ((index - 1) * tfduration))
@test candle.datetime == floor(dt, tfduration)
minutes = ((index-1) * Minute(tfduration).value) - offset
@test candle.open == round((1.0 + (minutes * 0.00001)), digits=5)
@test candle.low == round((candle.open - 0.00001), digits=5)
@test candle.high == candle.close
@test (candle.high > candle.low) == true
end
end
@testset "Testing m1h6 candles conversion" begin
tfduration = h6.duration
tfcandles = Candles.candles(first(m1_candles, 18000), h6)
offset = Minute(tfduration).value - (tfcandles[2].open * 100000 - 100000)
for index in 2:(length(tfcandles))
candle = tfcandles[index]
dt = (dt_candles_start + ((index - 1) * tfduration))
@test candle.datetime == floor(dt, tfduration)
minutes = ((index-1) * Minute(tfduration).value) - offset
@test candle.open == round((1.0 + (minutes * 0.00001)), digits=5)
@test candle.low == round((candle.open - 0.00001), digits=5)
@test candle.high == candle.close
@test (candle.high > candle.low) == true
end
end
@testset "Testing m1h12 candles conversion" begin
tfduration = h12.duration
tfcandles = Candles.candles(first(m1_candles, 36000), h12)
offset = Minute(tfduration).value - (tfcandles[2].open * 100000 - 100000)
for index in 2:(length(tfcandles))
candle = tfcandles[index]
dt = (dt_candles_start + ((index - 1) * tfduration))
@test candle.datetime == floor(dt, tfduration)
minutes = ((index-1) * Minute(tfduration).value) - offset
@test candle.open == round((1.0 + (minutes * 0.00001)), digits=5)
@test candle.low == round((candle.open - 0.00001), digits=5)
@test candle.high == candle.close
@test (candle.high > candle.low) == true
end
end
@testset "Testing m1D candles conversion" begin
tfduration = TimeFrames.D.duration
tfcandles = Candles.candles(first(m1_candles, 72000), TimeFrames.D)
offset = Minute(tfduration).value - (tfcandles[2].open * 100000 - 100000)
for index in 2:(length(tfcandles))
candle = tfcandles[index]
dt = (dt_candles_start + ((index - 1) * tfduration))
@test candle.datetime == floor(dt, tfduration)
minutes = ((index-1) * Minute(tfduration).value) - offset
@test candle.open == round((1.0 + (minutes * 0.00001)), digits=5)
@test candle.low == round((candle.open - 0.00001), digits=5)
@test candle.high == candle.close
@test (candle.high > candle.low) == true
end
end
end
@testset "Converting prices to candles" begin
#size set so as to not create a candle based on a single price.
# That messes up testing.
vsize = 3059
prices = Vector{Price}(undef, vsize)
startt = now(UTC)
# println("Creating $vsize prices at: $startt")
for i in 1:vsize
ct = dt_candles_start + Second(i)
price = round((1.0 + (i * 0.00001)), digits=5)
prices[i] = Price(ct, price)
end
endt = now(UTC)
# println("Finished creating $vsize prices at: $endt")
@testset "Testing prices_m1 candles conversion" begin
# println("Total time creating m1 candles")
tfduration = m1.duration
tfcandles = Candles.candles(prices, m1)
for index in 2:(length(tfcandles))
candle = tfcandles[index]
@test candle.datetime == dt_candles_start + ((index - 1) * tfduration)
seconds = (index - 1) * Second(tfduration).value
@test candle.open == round((1.0 + (seconds * 0.00001)), digits=5)
@test candle.low == candle.open
@test candle.high == candle.close
@test (candle.high > candle.low) == true
end
end
end
@testset "Converting CandlesFull" begin
#size set so as to not create a candle based on a single price.
# That messes up testing.
vsize = 3059
prices = Vector{PriceFull}(undef, vsize)
startt = now(UTC)
# println("Creating $vsize pricesfull at: $startt")
for i in 1:vsize
ct = dt_candles_start + Second(i)
price = round((1.0 + (i * 0.00001)), digits=5)
prices[i] = PriceFull(ct, price, price)
end
endt = now(UTC)
# println("Finished creating $vsize pricesfull at: $endt")
@testset "Testing prices_m1 candles conversion" begin
# println("Total time creating candlesfull")
tfduration = m1.duration
tfcandles = Candles.candles(prices, m1)
for index in 2:(length(tfcandles))
candle = tfcandles[index]
@test candle.datetime == dt_candles_start + ((index - 1) * tfduration)
seconds = (index - 1) * Second(tfduration).value
@test candle.bidopen == round((1.0 + (seconds * 0.00001)), digits=5)
@test candle.bidlow == candle.bidopen
@test candle.bidhigh == candle.bidclose
@test (candle.bidhigh > candle.bidlow) == true
@test candle.askopen == candle.bidopen
@test candle.asklow == candle.askopen
@test candle.askhigh == candle.askclose
@test (candle.askhigh > candle.asklow) == true
end
end
end
end
end #module CandlesTest
using CandlesTest
main()
Now the using CandlesTest
and main()
at the end are new, from desperately trying to make it work attempting things I have read here.
Again thanks for helping solve this.
Jimmie