Instances
Instances.LEVERAGE_PRECISION
— ConstantThe number of digits allowed for leverage values.
Instances.POSITION_PRECISION
— ConstantThe number of digits to keep for margin calculations.
Instances.POSITION_ROUNDING_MODE
— ConstantA constant defining the rounding mode for positions as RoundToZero
.
Instances.PositionChange
— TypePosition change is one of PositionOpen
, PositionUpdate
, PositionClose
.
Instances.PositionStatus
— TypePosition status is one of PositionOpen
, PositionClose
.
Instances.AbstractInstance
— TypeDefines 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
.
Instances.AssetInstance
— TypeDefines 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
.
Instances.AssetInstance
— MethodCreates 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.
Instances.AssetInstance
— MethodCreate 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.
Instances.AssetInstance
— MethodCreates 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
).
Instances.CCash
— TypeDefines a type for currency cash, which is parameterized by an exchange E
and a symbol S
.
Instances.CrossInstance
— TypeA type alias representing an asset instance with cross margin.
Instances.Fees
— TypeDefines a NamedTuple structure for fees, including taker, maker, minimum, and maximum fees, each of which is a subtype of Real.
Instances.HedgedInstance
— TypeA type alias for an asset instance with either isolated or cross hedged margin.
Instances.Limits
— TypeDefines a NamedTuple structure for limits, including leverage, amount, price, and cost, each of which is a subtype of Real.
Instances.LongPosition
— TypeA constant representing a long position with margin in a specific exchange.
Instances.MarginInstance
— TypeA type alias for an asset instance with either isolated or cross margin.
Instances.NoMarginInstance
— TypeA type alias representing an asset instance with no margin.
Instances.OneVec
— TypeA constant representing a vector of DFT
type.
Instances.Position
— TypeA position tracks the margin state of an asset instance.
status
: Current status of the positionasset
: Asset being trackedtimestamp
: Timestamp of the last updateliquidation_price
: Asset 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
Instances.PositionClose
— TypeA position has been closed.
Instances.PositionOpen
— TypeA position has been opened.
Instances.PositionUpdate
— TypeA position has been updated.
Instances.Precision
— TypeDefines a NamedTuple structure for precision, including amount and price, each of which is a subtype of Real.
Instances.ShortPosition
— TypeA constant representing a short position with margin in a specific exchange.
Base.float
— MethodGet the cash value of a AssetInstance
.
Base.isapprox
— MethodCheck 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.
Base.isapprox
— MethodCheck 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.
Base.isopen
— MethodCheck if the AssetInstance
is open.
Base.isopen
— MethodCheck if an asset position is open.
Base.iszero
— MethodCheck 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.
Base.iszero
— MethodCheck 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).
Base.iszero
— MethodCheck 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.
Base.position
— MethodAsset instance long position.
Base.position
— MethodAsset instance short position.
Base.position
— MethodReturns the last open asset position or nothing.
Base.position
— MethodAsset position by order.
Base.similar
— MethodCreate 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.
Data.candlelast
— FunctionGet 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
.
Data.stub!
— MethodStub 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.
ExchangeTypes.exchange
— MethodThe exchange of the asset instance.
ExchangeTypes.exchangeid
— MethodExchangeID for the asset instance.
Exchanges.lastprice
— MethodGet 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.
Exchanges.lastprice
— MethodGet 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.
Exchanges.leverage!
— MethodUpdate 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.
Exchanges.leverage!
— MethodSet 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.
Exchanges.leverage!
— MethodUpdates position leverage.
Exchanges.maxleverage
— MethodReturns 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.
Exchanges.tier
— MethodRetrieves 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.
Exchanges.tier
— MethodGet 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.
Instances._roundpos
— FunctionRound 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
.
Instances.additional!
— FunctionSets 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.
Instances.additional
— MethodPosition additional margin.
Instances.additional
— MethodAsset position additional margin.
Instances.addmargin!
— MethodAdds 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
).
Instances.amount_with_fees
— MethodThe amount of a trade include fees (either positive or negative).
Instances.asset
— MethodGet the parsed AbstractAsset
of an AssetInstance
.
Instances.bankruptcy
— MethodThe 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.
Instances.bankruptcy
— MethodGet 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
).
Instances.collateral
— MethodMaximum value that can be lost by the position
Instances.committed
— MethodGet the asset instance committed cash.
Instances.committed
— MethodPosition locked in pending orders.
Instances.entryprice!
— FunctionUpdate the entry price.
entryprice!(po::Instances.Position) -> Any
entryprice!(po::Instances.Position, v) -> Any
Instances.entryprice
— MethodAsset entry price.
Instances.initial!
— FunctionSets 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.
Instances.instance
— FunctionCreate 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.
Instances.isdust
— MethodCheck 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.
Instances.ishedged
— MethodCheck if the margin mode is hedged.
Instances.ishedged
— MethodCheck if the AssetInstance
is hedged.
Instances.leverage
— MethodPosition leverage.
Instances.leverage
— MethodAsset position leverage.
Instances.liqprice!
— MethodSets 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
).
Instances.liqprice!
— MethodSets 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
).
Instances.liqprice!
— MethodSets 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
Instances.liqprice
— MethodPosition liquidation price.
Instances.liqprice
— MethodAsset position liquidation price.
Instances.maintenance!
— MethodSets maintenance margin.
maintenance!(po::Instances.Position, v) -> Any
This function sets the maintenance margin of a given position (po
) to the provided value (v
).
Instances.maintenance
— MethodPosition maintenance margin.
Instances.maintenance
— MethodAsset position maintenance margin.
Instances.makerfees
— MethodMaker fees for the asset instance (usually lower than taker fees.)
Instances.margin!
— MethodSets 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.
Instances.margin
— MethodPosition initial margin (includes additional).
Instances.margin
— MethodAsset position initial margin.
Instances.maxfees
— MethodThe maximum fees for trading in the asset market (usually the lowest vip level.)
Instances.minfees
— MethodThe minimum fees for trading in the asset market (usually the highest vip level.)
Instances.mmr
— MethodGet 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.
Instances.mmr
— MethodPosition maintenance margin rate.
Instances.nondust
— FunctionGet 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.
Instances.notional!
— MethodUpdate 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
).
Instances.notional
— MethodPosition notional value.
Instances.notional
— MethodAsset position notional value.
Instances.ohlcv
— MethodGet the asset instance ohlcv data for the smallest time frame.
Instances.ohlcv_dict
— MethodGet the asset instance ohlcv data dictionary.
Instances.pnl
— FunctionCalc 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.
Instances.pnl
— FunctionCalc 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.
Instances.pnl
— MethodCalculate 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.
Instances.pnl
— MethodCalculate 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.
Instances.pnl
— MethodCalculate 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.
Instances.pnlpct
— MethodCalc 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
).
Instances.pnlpct
— MethodCalculate 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.
Instances.positions
— MethodGenerate positions for a specific margin mode.
positions(
M::Type{<:Misc.MarginMode},
a::AbstractAsset,
limits::NamedTuple{(:leverage, :amount, :price, :cost), <:NTuple{4, @NamedTuple{min::var"#s238", max::var"#s238"} where var"#s238"<: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
.
Instances.posside
— MethodGet the position side of an AssetInstance
.
Instances.price
— MethodAsset entry price.
price(
_::Instances.AssetInstance{<:AbstractAsset, <:ExchangeID, NoMargin},
fromprice,
args...
) -> Any
Instances.price
— MethodPosition entryprice.
Instances.price
— MethodAsset 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
Instances.status!
— MethodUpdate 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.
Instances.status!
— MethodUpdate 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.
Instances.status
— MethodPosition status (open or closed).
Instances.status
— MethodAsset position status (open or closed).
Instances.takerfees
— MethodTaker fees for the asset instance (usually higher than maker fees.)
Instances.tier!
— FunctionUpdates 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.
Instances.timestamp
— FunctionGet the timestamp of the last trade.
Instances.timestamp!
— MethodUpdates 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
).
Instances.timestamp
— MethodLast position update time
Instruments.bc
— MethodGet the base currency of an AssetInstance
.
Instruments.cash!
— MethodUpdate 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.
Instruments.cash!
— MethodUpdate 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.
Instruments.cash!
— MethodUpdate 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.
Instruments.cash!
— MethodSet position cash value.
Instruments.cash
— MethodGet the asset instance cash for the long position.
Instruments.cash
— MethodGet the asset instance cash for the short position.
Instruments.cash
— MethodGet the asset instance cash.
Instruments.cash
— MethodHeld position.
Instruments.freecash
— MethodCalculate 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.
Instruments.freecash
— MethodCalculate 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.
Instruments.freecash
— MethodCalculate 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
.
Instruments.qc
— MethodGet the quote currency of an AssetInstance
.
Instruments.raw
— MethodGet the raw string id of an AssetInstance
.
Instruments.value
— MethodCalculate 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.
Instruments.value
— MethodCalculate 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.
Misc.gtxzero
— MethodCheck 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.
Misc.gtxzero
— MethodCheck 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.
Misc.gtxzero
— MethodCheck 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.
Misc.load!
— MethodLoad 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.
Misc.ltxzero
— MethodCheck 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.
Misc.ltxzero
— MethodCheck 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.
Misc.ltxzero
— MethodCheck 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.
Misc.marginmode
— MethodRetrieve the margin mode of an AssetInstance
.
Misc.opposite
— MethodThe opposite position w.r.t. the asset instance and another Position
or PositionSide
.
Misc.reset!
— MethodResets 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.
Misc.reset!
— MethodResets 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.
Misc.reset!
— MethodMisc.reset!
— MethodResets the bare fields to close a position.
reset!(po::Instances.Position) -> Cash{S1, Float64} where S1
OrderTypes.commit!
— MethodSet position committed cash value.
OrderTypes.islong
— MethodCheck if the AssetInstance
is long.
OrderTypes.isshort
— MethodCheck if the AssetInstance
is short.
OrderTypes.trades
— MethodGet the trade history of an AssetInstance
.
TimeTicks.timeframe
— MethodGet 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.
Instances.@_round
— MacroRound 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.
Instances.@ramount
— MacroRound 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
.
Instances.@rprice
— MacroRound 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
.