Skip to content

@livefolio/sdk / withSynthetics

Function: withSynthetics()

withSynthetics(dataFeed, synthetics): DataFeed

Defined in: tactical/synthetics.ts:76

Wraps a DataFeed so that requests for any asset whose id appears in synthetics are intercepted and their bar stream is derived on-the-fly from the corresponding underlying asset.

The synthesized close price on each bar is computed as:

close_t = close_{t-1} × (1 + leverage × underlyingReturn_t) × (1 − expense/252)

The first bar in the stream uses the underlying close directly. OHLC fields other than close are all set to the synthesized close (they are not independently scaled); volume is passed through from the underlying bar.

Non-synthetic assets are proxied transparently to the original dataFeed. fundamentals and events methods, if present, are forwarded unchanged.

Throws at construction time if synthetics contains duplicate id values.

Parameters

ParameterTypeDescription
dataFeedDataFeedThe real data feed to wrap.
syntheticsreadonly SyntheticAsset[]Synthetic asset definitions; typically spec.synthetics ?? [].

Returns

DataFeed

A new DataFeed that intercepts synthetic asset ids.

Example

ts
import { withSynthetics } from '@livefolio/sdk';
import type { SyntheticAsset } from '@livefolio/sdk';

const leveraged: SyntheticAsset = {
  id: 'SPY_3X', symbol: 'SPY3X',
  underlying: { id: 'SPY', symbol: 'SPY' },
  leverage: 3,
  expense: 0.01,
};

const feed = withSynthetics(realFeed, [leveraged]);
// Requesting bars for asset { id: 'SPY_3X', ... } now returns 3× leveraged returns.

Released under the MIT License.