timeseries-julia-python
random julia and python scripts analyzing stock timeseries data
main.jl
(2877B)
1 using TimeSeries
2 using Statistics
3 using Dates
4
5 include("time_series.jl")
6 include("talib.jl")
7
8 ## data
9
10 AAPL_D = readtimearray("AAPL.csv")
11 SPX_D = readtimearray("SPX.csv")
12
13 AAPL_W = collapse_ohlc_weekly(AAPL_D)
14 SPX_W = collapse_ohlc_weekly(SPX_D)
15
16 ## indicators
17
18 SPX_W_SMA_10 = sma(SPX_W.Close, 10)
19 AAPL_W_High_20 = high(AAPL_W.High, 20)
20 AAPL_W_RoC_20 = roc(AAPL_W.Close, 20)
21 AAPL_W_EMA_07 = ema(AAPL_W.Close, 7)
22
23 ## strategy
24
25 bullish_SPX = findwhen(SPX_W.Close .> SPX_W_SMA_10)
26 higher_high_APPL = findwhen(AAPL_W.High .== AAPL_W_High_20)
27 momentum_AAPL = findwhen(AAPL_W_RoC_20 .> 0.3)
28 buy_dates = intersect(bullish_SPX, higher_high_APPL, momentum_AAPL) .+ Week(1) # buy next week because indicators use closing price
29
30
31 ################### backtest
32 gettime(ta::TimeArray) = timestamp(ta)[1]
33 getvalue(ta::TimeArray) = values(ta)[1]
34
35 account_capital_initial = 1.0
36 account_capital = account_capital_initial
37 percent_gain = 0.0
38 no_of_trades = 0
39 no_of_wins = 0
40
41 bt_date = buy_dates[1]
42 buy_price = getvalue(AAPL_W[bt_date].Open)
43
44 function set_stop_loss(date::TimeType, buy_price::Float64)
45 bullish_broader_market = getvalue(SPX_W[date].Close) > getvalue(SPX_W_SMA_10[date])
46 risk = bullish_broader_market ? 0.2 : 0.1
47 initial_stop_loss = buy_price - (buy_price * risk)
48
49 enable_trailing_stop = getvalue(AAPL_W[date].Close) > buy_price + (buy_price * 0.1)
50 ema = getvalue(AAPL_W_EMA_07[date])
51 enable_trailing_stop ? max(buy_price, ema) : initial_stop_loss
52 end
53
54 stop_loss = set_stop_loss(bt_date, buy_price)
55 profit_target = buy_price + (buy_price * 1)
56
57 lastdate = gettime(AAPL_W[end])
58 while bt_date <= lastdate
59 o,h,l,c = values(AAPL_W[bt_date]) # assumes order in ohlc
60
61 if l <= stop_loss # includes inital and trailing
62 net_profit = stop_loss - buy_price
63 println("netp", net_profit)
64 global account_capital += net_profit
65 global percent_gain = (account_capital - account_capital_initial) / account_capital_initial
66 global no_of_trades += 1
67 global no_of_wins += net_profit > 0 ? 1 : 0
68 global bt_date = buy_dates[findfirst(date -> date > bt_date, buy_dates)]
69 if isnothing(bt_date) break end
70 elseif h > profit_target
71 net_profit = profit_target - buy_price
72 global account_capital += net_profit
73 global percent_gain = (account_capital - account_capital_initial) / account_capital_initial
74 global no_of_trades += 1
75 global no_of_wins += net_profit > 0 ? 1 : 0
76 global bt_date = buy_dates[findfirst(date -> date > bt_date, buy_dates)]
77 if isnothing(bt_date) break end
78 else
79 global stop_loss = set_stop_loss(bt_date, buy_price)
80 global bt_date += Week(1)
81 end
82 end
83
84 ## for each tick
85 # update bt_date
86 # get ohlc of bt_date
87 # check if stop_loss was hit, if so, update account capital w/net profit, and buy next buy_dates on bt_date
88 # check if profit_tg was hit, if so, update account capital w/net profit, and buy next buy_dates on bt_date
89 # update stop loss