API

Asset

SaguaroTrader.EquityType

Equity asset type.

Fields

  • symbol::Symbol - asset symbol
  • name::String - name of the asset
  • tax_exempt::Bool=false - asset tax exemption status
source

Order

SaguaroTrader.OrderType

This is an Order for an asset.

Fields

  • created_dt::DateTime - datetime that the order was placed
  • quantity::Real - order quantity
  • asset::Asset - order asset
  • fee::Float64 - order fee
  • order_id::String - unique order id
  • direction::Int 1=>buy, -1=>sell
  • direction::Float64 - Asset volume (for predicting slippage)
source

Portfolio Optimization

Exchange

Fee Model

SaguaroTrader.PercentFeeModelType

A Fee model that calculates tax and fee fees using a percentage of the total order price (considaration).

feefee = (order.price * order.quantity) * feemodel.feepct taxfee = (order.price * order.quantity) * feemodel.taxpct totalfee = taxfee + fee_fee

Fields

  • fee_pct::Float64 = 0.0
  • tax_pct::Float64 = 0.0
source
SaguaroTrader.calculate_feeFunction
calculate_fee(fee_model, quantity::Real, price::Float64)

Calculate the fee for an order

Parameters

  • fee_model
  • quantity::Real
  • price::Float64

Returns

  • Float64: The total fee for the order (tax + fee)
source

Data Source

SaguaroTrader.CSVDailyBarSourceType

Encapsulates loading, preparation and querying of CSV files of daily 'bar' OHLCV data. The CSV files are converted into a intraday timestamped Pandas DataFrame with opening and closing prices.

Optionally utilises adjusted closing prices (if available) to adjust both the close and open.

Fields

  • csv_dir::String The full path to the directory where the CSV is located.
  • asset_type::String The asset type that the price/volume data is for.
  • adjust_prices::String Whether to utilise corporate-action adjusted prices for both the open and closing prices. Defaults to True.
  • csv_symbols::Vector An optional list of CSV symbols to restrict the data source to. The alternative is to convert all CSVs found within the provided directory.
  • dict_asset_dfs::Dict{Symbol, DataFrame} Automatically generated dictionary of asset symbols, and pricing dataframes
source
SaguaroTrader.get_bidFunction
get_bid(ds::CSVDailyBarSource, dt::DateTime, asset::Symbol)

Get the bid price for an asset at a certain time.

Parameters

  • ds::CSVDailyBarSource
  • dt::DateTime
  • asset::Symbol

Returns

  • Float64: bid for an asset at the given datetime
source
SaguaroTrader.get_askFunction
get_ask(ds::CSVDailyBarSource, dt::DateTime, asset::Symbol)

Get the ask price for an asset at a certain time.

Parameters

  • ds::CSVDailyBarSource
  • dt::DateTime
  • asset::Symbol

Returns

  • Float64: ask for an asset at the given datetime
source
SaguaroTrader.get_assets_historical_bidsFunction
get_ask(ds::CSVDailyBarSource, dt::DateTime, asset::Symbol)

Obtain a multi-asset historical range of closing prices as a DataFrame

Parameters

  • ds::CSVDailyBarSource
  • start_dt::DateTime
  • end_dt::DateTime
  • asset::Symbol

Returns

  • DataFrame: Dataframe with the bid timestamps, asset symbols, and
source

Data Handler

SaguaroTrader.get_asset_latest_bid_ask_priceFunction
get_asset_latest_bid_ask_price(dh::DataHandler, dt::DateTime, asset::Symbol)

Get the bid, ask price for an asset at a certain time.

Parameters

  • dh::DataHandler
  • dt::DateTime
  • asset::Symbol

Returns

  • Tuple{Float64, Float64}: (bid, ask) for an asset at the given datetime
source
SaguaroTrader.get_asset_latest_mid_priceFunction
get_asset_latest_mid_price(dh::DataHandler, dt::DateTime, asset::Symbol)

Get the average of bid/ask price for an asset at a certain time.

Parameters

  • dh::DataHandler
  • dt::DateTime
  • asset::Symbol

Returns

  • Float64: mid point between bid/ask for an asset at the given datetime
source
SaguaroTrader.get_assets_historical_range_close_priceFunction
get_assets_historical_range_close_price(
    dh::DataHandler,
    start_dt::DateTime,
    end_dt::DateTime,
    assets::AbstractVector{Symbol},
)  

All closing prices for the given assets in the given timerange

Parameters

  • dh::DataHandler
  • start_dt::DateTime
  • end_dt::DateTime
  • asset::AbstractVector{Symbol}

Returns

  • DataFrame: dataframe of close prices for the assets
source

Transaction

SaguaroTrader.TransactionType

Metadata about a transaction of an asset

