Instances API

The Instances module manages asset instances within strategies, handling position tracking, margin management, and asset-specific data. It provides the bridge between abstract assets and their concrete usage in trading strategies.

Overview

The Instances module handles:

  • Asset instance creation and management
  • Position tracking for spot and margin trading
  • OHLCV data association with assets
  • Fee calculation and management
  • Balance and cash tracking
  • Margin and leverage management

Core Types

Asset Instance

Position Types

Asset Instance Management

Basic Asset Instance Operations

Asset Instance Information

Data Access and Management

Position Management

Spot Trading Positions

Margin Trading Positions

Fee Management

Fee Calculation

Cash and Balance Management

Cash Operations

Portfolio Value Calculation

Performance Optimization

Efficient Instance Operations

Integration Examples

Strategy Integration

Complete API Reference

Instances.AbstractInstanceType

Defines the abstract type for an instance.

The AbstractInstance type is a generic abstract type for an instance. It is parameterized by two types: A, which must be a subtype of AbstractAsset, and E, which must be a subtype of ExchangeID.

source
Instances.AssetInstanceType

Defines a structure for an asset instance.

  • attrs: Genric dict for instance specific parameters.

  • asset: The identifier of the asset.

  • data: The OHLCV (Open, High, Low, Close, Volume) series for the asset.

  • history: The trade history of the pair.

  • lock: A lock for synchronizing access to the asset instance.

  • _internal_lock

  • cash: The amount of the asset currently held. This can be positive or negative (short).

  • cash_committed: The amount of the asset currently committed for orders.

  • exchange: The exchange instance that this asset instance belongs to.

  • longpos: The long position of the asset.

  • shortpos: The short position of the asset.

  • lastpos: The last position of the asset.

  • limits: The minimum order size (from the exchange).

  • precision: The number of decimal points (from the exchange).

  • fees: The fees associated with the asset (from the exchange).

An AssetInstance holds all known state about an exchange asset like BTC/USDT.

source
Instances.AssetInstanceMethod

Creates an AssetInstance.

AssetInstance(a; data, exc, margin, min_amount)

This function creates an AssetInstance with the specified asset (a), data, exchange (exc), margin, and an optional minimum amount (min_amount). If no minimum amount is provided, it defaults to 1e-15.

source
Instances.AssetInstanceMethod

Create an AssetInstance object.

AssetInstance(
    a::AbstractAsset,
    data,
    e::Exchange{E<:ExchangeID},
    margin::Misc.MarginMode;
    limits,
    precision,
    fees
)

This function constructs an AssetInstance with defined asset, data, exchange, margin, and optional parameters for limits, precision, and fees. It initializes long and short positions based on the provided margin and ensures that the margin is not hedged.

source
Instances.AssetInstanceMethod

Creates an AssetInstance from strings.

AssetInstance(
    s::AbstractString,
    t::AbstractString,
    e::AbstractString,
    m::AbstractString;
    sandbox,
    params,
    account
)

This function creates an AssetInstance using the provided strings for the asset (s), data type (t), exchange (e), and margin type (m).

source
Instances.CCashType

Defines a type for currency cash, which is parameterized by an exchange E and a symbol S.

source
Instances.FeesType

Defines a NamedTuple structure for fees, including taker, maker, minimum, and maximum fees, each of which is a subtype of Real.

source
Instances.LimitsType

Defines a NamedTuple structure for limits, including leverage, amount, price, and cost, each of which is a subtype of Real.

source
Instances.PositionType

A position tracks the margin state of an asset instance.

  • status: Current status of the position

  • asset: Asset being tracked

  • timestamp: Timestamp of the last update

  • liquidation_price: Asset liquidation price

  • entryprice: Price at which the position was entered

  • maintenance_margin: Maintenance margin required for the position

  • initial_margin: Initial margin required for the position

  • additional_margin: Additional margin required for the position

  • notional: Notional value of the position

  • cash: Cash value of the position

  • cash_committed: Cash committed to the position

  • leverage: Leverage applied to the position

  • min_size: Minimum size of the position

  • hedged: Whether the position is hedged or not

  • tiers: Leverage tiers applicable to the position

  • this_tier: Current tier applicable to the position

