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

Complete API Reference

PlanarCore.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 AbstractInstrument, and E, which must be a subtype of ExchangeID.

source
PlanarCore.Instances.FeesType

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

source
PlanarCore.Instances.InstrumentInstanceType

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 InstrumentInstance holds all known state about an exchange asset like BTC/USDT.

source
PlanarCore.Instances.InstrumentInstanceMethod

Creates an InstrumentInstance.

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

This function creates an InstrumentInstance 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
PlanarCore.Instances.InstrumentInstanceMethod

Create an InstrumentInstance object.

InstrumentInstance(
    a::PlanarCore.Instruments.AbstractInstrument,
    data,
    e::PlanarCore.ExchangeTypes.Exchange{E<:ExchangeID},
    margin::MarginMode;
    limits,
    precision,
    fees
)

This function constructs an InstrumentInstance 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
PlanarCore.Instances.InstrumentInstanceMethod

Creates an InstrumentInstance from strings.

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

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

source
PlanarCore.Instances.LimitsType

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

source
PlanarCore.Instances.PositionType

A position tracks the margin state of an asset instance.

  • status: Current status of the position

  • asset: Instrument being tracked

  • timestamp: Timestamp of the last update

  • liquidation_price: Instrument 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
Base.isapproxMethod

Check if two amounts are approximately equal for an InstrumentInstance.

isapprox(
    ii::PlanarCore.Instances.InstrumentInstance,
    v1,
    v2,
    ::Val{:amount};
    atol
) -> Any

This function checks if two specified amounts v1 and v2 are approximately equal for an InstrumentInstance. 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 InstrumentInstance.

isapprox(
    ii::PlanarCore.Instances.InstrumentInstance,
    v1,
    v2,
    ::Val{:price};
    atol
) -> Any

This function checks if two specified prices v1 and v2 are approximately equal for an InstrumentInstance. 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(
    ii::PlanarCore.Instances.InstrumentInstance,
    v;
    atol
) -> Any

This function checks if a specified amount in base currency is considered zero with respect to an InstrumentInstance'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(
    ii::PlanarCore.Instances.InstrumentInstance,
    p::PlanarCore.Misc.PositionSide
) -> Any

This function checks if the cash value of an InstrumentInstance 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(ii::PlanarCore.Instances.InstrumentInstance) -> Any

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

source
Base.similarMethod

Create a similar InstrumentInstance with cash and orders reset.

similar(
    ii::PlanarCore.Instances.InstrumentInstance;
    exc,
    limits,
    precision,
    fees
) -> PlanarCore.Instances.InstrumentInstance

This function returns a similar InstrumentInstance 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
PlanarCore.Data.candlelastFunction

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

candlelast(
    ii::PlanarCore.Instances.InstrumentInstance
) -> PlanarCore.Data.Candle{Float64}
candlelast(
    ii::PlanarCore.Instances.InstrumentInstance,
    tf::TimeFrames.TimeFrame,
    args...
) -> PlanarCore.Data.Candle{Float64}

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

source
PlanarCore.Data.stub!Method

Stub data for an InstrumentInstance with a DataFrame.

stub!(
    ii::PlanarCore.Instances.InstrumentInstance,
    df::DataFrames.DataFrame
) -> DataFrames.DataFrame

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

source
PlanarCore.Exchanges.lastpriceMethod

Get the last price from the history for an InstrumentInstance.

lastprice(
    ii::PlanarCore.Instances.InstrumentInstance,
    _::Val{:history}
) -> Any

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

source
PlanarCore.Exchanges.lastpriceMethod

Get the last price for an InstrumentInstance.

lastprice(
    ii::PlanarCore.Instances.InstrumentInstance,
    args...;
    hist,
    kwargs...
) -> Any

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

source
PlanarCore.Exchanges.leverage!Method

Update the leverage for an asset position.

