@@ -283,7 +283,7 @@ const Chart = (() => {
283283 ctx . fillRect ( x + 1 , bodyTop , candleWidth - 2 , bodyHeight ) ;
284284 } ) ;
285285
286- // === Trade Lines with P&L ===
286+ // === Trade Lines - Entry @ Price ===
287287 state . openTrades . forEach ( trade => {
288288 const drawLine = ( price , color , label , trade ) => {
289289 const y = height - ( price - min ) * scaleY - padding ;
@@ -302,7 +302,9 @@ const Chart = (() => {
302302 ctx . fillText ( `${ label } ${ price . toFixed ( 2 ) } ${ dollar } ` , 8 , y - 4 ) ;
303303
304304 // Add countdown timer for this trade
305- const remaining = Math . max ( 0 , ( trade . entryTime + trade . duration - Date . now ( ) ) / 1000 ) ;
305+ const remaining = trade . endTime
306+ ? Math . max ( 0 , ( trade . endTime - Date . now ( ) ) / 1000 )
307+ : 0 ;
306308 const countdown = `${ Math . floor ( remaining ) } s` ;
307309 ctx . textAlign = "left" ;
308310 ctx . fillStyle = "#fff" ;
@@ -335,25 +337,6 @@ const Chart = (() => {
335337
336338// === TRADES MODULE ===
337339const Trades = ( ( ) => {
338- function toggleTradeButtons ( disabled ) {
339- const buttons = [
340- document . getElementById ( "balance" ) ,
341- document . getElementById ( "performance" ) ,
342- document . getElementById ( "buy" ) ,
343- document . getElementById ( "sell" ) ,
344- document . getElementById ( "wager" ) ,
345- document . getElementById ( "duration" )
346- ] ;
347-
348- buttons . forEach ( btn => {
349- if ( btn ) {
350- btn . disabled = disabled ;
351- btn . classList . toggle ( "opacity-50" , disabled ) ;
352- btn . classList . toggle ( "cursor-not-allowed" , disabled ) ;
353- }
354- } ) ;
355- }
356-
357340 function place ( type , state ) {
358341 // Check if user has enough balance to place this trade
359342 if ( state . balance < state . wager ) {
@@ -364,18 +347,22 @@ const Trades = (() => {
364347 const entry = state . currentPrice ;
365348 const durationMs = ( state . duration . hour * 3600 + state . duration . minute * 60 + state . duration . second ) * 1000 ;
366349
350+ const now = Date . now ( ) ;
367351 const trade = {
368352 type,
369353 entry,
370354 wager : state . wager ,
371- entryTime : Date . now ( ) ,
372- duration : durationMs
355+ entryTime : now ,
356+ duration : durationMs ,
357+ endTime : now + durationMs
373358 } ;
359+
374360 state . balance -= state . wager ;
375361
376362 state . openTrades . push ( trade ) ;
377363
378364 setTimeout ( ( ) => resolveTrade ( trade , state ) , durationMs ) ;
365+ saveStateToLocalStorage ( ) ;
379366 }
380367
381368 function resolveTrade ( trade , state ) {
@@ -409,7 +396,8 @@ const Trades = (() => {
409396 } ) ;
410397
411398 const index = state . openTrades . indexOf ( trade ) ;
412- if ( index > - 1 ) state . openTrades . splice ( index , 1 ) ;
399+ // if (index > -1) state.openTrades.splice(index, 1);
400+ if ( index !== - 1 ) state . openTrades . splice ( index , 1 ) ;
413401
414402 saveStateToLocalStorage ( ) ;
415403 Stats . update ( state ) ;
@@ -602,8 +590,11 @@ function formatMs(ms) {
602590function saveStateToLocalStorage ( ) {
603591 const stateCopy = { ...Core . state } ;
604592
605- // Remove volatile open trades
606- stateCopy . openTrades = [ ] ;
593+ // Saves the remaining time until each trade resolves
594+ stateCopy . openTrades = stateCopy . openTrades . map ( t => ( {
595+ ...t ,
596+ remainingTime : t . entryTime + t . duration - Date . now ( )
597+ } ) ) ;
607598
608599 try {
609600 localStorage . setItem ( "TheTradingGame" , JSON . stringify ( stateCopy ) ) ;
@@ -619,6 +610,20 @@ function loadStateFromLocalStorage() {
619610
620611 // Restore safely
621612 Object . assign ( Core . state , parsed ) ;
613+
614+ // Resumes and resolves trades
615+ Core . state . openTrades . forEach ( trade => {
616+ const now = Date . now ( ) ;
617+ const timeLeft = trade . endTime - now ;
618+
619+ if ( timeLeft <= 0 ) {
620+ // Trade should already be resolved
621+ Trades . resolveTrade ( trade , Core . state ) ;
622+ } else {
623+ // Still pending, resume timer
624+ setTimeout ( ( ) => Trades . resolveTrade ( trade , Core . state ) , timeLeft ) ;
625+ }
626+ } ) ;
622627 } catch ( err ) {
623628 console . error ( "Failed to load state:" , err ) ;
624629 }
@@ -757,6 +762,7 @@ document.getElementById('performance').onclick = () => {
757762 title : "🎯 Performance Card" ,
758763 content,
759764 CloseLabel : "Close" ,
765+ ConfirmLabel : "Reset" ,
760766 onLoad : ( ) => {
761767 setTimeout ( ( ) => {
762768 const exportBtn = document . getElementById ( "exportBackupBtn" ) ;
@@ -807,6 +813,21 @@ document.getElementById('performance').onclick = () => {
807813 reader . readAsText ( file ) ;
808814 } ) ;
809815 } , 50 ) ; // slight delay to ensure DOM is mounted
816+ } ,
817+ onConfirm : ( ) => {
818+ Core . clearStorage ( ) ;
819+
820+ // Get the input value and update the balance
821+ Core . state . balance = Core . state . startBalance ;
822+ Core . state . trades = [ ] ;
823+
824+ // Update the balance display
825+ document . getElementById ( 'balance' ) . querySelector ( 'span' ) . textContent = Core . state . balance . toLocaleString ( undefined , {
826+ minimumFractionDigits : 2 ,
827+ maximumFractionDigits : 2
828+ } ) ;
829+
830+ saveStateToLocalStorage ( ) ;
810831 }
811832 } ) ;
812833} ;
0 commit comments