source
Instances.PrecisionType

Defines a NamedTuple structure for precision, including amount and price, each of which is a subtype of Real.

source
Base.isapproxMethod

Check if two amounts are approximately equal for an AssetInstance.

isapprox(
    ai::Instances.AssetInstance,
    v1,
    v2,
    ::Val{:amount};
    atol
) -> Any

This function checks if two specified amounts v1 and v2 are approximately equal for an AssetInstance. It's used to validate whether two amounts are similar considering small variations.

source
Base.isapproxMethod

Check if two prices are approximately equal for an AssetInstance.

isapprox(
    ai::Instances.AssetInstance,
    v1,
    v2,
    ::Val{:price};
    atol
) -> Any

This function checks if two specified prices v1 and v2 are approximately equal for an AssetInstance. It's used to validate whether two prices are similar considering small variations.

source
Base.iszeroMethod

Check if the amount is below the asset instance's minimum limit.

iszero(ai::Instances.AssetInstance, v; atol) -> Any

This function checks if a specified amount in base currency is considered zero with respect to an AssetInstance's minimum limit. The amount is considered zero if it is less than the minimum limit minus a small epsilon value.

source
Base.iszeroMethod

Check if the asset cash for a position side is zero.

iszero(
    ai::Instances.AssetInstance,
    p::Misc.PositionSide
) -> Any

This function checks if the cash value of an AssetInstance for a specific PositionSide is zero. This is used to determine if there are no funds in a certain position side (long or short).

source
Base.iszeroMethod

Check if the asset cash is zero.

iszero(ai::Instances.AssetInstance) -> Any

This function checks if the cash value of an AssetInstance is zero. This is used to determine if there are no funds in the asset.

source
Base.similarMethod

Create a similar AssetInstance with cash and orders reset.

similar(
    ai::Instances.AssetInstance;
    exc,
    limits,
    precision,
    fees
) -> Instances.AssetInstance

This function returns a similar AssetInstance to the one provided, but resets the cash and orders. The limits, precision, and fees can be specified, and will default to those of the original instance.

source
Data.candlelastFunction

Get the last available candle strictly lower than apply(tf, date).

candlelast(
    ai::Instances.AssetInstance
) -> Data.Candle{Float64}
candlelast(
    ai::Instances.AssetInstance,
    tf::TimeFrames.TimeFrame,
    args...
) -> Data.Candle{Float64}

This function retrieves the last available candle (Open, High, Low, Close, Volume data for a specific time period) from the AssetInstance that is strictly lower than the date adjusted by the TimeFrame tf.

source
Data.stub!Method

Stub data for an AssetInstance with a DataFrame.

stub!(
    ai::Instances.AssetInstance,
    df::DataFrames.DataFrame
) -> DataFrames.DataFrame

This function stabs data of an AssetInstance with a given DataFrame. It's used for testing or simulating scenarios with pre-defined data.

source
Exchanges.lastpriceMethod

Get the last price from the history for an AssetInstance.

lastprice(
    ai::Instances.AssetInstance,
    _::Val{:history}
) -> Any

This function returns the last known price from the historical data for an AssetInstance. It's useful when you need to reference the most recent historical price for calculations or comparisons.

source
Exchanges.lastpriceMethod

Get the last price for an AssetInstance.

lastprice(
    ai::Instances.AssetInstance,
    args...;
    hist,
    kwargs...
) -> Any

This function returns the last known price for an AssetInstance. Additional arguments and keyword arguments can be provided to adjust the way the last price is calculated, if necessary.

source
Exchanges.leverage!Method

Update the leverage for an asset position.

leverage!(ai, v, p::Misc.PositionSide)

This function updates the leverage for a position in an asset instance. Leverage is the use of various financial instruments or borrowed capital to increase the potential return of an investment. The function takes a leverage value v and a position side (Long or Short) as inputs.

