In [130]:
#Load packages
Pkg.add("TimeSeries");Pkg.add("MarketData");Pkg.add("Plots");Pkg.add("PlotlyJS");
Pkg.add("Gadfly");Pkg.add("StatsBase");Pkg.update(); Pkg.add("PlotRecipes");
In [1]:
using TimeSeries, MarketData, Plots, PlotlyJS, StatsBase,PlotRecipes;
In [131]:
typeof(ohlc)
Out[131]:
In [132]:
ohlc
Out[132]:
In [ ]:
In [6]:
α = .5
@code_llvm println("Helloworld")
In [14]:
function hola1(x::Int64)
x +3
end
Out[14]:
In [12]:
hola(3)
Out[12]:
In [17]:
peakflops()
Out[17]:
In [18]:
@time hola1(3)
Out[18]:
In [19]:
using BenchmarkTools
In [20]:
@benchmark hola1(3+5*4)
Out[20]:
In [133]:
ohlc["Open"]
Out[133]:
In [134]:
import TimeSeries.moving
In [135]:
# Función ventana
function moving{T}(ta::TimeArray{T,1}, f::Function, window::Int; padding::Bool=false)
tstamps = padding ? ta.timestamp : ta.timestamp[window:end]
vals = zeros(ta.values[window:end])
@inbounds for i=1:length(vals)
vals[i] = f(ta.values[(window-1)-i:])
end
padding && (vals = [NaN*ones(window-1); vals])
TimeArray(tstamps, vals, ta.colnames, ta.meta)
end
function moving{T}(ta::TimeArray{T,2}, f::Function, window::Int; padding::Bool=false)
tstamps = padding ? ta.timestamp : ta.timestamp[window:end]
vals = zeros(ta.values[window:end, :])
@inbounds for i=1:size(vals,1), j=1:size(vals, 2)
vals[i, j] = f(ta.values[i:i+(window-1), j])
end
padding && (vals = [NaN*ones(ta.values[1:(window-1), :]); vals])
TimeArray(tstamps, vals, ta.colnames, ta.meta)
end
Out[135]:
In [179]:
?first
Out[179]:
In [175]:
#Nice, ya podemos tomar promedios en ventanas de tamaño N = 10
moving(ohlc,mean,10)
Out[175]:
In [139]:
# Aquí se van a desarrollar las funciones estrategia
In [144]:
moving(ohlc,psnr,10)
In [145]:
?psnr
Out[145]:
In [149]:
using TimeSeries
In [150]:
@recipe function f{T<:TimeArray}(ta::T)
st = get(d, :seriestype, :path)
if in(st, [:candlestick, :heikinashi])
Candlestick(ta)
#elseif st == :ohlc #ohlc (meaning sticks with steps on the sides) should be passed on to Plots internal ohlc plot engine
# ta, ohlc = extract_ohlc(ta)
# collect(zip(ohlc)) # But there are currently issues with that
else
labels --> reshape(ta.colnames,1,length(ta.colnames))
seriestype := st
ta.timestamp, ta.values
end
end
type Candlestick{D <: TimeType}
time::Vector{D}
open::AbstractVector
high::AbstractVector
low::AbstractVector
close::AbstractVector
end
Candlestick(ta::TimeArray) = Candlestick(extract_ohlc(ta)...)
function extract_ohlc(ta::TimeArray)
indices = [find(x->lowercase(x) == name, ta.colnames) for name in ["open", "high", "low", "close"]]
minimum(length.(indices)) < 1 && error("The time array did not have variables named open, high, low and close")
(ta.timestamp, [ta.values[:,i] for i in 1:4]...)
end
function HeikinAshi!(cs::Candlestick) #some values here are made too high!
cs.close[1] = (cs.open[1] + cs.low[1] + cs.close[1] + cs.high[1]) / 4
cs.open[1] = (cs.open[1] + cs.close[1])/2
cs.high[1] = cs.high[1]
cs.low[1] = cs.low[1]
for i in 2:length(cs.open)
cs.close[i] = (cs.open[i] + cs.low[i] + cs.close[i] + cs.high[i]) / 4
cs.open[i] = (cs.open[i-1] + cs.close[i-1]) / 2
cs.high[i] = maximum([cs.high[i], cs.open[i], cs.close[i]])
cs.low[i] = minimum([cs.low[i], cs.open[i], cs.close[i]])
end
end
@recipe function f(cs::Candlestick)
st = get(d, :seriestype, :candlestick)
st == :heikinashi && HeikinAshi!(cs)
seriestype := :candlestick
legend --> false
linewidth --> 0.7
grid --> false
bw = get(d, :bar_width, nothing)
bw == nothing && (bw = 0.8)
bar_width := bw / 2 * minimum(diff(unique(Int.(cs.time))))
# allow passing alternative colors as a vector
cols = get(d, :seriescolor, nothing)
cols = (isa(cols, Vector{Symbol}) && length(cols) == 2) ? cols : [:red, :blue]
attributes = [
Dict(:close_open => <, :close_prev => <, :bottombox => cs.close, :topbox => cs.open, :fill => cols[1], :line => cols[1], :fillalpha => 1),
Dict(:close_open => <, :close_prev => >=, :bottombox => cs.close, :topbox => cs.open, :fill => cols[2], :line => cols[2], :fillalpha => 1),
Dict(:close_open => >=, :close_prev => <, :bottombox => cs.open, :topbox => cs.close, :fill => :white, :line => cols[1], :fillalpha => 0),
Dict(:close_open => >=, :close_prev => >=, :bottombox => cs.open, :topbox => cs.close, :fill => :white, :line => cols[2], :fillalpha => 0)
]
for att in attributes
inds = Vector{Int}(length(cs.close))
inds[1] = att[:close_open](cs.close[1], cs.open[1]) & att[:close_prev](cs.close[1], cs.close[1])
inds[2:end] .= att[:close_open].(cs.close[2:end], cs.open[2:end]) & att[:close_prev].(diff(cs.close), 0)
inds = find(inds)
if length(inds) > 0
@series begin
linecolor := att[:line]
fillcolor := att[:fill]
fillalpha := att[:fillalpha]
fillto := att[:bottombox][inds]
seriestype := :bar
cs.time[inds], att[:topbox][inds]
end
for j in 1:2
@series begin
primary := false
linecolor := att[:line]
seriestype := :sticks
fillto := j == 1 ? cs.low[inds] : att[:topbox][inds]
cs.time[inds], j == 1 ? att[:bottombox][inds] : cs.high[inds]
end
end
end
end
end
In [154]:
@recipe Candlestick(ohlc)
In [ ]:
In [152]:
plot(Candlestick(ohlc))
Out[152]:
In [153]:
@recipe function f{T<:TimeArray}(ta::T)
st = get(d, :seriestype, :path)
if in(st, [:candlestick, :heikinashi])
Candlestick(ta)
#elseif st == :ohlc #ohlc (meaning sticks with steps on the sides) should be passed on to Plots internal ohlc plot engine
# ta, ohlc = extract_ohlc(ta)
# collect(zip(ohlc)) # But there are currently issues with that
else
labels --> reshape(ta.colnames,1,length(ta.colnames))
seriestype := st
ta.timestamp, ta.values
end
end
In [157]:
Pkg.add("Indicators")
In [158]:
using Indicators
In [161]:
ohlc["Open"]
Out[161]:
In [163]:
Indicators.
Out[163]:
In [164]:
function module_functions(modname)
list = Symbol[]
for nm in names(modname)
typeof(eval(nm)) == Function && push!(list,nm)
end
return list
end
module_functions(Indicators)
Out[164]:
In [165]:
module_functions(Indicators)
Out[165]:
In [170]:
Indicators.adx
Indicators.alma
Indicators.aroon
Indicators.atr
Indicators.bbands
Indicators.cci
Indicators.close_fun
Indicators.crossover
Indicators.crossunder
Indicators.dema
Indicators.diffn
Indicators.donch
Indicators.ema
Indicators.eval
Indicators.hl_fun
Indicators.hlc_fun
Indicators.hma
Indicators.interpolate
Indicators.kama
Indicators.keltner
Indicators.kst
Indicators.macd
Indicators.mama
Indicators.maxima
Indicators.minima
Indicators.mlr
Indicators.mlr_bands
Indicators.mlr_intercept
Indicators.mlr_lb
Indicators.mlr_rsq
Indicators.mlr_se
Indicators.mlr_slope
Indicators.mlr_ub
Indicators.psar
Indicators.resistance
Indicators.roc
Indicators.rsi
Indicators.runcor
Indicators.runcov
Indicators.runmad
Indicators.runmax
Indicators.runmean
Indicators.runsd
Indicators.runsum
Indicators.runvar
Indicators.sma
Indicators.smi
Indicators.stoch
Indicators.support
Indicators.swma
Indicators.tema
Indicators.tr
Indicators.trima
Indicators.wilder_sum
Indicators.wma
Indicators.wpr
In [171]:
dump(names(Indicators))
In [58]:
function rates1(rate,daily)
println( "Annualized rate of ",rate," yearly is ",(rate/daily + 1)^365)
end
Out[58]:
In [59]:
rates1(.0643,365)
In [67]:
rates
Out[67]:
In [83]:
ratesss = [.0643, .0665, .0681, .0690]
dailies = [365, 6365/4, 365/2, 1]
for i in 1:4
rates1(ratesss[i],dailies[i])
end
In [ ]: