@drift-labs/common
Version:
Common functions for Drift
95 lines (94 loc) • 4.46 kB
TypeScript
import { BN, CandleResolution, WrappedEvent } from '@drift-labs/sdk';
import { SerializableCandle, UISerializableCandle } from '../../serializableTypes';
import { CandleType } from '../../types';
import { PartialUISerializableOrderActionRecord } from '..';
import { Bar } from './types';
type CandleData = Pick<Candle, 'baseVolume' | 'fillOpen' | 'fillHigh' | 'fillClose' | 'fillLow' | 'oracleOpen' | 'oracleHigh' | 'oracleClose' | 'oracleLow' | 'quoteVolume' | 'resolution' | 'start'>;
type CandleProps = {
start: BN;
fillOpen: BN;
fillHigh: BN;
fillClose: BN;
fillLow: BN;
oracleOpen: BN;
oracleHigh: BN;
oracleClose: BN;
oracleLow: BN;
quoteVolume: BN;
baseVolume: BN;
resolution: CandleResolution;
};
export declare class Candle {
start: BN;
fillOpen: BN;
fillHigh: BN;
fillClose: BN;
fillLow: BN;
oracleOpen: BN;
oracleHigh: BN;
oracleClose: BN;
oracleLow: BN;
quoteVolume: BN;
baseVolume: BN;
resolution: CandleResolution;
static getSafeProps(props: CandleProps): {
start: BN;
fillOpen: BN;
fillHigh: BN;
fillClose: BN;
fillLow: BN;
oracleOpen: BN;
oracleHigh: BN;
oracleClose: BN;
oracleLow: BN;
quoteVolume: BN;
baseVolume: BN;
resolution: CandleResolution;
};
static sanityCheckProps(props: CandleProps): void;
private constructor();
print(): string;
toSerializable(): SerializableCandle;
toUISerializable(): UISerializableCandle;
static fromData(candle: CandleData): Candle;
static fromUICandle(candle: UISerializableCandle, handleBadCandle?: boolean): Candle;
static blank(startMs: BN, resolution: CandleResolution, fillPrice?: BN, oraclePrice?: BN): Candle;
static clone(candle: Candle): Candle;
static mergeSmallCandlesIntoBig(start: number, smallCandles: Candle[], outputResolution: CandleResolution): Candle;
/**
* Merge two candles together. The previous candle should be from either the same window or a previous window.
*
* - If the previous candle is in the same window => outputs both the candles merged into one
* - If the previous candle is in the previous window => ensures the current candle correctly "follows" the previous one (updates open price, etc.)
*
* @param current
* @param previous
* @returns
*/
static fromMerge(current: Candle, previous?: Candle): Candle;
static fromBlankAfterPrevious(previousCandle: Candle, resolution: CandleResolution, start: number): Candle;
static fromMergeAll(candles: Candle[]): Candle;
static resolutionStringToCandleLengthMs(resolutionString: CandleResolution): number;
private static tsIsSeconds;
static startTimeForCandle(ts: number, resolution: CandleResolution): number;
static endTimeForCandle(ts: number, resolution: CandleResolution): number;
static getDividingResolution(resolutionString: CandleResolution): CandleResolution;
/**
* Filter for trades which we want to include in candles, which may change over time.
*/
static filterOrderActionsForCandles(tradeEvent: Pick<WrappedEvent<'OrderActionRecord'>, 'action' | 'actionExplanation'>): boolean;
static candleFromTrade(start: number, resolution: CandleResolution, trade: Omit<PartialUISerializableOrderActionRecord, 'slot'>): Candle;
static stitchCandles(allCandles: Candle[]): Candle[];
/**
* Create an array of candles from an array of trades. Merges trades into a single candle if they are in the same candle window.
*/
static mergeTradesIntoCandles(trades: PartialUISerializableOrderActionRecord[], resolution: CandleResolution): Candle[];
static mergeTradesIntoCandle(trades: PartialUISerializableOrderActionRecord[], candle: UISerializableCandle): Candle;
static convertTradesToCandle(trades: PartialUISerializableOrderActionRecord[], fromMs: number, resolution: CandleResolution, previousCandle?: UISerializableCandle): Candle;
static candleToTvBar(candle: UISerializableCandle, candleType: CandleType): Bar;
/**
* This method handles the candles that come back from the exchange history server and converts them into Bars for the TradingView Chart. It also fills any gaps in the candles with blanks.
*/
static candlesToTvBars(candles: UISerializableCandle[], resolution: CandleResolution, candleType: CandleType): Bar[];
}
export {};