source
Exchanges.leverage!Method

Set the leverage to maximum for a CrossInstance.

leverage!(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.CrossMargin,
    p::Misc.PositionSide,
    _::Val{:max}
) -> Float64

This function sets the leverage for a CrossInstance to the maximum value for the current tier. Some exchanges interpret a leverage value of 0 as max leverage in cross margin mode. This means that the maximum amount of borrowed capital will be used to increase the potential return of the investment.

source
Exchanges.maxleverageMethod

Returns the maximum leverage for a given position and size.

maxleverage(po::Instances.Position, size::Real) -> Real

The function retrieves the leverage tier applicable to the provided position and size, and returns the maximum leverage allowed within that tier.

source
Exchanges.tierMethod

Retrieves the leverage tier for a given position and size.

tier(
    po::Instances.Position,
    size
) -> Tuple{Union{Nothing, Int64}, Exchanges.LeverageTier}

This function returns the tier that applies to a position of the provided size.

source
Exchanges.tierMethod

Get the position tier for a MarginInstance.

tier(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.WithMargin,
    size,
    _::Union{Type{S<:Misc.PositionSide}, Type{O} where O<:(OrderTypes.Order{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, S<:Misc.PositionSide}), Type{T} where T<:(OrderTypes.Trade{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, S<:Misc.PositionSide}), OrderTypes.Order{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, S<:Misc.PositionSide}, OrderTypes.Trade{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, S<:Misc.PositionSide}, S<:Misc.PositionSide}
) -> Tuple{Union{Nothing, Int64}, Exchanges.LeverageTier}

This function returns the tier of the position for a MarginInstance for a given size and position side (Long or Short). The tier indicates the level of risk or capital requirement for the position.

source
Instances._roundposFunction

Round function for values of position fields.

_roundpos(v) -> Any
_roundpos(v, digits) -> Any

This function rounds the values of position fields to a specified precision. The default precision is POSITION_PRECISION.

source
Instances.additional!Function

Sets additional margin (should always be positive).

additional!(po::Instances.Position) -> Float64
additional!(po::Instances.Position, v) -> Any

This function sets the additional margin of a given position (po) to the provided value (v). If no value is provided, it defaults to 0.0.

source
Instances.addmargin!Method

Adds margin to a position.

addmargin!(po::Instances.Position, v) -> Any

This function adds a specified amount (v) to the margin of a given position (po).

source
Instances.bankruptcyMethod

The price where the position is fully liquidated.

bankruptcy(price::Real, lev::Real, _::Misc.Long) -> Any

This function calculates and returns the price at which a position, given its leverage (lev), would be fully liquidated.

source
Instances.bankruptcyMethod

Get the bankruptcy price for an asset position.

bankruptcy(
    ai,
    price,
    ps::Union{Type{P<:Misc.PositionSide}, Type{O} where O<:(OrderTypes.Order{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, P<:Misc.PositionSide}), Type{T} where T<:(OrderTypes.Trade{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, P<:Misc.PositionSide}), OrderTypes.Order{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, P<:Misc.PositionSide}, OrderTypes.Trade{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, P<:Misc.PositionSide}, P<:Misc.PositionSide}
) -> Any

This function calculates the bankruptcy price, which is the price at which the asset position would be fully liquidated. It takes into account the current price of the asset and the position side (Long or Short).

source
Instances.entryprice!Function

Update the entry price.

entryprice!(po::Instances.Position) -> Any
entryprice!(po::Instances.Position, v) -> Any
source
Instances.initial!Function

Sets initial margin (should always be positive).

initial!(po::Instances.Position) -> Float64
initial!(po::Instances.Position, v) -> Any

This function sets the initial margin of a given position (po) to the provided value (v). If no value is provided, it defaults to 0.0.

source
Instances.instanceFunction

Create an AssetInstance from a zarr instance.