leverage!(ii, v, p::PlanarCore.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
PlanarCore.Exchanges.leverage!Method

Set the leverage to maximum for a CrossInstance.

leverage!(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:PlanarCore.Misc.CrossMargin,
    p::PlanarCore.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. We use a very high leverage value (1e10) instead of 0 to avoid division by zero in cost calculations, while preserving the "infinite leverage" semantics.

source
PlanarCore.Exchanges.maxleverageMethod

Returns the maximum leverage for a given position and size.

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

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

source
PlanarCore.Exchanges.tierMethod

Retrieves the leverage tier for a given position and size.

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

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

source
PlanarCore.Exchanges.tierMethod

Get the position tier for a MarginInstance.

tier(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:PlanarCore.Misc.WithMargin,
    size,
    _::Union{Type{S<:PlanarCore.Misc.PositionSide}, Type{O} where O<:(PlanarCore.OrderTypes.Order{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, S<:PlanarCore.Misc.PositionSide}), Type{T} where T<:(PlanarCore.OrderTypes.Trade{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, S<:PlanarCore.Misc.PositionSide}), PlanarCore.OrderTypes.Order{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, S<:PlanarCore.Misc.PositionSide}, PlanarCore.OrderTypes.Trade{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, S<:PlanarCore.Misc.PositionSide}, S<:PlanarCore.Misc.PositionSide}
) -> Any

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
PlanarCore.Instances._check_ticks_ordered!Method

Validate that the asset's tick timestamps are non-decreasing.

_check_ticks_ordered!(
    ii::PlanarCore.Instances.InstrumentInstance
) -> Any

Equal timestamps are allowed (same-millisecond ticks). On a decrease throws ArgumentError. The result is cached in ii.attrs[:_tick_order] keyed by the fingerprint (first(ts), last(ts), length(ts)), so repeat calls are O(1) and the validation re-runs only when the first/last tick or the length changes.

source
PlanarCore.Instances._ohlcv_keysMethod

Yield the OHLCV timeframe keys of an asset instance, excluding the tick sentinel.

_ohlcv_keys(
    ii::PlanarCore.Instances.InstrumentInstance
) -> Base.Generator{I, typeof(identity)} where I<:(Base.Iterators.Filter{PlanarCore.Instances.var"#_ohlcv_keys##0#_ohlcv_keys##1", I} where I<:(DataStructures.IterableObject{C, DataStructures.EntireContainer, DataStructures.KeysIter, DataStructures.NoTokens, DataStructures.ForwardIter} where C<:(SortedDict{TimeFrames.TimeFrame, DataFrames.DataFrame})))

The single exclusion helper used by every OHLCV accessor (ohlcv, candlelast, timeframe, ...) so the TICK_TIMEFRAME entry is never treated as the smallest OHLCV timeframe.

source
PlanarCore.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
PlanarCore.Instances.additional!Function

Sets additional margin (should always be positive).

additional!(po::PlanarCore.Instances.Position) -> Float64
additional!(po::PlanarCore.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
PlanarCore.Instances.addmargin!Method

Adds margin to a position.

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

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

source
PlanarCore.Instances.bankruptcyMethod

The price where the position is fully liquidated.

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

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

source
PlanarCore.Instances.bankruptcyMethod

Get the bankruptcy price for an asset position.

bankruptcy(
    ii,
    price,
    ps::Union{Type{P<:PlanarCore.Misc.PositionSide}, Type{O} where O<:(PlanarCore.OrderTypes.Order{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, P<:PlanarCore.Misc.PositionSide}), Type{T} where T<:(PlanarCore.OrderTypes.Trade{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, P<:PlanarCore.Misc.PositionSide}), PlanarCore.OrderTypes.Order{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, P<:PlanarCore.Misc.PositionSide}, PlanarCore.OrderTypes.Trade{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, P<:PlanarCore.Misc.PositionSide}, P<:PlanarCore.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
PlanarCore.Instances.entryprice!Function

Update the entry price.

When called without an explicit value, the entry price is inferred from the position notional and cash (position size). cash(po) is a Cash wrapper, so it must be unwrapped via value before division; a zero cash (closed or freshly created position) would otherwise yield Float64 / Cash -> MethodError or a divide-by-zero -> Inf. Guard it to 0.0 in that case.

source
PlanarCore.Instances.initial!Function

Sets initial margin (should always be positive).

initial!(po::PlanarCore.Instances.Position) -> Float64
initial!(po::PlanarCore.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
PlanarCore.Instances.instanceFunction

Create an InstrumentInstance from a zarr instance.

instance(
    exc::PlanarCore.ExchangeTypes.Exchange,
    a::PlanarCore.Instruments.AbstractInstrument;
    ...
) -> PlanarCore.Instances.InstrumentInstance{A, E, NoMargin} where {A<:PlanarCore.Instruments.AbstractInstrument, E<:ExchangeID}
instance(
    exc::PlanarCore.ExchangeTypes.Exchange,
    a::PlanarCore.Instruments.AbstractInstrument,
    m::MarginMode;
    zi
) -> PlanarCore.Instances.InstrumentInstance

This function constructs an InstrumentInstance 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
PlanarCore.Instances.isdustMethod

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

isdust(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:PlanarCore.Misc.WithMargin,
    price::Number,
    p::PlanarCore.Misc.PositionSide
) -> Any

This function checks if the position value of a given InstrumentInstance 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
PlanarCore.Instances.liqprice!Method

Sets the liquidation price for a long position.

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

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

source
PlanarCore.Instances.liqprice!Method

Sets the liquidation price for a short position.

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

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

source
PlanarCore.Instances.liqprice!Method

Sets asset position liquidation price.

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

Sets maintenance margin.

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

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

source
PlanarCore.Instances.margin!Method

Sets initial margin given notional and leverage values.

margin!(
    po::PlanarCore.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
PlanarCore.Instances.mmrMethod

Get the maintenance margin rate for a MarginInstance.

mmr(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:PlanarCore.Misc.WithMargin,
    size,
    s::PlanarCore.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
PlanarCore.Instances.nondustFunction

Get the asset cash rounded to precision.

nondust(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:PlanarCore.Misc.WithMargin,
    price::Number
) -> Any
nondust(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:PlanarCore.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
PlanarCore.Instances.notional!Method

Update the notional value.

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

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

source
PlanarCore.Instances.pnlFunction

Calc PNL for short position given current_price as input.

pnl(
    po::PlanarCore.Instances.Position{PlanarCore.Misc.Short},
    current_price
) -> Any
pnl(
    po::PlanarCore.Instances.Position{PlanarCore.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
PlanarCore.Instances.pnlFunction

Calc PNL for long position given current_price as input.

pnl(
    po::PlanarCore.Instances.Position{PlanarCore.Misc.Long},
    current_price
) -> Any
pnl(
    po::PlanarCore.Instances.Position{PlanarCore.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
PlanarCore.Instances.pnlMethod

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

pnl(
    ii,
    _::Union{Type{P}, Type{O} where O<:(PlanarCore.OrderTypes.Order{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, P}), Type{T} where T<:(PlanarCore.OrderTypes.Trade{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, P}), PlanarCore.OrderTypes.Order{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, P}, PlanarCore.OrderTypes.Trade{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <: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
PlanarCore.Instances.pnlMethod

Calculate PNL for a long position.

pnl(
    entryprice,
    current_price,
    amount,
    _::PlanarCore.OrderTypes.ByPos{PlanarCore.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
PlanarCore.Instances.pnlMethod

Calculate PNL for a short position.

pnl(
    entryprice,
    current_price,
    amount,
    _::PlanarCore.OrderTypes.ByPos{PlanarCore.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
PlanarCore.Instances.pnlpctMethod

Calc PNL percentage.

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

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

source
PlanarCore.Instances.pnlpctMethod

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

pnlpct(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:PlanarCore.Misc.WithMargin,
    ::Union{Type{P}, Type{O} where O<:(PlanarCore.OrderTypes.Order{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, P}), Type{T} where T<:(PlanarCore.OrderTypes.Trade{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, P}), PlanarCore.OrderTypes.Order{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, P}, PlanarCore.OrderTypes.Trade{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <: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
PlanarCore.Instances.positionsMethod

Generate positions for a specific margin mode.

positions(
    M::Type{<:MarginMode},
    a::PlanarCore.Instruments.AbstractInstrument,
    limits::NamedTuple{(:leverage, :amount, :price, :cost), <:NTuple{4, @NamedTuple{min::var"#s3529", max::var"#s3529"} where var"#s3529"<:T}} where T<:Real,
    e::PlanarCore.ExchangeTypes.Exchange
) -> Union{Tuple{Nothing, Nothing}, Tuple{PlanarCore.Instances.Position, PlanarCore.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
PlanarCore.Instances.priceMethod

Instrument entry price.

price(
    _::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, NoMargin},
    fromprice,
    args...
) -> Any
source
PlanarCore.Instances.priceMethod

Instrument entry price.

price(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:PlanarCore.Misc.WithMargin,
    fromprice,
    _::Union{Type{S<:PlanarCore.Misc.PositionSide}, Type{O} where O<:(PlanarCore.OrderTypes.Order{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, S<:PlanarCore.Misc.PositionSide}), Type{T} where T<:(PlanarCore.OrderTypes.Trade{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, S<:PlanarCore.Misc.PositionSide}), PlanarCore.OrderTypes.Order{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, S<:PlanarCore.Misc.PositionSide}, PlanarCore.OrderTypes.Trade{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, S<:PlanarCore.Misc.PositionSide}, S<:PlanarCore.Misc.PositionSide}
) -> Any
source
PlanarCore.Instances.setticks!Method

Set the tick data for an asset instance.

setticks!(
    ii::PlanarCore.Instances.InstrumentInstance,
    df::DataFrames.DataFrame
) -> DataFrames.DataFrame

Stores df under the TICK_TIMEFRAME sentinel key in the asset data dict and tags the :timestamp column with the TICK_TIMEFRAME metadata (matching the timeframe! convention). df must have columns :timestamp (DateTime or Integer Unix-ms), :price, :amount. Timestamps must be non-decreasing; equal timestamps are allowed (ticks within the same millisecond keep their row order, which the tick range merge sorts stably). Anything else (e.g. :side) is ignored.

source
PlanarCore.Instances.status!Method

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

status!(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:PlanarCore.Misc.WithMargin,
    p::PlanarCore.Misc.PositionSide,
    pstat::PlanarCore.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
PlanarCore.Instances.status!Method

Update the status of a hedged position in a HedgedInstance.

status!(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:Union{PlanarCore.Misc.CrossMargin{PlanarCore.Misc.Hedged}, PlanarCore.Misc.IsolatedMargin{PlanarCore.Misc.Hedged}},
    p::PlanarCore.Misc.PositionSide,
    pstat::PlanarCore.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
PlanarCore.Instances.ticksMethod

Get the tick DataFrame for an asset instance.

ticks(ii::PlanarCore.Instances.InstrumentInstance) -> Any

Returns the tick data (columns :timestamp, :price, :amount) stored under the TICK_TIMEFRAME sentinel key of the asset data dict, or an empty DataFrame when absent.

source
PlanarCore.Instances.tier!Function

Updates position leverage tier according to size.

tier!(
    po::PlanarCore.Instances.Position
) -> Union{Bool, PlanarCore.Exchanges.LeverageTier}
tier!(
    po::PlanarCore.Instances.Position,
    size
) -> Union{Bool, PlanarCore.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
PlanarCore.Instances.timestamp!Method

Updates the timestamp of a position.

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

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

source
PlanarCore.Instruments.cash!Method

Update the cash value for a MarginInstance after an IncreaseTrade.

cash!(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:PlanarCore.Misc.WithMargin,
    t::PlanarCore.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
PlanarCore.Instruments.cash!Method

Update the cash value for a MarginInstance after a ReduceTrade.

cash!(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:PlanarCore.Misc.WithMargin,
    t::PlanarCore.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
PlanarCore.Instruments.cash!Method

Update the cash value for a NoMarginInstance after a SellTrade.

cash!(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, NoMargin},
    t::PlanarCore.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
PlanarCore.Instruments.freecashMethod

Calculate the free cash for a MarginInstance with long position.

freecash(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:PlanarCore.Misc.WithMargin,
    p::PlanarCore.OrderTypes.ByPos{PlanarCore.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
PlanarCore.Instruments.freecashMethod

Calculate the free cash for a MarginInstance with short position.

freecash(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:PlanarCore.Misc.WithMargin,
    p::PlanarCore.OrderTypes.ByPos{PlanarCore.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
PlanarCore.Instruments.freecashMethod

Calculate the free cash for a NoMarginInstance.

freecash(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <: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
PlanarCore.Instruments.valueMethod

Calculate the value of a NoMarginInstance.

value(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <: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
PlanarCore.Instruments.valueMethod

Calculate the value of a MarginInstance.

value(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:PlanarCore.Misc.WithMargin;
    ...
) -> Any
value(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:PlanarCore.Misc.WithMargin,
    ::Union{Type{P}, Type{O} where O<:(PlanarCore.OrderTypes.Order{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, P}), Type{T} where T<:(PlanarCore.OrderTypes.Trade{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, P}), PlanarCore.OrderTypes.Order{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, P}, PlanarCore.OrderTypes.Trade{<:PlanarCore.OrderTypes.OrderType, <:PlanarCore.Instruments.AbstractInstrument, <: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
PlanarCore.Misc.gtxzeroMethod

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

gtxzero(
    ii::PlanarCore.Instances.InstrumentInstance,
    v,
    _::Val{:amount}
) -> Any

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

source
PlanarCore.Misc.gtxzeroMethod

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

gtxzero(
    ii::PlanarCore.Instances.InstrumentInstance,
    v,
    _::Val{:cost}
) -> Any

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

source
PlanarCore.Misc.gtxzeroMethod

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

gtxzero(
    ii::PlanarCore.Instances.InstrumentInstance,
    v,
    _::Val{:price}
) -> Any

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

source
PlanarCore.Misc.load!Method

Load OHLCV data for an InstrumentInstance.

load!(
    ii::PlanarCore.Instances.InstrumentInstance;
    reset,
    zi
)

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

source
PlanarCore.Misc.ltxzeroMethod

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

ltxzero(
    ii::PlanarCore.Instances.InstrumentInstance,
    v,
    _::Val{:amount}
) -> Any

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

source
PlanarCore.Misc.ltxzeroMethod

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

ltxzero(
    ii::PlanarCore.Instances.InstrumentInstance,
    v,
    _::Val{:cost}
) -> Any

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

source
PlanarCore.Misc.ltxzeroMethod

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

ltxzero(
    ii::PlanarCore.Instances.InstrumentInstance,
    v,
    _::Val{:price}
) -> Any

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

source
PlanarCore.Misc.reset!Method

Resets asset positions for a MarginInstance.

reset!(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, M} where M<:PlanarCore.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
PlanarCore.Misc.reset!Method

Resets asset cash and commitments for a NoMarginInstance.

reset!(
    ii::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <: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
PlanarCore.Misc.reset!Method

Resets position to initial state.

Also resets leverage

When reopening a position, leverage should be set again.

source
PlanarCore.Misc.reset!Method

Resets the bare fields to close a position.

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

Get the timeframe for an InstrumentInstance.

timeframe(
    ii::PlanarCore.Instances.InstrumentInstance
) -> TimeFrames.TimeFrame

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

source
PlanarCore.Instances.@_roundMacro

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

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

source
PlanarCore.Instances.@ramountMacro

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

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

source
PlanarCore.Instances.@rpriceMacro

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

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

source

See Also