Skip to content

Commit

Permalink
Merge pull request #36 from SaguaroCapital/tyler-rework-impute
Browse files Browse the repository at this point in the history
simplify impute
  • Loading branch information
tylerjthomas9 authored Nov 30, 2023
2 parents 3aef664 + e17aade commit 575c410
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 227 deletions.
20 changes: 10 additions & 10 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SaguaroTrader"
uuid = "26277856-a3f7-4646-aaac-f090473ab108"
authors = ["Tyler Thomas <[email protected]>", "Benjamin Ciccarelli <[email protected]>"]
version = "0.4"
version = "0.4.0"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand All @@ -12,21 +12,24 @@ PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[weakdeps]
MarketData = "945b72a4-3b13-509d-9b46-1525bb5c06de"

[extensions]
MarketDataExt = "MarketData"

[compat]
Aqua = "0.8"
CSV = "0.10"
DataFrames = "1"
DataStructures = "0.18"
Dates = "1.6"
MarketData = "0.13"
PrecompileTools = "1"
julia = "1.6"
Dates = "1.6"
Random = "1.6"
UUIDs = "1.6"
Test = "1.6"

[extensions]
MarketDataExt = "MarketData"
UUIDs = "1.6"
julia = "1.6"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Expand All @@ -35,6 +38,3 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Aqua", "MarketData", "Test"]

[weakdeps]
MarketData = "945b72a4-3b13-509d-9b46-1525bb5c06de"
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ makedocs(;
assets=String[],
),
pages=["Home" => "index.md", "API" => "api.md", "Examples" => "examples.md"],
warnonly = true
warnonly=true,
)

deploydocs(; repo="github.com/SaguaroCapital/SaguaroTrader.jl.git")
11 changes: 9 additions & 2 deletions examples/buy_and_hold/buy_and_hold.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ end_dt = DateTime(2023, 5, 1)
initial_cash = 100_000.0

# Download market data for GLD
download_market_data([:GLD, :SPY]; start_dt=DateTime(2006, 12, 26), end_dt=DateTime(2023, 4, 27), check_exists=true)
download_market_data(
[:GLD, :SPY];
start_dt=DateTime(2006, 12, 26),
end_dt=DateTime(2023, 4, 27),
check_exists=true,
)

#####################################################
# Prepare broker
Expand Down Expand Up @@ -63,7 +68,9 @@ run!(strategy_trading_session)

# Download market data for SPY

download_market_data(:SPY; start_dt=DateTime(2006, 12, 26), end_dt=DateTime(2023, 4, 27), check_exists=true)
download_market_data(
:SPY; start_dt=DateTime(2006, 12, 26), end_dt=DateTime(2023, 4, 27), check_exists=true
)

data_source = CSVDailyBarSource("./temp/"; csv_symbols=[Symbol(:SPY)])

Expand Down
8 changes: 4 additions & 4 deletions examples/sixty_forty_fees/sixty_forty_fees.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ data_handler = BacktestDataHandler([data_source])
port_optimizer = FixedWeightPortfolioOptimizer(data_handler)

# create percent fee model to be used in simulated broker
fee_model = PercentFeeModel(fee_pct=0.1/100, tax_pct=0.5/100)
fee_model = PercentFeeModel(; fee_pct=0.1 / 100, tax_pct=0.5 / 100)

# create exchange, broker
exchange = SimulatedExchange(start_dt)
broker = SimulatedBroker(start_dt, exchange, data_handler; initial_cash=initial_cash, fee_model=fee_model)
broker = SimulatedBroker(
start_dt, exchange, data_handler; initial_cash=initial_cash, fee_model=fee_model
)

#####################################################
# 60% SPY, 40% AAG (bonds)
Expand Down Expand Up @@ -65,7 +67,6 @@ run!(strategy_trading_session)
# SPY Benchmark
######################################################


# create exchange, broker. use default zero fee model
exchange = SimulatedExchange(start_dt)
broker = SimulatedBroker(start_dt, exchange, data_handler; initial_cash=initial_cash)
Expand All @@ -80,7 +81,6 @@ alpha_model = FixedSignalsAlphaModel(signal_weights)
order_sizer = DollarWeightedOrderSizer(0.001)
rebalance = BuyAndHoldRebalance(Date(start_dt))