instance(
    exc::Exchange,
    a::AbstractAsset;
    ...
) -> Instances.AssetInstance{A, E, NoMargin} where {A<:AbstractAsset, E<:ExchangeID}
instance(
    exc::Exchange,
    a::AbstractAsset,
    m::Misc.MarginMode;
    zi
) -> Instances.AssetInstance

This function constructs an AssetInstance by loading data from a zarr instance and requires an external constructor defined in Engine. The MarginMode can be specified, with NoMargin being the default.

source
Instances.isdustMethod

Check if the position value of the asset is below minimum quantity.

isdust(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.WithMargin,
    price::Number,
    p::Misc.PositionSide
) -> Any

This function checks if the position value of a given AssetInstance at a specific price is below the minimum limit for that asset. The position side p determines if it's a long or short position.

source
Instances.liqprice!Method

Sets the liquidation price for a long position.

liqprice!(po::Instances.Position{Misc.Long}, v) -> Any

This function sets the liquidation price of a given long position (po) to the provided value (v).

source
Instances.liqprice!Method

Sets the liquidation price for a short position.

liqprice!(po::Instances.Position{Misc.Short}, v) -> Any

This function sets the liquidation price of a given short position (po) to the provided value (v).

source
Instances.liqprice!Method

Sets asset position liquidation price.

liqprice!(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.WithMargin,
    v,
    _::Union{Type{S<:Misc.PositionSide}, Type{O} where O<:(OrderTypes.Order{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, S<:Misc.PositionSide}), Type{T} where T<:(OrderTypes.Trade{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, S<:Misc.PositionSide}), OrderTypes.Order{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, S<:Misc.PositionSide}, OrderTypes.Trade{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, S<:Misc.PositionSide}, S<:Misc.PositionSide}
) -> Any
source
Instances.maintenance!Method

Sets maintenance margin.

maintenance!(po::Instances.Position, v) -> Any

This function sets the maintenance margin of a given position (po) to the provided value (v).

source
Instances.margin!Method

Sets initial margin given notional and leverage values.

margin!(po::Instances.Position; ntl, lev) -> Float64

This function sets the initial margin of a given position (po) based on the provided notional value (ntl) and leverage (lev). If no values are provided, the current notional value and leverage of the position are used.

source
Instances.minfeesMethod

The minimum fees for trading in the asset market (usually the highest vip level.)

source
Instances.mmrMethod

Get the maintenance margin rate for a MarginInstance.

mmr(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.WithMargin,
    size,
    s::OrderTypes.ByPos
)

This function returns the maintenance margin rate for a MarginInstance for a given size and position side (Long or Short). The maintenance margin rate is the minimum amount of equity that must be maintained in a margin account.

source
Instances.nondustFunction

Get the asset cash rounded to precision.

nondust(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.WithMargin,
    price::Number
) -> Any
nondust(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.WithMargin,
    price::Number,
    p
) -> Any

This function returns the asset cash of a MarginInstance rounded according to the asset's precision. The position side p is determined by the posside function.

source
Instances.notional!Method

Update the notional value.

notional!(po::Instances.Position, v) -> Any

This function updates the notional value of a given position (po) to the provided value (v).

source
Instances.pnlFunction

Calc PNL for long position given current_price as input.

pnl(po::Instances.Position{Misc.Long}, current_price) -> Any
pnl(
    po::Instances.Position{Misc.Long},
    current_price,
    amount
) -> Any

This function calculates the Profit and Loss (PNL) for a long position (po), given the current price (current_price) and an optional amount (amount). If no amount is provided, the cash value of the position is used.

source
Instances.pnlFunction

Calc PNL for short position given current_price as input.

pnl(
    po::Instances.Position{Misc.Short},
    current_price
) -> Any
pnl(
    po::Instances.Position{Misc.Short},
    current_price,
    amount
) -> Any

This function calculates the Profit and Loss (PNL) for a short position (po), given the current price (current_price) and an optional amount (amount). If no amount is provided, the cash value of the position is used.

source
Instances.pnlMethod

Calculate the profit and loss (PnL) of an asset position.

