-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(bonsai-core): candles #1433
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e08069a
candles
tyleroooo 82885bd
fix
tyleroooo 3a548d2
fix
tyleroooo 7f144e8
fix
tyleroooo 91d6b1b
fix
tyleroooo 980c942
fix
tyleroooo 1a9c04d
Merge remote-tracking branch 'origin/main' into tu/candles-bonsai
tyleroooo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { orderBy } from 'lodash'; | ||
|
||
import { isWsCandlesResponse, isWsCandlesUpdateResponse } from '@/types/indexer/indexerChecks'; | ||
import { IndexerWsCandleResponse } from '@/types/indexer/indexerManual'; | ||
|
||
import { Loadable, loadableLoaded, loadablePending } from '../lib/loadable'; | ||
import { logAbacusTsError } from '../logs'; | ||
import { makeWsValueManager } from './lib/indexerValueManagerHelpers'; | ||
import { IndexerWebsocket } from './lib/indexerWebsocket'; | ||
import { WebsocketDerivedValue } from './lib/websocketDerivedValue'; | ||
|
||
function candlesWebsocketValueCreator( | ||
websocket: IndexerWebsocket, | ||
{ marketIdAndResolution }: { marketIdAndResolution: string } | ||
) { | ||
return new WebsocketDerivedValue<Loadable<IndexerWsCandleResponse>>( | ||
websocket, | ||
{ | ||
channel: 'v4_candles', | ||
id: marketIdAndResolution, | ||
handleBaseData: (baseMessage) => { | ||
const message = isWsCandlesResponse(baseMessage); | ||
return loadableLoaded({ | ||
candles: orderBy(message.candles, [(a) => a.startedAt], ['asc']), | ||
}); | ||
}, | ||
handleUpdates: (baseUpdates, value) => { | ||
const updates = isWsCandlesUpdateResponse(baseUpdates); | ||
const startingValue = value.data; | ||
if (startingValue == null) { | ||
logAbacusTsError('CandlesTracker', 'found unexpectedly null base data in update'); | ||
return value; | ||
} | ||
if (startingValue.candles.length === 0) { | ||
return loadableLoaded({ candles: updates }); | ||
} | ||
|
||
const allNewTimes = new Set(updates.map(({ startedAt }) => startedAt)); | ||
const newArr = [ | ||
...updates, | ||
...startingValue.candles.filter(({ startedAt }) => !allNewTimes.has(startedAt)), | ||
]; | ||
const sorted = orderBy(newArr, [(a) => a.startedAt], ['asc']); | ||
return loadableLoaded({ candles: sorted }); | ||
}, | ||
}, | ||
loadablePending() | ||
); | ||
} | ||
|
||
export const CandlesValuesManager = makeWsValueManager(candlesWebsocketValueCreator); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { createStoreEffect } from '@/abacus-ts/lib/createStoreEffect'; | ||
import { selectWebsocketUrl } from '@/abacus-ts/socketSelectors'; | ||
import { CandlesValuesManager } from '@/abacus-ts/websocket/candles'; | ||
import { subscribeToWsValue } from '@/abacus-ts/websocket/lib/indexerValueManagerHelpers'; | ||
import type { | ||
LibrarySymbolInfo, | ||
ResolutionString, | ||
SubscribeBarsCallback, | ||
} from 'public/tradingview/charting_library'; | ||
|
||
import { CandleResolution, RESOLUTION_MAP } from '@/constants/candles'; | ||
|
||
import { type RootStore } from '@/state/_store'; | ||
|
||
import { mapCandle } from '../../lib/tradingView/utils'; | ||
|
||
export const subscriptionsByGuid: { | ||
[guid: string]: | ||
| { | ||
guid: string; | ||
unsub: () => void; | ||
} | ||
| undefined; | ||
} = {}; | ||
|
||
export const subscribeOnStream = ({ | ||
store, | ||
orderbookCandlesToggleOn, | ||
symbolInfo, | ||
resolution, | ||
onRealtimeCallback, | ||
listenerGuid, | ||
onResetCacheNeededCallback, | ||
}: { | ||
store: RootStore; | ||
orderbookCandlesToggleOn: boolean; | ||
symbolInfo: LibrarySymbolInfo; | ||
resolution: ResolutionString; | ||
onRealtimeCallback: SubscribeBarsCallback; | ||
listenerGuid: string; | ||
onResetCacheNeededCallback: Function; | ||
}) => { | ||
if (!symbolInfo.ticker) return; | ||
|
||
const channelId = `${symbolInfo.ticker}/${RESOLUTION_MAP[resolution]}`; | ||
let isFirstRun = true; | ||
|
||
const tearDown = createStoreEffect(store, selectWebsocketUrl, (wsUrl) => { | ||
// if the websocket url changes, force a refresh | ||
if (isFirstRun) { | ||
isFirstRun = false; | ||
} else { | ||
setTimeout(() => onResetCacheNeededCallback(), 0); | ||
} | ||
|
||
let mostRecentFirstPointStartedAt: string | undefined; | ||
const unsub = subscribeToWsValue( | ||
CandlesValuesManager, | ||
{ wsUrl, marketIdAndResolution: channelId }, | ||
({ data }) => { | ||
if (data == null || data.candles.length === 0) { | ||
return; | ||
} | ||
// if we've never seen data before, it's either the existing data or the subscribed message | ||
// either way, we take it as the basis and only send further updates | ||
// there is a small race condition where messages could be missed between | ||
// when trandingview does the rest query and when we start receiving updates | ||
if (mostRecentFirstPointStartedAt == null) { | ||
mostRecentFirstPointStartedAt = data.candles.at(-1)?.startedAt; | ||
return; | ||
} | ||
data.candles.forEach((candle) => { | ||
if (candle.startedAt >= mostRecentFirstPointStartedAt!) { | ||
onRealtimeCallback( | ||
mapCandle(orderbookCandlesToggleOn)({ | ||
...candle, | ||
resolution: candle.resolution as unknown as CandleResolution, | ||
orderbookMidPriceClose: candle.orderbookMidPriceClose ?? undefined, | ||
orderbookMidPriceOpen: candle.orderbookMidPriceOpen ?? undefined, | ||
}) | ||
); | ||
} | ||
}); | ||
mostRecentFirstPointStartedAt = data.candles.at(-1)?.startedAt; | ||
} | ||
); | ||
|
||
// happens on network change or unsubscribe from stream | ||
return () => { | ||
unsub(); | ||
}; | ||
}); | ||
|
||
subscriptionsByGuid[listenerGuid] = { guid: listenerGuid, unsub: tearDown }; | ||
}; | ||
|
||
export const unsubscribeFromStream = (subscriberUID: string) => { | ||
subscriptionsByGuid[subscriberUID]?.unsub(); | ||
subscriptionsByGuid[subscriberUID] = undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this value trigger an immediate refresh of the current candle?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a statsig feature flag now so can't change during runtime (I hope).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
by gawd you're right. i remember we used to expose a toggle