# Run SPY backtest
benchmark_trading_session = BacktestTradingSession(
start_dt,
Expand Down
2 changes: 1 addition & 1 deletion ext/MarketDataExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function SaguaroTrader.download_market_data(
if !isdir(data_dir)
mkdir(data_dir)
end

df = DataFrame(yahoo(security, YahooOpt(; period1=start_dt, period2=end_dt)))
CSV.write(joinpath(data_dir, "$security.csv"), df)
return nothing
Expand Down
2 changes: 1 addition & 1 deletion src/alpha_model/single_signal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Fields
- `signal::Float64`
"""
struct SingleSignalAlphaModel <: AlphaModel
universe
universe::Any
signal::Float64
end

Expand Down
2 changes: 1 addition & 1 deletion src/asset/universe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Parameters
- `assets`
"""
struct StaticUniverse <: Universe
assets
assets::Any
end

"""
Expand Down
6 changes: 3 additions & 3 deletions src/backtest_trading_session/backtest_trading_session.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Fields
struct BacktestTradingSession
start_dt::DateTime
end_dt::DateTime
pcm
exec_handler
rebalance
pcm::Any
exec_handler::Any
rebalance::Any
start_tracking_dt::DateTime
equity_curve::DataFrame
function BacktestTradingSession(
Expand Down
8 changes: 4 additions & 4 deletions src/backtest_trading_session/qts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"""
struct QuantTradingSystem
universe
broker
universe::Any
broker::Any
portfolio_id::String
alpha_model
order_sizer
alpha_model::Any
order_sizer::Any
# risk_model #TODO: Implement
long_only::Bool
submit_orders::Bool
Expand Down
11 changes: 4 additions & 7 deletions src/broker/simulated_broker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ Fields
mutable struct SimulatedBroker <: Broker
start_dt::DateTime
current_dt::DateTime
exchange
exchange::Any
data_handler::DataHandler
account_id::String
base_currency::String
initial_cash::Float64
fee_model
slippage_model
fee_model::Any
slippage_model::Any
# market_impact_model::AbstractMarketImpactModel # TODO: Implement
cash_balances::Dict{String,Float64}
portfolios::Dict{String,Portfolio}
Expand Down Expand Up @@ -108,10 +108,7 @@ function get_account_total_equity(broker)
end

function create_portfolio!(
broker,
initial_cash::Real;
name::String="",
portfolio_id::String=string(UUIDs.uuid1()),
broker, initial_cash::Real; name::String="", portfolio_id::String=string(UUIDs.uuid1())
)
error_msg = "Not enough cash in the broker $(broker.cash_balances[broker.base_currency])"
@assert initial_cash <= broker.cash_balances[broker.base_currency] error_msg
Expand Down
19 changes: 14 additions & 5 deletions src/data/daily_bar_csv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,15 @@ function _detect_adj_column(columns::AbstractVector{String}, identifier::String)
return error("Unable to detect '$identifier' column in columns: $columns")
end

_detect_adj_column(columns::AbstractVector{Symbol}, identifier::String) = _detect_adj_column(string.(columns), identifier)
_detect_adj_column(columns::AbstractVector{String}, identifier::Symbol) = _detect_adj_column(columns, string(identifier))
_detect_adj_column(columns::AbstractVector{Symbol}, identifier::Symbol) = _detect_adj_column(string.(columns), string(identifier))
function _detect_adj_column(columns::AbstractVector{Symbol}, identifier::String)
return _detect_adj_column(string.(columns), identifier)
end
function _detect_adj_column(columns::AbstractVector{String}, identifier::Symbol)
return _detect_adj_column(columns, string(identifier))
end
function _detect_adj_column(columns::AbstractVector{Symbol}, identifier::Symbol)
return _detect_adj_column(string.(columns), string(identifier))
end

"""
Estimate Bid-Ask spreads from OHLCV data
Expand Down Expand Up @@ -154,7 +160,7 @@ function _convert_bar_frame_into_df_bid_ask(
if any(ismissing.(df_bid.Ask))
df_bid = transform(
df_bid,
"Ask" .=> x -> impute(x, LOCF(; limit=nothing); dims=:cols);
"Ask" .=> x -> impute!(x, LOCF(; limit=nothing); dims=:cols);
renamecols=false,
)
end
Expand Down Expand Up @@ -295,7 +301,10 @@ function _get_timestamp_end(end_dt::DateTime, v::AbstractVector{DateTime})::keyt
end

function get_assets_historical_bids(
ds::CSVDailyBarSource, start_dt::DateTime, end_dt::DateTime, assets::AbstractVector{Symbol}
ds::CSVDailyBarSource,
start_dt::DateTime,
end_dt::DateTime,
assets::AbstractVector{Symbol},
)
# TODO: do we want historical close like qstrader uses?
prices_df = DataFrame(; timestamp=Vector{DateTime}())
Expand Down
2 changes: 1 addition & 1 deletion src/data/data_handler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Fields
- `data_sources
"""
struct BacktestDataHandler <: DataHandler
data_sources
data_sources::Any
function BacktestDataHandler(data_sources)
_verify_unique_assets(data_sources)
return new(data_sources)
Expand Down
Loading

0 comments on commit 575c410

Please sign in to comment.