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.LEVERAGE_PRECISION — Constant
The number of digits allowed for leverage values.
PlanarCore.Instances.POSITION_PRECISION — Constant
The number of digits to keep for margin calculations.
PlanarCore.Instances.POSITION_ROUNDING_MODE — Constant
A constant defining the rounding mode for positions as RoundToZero.
PlanarCore.Instances.PositionChange — Type
Position change is one of PositionOpen, PositionUpdate, PositionClose.
PlanarCore.Instances.PositionStatus — Type
Position status is one of PositionOpen, PositionClose.
PlanarCore.Instances.AbstractInstance — Type
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.
PlanarCore.Instances.CCash — Type
Defines a type for currency cash, which is parameterized by an exchange E and a symbol S.
PlanarCore.Instances.CrossInstance — Type
A type alias representing an asset instance with cross margin.
PlanarCore.Instances.Fees — Type
Defines a NamedTuple structure for fees, including taker, maker, minimum, and maximum fees, each of which is a subtype of Real.
PlanarCore.Instances.HedgedInstance — Type
A type alias for an asset instance with either isolated or cross hedged margin.
PlanarCore.Instances.InstrumentInstance — Type
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_lockcash: 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.
PlanarCore.Instances.InstrumentInstance — Method
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.
PlanarCore.Instances.InstrumentInstance — Method
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.
PlanarCore.Instances.InstrumentInstance — Method
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).
PlanarCore.Instances.Limits — Type
Defines a NamedTuple structure for limits, including leverage, amount, price, and cost, each of which is a subtype of Real.
PlanarCore.Instances.LongPosition — Type
A constant representing a long position with margin in a specific exchange.
PlanarCore.Instances.MarginInstance — Type
A type alias for an asset instance with either isolated or cross margin.
PlanarCore.Instances.NoMarginInstance — Type
A type alias representing an asset instance with no margin.
PlanarCore.Instances.OneVec — Type
A constant representing a vector of DFT type.
PlanarCore.Instances.Position — Type
A position tracks the margin state of an asset instance.
status: Current status of the positionasset: Instrument being trackedtimestamp: Timestamp of the last updateliquidation_price: Instrument liquidation priceentryprice: Price at which the position was enteredmaintenance_margin: Maintenance margin required for the positioninitial_margin: Initial margin required for the positionadditional_margin: Additional margin required for the positionnotional: Notional value of the positioncash: Cash value of the positioncash_committed: Cash committed to the positionleverage: Leverage applied to the positionmin_size: Minimum size of the positionhedged: Whether the position is hedged or nottiers: Leverage tiers applicable to the positionthis_tier: Current tier applicable to the position
PlanarCore.Instances.PositionClose — Type
A position has been closed.
PlanarCore.Instances.PositionOpen — Type
A position has been opened.
PlanarCore.Instances.PositionUpdate — Type
A position has been updated.
PlanarCore.Instances.Precision — Type
Defines a NamedTuple structure for precision, including amount and price, each of which is a subtype of Real.
PlanarCore.Instances.ShortPosition — Type
A constant representing a short position with margin in a specific exchange.
Base.float — Method
Get the cash value of a InstrumentInstance.
Base.isapprox — Method
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.
Base.isapprox — Method
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.
Base.isopen — Method
Check if the InstrumentInstance is open.
Base.isopen — Method
Check if an asset position is open.
Base.iszero — Method
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.
Base.iszero — Method
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).
Base.iszero — Method
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.
Base.position — Method
Instrument instance long position.
Base.position — Method
Instrument instance short position.
Base.position — Method
Returns the last open asset position or nothing.
Base.position — Method
Instrument position by order.
Base.similar — Method
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.
PlanarCore.Data.candlelast — Function
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.
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.
PlanarCore.ExchangeTypes.exchange — Method
The exchange of the asset instance.
PlanarCore.ExchangeTypes.exchangeid — Method
ExchangeID for the asset instance.
PlanarCore.Exchanges.lastprice — Method
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.
PlanarCore.Exchanges.lastprice — Method
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.
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.
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.
PlanarCore.Exchanges.leverage! — Method
Updates position leverage.
PlanarCore.Exchanges.maxleverage — Method
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.
PlanarCore.Exchanges.tier — Method
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.
PlanarCore.Exchanges.tier — Method
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.
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.
PlanarCore.Instances._ohlcv_keys — Method
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.
PlanarCore.Instances._roundpos — Function
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.
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.
PlanarCore.Instances.additional — Method
Position additional margin.
PlanarCore.Instances.additional — Method
Instrument position additional margin.
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).
PlanarCore.Instances.amount_with_fees — Method
The amount of a trade include fees (either positive or negative).
PlanarCore.Instances.asset — Method
Get the parsed AbstractInstrument of an InstrumentInstance.
PlanarCore.Instances.bankruptcy — Method
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.
PlanarCore.Instances.bankruptcy — Method
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).
PlanarCore.Instances.collateral — Method
Maximum value that can be lost by the position
PlanarCore.Instances.committed — Method
Get the asset instance committed cash.
PlanarCore.Instances.committed — Method
Position locked in pending orders.
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.
PlanarCore.Instances.entryprice — Method
Instrument entry price.
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.
PlanarCore.Instances.instance — Function
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.
PlanarCore.Instances.isdust — Method
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.
PlanarCore.Instances.ishedged — Method
Check if the margin mode is hedged.
PlanarCore.Instances.ishedged — Method
Check if the InstrumentInstance is hedged.
PlanarCore.Instances.leverage — Method
Position leverage.
PlanarCore.Instances.leverage — Method
Instrument position leverage.
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).
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).
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
PlanarCore.Instances.liqprice — Method
Position liquidation price.
PlanarCore.Instances.liqprice — Method
Instrument position liquidation price.
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).
PlanarCore.Instances.maintenance — Method
Position maintenance margin.
PlanarCore.Instances.maintenance — Method
Instrument position maintenance margin.
PlanarCore.Instances.makerfees — Method
Maker fees for the asset instance (usually lower than taker fees.)
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.
PlanarCore.Instances.margin — Method
Position initial margin (includes additional).
PlanarCore.Instances.margin — Method
Instrument position initial margin.
PlanarCore.Instances.maxfees — Method
The maximum fees for trading in the asset market (usually the lowest vip level.)
PlanarCore.Instances.minfees — Method
The minimum fees for trading in the asset market (usually the highest vip level.)
PlanarCore.Instances.mmr — Method
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.
PlanarCore.Instances.mmr — Method
Position maintenance margin rate.
PlanarCore.Instances.nondust — Function
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.
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).
PlanarCore.Instances.notional — Method
Position notional value.
PlanarCore.Instances.notional — Method
Instrument position notional value.
PlanarCore.Instances.ohlcv — Method
Get the asset instance ohlcv data for the smallest time frame.
PlanarCore.Instances.ohlcv_dict — Method
Get the asset instance ohlcv data dictionary.
PlanarCore.Instances.pnl — Function
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.
PlanarCore.Instances.pnl — Function
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.
PlanarCore.Instances.pnl — Method
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.
PlanarCore.Instances.pnl — Method
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.
PlanarCore.Instances.pnl — Method
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.
PlanarCore.Instances.pnlpct — Method
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).
PlanarCore.Instances.pnlpct — Method
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.
PlanarCore.Instances.positions — Method
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.
PlanarCore.Instances.posside — Method
Get the position side of an InstrumentInstance.
PlanarCore.Instances.price — Method
Instrument entry price.
price(
_::PlanarCore.Instances.InstrumentInstance{<:PlanarCore.Instruments.AbstractInstrument, <:ExchangeID, NoMargin},
fromprice,
args...
) -> Any
PlanarCore.Instances.price — Method
Position entryprice.
PlanarCore.Instances.price — Method
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
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.
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.
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.
PlanarCore.Instances.status — Method
Position status (open or closed).
PlanarCore.Instances.status — Method
Instrument position status (open or closed).
PlanarCore.Instances.takerfees — Method
Taker fees for the asset instance (usually higher than maker fees.)
PlanarCore.Instances.ticks — Method
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.
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.
PlanarCore.Instances.timestamp — Function
Get the timestamp of the last trade.
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).
PlanarCore.Instances.timestamp — Method
Last position update time
PlanarCore.Instruments.bc — Method
Get the base currency of an InstrumentInstance.
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.
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.
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.
PlanarCore.Instruments.cash! — Method
Set position cash value.
PlanarCore.Instruments.cash — Method
Get the asset instance cash for the long position.
PlanarCore.Instruments.cash — Method
Get the asset instance cash for the short position.
PlanarCore.Instruments.cash — Method
Get the asset instance cash.
PlanarCore.Instruments.cash — Method
Held position.
PlanarCore.Instruments.freecash — Method
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.
PlanarCore.Instruments.freecash — Method
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.
PlanarCore.Instruments.freecash — Method
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.
PlanarCore.Instruments.qc — Method
Get the quote currency of an InstrumentInstance.
PlanarCore.Instruments.raw — Method
Get the raw string id of an InstrumentInstance.
PlanarCore.Instruments.value — Method
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.
PlanarCore.Instruments.value — Method
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.
PlanarCore.Misc.gtxzero — Method
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.
PlanarCore.Misc.gtxzero — Method
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.
PlanarCore.Misc.gtxzero — Method
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.
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.
PlanarCore.Misc.ltxzero — Method
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.
PlanarCore.Misc.ltxzero — Method
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.
PlanarCore.Misc.ltxzero — Method
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.
PlanarCore.Misc.marginmode — Method
Retrieve the margin mode of an InstrumentInstance.
PlanarCore.Misc.opposite — Method
The opposite position w.r.t. the asset instance and another Position or PositionSide.
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.
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.
PlanarCore.Misc.reset! — Method
Resets position to initial state.
When reopening a position, leverage should be set again.
PlanarCore.Misc.reset! — Method
Resets the bare fields to close a position.
reset!(
po::PlanarCore.Instances.Position
) -> PlanarCore.Instruments.Cash{_A, Float64} where _A
PlanarCore.OrderTypes.commit! — Method
Set position committed cash value.
PlanarCore.OrderTypes.islong — Method
Check if the InstrumentInstance is long.
PlanarCore.OrderTypes.isshort — Method
Check if the InstrumentInstance is short.
PlanarCore.OrderTypes.trades — Method
Get the trade history of an InstrumentInstance.
PlanarCore.TimeTicks.timeframe — Method
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.
PlanarCore.Instances.@_round — Macro
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.
PlanarCore.Instances.@ramount — Macro
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.
PlanarCore.Instances.@rprice — Macro
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.
See Also
- Instruments API - Financial instrument definitions
- Strategies API - Strategy base classes and interfaces
- Data API - Data structures and management
- Executors API - Order execution and management
- Strategy Development Guide - Building trading strategies
- Advanced Trading Guide - Margin and derivative trading