Skip to content

Instantly share code, notes, and snippets.

@flare9x
flare9x / rleid.jl
Created April 13, 2018 17:45
Julia data.table::rleid() function
function rleid(x::AbstractVector)
isempty(x) && return Int[]
rle = similar(x, Int)
idx = 1
rle[1] = idx
prev = x[1]
for i in 2:length(x)
this = x[i]
if ismissing(this)
if !ismissing(prev)
@flare9x
flare9x / alpha_vantage.jl
Last active April 22, 2018 12:06
Julia - Download free data alphavantage api
# Download Data Using Alphavantage
using HTTP
using StatPlots
res = HTTP.get("https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=AAPL&interval=1min&apikey=your_api_key&datatype=csv")
mycsv = readcsv(res.body)
x = convert(DataFrame, mycsv)
x = x[2:nrow(x),:] # subset remove header row
# Rename Columns
colnames = ["Date","Open","High","Low","Close","Volume"]
@flare9x
flare9x / rolling_autocorrelation.jl
Created June 12, 2018 01:48
Rolling auto correlation
# Rolling Autocorrelation
# Sliding window (k width)
mean_out = zeros(size(d_es_close,1))
var_out = zeros(size(d_es_close,1))
cov_out = zeros(size(d_es_close,1))
autocor_out = zeros(size(d_es_close,1))
devmean_out = zeros(size(d_es_close,1))
n=1000
k = 1 # set lag
n_lag = 100 # return series lag
@flare9x
flare9x / train_test_set_split.jl
Created June 15, 2018 20:17
Train and Test Sets
# Split data to train and test sets
# dummy data
data = collect(1:1:100)
# Train and test sets
# Set specific % for train and test sets
len = length(data)
mult = .30
split_no = Int64.(round(mult * len))
train_set = data[1:split_no]
test_set = data[split_no+1:len]
@flare9x
flare9x / 1min_data.jl
Last active June 30, 2018 11:09
build any time resolution using 1 minute data
using DataFrames
using CSV
using Missings
# Load 1 minute data - close data sampled every 1 minute
main_data1_1min = CSV.read("C:/Users/Andrew.Bannerman/Desktop/Julia/GE/ES Data/1.min.reg.sess.es_0830_to_1500.txt",types=[String; String; fill(Float64, 4); Int;Int],header=true)
main_data2_1min = CSV.read("C:/Users/Andrew.Bannerman/Desktop/Julia/GE/ES Data/1.min.reg.sess.es_0830_to_1500.txt", header=true)
main_data3_1min = CSV.read("C:/Users/Andrew.Bannerman/Desktop/Julia/GE/ES Data/1.min.reg.sess.es_0830_to_1500.txt", header=true)
test_one = CSV.read("C:/Users/Andrew.Bannerman/Desktop/Julia/GE/ES Data/10.min.reg.sess.es.txt", header=true)
test = convert(Array{Float64},test_one[:Close])
@flare9x
flare9x / na.locf.jl
Last active July 8, 2018 19:18
Carry forward previous value forward to fill NAs
# NA LOCF
# First convert missing to 0 (just easier to work with)
# Pull data to array
example_array = df[:col]
example_array = Float64.(collect(Missings.replace(example_array, 0.0)))
# NA LOCF Function
function locf(x::Array{Float64})
dx = zeros(x)
@flare9x
flare9x / chandelier_exit.jl
Last active August 17, 2018 22:06
chandelier_exit
# Updated to Julia 0.7 - fixed depreciation warnings
# Passed on v1.0
# Volatility Adjusted Exit
# https://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:chandelier_exit
# Step 1 = Rolling n_bar ATR
# Step 2 = Rolling n_bar high
# Step 3 = On any i calcualte ATR * mult (1,2,3,4,5 etc..)
# Step 4 = Subtract ATR mult - rolling n_bar high
@flare9x
flare9x / set_exit_on_close.jl
Last active August 17, 2018 22:07
Set Exit On Close
# Updated to Julia 0.7 - Fixed depreciation warnings
# Passed on v1.0
# Dummy Data
test_entry = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0]
test_data1_day = [5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10]
index = collect(1:1:length(test_entry))
# initialize output
test_n_bar = zero(test_entry)
@flare9x
flare9x / exit_nbar_highest_lowest.jl
Last active August 18, 2018 21:51
Exit Highest / Lowest Previous N Bars
# Built on Julia v1.0
# Sell n_bar highest or lowest
# Dummy data
c = [2,3,4,5,6,7,10,7,4,5,5,300,2,1,3,4,5,900,11,12,13,14,15,11,12,6,1,6,7,100]
h = [4, 5, 6, 7, 8, 9, 12, 9, 6, 7, 7, 5, 4, 3, 5, 6, 7, 79, 13, 14, 15, 16, 17, 13, 7, 8, 7, 8, 9, 10]
l = [4, 5, 6, 7, 8, 9, 12, 9, 6, 7, 7, 5, 4, 3, 5, 6, 7, 79, 13, 14, 15, 16, 17, 13, 7, 8, 7, 8, 9, 10]
some_entry_signal = [0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0] # any type of signal entry
time_index = ["09:00","09:30","10:00","10:30","11:00","11:30","12:00","12:30","13:00","13:30","14:00","14:30","15:00","09:00","09:30","10:00","10:30","11:00","11:30","12:00","12:30","13:00","09:30","10:00",
"10:30","11:00","11:30","12:00","12:30","13:00"]
@flare9x
flare9x / trail_point_exit.jl
Last active August 18, 2018 21:56
Trailing Point Exit
# Built on Julia 1.0
# Trailing point stop
# dummy h l c
c = [2648,2649.75,2654.5,2725.5,2732.75,2729.5,2728,2720.25,2726.75,(2763.25-50),2790.5,2765.25,2721.75,2665,2684.75,2740.25,2711,2742.25,2763.5,2798.75,2805.25,2784,(2763.75-70),2760,2748.75
,2724,2723.75,2692.25,2653.75,2633.5]
l = [2643.0, 2644.75, 2649.5, 2720.5, 2727.75, 2724.5, 2723.0, 2715.25, 2721.75, 2763.25, 2785.5, 2760.25, 2716.75, 2660.0, 2679.75, 2735.25, 2706.0, 2737.25, 2758.5, 2793.75, 2800.25, 2779.0, 2758.75, 2755.0, 2743.75, 2719.0, 2718.75, 2687.25, 2648.75, 2628.5]
h = [2656.0, 2657.75, 2662.5, 2733.5, 2740.75, 2737.5, 2736.0, 2728.25, 2734.75, 2776.25, 2798.5, 2773.25, 2729.75, 2673.0, 2692.75, 2748.25, 2719.0, 2750.25, 2771.5, 2806.75, 2813.25, 2792.0, 2771.75, 2968.0, 2756.75, 2732.0, 2731.75, 2700.25, 2661.75, 2641.5]
some_entry_signal = [0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0]
time_index = ["09:00","09:30","10:00","10:30","11:00","11:30","12:00","15:00","13:00","13:30","15:00","15:00","15:00","09:00","09: