Documentation ¶
Index ¶
- Variables
- func CreateFuturesCurrencyCode(b, q currency.Code) currency.Code
- type BasicItem
- type CollateralPair
- func (c *CollateralPair) AvailableFunds() decimal.Decimal
- func (c *CollateralPair) CanPlaceOrder(_ gctorder.Side) bool
- func (c *CollateralPair) CollateralCurrency() currency.Code
- func (c *CollateralPair) CollateralReleaser() (ICollateralReleaser, error)
- func (c *CollateralPair) ContractCurrency() currency.Code
- func (c *CollateralPair) CurrentHoldings() decimal.Decimal
- func (c *CollateralPair) FundReader() IFundReader
- func (c *CollateralPair) FundReleaser() IFundReleaser
- func (c *CollateralPair) FundReserver() IFundReserver
- func (c *CollateralPair) GetCollateralReader() (ICollateralReader, error)
- func (c *CollateralPair) GetPairReader() (IPairReader, error)
- func (c *CollateralPair) InitialFunds() decimal.Decimal
- func (c *CollateralPair) Liquidate()
- func (c *CollateralPair) PairReleaser() (IPairReleaser, error)
- func (c *CollateralPair) ReleaseContracts(amount decimal.Decimal) error
- func (c *CollateralPair) Reserve(amount decimal.Decimal, side gctorder.Side) error
- func (c *CollateralPair) TakeProfit(contracts, positionReturns decimal.Decimal) error
- func (c *CollateralPair) UpdateContracts(s gctorder.Side, amount decimal.Decimal) error
- type CurrencyContribution
- type FundManager
- func (f *FundManager) AddItem(item *Item) error
- func (f *FundManager) AddPair(p *SpotPair) error
- func (f *FundManager) AddUSDTrackingData(k *kline.DataFromKline) error
- func (f *FundManager) CreateSnapshot(t time.Time) error
- func (f *FundManager) Exists(item *Item) bool
- func (f *FundManager) GenerateReport() (*Report, error)
- func (f *FundManager) GetAllFunding() ([]BasicItem, error)
- func (f *FundManager) GetFundingForEvent(ev common.Event) (IFundingPair, error)
- func (f *FundManager) HasExchangeBeenLiquidated(ev common.Event) bool
- func (f *FundManager) HasFutures() bool
- func (f *FundManager) IsUsingExchangeLevelFunding() bool
- func (f *FundManager) LinkCollateralCurrency(item *Item, code currency.Code) error
- func (f *FundManager) Liquidate(ev common.Event) error
- func (f *FundManager) RealisePNL(receivingExchange string, receivingAsset asset.Item, ...) error
- func (f *FundManager) Reset() error
- func (f *FundManager) SetFunding(exchName string, item asset.Item, balance *account.Balance, ...) error
- func (f *FundManager) Transfer(amount decimal.Decimal, sender, receiver *Item, inclusiveFee bool) error
- func (f *FundManager) USDTrackingDisabled() bool
- func (f *FundManager) UpdateAllCollateral(isLive, initialFundsSet bool) error
- func (f *FundManager) UpdateCollateralForEvent(ev common.Event, isLive bool) error
- func (f *FundManager) UpdateFundingFromLiveData(initialFundsSet bool) error
- type ICollateralReader
- type ICollateralReleaser
- type IFundReader
- type IFundReleaser
- type IFundReserver
- type IFundingManager
- type IFundingPair
- type IFundingReader
- type IFundingTransferer
- type IPairReader
- type IPairReleaser
- type Item
- func (i *Item) AddContracts(amount decimal.Decimal) error
- func (i *Item) BasicEqual(exch string, a asset.Item, currency, pairedCurrency currency.Code) bool
- func (i *Item) CanPlaceOrder() bool
- func (i *Item) Equal(item *Item) bool
- func (i *Item) IncreaseAvailable(amount decimal.Decimal) error
- func (i *Item) MatchesCurrency(c currency.Code) bool
- func (i *Item) MatchesExchange(item *Item) bool
- func (i *Item) MatchesItemCurrency(item *Item) bool
- func (i *Item) ReduceContracts(amount decimal.Decimal) error
- func (i *Item) Release(amount, diff decimal.Decimal) error
- func (i *Item) Reserve(amount decimal.Decimal) error
- func (i *Item) TakeProfit(amount decimal.Decimal) error
- type ItemSnapshot
- type Report
- type ReportItem
- type SpotPair
- func (p *SpotPair) BaseAvailable() decimal.Decimal
- func (p *SpotPair) BaseInitialFunds() decimal.Decimal
- func (p *SpotPair) CanPlaceOrder(side order.Side) bool
- func (p *SpotPair) CollateralReleaser() (ICollateralReleaser, error)
- func (p *SpotPair) FundReader() IFundReader
- func (p *SpotPair) FundReleaser() IFundReleaser
- func (p *SpotPair) FundReserver() IFundReserver
- func (p *SpotPair) GetCollateralReader() (ICollateralReader, error)
- func (p *SpotPair) GetPairReader() (IPairReader, error)
- func (p *SpotPair) IncreaseAvailable(amount decimal.Decimal, side order.Side) error
- func (p *SpotPair) Liquidate()
- func (p *SpotPair) PairReleaser() (IPairReleaser, error)
- func (p *SpotPair) QuoteAvailable() decimal.Decimal
- func (p *SpotPair) QuoteInitialFunds() decimal.Decimal
- func (p *SpotPair) Release(amount, diff decimal.Decimal, side order.Side) error
- func (p *SpotPair) Reserve(amount decimal.Decimal, side order.Side) error
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotPair is returned when a user requests funding pair details when it is a collateral pair ErrNotPair = errors.New("not a funding pair") ErrIsCollateral = errors.New("is collateral pair") ErrNilPair = errors.New("nil pair") )
collateral related errors
var ( // ErrFundsNotFound used when funds are requested but the funding is not found in the manager ErrFundsNotFound = errors.New("funding not found") // ErrAlreadyExists used when a matching item or pair is already in the funding manager ErrAlreadyExists = errors.New("funding already exists") // ErrUSDTrackingDisabled used when attempting to track USD values when disabled ErrUSDTrackingDisabled = errors.New("USD tracking disabled") )
var ( // ErrNotCollateral is returned when a user requests collateral pair details when it is a funding pair ErrNotCollateral = errors.New("not a collateral pair") )
Functions ¶
func CreateFuturesCurrencyCode ¶
CreateFuturesCurrencyCode converts a currency pair into a code The main reasoning is that as a contract, it exists as an item even if it is formatted as BTC-1231. To treat it as a pair in the funding system would cause an increase in funds for BTC, when it is an increase in contracts This function is basic, but is important be explicit in why this is occurring
Types ¶
type BasicItem ¶
type BasicItem struct { Exchange string Asset asset.Item Currency currency.Code InitialFunds decimal.Decimal Available decimal.Decimal Reserved decimal.Decimal USDPrice decimal.Decimal }
BasicItem is a representation of Item
type CollateralPair ¶
type CollateralPair struct {
// contains filtered or unexported fields
}
CollateralPair consists of a currency pair for a futures contract and associates it with an addition collateral pair to take funding from
func CreateCollateral ¶
func CreateCollateral(contract, collateral *Item) (*CollateralPair, error)
CreateCollateral adds two funding items and associates them with one another the association allows for the same currency to be used multiple times when usingExchangeLevelFunding is false. eg BTC-USDT and LTC-USDT do not share the same USDT level funding
func (*CollateralPair) AvailableFunds ¶
func (c *CollateralPair) AvailableFunds() decimal.Decimal
AvailableFunds returns available funds of collateral
func (*CollateralPair) CanPlaceOrder ¶
func (c *CollateralPair) CanPlaceOrder(_ gctorder.Side) bool
CanPlaceOrder checks if there is any collateral to spare
func (*CollateralPair) CollateralCurrency ¶
func (c *CollateralPair) CollateralCurrency() currency.Code
CollateralCurrency returns collateral currency
func (*CollateralPair) CollateralReleaser ¶
func (c *CollateralPair) CollateralReleaser() (ICollateralReleaser, error)
CollateralReleaser returns an ICollateralReleaser to interact with collateral
func (*CollateralPair) ContractCurrency ¶
func (c *CollateralPair) ContractCurrency() currency.Code
ContractCurrency returns the contract currency
func (*CollateralPair) CurrentHoldings ¶
func (c *CollateralPair) CurrentHoldings() decimal.Decimal
CurrentHoldings returns available contract holdings
func (*CollateralPair) FundReader ¶
func (c *CollateralPair) FundReader() IFundReader
FundReader returns a fund reader interface of collateral
func (*CollateralPair) FundReleaser ¶
func (c *CollateralPair) FundReleaser() IFundReleaser
FundReleaser returns an IFundReleaser to interact with collateral
func (*CollateralPair) FundReserver ¶
func (c *CollateralPair) FundReserver() IFundReserver
FundReserver returns a fund reserver interface of CollateralPair
func (*CollateralPair) GetCollateralReader ¶
func (c *CollateralPair) GetCollateralReader() (ICollateralReader, error)
GetCollateralReader returns a collateral reader interface of CollateralPair
func (*CollateralPair) GetPairReader ¶
func (c *CollateralPair) GetPairReader() (IPairReader, error)
GetPairReader returns an error because collateral isn't a pair
func (*CollateralPair) InitialFunds ¶
func (c *CollateralPair) InitialFunds() decimal.Decimal
InitialFunds returns initial funds of collateral
func (*CollateralPair) Liquidate ¶
func (c *CollateralPair) Liquidate()
Liquidate kills your funds and future all value storage are reduced to zero when triggered
func (*CollateralPair) PairReleaser ¶
func (c *CollateralPair) PairReleaser() (IPairReleaser, error)
PairReleaser returns an error as there is no such thing for collateral
func (*CollateralPair) ReleaseContracts ¶
func (c *CollateralPair) ReleaseContracts(amount decimal.Decimal) error
ReleaseContracts lowers the amount of available contracts
func (*CollateralPair) TakeProfit ¶
func (c *CollateralPair) TakeProfit(contracts, positionReturns decimal.Decimal) error
TakeProfit handles both the reduction of contracts and the change in collateral
func (*CollateralPair) UpdateContracts ¶
UpdateContracts adds or subtracts contracts based on order direction
type CurrencyContribution ¶
CurrencyContribution helps breakdown how a USD value determines its number
type FundManager ¶
type FundManager struct {
// contains filtered or unexported fields
}
FundManager is the benevolent holder of all funding levels across all currencies used in the backtester
func SetupFundingManager ¶
func SetupFundingManager(exchManager *engine.ExchangeManager, usingExchangeLevelFunding, disableUSDTracking, verbose bool) (*FundManager, error)
SetupFundingManager creates the funding holder. It carries knowledge about levels of funding across all execution handlers and enables fund transfers
func (*FundManager) AddItem ¶
func (f *FundManager) AddItem(item *Item) error
AddItem appends a new funding item. Will reject if exists by exchange asset currency
func (*FundManager) AddPair ¶
func (f *FundManager) AddPair(p *SpotPair) error
AddPair adds a pair to the fund manager if it does not exist
func (*FundManager) AddUSDTrackingData ¶
func (f *FundManager) AddUSDTrackingData(k *kline.DataFromKline) error
AddUSDTrackingData adds USD tracking data to a funding item only in the event that it is not USD and there is data
func (*FundManager) CreateSnapshot ¶
func (f *FundManager) CreateSnapshot(t time.Time) error
CreateSnapshot creates a Snapshot for an event's point in time as funding.snapshots is a map, it allows for the last event in the chronological list to establish the canon at X time
func (*FundManager) Exists ¶
func (f *FundManager) Exists(item *Item) bool
Exists verifies whether there is a funding item that exists with the same exchange, asset and currency
func (*FundManager) GenerateReport ¶
func (f *FundManager) GenerateReport() (*Report, error)
GenerateReport builds report data for result HTML report
func (*FundManager) GetAllFunding ¶
func (f *FundManager) GetAllFunding() ([]BasicItem, error)
GetAllFunding returns basic representations of all current holdings from the latest point
func (*FundManager) GetFundingForEvent ¶
func (f *FundManager) GetFundingForEvent(ev common.Event) (IFundingPair, error)
GetFundingForEvent This will construct a funding based on a backtesting event
func (*FundManager) HasExchangeBeenLiquidated ¶
func (f *FundManager) HasExchangeBeenLiquidated(ev common.Event) bool
HasExchangeBeenLiquidated checks for any items with a matching exchange and returns whether it has been liquidated
func (*FundManager) HasFutures ¶
func (f *FundManager) HasFutures() bool
HasFutures returns whether the funding manager contains any futures assets
func (*FundManager) IsUsingExchangeLevelFunding ¶
func (f *FundManager) IsUsingExchangeLevelFunding() bool
IsUsingExchangeLevelFunding returns if using usingExchangeLevelFunding
func (*FundManager) LinkCollateralCurrency ¶
func (f *FundManager) LinkCollateralCurrency(item *Item, code currency.Code) error
LinkCollateralCurrency links an item to an existing currency code for collateral purposes
func (*FundManager) Liquidate ¶
func (f *FundManager) Liquidate(ev common.Event) error
Liquidate will remove all funding for all items belonging to an exchange
func (*FundManager) RealisePNL ¶
func (f *FundManager) RealisePNL(receivingExchange string, receivingAsset asset.Item, receivingCurrency currency.Code, realisedPNL decimal.Decimal) error
RealisePNL adds the realised PNL to a receiving exchange asset pair
func (*FundManager) SetFunding ¶
func (f *FundManager) SetFunding(exchName string, item asset.Item, balance *account.Balance, initialFundsSet bool) error
SetFunding overwrites a funding setting. This is for live trading where external wallet amounts need to be synced As external sources may have additional currencies and balances versus the strategy currencies, they must be appended to help calculate collateral
func (*FundManager) Transfer ¶
func (f *FundManager) Transfer(amount decimal.Decimal, sender, receiver *Item, inclusiveFee bool) error
Transfer allows transferring funds from one pretend exchange to another
func (*FundManager) USDTrackingDisabled ¶
func (f *FundManager) USDTrackingDisabled() bool
USDTrackingDisabled clears all settings
func (*FundManager) UpdateAllCollateral ¶
func (f *FundManager) UpdateAllCollateral(isLive, initialFundsSet bool) error
UpdateAllCollateral will update the collateral values of all stored exchanges
func (*FundManager) UpdateCollateralForEvent ¶
func (f *FundManager) UpdateCollateralForEvent(ev common.Event, isLive bool) error
UpdateCollateralForEvent will recalculate collateral for an exchange based on the event passed in
func (*FundManager) UpdateFundingFromLiveData ¶
func (f *FundManager) UpdateFundingFromLiveData(initialFundsSet bool) error
UpdateFundingFromLiveData forcefully updates funding from a live source
type ICollateralReader ¶
type ICollateralReader interface { ContractCurrency() currency.Code CollateralCurrency() currency.Code InitialFunds() decimal.Decimal AvailableFunds() decimal.Decimal CurrentHoldings() decimal.Decimal }
ICollateralReader is used to read data from collateral pairs
type ICollateralReleaser ¶
type ICollateralReleaser interface { ICollateralReader UpdateContracts(order.Side, decimal.Decimal) error TakeProfit(contracts, positionReturns decimal.Decimal) error ReleaseContracts(decimal.Decimal) error Liquidate() }
ICollateralReleaser limits funding usage for exchange event handling
type IFundReader ¶
type IFundReader interface { GetPairReader() (IPairReader, error) GetCollateralReader() (ICollateralReader, error) }
IFundReader can read either collateral or pair details
type IFundReleaser ¶
type IFundReleaser interface { IFundReader PairReleaser() (IPairReleaser, error) CollateralReleaser() (ICollateralReleaser, error) }
IFundReleaser can read or release pair or collateral funds
type IFundReserver ¶
type IFundReserver interface { IFundReader CanPlaceOrder(order.Side) bool Reserve(decimal.Decimal, order.Side) error }
IFundReserver limits funding usage for portfolio event handling
type IFundingManager ¶
type IFundingManager interface { Reset() error IsUsingExchangeLevelFunding() bool GetFundingForEvent(common.Event) (IFundingPair, error) Transfer(decimal.Decimal, *Item, *Item, bool) error GenerateReport() (*Report, error) AddUSDTrackingData(*kline.DataFromKline) error CreateSnapshot(time.Time) error USDTrackingDisabled() bool Liquidate(common.Event) error GetAllFunding() ([]BasicItem, error) UpdateCollateralForEvent(common.Event, bool) error UpdateAllCollateral(isLive, hasUpdateFunding bool) error UpdateFundingFromLiveData(hasUpdatedFunding bool) error HasFutures() bool HasExchangeBeenLiquidated(handler common.Event) bool RealisePNL(receivingExchange string, receivingAsset asset.Item, receivingCurrency currency.Code, realisedPNL decimal.Decimal) error SetFunding(string, asset.Item, *account.Balance, bool) error }
IFundingManager limits funding usage for portfolio event handling
type IFundingPair ¶
type IFundingPair interface { FundReader() IFundReader FundReserver() IFundReserver FundReleaser() IFundReleaser }
IFundingPair allows conversion into various funding interfaces
type IFundingReader ¶
type IFundingReader interface { GetFundingForEvent(common.Event) (IFundingPair, error) GetAllFunding() []BasicItem }
IFundingReader is a simple interface of IFundingManager for readonly access at portfolio manager
type IFundingTransferer ¶
type IFundingTransferer interface { IsUsingExchangeLevelFunding() bool Transfer(decimal.Decimal, *Item, *Item, bool) error GetFundingForEvent(common.Event) (IFundingPair, error) HasExchangeBeenLiquidated(handler common.Event) bool }
IFundingTransferer allows for funding amounts to be transferred implementation can be swapped for live transferring
type IPairReader ¶
type IPairReader interface { BaseInitialFunds() decimal.Decimal QuoteInitialFunds() decimal.Decimal BaseAvailable() decimal.Decimal QuoteAvailable() decimal.Decimal }
IPairReader is used to limit pair funding functions to readonly
type IPairReleaser ¶
type IPairReleaser interface { IPairReader IncreaseAvailable(decimal.Decimal, order.Side) error Release(decimal.Decimal, decimal.Decimal, order.Side) error Liquidate() }
IPairReleaser limits funding usage for exchange event handling
type Item ¶
type Item struct {
// contains filtered or unexported fields
}
Item holds funding data per currency item
func CreateItem ¶
func CreateItem(exch string, a asset.Item, ci currency.Code, initialFunds, transferFee decimal.Decimal) (*Item, error)
CreateItem creates a new funding item
func (*Item) AddContracts ¶
AddContracts allocates an amount of funds to be used at a later time it prevents multiple events from claiming the same resource
func (*Item) BasicEqual ¶
BasicEqual checks for equality via passed in values
func (*Item) CanPlaceOrder ¶
CanPlaceOrder checks if the item has any funds available
func (*Item) IncreaseAvailable ¶
IncreaseAvailable adds funding to the available amount
func (*Item) MatchesCurrency ¶
MatchesCurrency checks that an item's currency is equal
func (*Item) MatchesExchange ¶
MatchesExchange checks that an item's exchange is equal
func (*Item) MatchesItemCurrency ¶
MatchesItemCurrency checks that an item's currency is equal
func (*Item) ReduceContracts ¶
ReduceContracts allocates an amount of funds to be used at a later time it prevents multiple events from claiming the same resource
func (*Item) Release ¶
Release reduces the amount of funding reserved and adds any difference back to the available amount
type ItemSnapshot ¶
type ItemSnapshot struct { Time time.Time Available decimal.Decimal USDClosePrice decimal.Decimal USDValue decimal.Decimal Breakdown []CurrencyContribution }
ItemSnapshot holds USD values to allow for tracking across backtesting results
type Report ¶
type Report struct { DisableUSDTracking bool UsingExchangeLevelFunding bool Items []ReportItem USDTotalsOverTime []ItemSnapshot InitialFunds decimal.Decimal FinalFunds decimal.Decimal }
Report holds all funding data for result reporting
type ReportItem ¶
type ReportItem struct { Exchange string Asset asset.Item Currency currency.Code TransferFee decimal.Decimal InitialFunds decimal.Decimal FinalFunds decimal.Decimal USDInitialFunds decimal.Decimal USDInitialCostForOne decimal.Decimal USDFinalFunds decimal.Decimal USDFinalCostForOne decimal.Decimal Snapshots []ItemSnapshot USDPairCandle *kline.DataFromKline Difference decimal.Decimal ShowInfinite bool IsCollateral bool AppendedViaAPI bool PairedWith currency.Code }
ReportItem holds reporting fields
type SpotPair ¶
type SpotPair struct {
// contains filtered or unexported fields
}
SpotPair holds two currencies that are associated with each other
func CreatePair ¶
CreatePair adds two funding items and associates them with one another the association allows for the same currency to be used multiple times when usingExchangeLevelFunding is false. eg BTC-USDT and LTC-USDT do not share the same USDT level funding
func (*SpotPair) BaseAvailable ¶
BaseAvailable returns the available funds from the base in a currency pair
func (*SpotPair) BaseInitialFunds ¶
BaseInitialFunds returns the initial funds from the base in a currency pair
func (*SpotPair) CanPlaceOrder ¶
CanPlaceOrder does a > 0 check to see if there are any funds to place an order with changes which currency to affect based on the order side
func (*SpotPair) CollateralReleaser ¶
func (p *SpotPair) CollateralReleaser() (ICollateralReleaser, error)
CollateralReleaser returns an error because a pair is not collateral
func (*SpotPair) FundReader ¶
func (p *SpotPair) FundReader() IFundReader
FundReader returns a fund reader interface of the pair
func (*SpotPair) FundReleaser ¶
func (p *SpotPair) FundReleaser() IFundReleaser
FundReleaser returns a pair releaser interface of the pair
func (*SpotPair) FundReserver ¶
func (p *SpotPair) FundReserver() IFundReserver
FundReserver returns a fund reserver interface of the pair
func (*SpotPair) GetCollateralReader ¶
func (p *SpotPair) GetCollateralReader() (ICollateralReader, error)
GetCollateralReader returns an error because its not collateral
func (*SpotPair) GetPairReader ¶
func (p *SpotPair) GetPairReader() (IPairReader, error)
GetPairReader returns an interface of a SpotPair
func (*SpotPair) IncreaseAvailable ¶
IncreaseAvailable adds funding to the available amount changes which currency to affect based on the order side
func (*SpotPair) Liquidate ¶
func (p *SpotPair) Liquidate()
Liquidate basic liquidation response to remove all asset value
func (*SpotPair) PairReleaser ¶
func (p *SpotPair) PairReleaser() (IPairReleaser, error)
PairReleaser returns a pair releaser interface of the pair
func (*SpotPair) QuoteAvailable ¶
QuoteAvailable returns the available funds from the quote in a currency pair
func (*SpotPair) QuoteInitialFunds ¶
QuoteInitialFunds returns the initial funds from the quote in a currency pair