Fields

  • asset::Symbol
  • quantity::Float64
  • dt::DateTime
  • price::Float64
  • fee::Float64
  • order_id::String
source

Position

SaguaroTrader.PositionType

Handles the accounting of a position in an asset.

Fields

  • asset::Symbol
  • current_price::Float64
  • current_dt::DateTime
  • buy_quantity::Float64
  • sell_quantity::Float64
  • net_quantity::Float64
  • avg_bought::Float64
  • avg_sold::Float64
  • buy_fee::Float64
  • sell_fee::Float64
source

Portfolio

SaguaroTrader.create_subscriptionFunction
create_subscription(dt::DateTime, credit::Float64, balance::Float64)

Create a subscription portolio event

Parameters

  • dt::DateTime
  • credit::Float64
  • balance::Float64

Returns

  • PortfolioEvent
source
SaguaroTrader.create_withdrawalFunction
create_withdrawal(dt::DateTime, credit::Float64, balance::Float64)

Create a withdrawal portolio event

Parameters

  • dt::DateTime
  • credit::Float64
  • balance::Float64

Returns

  • PortfolioEvent
source
SaguaroTrader.create_asset_transactionFunction
create_asset_transaction(
    dt::DateTime,
    txn_total_cost::Float64,
    balance::Float64,
    direction::Int,
)

Create an asset buy/sell portfolio event

Parameters

  • dt::DateTime
  • txn_total_cost::Float64
  • balance::Float64
  • direction::Int

Returns

  • PortfolioEvent
source
SaguaroTrader.PortfolioType

Portfolio

Fields

  • current_dt::DateTime
  • cash::Float64
  • currency::String
  • pos_handler::PositionHandler
  • portfolio_id::String
  • name::String
  • history::AbstractVector{PortfolioEvent}
source
SaguaroTrader.total_market_valueFunction
total_market_value(pos_handler::PositionHandler)

Total market value of all open positions

Parameters

  • pos_handler::PositionHandler

Returns

  • Float64
source
total_market_value(port::Portfolio)

Total market value of the portfolio excluding cash.

Parameters

  • ord1
  • ord2

Returns

  • Float64
source

Alpha Model

Slippage Model

SaguaroTrader.FixedSlippageModelType

Fixed spread for all assets

Buy orders will execute at ask + spread / 2 Sell orders will execute at bid - spread / 2

Fields

  • spread::Float64

Returns

  • Float64: The price including slippage
source
SaguaroTrader.PercentSlippageModelType

Spread is a percent of the asset price

Buy orders will execute at ask * (1 + (slippage_pct/100)) Sell orders will execute at bid * (1 - (slippage_pct/100))

Fields

  • slippage_pct::Float64

Returns

  • Float64: The price including slippage
source
SaguaroTrader.VolumeSharesSlippageModelType

Slippage is based on the ratio of order volume to total volume

Buy orders will execute at ask + (volume_share^2 * price_impact * price * direction) Sell orders will execute at bid - (volume_share^2 * price_impact * price * direction)

Fields

  • price_impact::Float64=0.1 - The scaling coefficient for price impact. Larger values will result in larger price impacts.

Returns

  • Float64: The price including slippage
source

Broker

SaguaroTrader.SimulatedBrokerType

Simlated broker for transacting assets

Fields

  • start_dt::DateTime
  • current_dt::DateTime
  • exchange
  • data_handler::DataHandler
  • account_id::String
  • base_currency::String
  • initial_cash::Float64
  • fee_model
  • cash_balances::Dict{String,Float64}
  • portfolios::Dict{String,Portfolio}
  • open_orders::Dict{String,Queue{Order}}
source
SaguaroTrader.create_portfolio!Function
function create_portfolio!(
    broker,
    initial_cash::Real;
    name::String = "",
    portfolio_id::String = string(UUIDs.uuid1()),
)
function create_portfolio!(
    broker;
    name::String = "",
    portfolio_id::String = string(UUIDs.uuid1()),
)

Create a portolio using the broker's cash balance to fund the initial capital.

Parameters

  • broker
  • initial_cash::Real
  • name::String = ""
  • portfolio_id::String = string(UUIDs.uuid1())
source
SaguaroTrader.subscribe_funds!Function
subscribe_funds!(broker, amount::Real)
subscribe_funds!(broker, amount::Real, currency::String)

Add funds to broker

Parameters

  • broker
  • amount::Real
  • currency::String
source
SaguaroTrader.withdraw_funds!Function
withdraw_funds!(broker, amount::Real)
withdraw_funds!(broker, amount::Real, currency::String)

Withdraw funds from broker

Parameters

  • broker
  • amount::Real
  • currency::String
source

Order Sizer

Portfolio Construction Model

Rebalance