@orangecharts/wrun-chart-host 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -6
- package/dist/constants/common.constants.d.ts +15 -0
- package/dist/constants/common.constants.js +23 -0
- package/dist/constants/data-source.constants.d.ts +1 -0
- package/dist/constants/data-source.constants.js +1 -0
- package/dist/constants/economic.constants.d.ts +39 -0
- package/dist/constants/economic.constants.js +77 -0
- package/dist/constants/etf-covered-coins.constants.d.ts +1 -0
- package/dist/constants/etf-covered-coins.constants.js +9 -0
- package/dist/constants/feature-flags.constants.d.ts +60 -0
- package/dist/constants/feature-flags.constants.js +308 -0
- package/dist/constants/feature-limits.constants.d.ts +134 -0
- package/dist/constants/feature-limits.constants.js +155 -0
- package/dist/constants/marketCapabilities.constants.d.ts +87 -0
- package/dist/constants/marketCapabilities.constants.js +213 -0
- package/dist/constants/option-venues.constants.d.ts +100 -0
- package/dist/constants/option-venues.constants.js +167 -0
- package/dist/constants/overlay-data.constants.d.ts +53 -0
- package/dist/constants/overlay-data.constants.js +80 -0
- package/dist/constants/pane-grid.constants.d.ts +15 -0
- package/dist/constants/pane-grid.constants.js +17 -0
- package/dist/constants/venue-presets.constants.d.ts +10 -0
- package/dist/constants/venue-presets.constants.js +22 -0
- package/dist/enums/chart.enum.d.ts +68 -0
- package/dist/enums/chart.enum.js +70 -0
- package/dist/helpers/data.helpers.d.ts +2 -0
- package/dist/helpers/data.helpers.js +14 -0
- package/dist/host-env.d.ts +161 -5
- package/dist/host-ports.types.d.ts +446 -0
- package/dist/host-ports.types.js +1 -0
- package/dist/index.d.ts +128 -0
- package/dist/index.js +128 -0
- package/dist/indicators/controls/technical-script/technical-script-input-rendering.d.ts +86 -0
- package/dist/indicators/controls/technical-script/technical-script-input-rendering.js +179 -0
- package/dist/interfaces/asset-venue.interface.d.ts +91 -0
- package/dist/interfaces/asset-venue.interface.js +1 -0
- package/dist/interfaces/cancel-reason.interface.d.ts +24 -0
- package/dist/interfaces/cancel-reason.interface.js +2 -0
- package/dist/interfaces/catalog-display.interface.d.ts +3 -0
- package/dist/interfaces/catalog-display.interface.js +1 -0
- package/dist/interfaces/cme-realtime.interface.d.ts +251 -0
- package/dist/interfaces/cme-realtime.interface.js +46 -0
- package/dist/interfaces/feed.interface.d.ts +80 -0
- package/dist/interfaces/feed.interface.js +7 -0
- package/dist/interfaces/market-kind.interface.d.ts +8 -0
- package/dist/interfaces/market-kind.interface.js +1 -0
- package/dist/interfaces/opra-realtime.interface.d.ts +224 -0
- package/dist/interfaces/opra-realtime.interface.js +105 -0
- package/dist/interfaces/option-instrument.interface.d.ts +32 -0
- package/dist/interfaces/option-instrument.interface.js +1 -0
- package/dist/interfaces/options-chain-scope.interface.d.ts +29 -0
- package/dist/interfaces/options-chain-scope.interface.js +7 -0
- package/dist/interfaces/prediction-market.interface.d.ts +88 -0
- package/dist/interfaces/prediction-market.interface.js +8 -0
- package/dist/interfaces/wrun-runtime.interface.d.ts +5 -0
- package/dist/kscript-data/controls/live-trades-merge.util.d.ts +185 -0
- package/dist/kscript-data/controls/live-trades-merge.util.js +357 -0
- package/dist/settings-templates/frvp-fields.d.ts +27 -0
- package/dist/settings-templates/frvp-fields.js +108 -0
- package/dist/settings-templates/index.d.ts +119 -0
- package/dist/settings-templates/index.js +442 -0
- package/dist/settings-templates/indicator-fields.d.ts +56 -0
- package/dist/settings-templates/indicator-fields.js +793 -0
- package/dist/settings-templates/tool-rules.d.ts +59 -0
- package/dist/settings-templates/tool-rules.js +49 -0
- package/dist/settings-templates/tool-tables.generated.d.ts +2 -0
- package/dist/settings-templates/tool-tables.generated.js +1668 -0
- package/dist/utility/blk-market.util.d.ts +5 -0
- package/dist/utility/blk-market.util.js +12 -0
- package/dist/utility/chart/chart-load-phase.util.d.ts +32 -0
- package/dist/utility/chart/chart-load-phase.util.js +78 -0
- package/dist/utility/chart/cme-cache-context.util.d.ts +108 -0
- package/dist/utility/chart/cme-cache-context.util.js +166 -0
- package/dist/utility/chart/cme-session-bucket.util.d.ts +82 -0
- package/dist/utility/chart/cme-session-bucket.util.js +343 -0
- package/dist/utility/chart/cme-trade-calendar.util.d.ts +26 -0
- package/dist/utility/chart/cme-trade-calendar.util.js +147 -0
- package/dist/utility/chart/continuous-contracts.util.d.ts +138 -0
- package/dist/utility/chart/continuous-contracts.util.js +374 -0
- package/dist/utility/chart/feed-interval.util.d.ts +119 -0
- package/dist/utility/chart/feed-interval.util.js +321 -0
- package/dist/utility/chart/fetch-dedupe-policy.util.d.ts +110 -0
- package/dist/utility/chart/fetch-dedupe-policy.util.js +407 -0
- package/dist/utility/chart/footprint-band.util.d.ts +76 -0
- package/dist/utility/chart/footprint-band.util.js +205 -0
- package/dist/utility/chart/footprint-cell-identity.util.d.ts +82 -0
- package/dist/utility/chart/footprint-cell-identity.util.js +346 -0
- package/dist/utility/chart/formula-calendar.util.d.ts +27 -0
- package/dist/utility/chart/formula-calendar.util.js +59 -0
- package/dist/utility/chart/history-wall.util.d.ts +67 -0
- package/dist/utility/chart/history-wall.util.js +87 -0
- package/dist/utility/chart/index-mode.util.d.ts +66 -0
- package/dist/utility/chart/index-mode.util.js +360 -0
- package/dist/utility/chart/indicator-coverage-planner.util.d.ts +217 -0
- package/dist/utility/chart/indicator-coverage-planner.util.js +443 -0
- package/dist/utility/chart/indicator-coverage-state.util.d.ts +33 -0
- package/dist/utility/chart/indicator-coverage-state.util.js +110 -0
- package/dist/utility/chart/indicator-gap-detector.util.d.ts +127 -0
- package/dist/utility/chart/indicator-gap-detector.util.js +260 -0
- package/dist/utility/chart/indicator-gap-repair.util.d.ts +88 -0
- package/dist/utility/chart/indicator-gap-repair.util.js +177 -0
- package/dist/utility/chart/indicator-inflight-registry.util.d.ts +26 -0
- package/dist/utility/chart/indicator-inflight-registry.util.js +254 -0
- package/dist/utility/chart/interval-registry.util.d.ts +23 -0
- package/dist/utility/chart/interval-registry.util.js +83 -0
- package/dist/utility/chart/kscript-main-grid-stages.util.d.ts +21 -0
- package/dist/utility/chart/kscript-main-grid-stages.util.js +332 -0
- package/dist/utility/chart/legacy-exchange.leaf.d.ts +11 -0
- package/dist/utility/chart/legacy-exchange.leaf.js +43 -0
- package/dist/utility/chart/live-bucket.util.d.ts +66 -0
- package/dist/utility/chart/live-bucket.util.js +165 -0
- package/dist/utility/chart/market-session-schedule.util.d.ts +91 -0
- package/dist/utility/chart/market-session-schedule.util.js +337 -0
- package/dist/utility/chart/marketKind.util.d.ts +181 -0
- package/dist/utility/chart/marketKind.util.js +371 -0
- package/dist/utility/chart/opra-read-access.leaf.d.ts +26 -0
- package/dist/utility/chart/opra-read-access.leaf.js +25 -0
- package/dist/utility/chart/prediction-identity.util.d.ts +159 -0
- package/dist/utility/chart/prediction-identity.util.js +236 -0
- package/dist/utility/chart/rth-session.util.d.ts +352 -0
- package/dist/utility/chart/rth-session.util.js +926 -0
- package/dist/utility/chart/titan-constants.pin.d.ts +43 -0
- package/dist/utility/chart/titan-constants.pin.js +50 -0
- package/dist/utility/chart/us-equities-session.util.d.ts +127 -0
- package/dist/utility/chart/us-equities-session.util.js +251 -0
- package/dist/utility/chart/wrun-bar-session-facts.util.d.ts +100 -0
- package/dist/utility/chart/wrun-bar-session-facts.util.js +354 -0
- package/dist/utility/chart/wrun-wire-ref.util.d.ts +23 -0
- package/dist/utility/chart/wrun-wire-ref.util.js +16 -0
- package/dist/utility/common/safe-math-eval.d.ts +18 -0
- package/dist/utility/common/safe-math-eval.js +160 -0
- package/dist/utility/common/time.util.d.ts +134 -0
- package/dist/utility/common/time.util.js +428 -0
- package/dist/utility/common/transformations.util.d.ts +126 -0
- package/dist/utility/common/transformations.util.js +711 -0
- package/dist/utility/common/visible-timeout.util.d.ts +17 -0
- package/dist/utility/common/visible-timeout.util.js +75 -0
- package/dist/utility/exchange-normalization.util.d.ts +8 -0
- package/dist/utility/exchange-normalization.util.js +32 -0
- package/dist/utility/intl-locale.util.d.ts +2 -0
- package/dist/utility/intl-locale.util.js +28 -0
- package/dist/utility/number-format.util.d.ts +14 -1
- package/dist/utility/number-format.util.js +50 -19
- package/dist/utility/options/option-symbol.util.d.ts +28 -0
- package/dist/utility/options/option-symbol.util.js +176 -0
- package/dist/utility/options/option-trade-units.util.d.ts +190 -0
- package/dist/utility/options/option-trade-units.util.js +283 -0
- package/dist/utility/options/option-venue.util.d.ts +75 -0
- package/dist/utility/options/option-venue.util.js +85 -0
- package/dist/utility/pool-symbol.leaf.d.ts +34 -0
- package/dist/utility/pool-symbol.leaf.js +95 -0
- package/dist/utility/scriptEditor/wrun-runtime-diagnostics.leaf.d.ts +109 -0
- package/dist/utility/scriptEditor/wrun-runtime-diagnostics.leaf.js +302 -0
- package/dist/utility/symbol-selection/contract-expiry.util.d.ts +39 -0
- package/dist/utility/symbol-selection/contract-expiry.util.js +111 -0
- package/dist/utility/v2/gate-plane.util.d.ts +58 -0
- package/dist/utility/v2/gate-plane.util.js +99 -0
- package/dist/utility/v2/gate-session-latch.util.d.ts +22 -0
- package/dist/utility/v2/gate-session-latch.util.js +34 -0
- package/dist/utility/watchlist/import/tradingview-symbols.leaf.d.ts +76 -0
- package/dist/utility/watchlist/import/tradingview-symbols.leaf.js +221 -0
- package/dist/utility/wrun/sources/host-grid-landing.d.ts +15 -0
- package/dist/utility/wrun/sources/host-grid-landing.js +26 -0
- package/dist/utility/wrun/sources/lanes/bitfinex_funding.d.ts +27 -0
- package/dist/utility/wrun/sources/lanes/bitfinex_funding.js +85 -0
- package/dist/utility/wrun/sources/lanes/economic.d.ts +27 -0
- package/dist/utility/wrun/sources/lanes/economic.js +79 -0
- package/dist/utility/wrun/sources/lanes/etf_holdings.d.ts +35 -0
- package/dist/utility/wrun/sources/lanes/etf_holdings.js +165 -0
- package/dist/utility/wrun/sources/lanes/etf_premium.d.ts +19 -0
- package/dist/utility/wrun/sources/lanes/etf_premium.js +114 -0
- package/dist/utility/wrun/sources/lanes/ethena_positions.d.ts +30 -0
- package/dist/utility/wrun/sources/lanes/ethena_positions.js +51 -0
- package/dist/utility/wrun/sources/lanes/index.d.ts +2 -0
- package/dist/utility/wrun/sources/lanes/index.js +32 -0
- package/dist/utility/wrun/sources/lanes/long_short_ratio.d.ts +17 -0
- package/dist/utility/wrun/sources/lanes/long_short_ratio.js +47 -0
- package/dist/utility/wrun/sources/lanes/option_daily.d.ts +30 -0
- package/dist/utility/wrun/sources/lanes/option_daily.js +53 -0
- package/dist/utility/wrun/sources/lanes/option_stats_10m.d.ts +23 -0
- package/dist/utility/wrun/sources/lanes/option_stats_10m.js +20 -0
- package/dist/utility/wrun/sources/lanes/options_flow.d.ts +50 -0
- package/dist/utility/wrun/sources/lanes/options_flow.js +155 -0
- package/dist/utility/wrun/sources/lanes/options_oi.d.ts +29 -0
- package/dist/utility/wrun/sources/lanes/options_oi.js +59 -0
- package/dist/utility/wrun/sources/lanes/options_volume.d.ts +38 -0
- package/dist/utility/wrun/sources/lanes/options_volume.js +63 -0
- package/dist/utility/wrun/sources/lanes/series.d.ts +67 -0
- package/dist/utility/wrun/sources/lanes/series.js +321 -0
- package/dist/utility/wrun/sources/lanes/token_supply.d.ts +30 -0
- package/dist/utility/wrun/sources/lanes/token_supply.js +145 -0
- package/dist/utility/wrun/sources/lanes/treasury_balance.d.ts +25 -0
- package/dist/utility/wrun/sources/lanes/treasury_balance.js +47 -0
- package/dist/utility/wrun/sources/lanes/volatility_index.d.ts +21 -0
- package/dist/utility/wrun/sources/lanes/volatility_index.js +11 -0
- package/dist/utility/wrun/sources/opra-option-lanes.util.d.ts +196 -0
- package/dist/utility/wrun/sources/opra-option-lanes.util.js +446 -0
- package/dist/utility/wrun/sources/registry.d.ts +21 -0
- package/dist/utility/wrun/sources/registry.js +207 -0
- package/dist/utility/wrun/sources/row-lane-registry.d.ts +28 -0
- package/dist/utility/wrun/sources/row-lane-registry.js +230 -0
- package/dist/utility/wrun/sources/row-lanes/index.d.ts +2 -0
- package/dist/utility/wrun/sources/row-lanes/index.js +12 -0
- package/dist/utility/wrun/sources/row-lanes/option_contract_flow.d.ts +2 -0
- package/dist/utility/wrun/sources/row-lanes/option_contract_flow.js +38 -0
- package/dist/utility/wrun/sources/row-lanes/option_contract_state.d.ts +6 -0
- package/dist/utility/wrun/sources/row-lanes/option_contract_state.js +70 -0
- package/dist/utility/wrun/sources/row-lanes/option_prints.d.ts +4 -0
- package/dist/utility/wrun/sources/row-lanes/option_prints.js +191 -0
- package/dist/utility/wrun/sources/row-lanes/option_strikes.d.ts +13 -0
- package/dist/utility/wrun/sources/row-lanes/option_strikes.js +137 -0
- package/dist/utility/wrun/sources/row-lanes/option_surface.d.ts +38 -0
- package/dist/utility/wrun/sources/row-lanes/option_surface.js +150 -0
- package/dist/utility/wrun/sources/wrun-poll-lane.types.d.ts +275 -0
- package/dist/utility/wrun/sources/wrun-poll-lane.types.js +26 -0
- package/dist/utility/wrun/sources/wrun-poll-lanes.util.d.ts +224 -0
- package/dist/utility/wrun/sources/wrun-poll-lanes.util.js +394 -0
- package/dist/utility/wrun/sources/wrun-row-lane.types.d.ts +283 -0
- package/dist/utility/wrun/sources/wrun-row-lane.types.js +16 -0
- package/dist/utility/wrun/sources/wrun-row-lanes.util.d.ts +205 -0
- package/dist/utility/wrun/sources/wrun-row-lanes.util.js +469 -0
- package/dist/utility/wrun/wrun-browser-sources.leaf.d.ts +28 -0
- package/dist/utility/wrun/wrun-browser-sources.leaf.js +66 -0
- package/dist/utility/wrun/wrun-cloud-record.util.d.ts +65 -0
- package/dist/utility/wrun/wrun-cloud-record.util.js +121 -0
- package/dist/utility/wrun/wrun-daily-lane.util.d.ts +95 -0
- package/dist/utility/wrun/wrun-daily-lane.util.js +297 -0
- package/dist/utility/wrun/wrun-deadline.util.d.ts +52 -0
- package/dist/utility/wrun/wrun-deadline.util.js +82 -0
- package/dist/utility/wrun/wrun-etf-flow-lane.util.d.ts +94 -0
- package/dist/utility/wrun/wrun-etf-flow-lane.util.js +224 -0
- package/dist/utility/wrun/wrun-etf-funds.util.d.ts +82 -0
- package/dist/utility/wrun/wrun-etf-funds.util.js +242 -0
- package/dist/utility/wrun/wrun-frames.d.ts +20 -1
- package/dist/utility/wrun/wrun-frames.js +103 -49
- package/dist/utility/wrun/wrun-hosted-wire-caps.d.ts +2 -2
- package/dist/utility/wrun/wrun-hosted-wire-caps.js +27 -0
- package/dist/utility/wrun/wrun-lane-refusal.leaf.d.ts +22 -0
- package/dist/utility/wrun/wrun-lane-refusal.leaf.js +32 -0
- package/dist/utility/wrun/wrun-lane-warmup.util.d.ts +18 -0
- package/dist/utility/wrun/wrun-lane-warmup.util.js +112 -0
- package/dist/utility/wrun/wrun-leg-close.util.d.ts +57 -0
- package/dist/utility/wrun/wrun-leg-close.util.js +187 -0
- package/dist/utility/wrun/wrun-leg-fetch.util.d.ts +144 -0
- package/dist/utility/wrun/wrun-leg-fetch.util.js +212 -0
- package/dist/utility/wrun/wrun-leg-stream-classes.d.ts +73 -0
- package/dist/utility/wrun/wrun-leg-stream-classes.js +86 -0
- package/dist/utility/wrun/wrun-levels-child.util.d.ts +101 -6
- package/dist/utility/wrun/wrun-levels-child.util.js +335 -157
- package/dist/utility/wrun/wrun-memory-clamp.util.d.ts +33 -0
- package/dist/utility/wrun/wrun-memory-clamp.util.js +146 -0
- package/dist/utility/wrun/wrun-odds-template-market.util.d.ts +51 -0
- package/dist/utility/wrun/wrun-odds-template-market.util.js +251 -0
- package/dist/utility/wrun/wrun-opra-flow.leaf.d.ts +65 -0
- package/dist/utility/wrun/wrun-opra-flow.leaf.js +94 -0
- package/dist/utility/wrun/wrun-opra-lane.util.d.ts +47 -0
- package/dist/utility/wrun/wrun-opra-lane.util.js +58 -0
- package/dist/utility/wrun/wrun-opra-stats.leaf.d.ts +30 -0
- package/dist/utility/wrun/wrun-opra-stats.leaf.js +96 -0
- package/dist/utility/wrun/wrun-opra-underlying.util.d.ts +12 -0
- package/dist/utility/wrun/wrun-opra-underlying.util.js +1 -0
- package/dist/utility/wrun/wrun-option-contract-flow.util.d.ts +142 -0
- package/dist/utility/wrun/wrun-option-contract-flow.util.js +451 -0
- package/dist/utility/wrun/wrun-option-knobs.util.d.ts +43 -0
- package/dist/utility/wrun/wrun-option-knobs.util.js +81 -0
- package/dist/utility/wrun/wrun-option-prints.util.d.ts +144 -0
- package/dist/utility/wrun/wrun-option-prints.util.js +356 -0
- package/dist/utility/wrun/wrun-option-summary-lane.util.d.ts +102 -0
- package/dist/utility/wrun/wrun-option-summary-lane.util.js +143 -0
- package/dist/utility/wrun/wrun-options-chain.util.d.ts +308 -0
- package/dist/utility/wrun/wrun-options-chain.util.js +502 -0
- package/dist/utility/wrun/wrun-overlay-identity.util.d.ts +9 -4
- package/dist/utility/wrun/wrun-overlay-identity.util.js +16 -6
- package/dist/utility/wrun/wrun-param-map.util.d.ts +298 -0
- package/dist/utility/wrun/wrun-param-map.util.js +581 -0
- package/dist/utility/wrun/wrun-pin-spelling.util.d.ts +149 -0
- package/dist/utility/wrun/wrun-pin-spelling.util.js +523 -0
- package/dist/utility/wrun/wrun-pin-venues.util.d.ts +116 -0
- package/dist/utility/wrun/wrun-pin-venues.util.js +320 -0
- package/dist/utility/wrun/wrun-pinned-interval.util.d.ts +20 -0
- package/dist/utility/wrun/wrun-pinned-interval.util.js +47 -0
- package/dist/utility/wrun/wrun-present-bits.util.d.ts +37 -0
- package/dist/utility/wrun/wrun-present-bits.util.js +73 -0
- package/dist/utility/wrun/wrun-protocol-pending.util.d.ts +33 -0
- package/dist/utility/wrun/wrun-protocol-pending.util.js +57 -0
- package/dist/utility/wrun/wrun-refusal.util.d.ts +41 -1
- package/dist/utility/wrun/wrun-refusal.util.js +69 -2
- package/dist/utility/wrun/wrun-registry-errors.util.d.ts +1 -14
- package/dist/utility/wrun/wrun-registry-errors.util.js +0 -16
- package/dist/utility/wrun/wrun-row-source.d.ts +158 -0
- package/dist/utility/wrun/wrun-row-source.js +75 -0
- package/dist/utility/wrun/wrun-run-health.util.d.ts +155 -0
- package/dist/utility/wrun/wrun-run-health.util.js +273 -0
- package/dist/utility/wrun/wrun-socket-row-source.d.ts +439 -0
- package/dist/utility/wrun/wrun-socket-row-source.js +1689 -0
- package/dist/utility/wrun/wrun-socket-session-losses.util.d.ts +18 -0
- package/dist/utility/wrun/wrun-socket-session-losses.util.js +25 -0
- package/dist/utility/wrun/wrun-source-rows.util.d.ts +911 -0
- package/dist/utility/wrun/wrun-source-rows.util.js +2597 -0
- package/dist/utility/wrun/wrun-tape-source.util.d.ts +82 -0
- package/dist/utility/wrun/wrun-tape-source.util.js +151 -0
- package/dist/utility/wrun/wrun-tier-grid.util.d.ts +137 -0
- package/dist/utility/wrun/wrun-tier-grid.util.js +279 -0
- package/dist/utility/wrun/wrun-trades-lane.util.d.ts +52 -0
- package/dist/utility/wrun/wrun-trades-lane.util.js +58 -0
- package/dist/utility/wrun/wrun-volume-profile-lane.util.d.ts +41 -0
- package/dist/utility/wrun/wrun-volume-profile-lane.util.js +66 -0
- package/dist/utility/wrun-display-name.util.d.ts +33 -0
- package/dist/utility/wrun-display-name.util.js +64 -0
- package/dist/workers/constants/kscript-transport-codes.d.ts +15 -0
- package/dist/workers/constants/kscript-transport-codes.js +15 -0
- package/dist/workers/constants/socket-slots.d.ts +574 -0
- package/dist/workers/constants/socket-slots.js +539 -0
- package/dist/workers/utils/number.worker.util.d.ts +5 -0
- package/dist/workers/utils/number.worker.util.js +10 -0
- package/dist/workers/utils/time.worker.util.d.ts +12 -0
- package/dist/workers/utils/time.worker.util.js +24 -0
- package/dist/workers/utils/volume-profile.worker.util.d.ts +200 -0
- package/dist/workers/utils/volume-profile.worker.util.js +631 -0
- package/package.json +10 -4
|
@@ -0,0 +1,711 @@
|
|
|
1
|
+
import { poolAwareUppercase } from '../pool-symbol.leaf.js';
|
|
2
|
+
import isArray from 'lodash-es/isArray.js';
|
|
3
|
+
import isNil from 'lodash-es/isNil.js';
|
|
4
|
+
import { delimiter } from '../../constants/data-source.constants.js';
|
|
5
|
+
import { Intervals } from '../../constants/interval.constants.js';
|
|
6
|
+
import { checkIsEconomic } from '../chart/marketKind.util.js';
|
|
7
|
+
import { safeMathEval } from './safe-math-eval.js';
|
|
8
|
+
export const operatorBracketRegex = /[+\-*/()]/;
|
|
9
|
+
const singleOperatorBracketRegex = new RegExp('^' + operatorBracketRegex.source + '$');
|
|
10
|
+
export const operatorRegex = /[+\-*/]/;
|
|
11
|
+
export const bracketRegex = /[()]/;
|
|
12
|
+
export function normalizeTransformations(value) {
|
|
13
|
+
return value ? poolAwareUppercase(value).replace(/\s+/g, '') : '';
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Transform and combine data from multiple tokens using mathematical expressions
|
|
17
|
+
*
|
|
18
|
+
* @param transformations - Mathematical expression with tokens (e.g., "BINANCE|BTCUSDT + NYSE|NVDA")
|
|
19
|
+
* @param data - Array of {token, data} objects where data is OHLCV or spline format
|
|
20
|
+
* @param plan - For legs on different calendars (formulaCalendarPlan): the anchor whose bars are the timeline and
|
|
21
|
+
* the legs carried onto it. Without one, legs join on exact timestamps.
|
|
22
|
+
* @returns Transformed data array in same format as input (OHLCV or spline)
|
|
23
|
+
*/
|
|
24
|
+
export function transformData(transformations, data, plan) {
|
|
25
|
+
if (!data || data.length === 0) {
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
const firstDataRow = data.find((item) => Array.isArray(item?.data) && item.data.length > 0)?.data?.[0];
|
|
29
|
+
if (!firstDataRow || firstDataRow.length === 0) {
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
// Detect chart format: 6=OHLCV with volume, 5=OHLC, 3-4=multi-value spline, 2=simple spline
|
|
33
|
+
const chartLength = firstDataRow.length;
|
|
34
|
+
const transformationTokens = extractTokensFromTransformations(transformations);
|
|
35
|
+
const economicTokens = new Set(transformationTokens.filter((token) => checkIsEconomic(token.exchange)).map((token) => token.token));
|
|
36
|
+
const aligned = carryEconomicSeries(data, economicTokens);
|
|
37
|
+
const dataMap = generateDataMap(plan ? carryOntoAnchor(aligned, plan) : aligned);
|
|
38
|
+
// Hoisted out of the loop: re-deriving Object.keys per row made this O(n^2).
|
|
39
|
+
// Integer-like keys enumerate in ascending numeric order per spec.
|
|
40
|
+
const timestamps = Object.keys(dataMap);
|
|
41
|
+
const dataLength = timestamps.length;
|
|
42
|
+
// Early return if dataMap is empty
|
|
43
|
+
if (dataLength === 0) {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
const transformedData = [];
|
|
47
|
+
// Longest token first: a token that prefixes another (SPY in SPYD) must never rewrite it.
|
|
48
|
+
const substitutionOrder = [...transformationTokens].sort((a, b) => b.token.length - a.token.length);
|
|
49
|
+
const evaluateField = (timestampData, field) => {
|
|
50
|
+
let expression = transformations;
|
|
51
|
+
for (const { token } of substitutionOrder) {
|
|
52
|
+
expression = expression.replaceAll(token, timestampData[token]?.[field]?.toString() ?? '0');
|
|
53
|
+
}
|
|
54
|
+
return safeMathEval(expression);
|
|
55
|
+
};
|
|
56
|
+
for (let i = 0; i < dataLength; i++) {
|
|
57
|
+
const timestamp = Number(timestamps[i]);
|
|
58
|
+
const timestampData = dataMap[timestamp];
|
|
59
|
+
// Check if all required tokens have data at this timestamp
|
|
60
|
+
const missingTokens = transformationTokens.filter((token) => !timestampData[token.token]);
|
|
61
|
+
if (missingTokens.length > 0) {
|
|
62
|
+
continue; // Skip this timestamp
|
|
63
|
+
}
|
|
64
|
+
const transformedRow = [timestamp];
|
|
65
|
+
switch (chartLength) {
|
|
66
|
+
case 6: {
|
|
67
|
+
// Case for OHLCV candle type data [timestamp, open, high, low, close, volume]
|
|
68
|
+
for (let j = 1; j < 5; j++)
|
|
69
|
+
transformedRow.push(evaluateField(timestampData, j));
|
|
70
|
+
// The volume is the first token's own, never evaluated
|
|
71
|
+
transformedRow.push(timestampData[transformationTokens[0].token]?.[5] ?? 0);
|
|
72
|
+
if (!boundOhlcRow(transformedRow))
|
|
73
|
+
continue;
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
case 2: {
|
|
77
|
+
// case for spline data [timestamp, data]
|
|
78
|
+
transformedRow.push(evaluateField(timestampData, 1));
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
case 3: // case for spline [timestamp, data, data]
|
|
82
|
+
case 4: // case for Bybit LTV, Implied Volatility with 3 tenor
|
|
83
|
+
case 5: // case for OHLCV candle type data [timestamp, open, high, low, close]
|
|
84
|
+
for (let j = 1; j < chartLength; j++)
|
|
85
|
+
transformedRow.push(evaluateField(timestampData, j));
|
|
86
|
+
if (chartLength === 5 && !boundOhlcRow(transformedRow))
|
|
87
|
+
continue;
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
transformedData.push(transformedRow);
|
|
91
|
+
}
|
|
92
|
+
return transformedData;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* A formula evaluates open, high, low and close one field at a time, so a quotient's "high" (H1 / H2) is not the
|
|
96
|
+
* ratio's high: it can sit below its own open or low, and 1 / X swaps high and low outright. The wicks are bounded
|
|
97
|
+
* by the max and min of the four evaluated fields. False when a field is not finite (a zero denominator): the row
|
|
98
|
+
* is dropped rather than painted at infinity.
|
|
99
|
+
*/
|
|
100
|
+
function boundOhlcRow(row) {
|
|
101
|
+
const fields = row.slice(1, 5);
|
|
102
|
+
if (!fields.every(Number.isFinite))
|
|
103
|
+
return false;
|
|
104
|
+
row[2] = Math.max(...fields);
|
|
105
|
+
row[3] = Math.min(...fields);
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* An economic series (FRED, the catalog, a publisher) posts a value per release, not per bar, so a
|
|
110
|
+
* formula reads it as of each chart bar: the latest observation at or before the bar, the way
|
|
111
|
+
* TradingView evaluates BTC / M2. The exact-stamp join kept only the bars an observation shared
|
|
112
|
+
* (one bar a day on a 1h chart). The timeline is every other token's bars; a formula of economic
|
|
113
|
+
* series alone joins on the union of their own stamps. Bars before a series' first observation
|
|
114
|
+
* stay out.
|
|
115
|
+
*/
|
|
116
|
+
export function carryEconomicSeries(data, economicTokens) {
|
|
117
|
+
if (economicTokens.size === 0)
|
|
118
|
+
return data;
|
|
119
|
+
const others = data.filter((series) => !economicTokens.has(series.token) && series.data.length > 0);
|
|
120
|
+
const timeline = extractTimeline(others.length > 0 ? others : data);
|
|
121
|
+
if (timeline.length === 0)
|
|
122
|
+
return data;
|
|
123
|
+
return data.map((series) => economicTokens.has(series.token) ? { token: series.token, data: carryAsOf(series.data, timeline) } : series);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Legs on another calendar than the anchor (a stock next to BTC, a forex pair next to a stock) are read as of each
|
|
127
|
+
* anchor bar, the way TradingView evaluates BTCUSD / SPY: the anchor's bars are the timeline and every other market
|
|
128
|
+
* leg rides them at its last close. Anchor bars before a carried leg's first bar drop out in the join, and legs on the
|
|
129
|
+
* anchor's own calendar still join on exact stamps.
|
|
130
|
+
*/
|
|
131
|
+
function carryOntoAnchor(data, plan) {
|
|
132
|
+
const anchorRows = data.find((series) => series.token === plan.anchor)?.data ?? [];
|
|
133
|
+
const timeline = anchorRows.map((row) => row[0]).sort((a, b) => a - b);
|
|
134
|
+
if (timeline.length === 0)
|
|
135
|
+
return data;
|
|
136
|
+
return data.map((series) => plan.carried.has(series.token) ? { token: series.token, data: carryAsOf(series.data, timeline) } : series);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Each timeline stamp takes the latest row that opened at or before it: its own row on an exact stamp, else that
|
|
140
|
+
* row carried flat at its close. A carried candle's old high and low are not prices at the stamp and would draw
|
|
141
|
+
* false wicks, and its volume belongs to its own bar.
|
|
142
|
+
*/
|
|
143
|
+
function carryAsOf(rows, timeline) {
|
|
144
|
+
const sorted = rows.every((row, i) => i === 0 || rows[i - 1][0] <= row[0])
|
|
145
|
+
? rows
|
|
146
|
+
: [...rows].sort((a, b) => a[0] - b[0]);
|
|
147
|
+
const carried = [];
|
|
148
|
+
let next = 0;
|
|
149
|
+
let last;
|
|
150
|
+
for (const ts of timeline) {
|
|
151
|
+
while (next < sorted.length && sorted[next][0] <= ts)
|
|
152
|
+
last = sorted[next++];
|
|
153
|
+
if (!last)
|
|
154
|
+
continue;
|
|
155
|
+
carried.push(last[0] === ts ? last : carriedRow(ts, last));
|
|
156
|
+
}
|
|
157
|
+
return carried;
|
|
158
|
+
}
|
|
159
|
+
/** `row` read at a later stamp: candles (5 or 6 wide) flat at their close with no volume, other shapes as they are. */
|
|
160
|
+
function carriedRow(ts, row) {
|
|
161
|
+
const carried = [ts, ...row.slice(1)];
|
|
162
|
+
if (row.length < 5)
|
|
163
|
+
return carried;
|
|
164
|
+
carried.fill(row[4], 1, 5);
|
|
165
|
+
if (carried.length > 5)
|
|
166
|
+
carried[5] = 0;
|
|
167
|
+
return carried;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Generate the exact-stamp data map (legs on other calendars are carried onto the anchor first)
|
|
171
|
+
*
|
|
172
|
+
* Creates a timestamp-aligned data structure containing only timestamps where
|
|
173
|
+
* ALL tokens have data. This strict alignment ensures transformation expressions
|
|
174
|
+
* can safely evaluate without missing values.
|
|
175
|
+
*
|
|
176
|
+
* **Algorithm:**
|
|
177
|
+
* 1. Collect all unique timestamps across all tokens
|
|
178
|
+
* 2. Sort timestamps in ascending order
|
|
179
|
+
* 3. Populate data map with token values at each timestamp
|
|
180
|
+
* 4. Filter to include only timestamps where ALL tokens have values
|
|
181
|
+
*
|
|
182
|
+
* @param data - Array of {token, data} objects from fetched chart data
|
|
183
|
+
* @returns Data map keyed by timestamp, containing only complete rows
|
|
184
|
+
*/
|
|
185
|
+
function generateDataMap(data) {
|
|
186
|
+
const dataMap = {};
|
|
187
|
+
// Collect all unique timestamps using Set
|
|
188
|
+
const allTimestamps = new Set();
|
|
189
|
+
data.forEach((item) => {
|
|
190
|
+
item.data.forEach((entry) => {
|
|
191
|
+
allTimestamps.add(entry[0]);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
// Convert Set to Array and sort timestamps ascending
|
|
195
|
+
const sortedTimestamps = Array.from(allTimestamps).sort((a, b) => a - b);
|
|
196
|
+
// Initialize the dataMap with timestamps
|
|
197
|
+
sortedTimestamps.forEach((timestamp) => {
|
|
198
|
+
dataMap[timestamp] = {};
|
|
199
|
+
});
|
|
200
|
+
// Populate the dataMap with data from each token
|
|
201
|
+
data.forEach((item) => {
|
|
202
|
+
item.data.forEach((entry) => {
|
|
203
|
+
const timestamp = entry[0];
|
|
204
|
+
if (dataMap[timestamp]) {
|
|
205
|
+
dataMap[timestamp][item.token] = entry; // Exclude timestamp
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
// Filter out items that dont have all the tokens which means that they don't have matching timestamps
|
|
210
|
+
const filteredDataMap = {};
|
|
211
|
+
Object.keys(dataMap).forEach((timestamp) => {
|
|
212
|
+
const tokenData = dataMap[timestamp];
|
|
213
|
+
if (Object.keys(tokenData).length === data.length) {
|
|
214
|
+
filteredDataMap[timestamp] = tokenData;
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
return filteredDataMap;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Extract unique sorted timeline from data arrays
|
|
221
|
+
*
|
|
222
|
+
* Collects all timestamps across multiple token series and returns them
|
|
223
|
+
* in ascending chronological order with duplicates removed.
|
|
224
|
+
*
|
|
225
|
+
* **Usage:** the timeline an economic series is carried onto.
|
|
226
|
+
*
|
|
227
|
+
* @param dataItems - Array of {token, data} objects
|
|
228
|
+
* @returns Sorted array of unique timestamps in milliseconds
|
|
229
|
+
*/
|
|
230
|
+
function extractTimeline(dataItems) {
|
|
231
|
+
const timestamps = new Set();
|
|
232
|
+
dataItems.forEach((item) => {
|
|
233
|
+
item.data.forEach((bar) => {
|
|
234
|
+
timestamps.add(bar[0]);
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
return Array.from(timestamps).sort((a, b) => a - b);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* A formula's live price from each token's latest price. Only the formula's own tokens are
|
|
241
|
+
* substituted, longest first: the price map holds every token the session has seen, and a token
|
|
242
|
+
* that prefixes another (FRED|DGS1 in FRED|DGS10) must never rewrite it. NaN when a token has no
|
|
243
|
+
* price or the expression does not evaluate; the caller skips that tick rather than paint a raw
|
|
244
|
+
* price into the formula.
|
|
245
|
+
*/
|
|
246
|
+
export function transformLiveFeed(transformations, cachedPrice, tokens = extractTokensFromTransformations(transformations).map((token) => token.token)) {
|
|
247
|
+
let expression = transformations;
|
|
248
|
+
for (const token of [...tokens].sort((a, b) => b.length - a.length)) {
|
|
249
|
+
expression = expression.replaceAll(token, String(cachedPrice[token]));
|
|
250
|
+
}
|
|
251
|
+
const price = safeMathEval(expression);
|
|
252
|
+
return Number.isFinite(price) ? price : Number.NaN;
|
|
253
|
+
}
|
|
254
|
+
export function parsedTransformations(transformations) {
|
|
255
|
+
const formattedTransformations = [];
|
|
256
|
+
const tokens = transformations.trim().split(/\s+/);
|
|
257
|
+
let currentSymbolToken;
|
|
258
|
+
tokens.forEach((token) => {
|
|
259
|
+
if (!isNaN(Number(token))) {
|
|
260
|
+
// Convert numerical tokens to numbers
|
|
261
|
+
formattedTransformations.push(Number(token));
|
|
262
|
+
currentSymbolToken = undefined;
|
|
263
|
+
}
|
|
264
|
+
else if (token.includes(delimiter)) {
|
|
265
|
+
// Handle token with delimiter
|
|
266
|
+
const [exchange, symbol] = token.split(delimiter);
|
|
267
|
+
currentSymbolToken = { exchange, symbol };
|
|
268
|
+
formattedTransformations.push(currentSymbolToken);
|
|
269
|
+
}
|
|
270
|
+
else if (singleOperatorBracketRegex.test(token) || ['×', '÷', '−'].includes(token)) {
|
|
271
|
+
// Handle operators and parentheses
|
|
272
|
+
formattedTransformations.push(token);
|
|
273
|
+
currentSymbolToken = undefined;
|
|
274
|
+
}
|
|
275
|
+
else if (currentSymbolToken) {
|
|
276
|
+
currentSymbolToken.symbol += ` ${token}`;
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
return formattedTransformations;
|
|
280
|
+
}
|
|
281
|
+
export function extractTokensFromTransformations(transformations, allowDuplicates = false) {
|
|
282
|
+
if (!transformations)
|
|
283
|
+
return [];
|
|
284
|
+
const items = parsedTransformations(transformations);
|
|
285
|
+
let tokens = [];
|
|
286
|
+
for (let i = 0; i < items.length; i++) {
|
|
287
|
+
const item = items[i];
|
|
288
|
+
if (typeof item === 'object' && item !== null) {
|
|
289
|
+
const { exchange, symbol } = item;
|
|
290
|
+
const token = `${exchange}${delimiter}${symbol}`;
|
|
291
|
+
tokens.push({ exchange, symbol, token });
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (allowDuplicates)
|
|
295
|
+
return tokens;
|
|
296
|
+
// clean up tokens to ensure no duplicates
|
|
297
|
+
tokens = tokens.filter((token, index) => {
|
|
298
|
+
return index === tokens.findIndex((t) => t.exchange === token.exchange && t.symbol === token.symbol);
|
|
299
|
+
});
|
|
300
|
+
return tokens;
|
|
301
|
+
}
|
|
302
|
+
export function subscribeOverlayToLiveFeed(subscribeToEachLiveFeed, params, overlayId) {
|
|
303
|
+
const multiSymbolExchanges = params?.multiSymbolExchanges;
|
|
304
|
+
const tokens = isNil(multiSymbolExchanges) || multiSymbolExchanges.length === 0
|
|
305
|
+
? extractTokensFromTransformations(params.transformations)
|
|
306
|
+
: params.multiSymbolExchanges;
|
|
307
|
+
const dexFields = {
|
|
308
|
+
...(params.chain && { chain: params.chain }),
|
|
309
|
+
...(params.poolAddress && { poolAddress: params.poolAddress }),
|
|
310
|
+
...(params.baseTokenAddress && { baseTokenAddress: params.baseTokenAddress }),
|
|
311
|
+
...(params.quoteTokenAddress && { quoteTokenAddress: params.quoteTokenAddress }),
|
|
312
|
+
};
|
|
313
|
+
if (tokens.length === 0) {
|
|
314
|
+
subscribeToEachLiveFeed(overlayId, params);
|
|
315
|
+
}
|
|
316
|
+
else if (multiSymbolExchanges?.length >= 2) {
|
|
317
|
+
multiSymbolExchanges.forEach((exchangeObject) => {
|
|
318
|
+
subscribeToEachLiveFeed(overlayId, {
|
|
319
|
+
exchange: exchangeObject.exchange,
|
|
320
|
+
symbol: exchangeObject.symbol,
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
tokens.forEach((token) => {
|
|
326
|
+
const exchange = token.exchange || params.exchange;
|
|
327
|
+
const symbol = token.symbol || params.symbol;
|
|
328
|
+
subscribeToEachLiveFeed(overlayId, {
|
|
329
|
+
exchange: exchange,
|
|
330
|
+
symbol: symbol,
|
|
331
|
+
...dexFields,
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Teardown mirror of `subscribeOverlayToLiveFeed`. The live-feed ownership
|
|
338
|
+
* union clears an owner's channel records only when the unsubscribe rebuilds
|
|
339
|
+
* channels that deep-equal the subscribe-time records, so every branch here
|
|
340
|
+
* must hand `unsubscribeFromEachLiveFeed` the SAME per-leg params shape the
|
|
341
|
+
* matching subscribe branch handed `subscribeToEachLiveFeed`, derived from the
|
|
342
|
+
* SAME token source (multiSymbolExchanges when present, transformations
|
|
343
|
+
* otherwise). Any drift (a leg skipped, a field added or dropped) leaks the
|
|
344
|
+
* record and the wire subscription forever.
|
|
345
|
+
*/
|
|
346
|
+
export function unsubscribeOverlayFromLiveFeed(unsubscribeFromEachLiveFeed, overlay, params) {
|
|
347
|
+
const multiSymbolExchanges = params?.multiSymbolExchanges;
|
|
348
|
+
const tokens = isNil(multiSymbolExchanges) || multiSymbolExchanges.length === 0
|
|
349
|
+
? extractTokensFromTransformations(params.transformations)
|
|
350
|
+
: params.multiSymbolExchanges;
|
|
351
|
+
const dexFields = {
|
|
352
|
+
...(params.chain && { chain: params.chain }),
|
|
353
|
+
...(params.poolAddress && { poolAddress: params.poolAddress }),
|
|
354
|
+
...(params.baseTokenAddress && { baseTokenAddress: params.baseTokenAddress }),
|
|
355
|
+
...(params.quoteTokenAddress && { quoteTokenAddress: params.quoteTokenAddress }),
|
|
356
|
+
};
|
|
357
|
+
if (tokens.length === 0) {
|
|
358
|
+
// Subscribe's empty-token branch subscribed with the whole params object;
|
|
359
|
+
// the old forEach over zero tokens silently retracted NOTHING here.
|
|
360
|
+
unsubscribeFromEachLiveFeed(overlay, params);
|
|
361
|
+
}
|
|
362
|
+
else if (multiSymbolExchanges?.length >= 2) {
|
|
363
|
+
// Bare { exchange, symbol } legs on purpose: the subscribe-side multi-leg
|
|
364
|
+
// branch omits dexFields, so spreading them here would build channels that
|
|
365
|
+
// never isEqual the subscribed records.
|
|
366
|
+
multiSymbolExchanges.forEach((exchangeObject) => {
|
|
367
|
+
unsubscribeFromEachLiveFeed(overlay, {
|
|
368
|
+
exchange: exchangeObject.exchange,
|
|
369
|
+
symbol: exchangeObject.symbol,
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
tokens.forEach((token) => {
|
|
375
|
+
const exchange = token.exchange || params.exchange;
|
|
376
|
+
const symbol = token.symbol || params.symbol;
|
|
377
|
+
unsubscribeFromEachLiveFeed(overlay, {
|
|
378
|
+
exchange: exchange,
|
|
379
|
+
symbol: symbol,
|
|
380
|
+
...dexFields,
|
|
381
|
+
});
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
export function mergeAllLiveDataPoints(mergeDataPoint, lastDataPoint, newPoint, interval, dataStore, lastMergedData) {
|
|
386
|
+
if (!lastDataPoint)
|
|
387
|
+
return {
|
|
388
|
+
mergedDataPoint: [],
|
|
389
|
+
updatedDataStore: [],
|
|
390
|
+
updatedLastMergedData: [],
|
|
391
|
+
};
|
|
392
|
+
const { transformations } = newPoint.settings;
|
|
393
|
+
const items = extractTokensFromTransformations(transformations);
|
|
394
|
+
const newExchange = newPoint.series.exchange;
|
|
395
|
+
const newSymbol = newPoint.series.symbol;
|
|
396
|
+
const newToken = `${newExchange}${delimiter}${newSymbol}`;
|
|
397
|
+
// this has to allow duplicates because if it has no transformations, it will default to single token behaviour
|
|
398
|
+
const tokenCount = parsedTransformations(transformations).length;
|
|
399
|
+
if (tokenCount === 1) {
|
|
400
|
+
return {
|
|
401
|
+
mergedDataPoint: mergeDataPoint(lastDataPoint, newPoint, interval),
|
|
402
|
+
updatedDataStore: dataStore,
|
|
403
|
+
updatedLastMergedData: lastMergedData,
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
// Ensure all tokens are present in dataStore with default values
|
|
407
|
+
items.forEach((item) => {
|
|
408
|
+
const token = item.token;
|
|
409
|
+
if (!(token in dataStore)) {
|
|
410
|
+
dataStore[token] = [];
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
// Remove tokens from dataStore that are not in items
|
|
414
|
+
Object.keys(dataStore).forEach((token) => {
|
|
415
|
+
if (!items.some((item) => item.token === token)) {
|
|
416
|
+
delete dataStore[token];
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
const mergedData = mergeDataPoint(lastDataPoint, newPoint, interval);
|
|
420
|
+
dataStore[newToken] = mergedData; // Replace the data with the new merged data
|
|
421
|
+
// Check if all data is present in the dataStore
|
|
422
|
+
const hasAllData = Object.keys(dataStore).length !== 0 &&
|
|
423
|
+
Object.values(dataStore).every((item) => {
|
|
424
|
+
return item.length > 0; // Make sure to return the boolean value
|
|
425
|
+
});
|
|
426
|
+
const hasTimeStamp = mergedData[0] !== 0 && lastDataPoint[0] !== 0;
|
|
427
|
+
const sameTimestamp = hasTimeStamp ? mergedData[0] === lastDataPoint[0] : false;
|
|
428
|
+
const allDataHasTimestamp = Object.values(dataStore).every((item) => {
|
|
429
|
+
return item[0] === mergedData[0]; // Ensure to return the boolean value
|
|
430
|
+
});
|
|
431
|
+
let mergedDataPoint;
|
|
432
|
+
if (hasAllData && allDataHasTimestamp) {
|
|
433
|
+
mergedDataPoint = transformMergedData(transformations, dataStore, lastDataPoint, sameTimestamp);
|
|
434
|
+
lastMergedData = mergedDataPoint;
|
|
435
|
+
}
|
|
436
|
+
else {
|
|
437
|
+
mergedDataPoint = lastDataPoint;
|
|
438
|
+
}
|
|
439
|
+
if (isArray(mergedDataPoint?.[0]) && mergedDataPoint?.[0]?.length) {
|
|
440
|
+
mergedDataPoint = mergedDataPoint[0];
|
|
441
|
+
}
|
|
442
|
+
return {
|
|
443
|
+
mergedDataPoint: mergedDataPoint,
|
|
444
|
+
updatedDataStore: dataStore,
|
|
445
|
+
updatedLastMergedData: lastMergedData,
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
function transformMergedData(transformations, ovData, lastDataPoint, sameTimeStamp = false) {
|
|
449
|
+
const transformationTokens = extractTokensFromTransformations(transformations);
|
|
450
|
+
if (!ovData[transformationTokens[0].token])
|
|
451
|
+
return;
|
|
452
|
+
const timestamp = ovData[transformationTokens[0].token][0];
|
|
453
|
+
const chartLength = ovData[transformationTokens[0].token].length;
|
|
454
|
+
let transformedData = [timestamp];
|
|
455
|
+
let expression = transformations;
|
|
456
|
+
switch (chartLength) {
|
|
457
|
+
case 5: {
|
|
458
|
+
for (let j = 1; j < chartLength; j++) {
|
|
459
|
+
let expression = transformations;
|
|
460
|
+
transformationTokens.forEach((token) => {
|
|
461
|
+
const tokenString = token.token.toString();
|
|
462
|
+
if (ovData[tokenString] === undefined)
|
|
463
|
+
return;
|
|
464
|
+
const tokenData = ovData[tokenString][j];
|
|
465
|
+
expression = expression.replaceAll(tokenString, tokenData.toString());
|
|
466
|
+
});
|
|
467
|
+
const result = safeMathEval(expression);
|
|
468
|
+
transformedData.push(result);
|
|
469
|
+
}
|
|
470
|
+
if (sameTimeStamp) {
|
|
471
|
+
if (!lastDataPoint.length) {
|
|
472
|
+
transformedData = [timestamp, transformedData[4], transformedData[4], transformedData[4], transformedData[4]];
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
// update to latest close price
|
|
476
|
+
transformedData = [
|
|
477
|
+
timestamp,
|
|
478
|
+
lastDataPoint[1],
|
|
479
|
+
Math.max(transformedData[4], lastDataPoint[2]), // high
|
|
480
|
+
Math.min(transformedData[4], lastDataPoint[3]), // low
|
|
481
|
+
transformedData[4], // close
|
|
482
|
+
];
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
// A new bar's fields were evaluated one at a time: bound its wicks like the history rows.
|
|
487
|
+
boundOhlcRow(transformedData);
|
|
488
|
+
}
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
case 3: {
|
|
492
|
+
for (let j = 1; j < chartLength; j++) {
|
|
493
|
+
let expression = transformations;
|
|
494
|
+
transformationTokens.forEach((token) => {
|
|
495
|
+
const tokenString = token.token.toString();
|
|
496
|
+
if (ovData[tokenString] === undefined)
|
|
497
|
+
return;
|
|
498
|
+
const tokenData = ovData[tokenString][j];
|
|
499
|
+
expression = expression.replaceAll(tokenString, tokenData.toString());
|
|
500
|
+
});
|
|
501
|
+
const result = safeMathEval(expression);
|
|
502
|
+
transformedData.push(result);
|
|
503
|
+
}
|
|
504
|
+
break;
|
|
505
|
+
}
|
|
506
|
+
case 2: {
|
|
507
|
+
transformationTokens.forEach((token) => {
|
|
508
|
+
const tokenString = token.token.toString();
|
|
509
|
+
if (ovData[tokenString] === undefined)
|
|
510
|
+
return;
|
|
511
|
+
const tokenData = ovData[tokenString][1];
|
|
512
|
+
expression = expression.replaceAll(tokenString, tokenData.toString());
|
|
513
|
+
});
|
|
514
|
+
const result = safeMathEval(expression);
|
|
515
|
+
transformedData.push(result);
|
|
516
|
+
break;
|
|
517
|
+
}
|
|
518
|
+
default:
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
return transformedData;
|
|
522
|
+
}
|
|
523
|
+
const VALID_NUMBER_RE = /^(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/;
|
|
524
|
+
const VALID_EXCHANGE_RE = /^[a-zA-Z0-9_.]+$/;
|
|
525
|
+
const VALID_SYMBOL_RE = /^[a-zA-Z0-9_.\-:/]+$/;
|
|
526
|
+
// StreamPoints CME option raw symbols are `<future> <C|P><strike>` (for
|
|
527
|
+
// example `E4DU6 C7740`). The space is part of the symbol, not a formula
|
|
528
|
+
// separator. Keep this deliberately narrower than VALID_SYMBOL_RE: arbitrary
|
|
529
|
+
// words after an exchange|symbol token must not become trusted expression
|
|
530
|
+
// input merely because the semantic parser can display them.
|
|
531
|
+
const VALID_SPACED_OPTION_LEG_RE = /^[CP]\d+(?:\.\d+)?$/i;
|
|
532
|
+
/** Returns true if the token is a valid component of a transformation expression */
|
|
533
|
+
function isValidTransformationToken(token) {
|
|
534
|
+
// Single operator or parenthesis: +, -, *, /, (, )
|
|
535
|
+
if (singleOperatorBracketRegex.test(token))
|
|
536
|
+
return true;
|
|
537
|
+
// Numbers including scientific notation (e.g., 2, 3.14, 1e5)
|
|
538
|
+
if (VALID_NUMBER_RE.test(token))
|
|
539
|
+
return true;
|
|
540
|
+
// Exchange|Symbol tokens (symbol may contain hyphens, dots, underscores)
|
|
541
|
+
if (token.includes(delimiter)) {
|
|
542
|
+
const delimIndex = token.indexOf(delimiter);
|
|
543
|
+
const exchange = token.substring(0, delimIndex);
|
|
544
|
+
const symbol = token.substring(delimIndex + 1);
|
|
545
|
+
return VALID_EXCHANGE_RE.test(exchange) && VALID_SYMBOL_RE.test(symbol);
|
|
546
|
+
}
|
|
547
|
+
return false;
|
|
548
|
+
}
|
|
549
|
+
/** Remove adjacent empty `()` pairs, repeating until none remain (handles nesting/chaining) */
|
|
550
|
+
function collapseEmptyParens(tokens) {
|
|
551
|
+
let changed = true;
|
|
552
|
+
while (changed) {
|
|
553
|
+
changed = false;
|
|
554
|
+
for (let i = 0; i < tokens.length - 1; i++) {
|
|
555
|
+
if (tokens[i] === '(' && tokens[i + 1] === ')') {
|
|
556
|
+
tokens.splice(i, 2);
|
|
557
|
+
changed = true;
|
|
558
|
+
break;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Sanitize a transformation string by removing any tokens that are not valid
|
|
565
|
+
* operators, numbers, or exchange|symbol references. Prevents code injection
|
|
566
|
+
* via malicious transformation strings from workspace imports or API responses.
|
|
567
|
+
*/
|
|
568
|
+
/** The transformations string a plain (non-formula) chart carries: its own
|
|
569
|
+
* exchange|symbol as a single token. */
|
|
570
|
+
export function selfTransformationToken(exchange, symbol) {
|
|
571
|
+
return `${exchange}${delimiter}${symbol}`;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Heal a workspace whose base `symbol`/`exchange` disagree with a SINGLE-token
|
|
575
|
+
* `transformations` string. Everything visible (title, symbol selector, the
|
|
576
|
+
* main data fetch) follows the token, while indicator creation, the kScript
|
|
577
|
+
* source identity, and the OHLCV cache key off the base fields; a desynced
|
|
578
|
+
* pair therefore renders price-space indicators in the wrong price domain
|
|
579
|
+
* (levels from symbol A smeared against symbol B's scale).
|
|
580
|
+
*
|
|
581
|
+
* The desync enters when a symbol switch is interrupted between the early
|
|
582
|
+
* exchange/symbol stamp and the transformations write (updateSymbolExchange
|
|
583
|
+
* now stamps the trio atomically, but documents persisted by older builds
|
|
584
|
+
* stay broken until opened). The token wins here because it is what the user
|
|
585
|
+
* has been seeing and charting against.
|
|
586
|
+
*
|
|
587
|
+
* Conservatively skipped for special markets whose token domain legitimately
|
|
588
|
+
* differs from the base fields (Polymarket condition-id tokens, DEX pools
|
|
589
|
+
* whose chain/pool metadata must move with the symbol, FRED series): those
|
|
590
|
+
* only get a console.warn so the inconsistency is at least visible.
|
|
591
|
+
*
|
|
592
|
+
* Returns true when the base fields were changed (caller should persist).
|
|
593
|
+
*/
|
|
594
|
+
export function reconcileSymbolWithTransformations(meta) {
|
|
595
|
+
const t = meta?.transformations;
|
|
596
|
+
if (!t || typeof t !== 'string')
|
|
597
|
+
return false;
|
|
598
|
+
if (t === selfTransformationToken(meta.exchange, meta.symbol))
|
|
599
|
+
return false;
|
|
600
|
+
if (parsedTransformations(t).length !== 1)
|
|
601
|
+
return false;
|
|
602
|
+
const tokens = extractTokensFromTransformations(t);
|
|
603
|
+
if (tokens.length !== 1)
|
|
604
|
+
return false;
|
|
605
|
+
const { exchange, symbol } = tokens[0];
|
|
606
|
+
if (!exchange || !symbol)
|
|
607
|
+
return false;
|
|
608
|
+
if (meta.exchange === exchange && meta.symbol === symbol)
|
|
609
|
+
return false;
|
|
610
|
+
const special = meta.polymarketMetadata != null ||
|
|
611
|
+
meta.fredMetadata != null ||
|
|
612
|
+
meta.isFredChart === true ||
|
|
613
|
+
meta.chain != null ||
|
|
614
|
+
meta.poolAddress != null ||
|
|
615
|
+
exchange === 'POLYMARKET' ||
|
|
616
|
+
meta.exchange === 'POLYMARKET' ||
|
|
617
|
+
checkIsEconomic(exchange) ||
|
|
618
|
+
checkIsEconomic(meta.exchange);
|
|
619
|
+
if (special) {
|
|
620
|
+
// eslint-disable-next-line no-console
|
|
621
|
+
console.warn(`[workspace] symbol/transformations desync on a special market left untouched: base='${meta.exchange}${delimiter}${meta.symbol}' token='${t}'`);
|
|
622
|
+
return false;
|
|
623
|
+
}
|
|
624
|
+
// eslint-disable-next-line no-console
|
|
625
|
+
console.warn(`[workspace] healing symbol/transformations desync: base '${meta.exchange}${delimiter}${meta.symbol}' -> token '${t}'`);
|
|
626
|
+
meta.exchange = exchange;
|
|
627
|
+
meta.symbol = symbol;
|
|
628
|
+
return true;
|
|
629
|
+
}
|
|
630
|
+
export function sanitizeTransformations(transformations) {
|
|
631
|
+
// Pad parentheses with spaces to isolate them as tokens, preserving hyphens in symbols
|
|
632
|
+
const padded = transformations.replace(/([()])/g, ' $1 ');
|
|
633
|
+
const tokens = padded.split(/\s+/).filter(Boolean);
|
|
634
|
+
// Keep valid expression tokens. A CME option leg immediately following an
|
|
635
|
+
// exchange|future token belongs to that same raw symbol, so rejoin it before
|
|
636
|
+
// hydration reconciliation compares transformations with metadata.symbol.
|
|
637
|
+
// Invalid input breaks adjacency and therefore cannot smuggle a later leg
|
|
638
|
+
// into an otherwise valid token.
|
|
639
|
+
const cleaned = [];
|
|
640
|
+
let optionParentIndex = null;
|
|
641
|
+
for (const token of tokens) {
|
|
642
|
+
if (isValidTransformationToken(token)) {
|
|
643
|
+
cleaned.push(token);
|
|
644
|
+
optionParentIndex = token.includes(delimiter) ? cleaned.length - 1 : null;
|
|
645
|
+
continue;
|
|
646
|
+
}
|
|
647
|
+
if (optionParentIndex !== null && VALID_SPACED_OPTION_LEG_RE.test(token)) {
|
|
648
|
+
cleaned[optionParentIndex] += ` ${token}`;
|
|
649
|
+
optionParentIndex = null;
|
|
650
|
+
continue;
|
|
651
|
+
}
|
|
652
|
+
optionParentIndex = null;
|
|
653
|
+
}
|
|
654
|
+
// Remove empty () pairs left after stripping injection content
|
|
655
|
+
collapseEmptyParens(cleaned);
|
|
656
|
+
// Must have at least one exchange|symbol token to be a valid transformation
|
|
657
|
+
if (!cleaned.some((t) => t.includes(delimiter)))
|
|
658
|
+
return '';
|
|
659
|
+
let result = cleaned.join(' ');
|
|
660
|
+
// Strip leading/trailing operators and normalize whitespace
|
|
661
|
+
result = result.replace(/^\s*[+\-*/]+\s*/, '').replace(/\s*[+\-*/]+\s*$/, '');
|
|
662
|
+
return result.replace(/\s{2,}/g, ' ').trim();
|
|
663
|
+
}
|
|
664
|
+
export function processDisplayExchangeSymbol(exchange, symbol) {
|
|
665
|
+
const instrumentList = {
|
|
666
|
+
FUTURES: '.F',
|
|
667
|
+
DERIVATIVES: '',
|
|
668
|
+
SPOT: '',
|
|
669
|
+
SWAP: '',
|
|
670
|
+
OPTIONS: '.O',
|
|
671
|
+
DELIVERY: '',
|
|
672
|
+
};
|
|
673
|
+
if (exchange.length === 0 && symbol.length === 0) {
|
|
674
|
+
return '';
|
|
675
|
+
}
|
|
676
|
+
let processedExchange = exchange;
|
|
677
|
+
for (const key of Object.keys(instrumentList)) {
|
|
678
|
+
if (exchange.includes(key)) {
|
|
679
|
+
processedExchange = exchange.replace('_' + key, instrumentList[key]);
|
|
680
|
+
break;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
const processedSymbol = symbol.includes('-') ? symbol.replace('-', '_') : symbol;
|
|
684
|
+
return `${processedExchange}${delimiter}${processedSymbol}`;
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Align an economic-series timestamp to the nearest calendar-interval boundary
|
|
688
|
+
* (1M / 3M / 1Y): the nearest multiple of the interval value. Finer intervals
|
|
689
|
+
* pass through untouched. Timestamps before 1970 are negative (C5); the
|
|
690
|
+
* remainder is folded into [0, intervalMs) so they round to the same grid.
|
|
691
|
+
*/
|
|
692
|
+
export function alignFredTimestamp(timestamp, interval) {
|
|
693
|
+
const intervalMs = Intervals.map_unit[interval];
|
|
694
|
+
if (!intervalMs) {
|
|
695
|
+
return timestamp;
|
|
696
|
+
}
|
|
697
|
+
if (intervalMs < Intervals.MONTH) {
|
|
698
|
+
return timestamp;
|
|
699
|
+
}
|
|
700
|
+
// Round to the nearest interval boundary
|
|
701
|
+
// If timestamp is exactly halfway, round up
|
|
702
|
+
const remainder = ((timestamp % intervalMs) + intervalMs) % intervalMs;
|
|
703
|
+
if (remainder < intervalMs / 2) {
|
|
704
|
+
// Round down
|
|
705
|
+
return timestamp - remainder;
|
|
706
|
+
}
|
|
707
|
+
else {
|
|
708
|
+
// Round up
|
|
709
|
+
return timestamp + (intervalMs - remainder);
|
|
710
|
+
}
|
|
711
|
+
}
|