pnl(
    ai,
    _::Union{Type{P}, Type{O} where O<:(OrderTypes.Order{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, P}), Type{T} where T<:(OrderTypes.Trade{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, P}), OrderTypes.Order{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, P}, OrderTypes.Trade{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, P}, P},
    price
) -> Any

This function calculates the profit and loss (PnL) for an asset position. It takes into account the current price and the position. The PnL represents the gain or loss made on the position, based on the current price compared to the price at which the position was opened.

source
Instances.pnlMethod

Calculate PNL for a long position.

pnl(
    entryprice,
    current_price,
    amount,
    _::OrderTypes.ByPos{Misc.Long}
) -> Any

This function calculates the Profit and Loss (PNL) for a long position, given the entry price (entryprice), the current price (current_price), and the amount.

source
Instances.pnlMethod

Calculate PNL for a short position.

pnl(
    entryprice,
    current_price,
    amount,
    _::OrderTypes.ByPos{Misc.Short}
) -> Any

This function calculates the Profit and Loss (PNL) for a short position, given the entry price (entryprice), the current price (current_price), and the amount.

source
Instances.pnlpctMethod

Calc PNL percentage.

pnlpct(po::Instances.Position, v) -> Float64

This function calculates the Profit and Loss (PNL) percentage for a given position (po) and value (v).

source
Instances.pnlpctMethod

Calculate the profit and loss percentage (PnL%) of an asset position.

pnlpct(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.WithMargin,
    ::Union{Type{P}, Type{O} where O<:(OrderTypes.Order{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, P}), Type{T} where T<:(OrderTypes.Trade{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, P}), OrderTypes.Order{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, P}, OrderTypes.Trade{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, P}, P},
    price;
    pos
) -> Float64

This function calculates the profit and loss percentage (PnL%) for an asset position in a MarginInstance. It takes into account the current price and the position. The PnL% represents the gain or loss made on the position, as a percentage of the investment, based on the current price compared to the price at which the position was opened.

source
Instances.positionsMethod

Generate positions for a specific margin mode.

positions(
    M::Type{<:Misc.MarginMode},
    a::AbstractAsset,
    limits::NamedTuple{(:leverage, :amount, :price, :cost), <:NTuple{4, @NamedTuple{min::var"#s1948", max::var"#s1948"} where var"#s1948"<:T}} where T<:Real,
    e::Exchange
) -> Union{Tuple{Nothing, Nothing}, Tuple{Instances.Position, Instances.Position}}

This function generates long and short positions for a given asset on a specific exchange. The number and size of the positions are determined by the limits argument and the margin mode M.

source
Instances.priceMethod

Asset entry price.

price(
    _::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, NoMargin},
    fromprice,
    args...
) -> Any
source
Instances.priceMethod

Asset entry price.

price(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.WithMargin,
    fromprice,
    _::Union{Type{S<:Misc.PositionSide}, Type{O} where O<:(OrderTypes.Order{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, S<:Misc.PositionSide}), Type{T} where T<:(OrderTypes.Trade{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, S<:Misc.PositionSide}), OrderTypes.Order{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, S<:Misc.PositionSide}, OrderTypes.Trade{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, S<:Misc.PositionSide}, S<:Misc.PositionSide}
) -> Any
source
Instances.status!Method

Update the status of a non-hedged position in a MarginInstance.

status!(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.WithMargin,
    p::Misc.PositionSide,
    pstat::Instances.PositionStatus
) -> Any

This function opens or closes the status of a non-hedged position in a MarginInstance. A non-hedged position is a position that is not offset by a corresponding position in a related commodity or security. The PositionSide and PositionStatus are provided as inputs.

source
Instances.status!Method

Update the status of a hedged position in a HedgedInstance.

status!(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Union{Misc.CrossMargin{Misc.Hedged}, Misc.IsolatedMargin{Misc.Hedged}},
    p::Misc.PositionSide,
    pstat::Instances.PositionStatus
) -> Any

