From 2c13099a2c5aa273f6278b219220f484238cd6fc Mon Sep 17 00:00:00 2001 From: darkeye Date: Sat, 11 Jan 2025 11:47:21 +0100 Subject: [PATCH] [#20] AssetDataAggregator erstellt und integriert --- .../data/asset/AggregatedDataPoint.js | 8 ++-- src/js/tradingbot/data/asset/AssetData.js | 20 +++++++++ .../data/asset/AssetDataAggregator.js | 44 +++++++++++++++++-- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/js/tradingbot/data/asset/AggregatedDataPoint.js b/src/js/tradingbot/data/asset/AggregatedDataPoint.js index 80c7134..eb223bf 100644 --- a/src/js/tradingbot/data/asset/AggregatedDataPoint.js +++ b/src/js/tradingbot/data/asset/AggregatedDataPoint.js @@ -119,11 +119,13 @@ export default class AggregatedDataPoint { } /** - * @param {TradeEvent} trade + * @param {number} time + * @param {number} price + * @param {number} quantity */ - pushTrade(trade){ + pushTrade(time, price, quantity){ this.tradeCount ++; - this.tradeVolume += trade.quantity; + this.tradeVolume += quantity; } /** diff --git a/src/js/tradingbot/data/asset/AssetData.js b/src/js/tradingbot/data/asset/AssetData.js index 653113a..68e3a8c 100644 --- a/src/js/tradingbot/data/asset/AssetData.js +++ b/src/js/tradingbot/data/asset/AssetData.js @@ -4,6 +4,7 @@ import OrderBookUpdateEvent from "../../../apiwrapper/event/OrderBookUpdateEvent import Ticker24hEvent from "../../../apiwrapper/event/Ticker24hEvent.js"; import OrderBook from "../../../apiwrapper/orderbook/OrderBook.js"; import OrderBookEntry from "../../../apiwrapper/orderbook/OrderBookEntry.js"; +import AssetDataAggregator from "./AssetDataAggregator.js"; import AssetDataCollectionConfig from "./AssetDataCollectionConfig.js"; import PeakDetector from "./PeakDetector.js"; import TimedAssetData from "./TimedAssetData.js"; @@ -69,6 +70,11 @@ export default class AssetData extends TimedAssetData { */ #peakDetector = new PeakDetector(); + /** + * @type {AssetDataAggregator} + */ + assetDataAggregator = new AssetDataAggregator(); + /** * @param {TradingPair} tradingPair * @param {AssetDataCollectionConfig} collectionConfig @@ -98,6 +104,7 @@ export default class AssetData extends TimedAssetData { */ updateKline(klineEvent) { this.push(klineEvent.high, klineEvent.low, klineEvent.open, klineEvent.close); + this.assetDataAggregator.pushKLine(klineEvent.timestamp, klineEvent.low, klineEvent.high, klineEvent.open, klineEvent.close); } /** @@ -108,6 +115,8 @@ export default class AssetData extends TimedAssetData { this.pushBestBid(this.#orderBook.getBestBid()); this.pushBestAsk(this.#orderBook.getBestAsk()); this.#peakDetector.push(this.#orderBook.getBestAsk()); + this.assetDataAggregator.pushBestAsk(this.#orderBook.getBestAsk()); + this.assetDataAggregator.pushBestBid(this.#orderBook.getBestBid()); } /** @@ -187,4 +196,15 @@ export default class AssetData extends TimedAssetData { slots[i].removeSubData(); } } + + /** + * @param {number} time + * @param {number} price + * @param {number} quantity + */ + pushTrade(time, price, quantity) { + super.pushTrade(time, price, quantity); + this.assetDataAggregator.pushTrade(time, price, quantity); + } + } \ No newline at end of file diff --git a/src/js/tradingbot/data/asset/AssetDataAggregator.js b/src/js/tradingbot/data/asset/AssetDataAggregator.js index a33fed1..bbdc2a7 100644 --- a/src/js/tradingbot/data/asset/AssetDataAggregator.js +++ b/src/js/tradingbot/data/asset/AssetDataAggregator.js @@ -1,4 +1,4 @@ -import TradeEvent from "../../../apiwrapper/event/TradeEvent.js"; +import OrderBookEntry from "../../../apiwrapper/orderbook/OrderBookEntry.js"; import UnitHelper from "../../util/UnitHelper.js"; import AggregatedDataPoint from "./AggregatedDataPoint.js"; @@ -123,8 +123,17 @@ export default class AssetDataAggregator { const durationInMs = UnitHelper.formatedDurationToMs(duration); // if seconds data required - if(duration <= 60000 && start == -1){ - + if(durationInMs <= 60000 && start == -1){ + const dpCount = Math.round(durationInMs / AssetDataAggregator.#SLOT_1_SEC); + if(dpCount <= this.#perSecondDataPoints.length){ + result = this.#perSecondDataPoints.slice(-1 * dpCount); + } + } else { + const dpCount = Math.round(durationInMs / AssetDataAggregator.#SLOT_1_MIN); + const pointOffset = start == -1 ? 0 : Math.round(start / AssetDataAggregator.#SLOT_1_MIN); + if(pointOffset + dpCount <= this.#perMinuteDataPoints.length){ + result = this.#perMinuteDataPoints.slice(-1 * (pointOffset + dpCount), -1 * pointOffset); + } } return result; @@ -137,7 +146,34 @@ export default class AssetDataAggregator { */ pushTrade(time, price, quantity) { this.#updateCurrentDataPoint(); - this.#currentDataPoint.pushTrade(/* TODO */); + this.#currentDataPoint.pushTrade(time, price, quantity); } + /** + * @param {number} time + * @param {number} low + * @param {number} high + * @param {number} open + * @param {number} close + */ + pushKLine(time, low, high, open, close){ + // ingore actual data and just update the point, as data is taken only from orderbook + this.#updateCurrentDataPoint(); + } + + /** + * @param {OrderBookEntry} ask + */ + pushBestAsk(ask){ + this.#updateCurrentDataPoint(); + this.#currentDataPoint.pushAsk(ask); + } + + /** + * @param {OrderBookEntry} bid + */ + pushBestBid(bid){ + this.#updateCurrentDataPoint(); + this.#currentDataPoint.pushBid(bid); + } } \ No newline at end of file