@@ -51,6 +51,48 @@ export default function PositionTable({
5151 const { config } = useConfig ( ) ;
5252 const { formatPrice, formatQuantity, formatPriceWithCommas } = useSymbolPrecision ( ) ;
5353
54+ // Initial load of VWAP data (fallback for when WebSocket is not yet connected)
55+ const loadVWAPData = useCallback ( async ( ) => {
56+ try {
57+ // Only fetch for symbols with VWAP protection enabled
58+ const symbolsWithVWAP = Object . entries ( config ?. symbols || { } )
59+ . filter ( ( [ _ , cfg ] ) => cfg . vwapProtection )
60+ . map ( ( [ symbol ] ) => symbol ) ;
61+
62+ if ( symbolsWithVWAP . length === 0 ) {
63+ console . log ( 'No symbols with VWAP protection enabled' ) ;
64+ return ;
65+ }
66+
67+ console . log ( 'Initial VWAP load for symbols:' , symbolsWithVWAP ) ;
68+
69+ const vwapPromises = symbolsWithVWAP . map ( async ( symbol ) => {
70+ try {
71+ const response = await fetch ( `/api/vwap/${ symbol } ` ) ;
72+ if ( response . ok ) {
73+ const data = await response . json ( ) ;
74+ return { symbol, data } ;
75+ }
76+ } catch ( error ) {
77+ console . error ( `Failed to load initial VWAP for ${ symbol } :` , error ) ;
78+ }
79+ return null ;
80+ } ) ;
81+
82+ const results = await Promise . all ( vwapPromises ) ;
83+ const vwapMap : Record < string , VWAPData > = { } ;
84+ results . forEach ( result => {
85+ if ( result ) {
86+ vwapMap [ result . symbol ] = result . data ;
87+ }
88+ } ) ;
89+ console . log ( 'Final VWAP map:' , vwapMap ) ;
90+ setVwapData ( vwapMap ) ;
91+ } catch ( error ) {
92+ console . error ( 'Failed to load VWAP data:' , error ) ;
93+ }
94+ } , [ config ?. symbols ] ) ;
95+
5496 // Load initial positions and set up WebSocket updates
5597 useEffect ( ( ) => {
5698 // Use data store if no positions passed as props
@@ -170,49 +212,8 @@ export default function PositionTable({
170212 cleanupVwap ( ) ;
171213 } ;
172214 }
173- } , [ positions . length ] ) ; // Only re-run when positions prop changes
215+ } , [ positions . length , loadVWAPData ] ) ; // Include loadVWAPData dependency
174216
175- // Initial load of VWAP data (fallback for when WebSocket is not yet connected)
176- const loadVWAPData = useCallback ( async ( ) => {
177- try {
178- // Only fetch for symbols with VWAP protection enabled
179- const symbolsWithVWAP = Object . entries ( config ?. symbols || { } )
180- . filter ( ( [ _ , cfg ] ) => cfg . vwapProtection )
181- . map ( ( [ symbol ] ) => symbol ) ;
182-
183- if ( symbolsWithVWAP . length === 0 ) {
184- console . log ( 'No symbols with VWAP protection enabled' ) ;
185- return ;
186- }
187-
188- console . log ( 'Initial VWAP load for symbols:' , symbolsWithVWAP ) ;
189-
190- const vwapPromises = symbolsWithVWAP . map ( async ( symbol ) => {
191- try {
192- const response = await fetch ( `/api/vwap/${ symbol } ` ) ;
193- if ( response . ok ) {
194- const data = await response . json ( ) ;
195- return { symbol, data } ;
196- }
197- } catch ( error ) {
198- console . error ( `Failed to load initial VWAP for ${ symbol } :` , error ) ;
199- }
200- return null ;
201- } ) ;
202-
203- const results = await Promise . all ( vwapPromises ) ;
204- const vwapMap : Record < string , VWAPData > = { } ;
205- results . forEach ( result => {
206- if ( result ) {
207- vwapMap [ result . symbol ] = result . data ;
208- }
209- } ) ;
210- console . log ( 'Final VWAP map:' , vwapMap ) ;
211- setVwapData ( vwapMap ) ;
212- } catch ( error ) {
213- console . error ( 'Failed to load VWAP data:' , error ) ;
214- }
215- } , [ config ?. symbols ] ) ;
216217
217218 // Use passed positions if available, otherwise use fetched positions
218219 // Apply live mark prices to calculate real-time PnL
0 commit comments