This function opens or closes the status of a hedged position in a HedgedInstance. A hedged position is a position that is offset by a corresponding position in a related commodity or security. The PositionSide and PositionStatus are provided as inputs.

source
Instances.tier!Function

Updates position leverage tier according to size.

tier!(po::Instances.Position) -> Exchanges.LeverageTier
tier!(
    po::Instances.Position,
    size
) -> Exchanges.LeverageTier

This function adjusts the leverage tier of a given position (po) based on the provided size. If no size is provided, the notional value of the position is used.

source
Instances.timestamp!Method

Updates the timestamp of a position.

timestamp!(
    po::Instances.Position,
    d::Dates.DateTime
) -> Dates.DateTime

This function sets the timestamp of a given position (po) to the provided DateTime value (d).

source
Instruments.cash!Method

Update the cash value for a MarginInstance after an IncreaseTrade.

cash!(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.WithMargin,
    t::OrderTypes.IncreaseTrade
) -> Any

This function updates the cash value of a MarginInstance after an IncreaseTrade. The cash value would typically decrease after an increase trade, as assets are bought using cash.

source
Instruments.cash!Method

Update the cash value for a MarginInstance after a ReduceTrade.

cash!(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.WithMargin,
    t::OrderTypes.ReduceTrade
) -> Any

This function updates the cash value of a MarginInstance after a ReduceTrade. The cash value would typically increase after a reduce trade, as assets are sold in exchange for cash.

source
Instruments.cash!Method

Update the cash value for a NoMarginInstance after a SellTrade.

cash!(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, NoMargin},
    t::OrderTypes.SellTrade
) -> Any

This function updates the cash value of a NoMarginInstance after a SellTrade. The cash value would typically increase after a sell trade, as assets are sold in exchange for cash.

source
Instruments.freecashMethod

Calculate the free cash for a MarginInstance with long position.

freecash(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.WithMargin,
    p::OrderTypes.ByPos{Misc.Long}
) -> Any

This function calculates the free cash (cash that is not tied up in trades) of a MarginInstance that has a long position. It takes into account the current cash, open long positions, and the margin requirements for those positions.

source
Instruments.freecashMethod

Calculate the free cash for a MarginInstance with short position.

freecash(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.WithMargin,
    p::OrderTypes.ByPos{Misc.Short}
) -> Any

This function calculates the free cash (cash that is not tied up in trades) of a MarginInstance that has a short position. It takes into account the current cash, open short positions, and the margin requirements for those positions.

source
Instruments.freecashMethod

Calculate the free cash for a NoMarginInstance.

freecash(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, NoMargin},
    args...
) -> Any

This function calculates the free cash (cash that is not tied up in trades) of a NoMarginInstance. It takes into account the current cash, open orders, and any additional factors specified in args.

source
Instruments.valueMethod

Calculate the value of a NoMarginInstance.

value(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, NoMargin};
    current_price,
    fees
) -> Any

This function calculates the value of a NoMarginInstance. It uses the current price (defaulting to the last historical price), the cash in the instance and the maximum fees. The value represents the amount of cash that could be obtained by liquidating the instance at the current price, taking into account the fees.

source
Instruments.valueMethod

Calculate the value of a MarginInstance.

value(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.WithMargin;
    ...
) -> Any
value(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.WithMargin,
    ::Union{Type{P}, Type{O} where O<:(OrderTypes.Order{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, P}), Type{T} where T<:(OrderTypes.Trade{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, P}), OrderTypes.Order{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, P}, OrderTypes.Trade{<:OrderTypes.OrderType, <:AbstractAsset, <:ExchangeID, P}, P};
    current_price,
    fees
) -> Any

This function calculates the value of a MarginInstance. It takes into account the current price (defaulting to the price of the position), the cash in the position and the maximum fees. The value represents the amount of cash that could be obtained by liquidating the position at the current price, taking into account the fees.

source
Misc.gtxzeroMethod

Check if an amount is greater than zero for an AssetInstance.

