Instruments API
The Instruments module provides definitions and management for financial instruments in Planar. It handles different asset types, currency management, and derivative instruments for advanced trading strategies.
Overview
The Instruments module includes:
- Base asset types and currency definitions
- Cash and currency management
- Derivative instruments (futures, options, etc.)
- Asset validation and conversion utilities
- Compact number formatting for financial data
Core Asset Types
Base Asset Types
Asset Creation and Management
Currency Operations
Advanced Cash Operations
Derivative Instruments
Derivative Types
Working with Derivatives
Derivative Portfolio Management
Number Formatting
Compact Number Display
Asset Validation and Utilities
Asset Validation
Asset Comparison and Sorting
Integration with Strategy Framework
Asset Instance Integration
Performance Considerations
Efficient Asset Operations
Complete API Reference
Instruments.AbstractAsset — TypeAbstract base type for representing an asset.
Defines the interface and common functionality for all asset types.
Instruments.AbstractCash — TypeAbstract base type for representing a variable quantity of some currency.
This type defines the interface and common functionality for all cash types.
Instruments.Asset — TypeAn Asset represents a parsed raw (usually ccxt) pair of base and quote currency.
raw: The raw underlying string e.g. 'BTC/USDT'bc: base currency (Symbol)qc: quote currency (Symbol)fiat: if both the base and quote currencies match a known fiat symbol e.g. 'USDT/USDC'leveraged: if parsing matched a leveraged token e.g. 'ETH3L/USDT' or 'ETH3S/USDT'unleveraged_bc: a leveraged token with themodremoved, e.g.ETH3L=>ETH
> asset = a"BTC/USDT"
> typeof(asset)
Asset{:BTC, :USDT}
endInstruments.BaseCurrency — TypeA symbol checked to be a valid base currency.
Instruments.Cash — TypeA variable quantity of some currency.
value
> ca = c"USDT"
> typeof(ca)
# Instruments.Cash{:USDT}Instruments.QuoteCurrency — TypeA symbol checked to be a valid quote currency.
Instruments.Derivatives.Derivative8 — TypeDerivative parsed accordingly to regex.
assetscidstrikekind
Instruments.Derivatives.DerivativeKind — TypeDifferentiates between perpetuals and options.
Instruments.Derivatives.SettlementCurrency — TypeA symbol parsed as settlement currency.
Instruments.addzero! — MethodAdd v to cash, approximating to zero if cash is a small value.
addzero!(c::Cash, v, args...; kwargs...) -> Cash
Instruments.bc — MethodReturns the base currency of a.
Instruments.cash! — MethodSets the cash object to v.
cash!(c::Cash, v, args...; kwargs...) -> Cash
Instruments.compactnum — MethodCompact a numeric value num to a smaller unit if possible.
compactnum(val::Number) -> Any
The function converts the numeric value to a smaller unit of time if the value is greater than or equal to 1000, and returns the compacted value.
Example:
num = 5000
result = compactnum(num) # returns 5 since 5000 can be compacted to 5Instruments.deleverage_pair — MethodRemove leveraged pair pre/suffixes from base currency.
Instruments.deleverage_qc — MethodRemove the leverage component from a CCXT quote currency quote.
deleverage_qc(pair::Array{T<:AbstractString, 1})
The function returns a new string with the leverage component removed.
Example:
quote = "3BTC/USDT"
deleveraged_quote = deleverage_qc(quote) # returns "USDT"Instruments.div! — MethodInplace division for Cash objects.
div!(c::Cash, v, args...; kwargs...) -> Cash
Instruments.has_punct — MethodCheck if a string s contains any punctuation characters.
has_punct(s::AbstractString) -> Bool
The function returns true if s contains any punctuation characters, and false otherwise.
Example:
s = "Hello, world!"
result = has_punct(s) # returns true since the string contains a punctuation characterInstruments.isfiatpair — MethodCheck if both base and quote are fiat currencies.
Instruments.isfiatquote — MethodCheck if quote currency is a stablecoin.
Instruments.isleveragedpair — MethodTest if pair has leveraged naming.
Instruments.mod! — MethodInplace modulo for Cash objects.
mod!(c::Cash, v, args...; kwargs...) -> Cash
Instruments.mul! — MethodInplace multiplication for Cash objects.
mul!(c::Cash, v, args...; kwargs...) -> Cash
Instruments.qc — MethodReturns the quote currency of a.
Instruments.raw — MethodConvert an AbstractAsset object a to its raw representation.
raw(a::AbstractAsset) -> Any
The function returns a new AbstractAsset object with special characters escaped using backslashes.
Example:
a = parse("BTC/USDT")
raw(a) # returns "BTC/USDT"Instruments.rdiv! — MethodInplace remaineder division for Cash objects.
rdiv!(c::Cash, v, args...; kwargs...) -> Cash
Instruments.splitpair — MethodSplit a CCXT pair (symbol) pair into its base and quote currencies.
splitpair(
pair::AbstractString
) -> Array{SubString{_A}, 1} where _A
The function returns a tuple containing the base currency and quote currency.
Example: pair = "BTC/USDT" base, quote = splitpair(pair) # returns ("BTC", "USDT")
Instruments.spotpair — MethodStrips the settlement currency from a symbol.
Instruments.subzero! — MethodSub v to cash, approximating to zero if cash is a small value.
subzero!(
c::Instruments.AbstractCash,
v,
args...;
kwargs...
) -> Union{Exchanges.CurrencyCash, Cash}
Instruments.Derivatives.is_settled — MethodPredicates according to OctoBot
Instruments.Derivatives.perpetual — MethodCreate a Derivative from a raw string representation raw, base currency bc, and quote currency qc.
perpetual(raw::AbstractString, bc, qc) -> Any
Instruments.Derivatives.sc — MethodShort-circuit the execution of a derivative calculation if the derivative d is zero.
sc(d::Instruments.Derivatives.Derivative8; orqc) -> Symbol
Instruments.@a_str — MacroParses pair to an Asset type. ```julia
typeof(a"BTC/USDT")
Instruments.Asset
Instruments.@c_str — MacroMacro to instantiate Cash statically.
Don't put spaces between the id and the value.
> ca = c"USDT"1000
USDT: 1000.0Instruments.@importcash! — MacroCash should not be edited by a strategy, therefore functions that mutate its value should be explicitly imported.
Instruments.@pusdt_str — MacroRewrites sym as a perpetual usdt symbol.
> pusdt"btc"
BTC/USDT:USDTInstruments.ALL_MARKET_SEPARATORS — ConstantA collection of all possible separators used in market symbols
Instruments.DEFAULT_MARKET_SEPARATOR — ConstantThe default separator used in market symbols
Instruments.FULL_SYMBOL_GROUPS_REGEX — ConstantInstruments.SETTLEMENT_SEPARATOR — ConstantThe separator used to separate the settlement currency from the quote currency in a market symbol.
Instruments.fiatnames — ConstantA list of fiat and fiat-like assets names.
Instruments.fiatsyms — ConstantA set of symbols representing fiat and fiat-like assets
Instruments.leverage_pair_rgx — ConstantA regular expression pattern used to match leveraged naming conventions in market symbols. It captures the separator used in leveraged pairs.
Instruments.Derivatives.@d_str — MacroShortand for parsing derivatives:
> drv = d"BTC/USDT:USDT"
> typeof(drv)
# Instruments.Derivatives.Derivative{Asset{:BTC, :USDT}}See Also
- Instances API - Asset instance management
- Strategies API - Strategy base classes and interfaces
- Data API - Data structures and management
- Strategy Development Guide - Building trading strategies
- Advanced Trading Guide - Margin and derivative trading