gtxzero(
    ai::Instances.AssetInstance,
    v,
    _::Val{:amount}
) -> Any

This function checks if a specified amount v is greater than zero for an AssetInstance. It's used to validate the amount before performing operations on the asset.

source
Misc.gtxzeroMethod

Check if a cost is greater than zero for an AssetInstance.

gtxzero(
    ai::Instances.AssetInstance,
    v,
    _::Val{:cost}
) -> Any

This function checks if a specified cost v is greater than zero for an AssetInstance. The cost is considered greater than zero if it is above the minimum limit minus a small epsilon value.

source
Misc.gtxzeroMethod

Check if a price is greater than zero for an AssetInstance.

gtxzero(
    ai::Instances.AssetInstance,
    v,
    _::Val{:price}
) -> Any

This function checks if a specified price v is greater than zero for an AssetInstance. The price is considered greater than zero if it is above the minimum limit minus a small epsilon value.

source
Misc.load!Method

Load OHLCV data for an AssetInstance.

load!(ai::Instances.AssetInstance; reset, zi)

This function loads OHLCV (Open, High, Low, Close, Volume) data for a given AssetInstance. If reset is set to true, it will re-fetch the data even if it's already been loaded.

source
Misc.ltxzeroMethod

Check if an amount is less than zero for an AssetInstance.

ltxzero(
    ai::Instances.AssetInstance,
    v,
    _::Val{:amount}
) -> Any

This function checks if a specified amount v is less than zero for an AssetInstance. It's used to validate the amount before performing operations on the asset.

source
Misc.ltxzeroMethod

Check if a cost is less than zero for an AssetInstance.

ltxzero(
    ai::Instances.AssetInstance,
    v,
    _::Val{:cost}
) -> Any

This function checks if a specified cost v is less than zero for an AssetInstance. The cost is considered less than zero if it is below the minimum limit minus a small epsilon value.

source
Misc.ltxzeroMethod

Check if a price is less than zero for an AssetInstance.

ltxzero(
    ai::Instances.AssetInstance,
    v,
    _::Val{:price}
) -> Any

This function checks if a specified price v is less than zero for an AssetInstance. The price is considered less than zero if it is below the minimum limit minus a small epsilon value.

source
Misc.oppositeMethod

The opposite position w.r.t. the asset instance and another Position or PositionSide.

source
Misc.reset!Method

Resets asset positions for a MarginInstance.

reset!(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, M} where M<:Misc.WithMargin,
    args...
) -> Any

This function resets the positions (open trades) of a MarginInstance to initial values. Any additional arguments in args are used to adjust the reset process, if necessary.

source
Misc.reset!Method

Resets asset cash and commitments for a NoMarginInstance.

reset!(
    ai::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, NoMargin},
    args...
)

This function resets the cash and commitments (open trades) of a NoMarginInstance to initial values. Any additional arguments in args are used to adjust the reset process, if necessary.

source
Misc.reset!Method

Resets position to initial state.

Also resets leverage

When reopening a position, leverage should be set again.

source
Misc.reset!Method

Resets the bare fields to close a position.

reset!(po::Instances.Position) -> Cash{S1, Float64} where S1
source
TimeTicks.timeframeMethod

Get the timeframe for an AssetInstance.

timeframe(
    ai::Instances.AssetInstance
) -> TimeFrames.TimeFrame

This function returns the timeframe for an AssetInstance. The timeframe represents the interval at which the asset's price data is sampled or updated.

source
Instances.@_roundMacro

Round a value based on the precision field of the ai asset instance.

This macro rounds a value v based on the precision field of an AssetInstance. By default, it rounds the amount, but it can also round other fields like price or cost if specified.

source
Instances.@ramountMacro

Round a value based on the precision (amount) field of the ai asset instance.

This macro rounds an amount value v based on the precision field of an AssetInstance.

source
Instances.@rpriceMacro

Round a value based on the precision (price) field of the ai asset instance.

This macro rounds a price value v based on the precision field of an AssetInstance.

source

See Also