feat(tide-chart): trade management enhancements (issue #29)#33
Conversation
1475386 to
f75a02b
Compare
|
Hi @MkDev11, I've been auditing this PR locally with live Synth API data. I noticed a regression regarding the Take Profit (TP) and Stop Loss (SL) logic for short positions. It seems the directional logic from PR #30 was overwritten in Verified locally on http://localhost:5000:
The fee estimation and market hours features look great, but this logic needs to be restored to handle both directions correctly before merging. |
f75a02b to
f49ec05
Compare
|
@e35ventura please review the screenshots, implemented your comments |
|
if a trade gets gets close via liquidation it does not show up in the trade history |
- Add TP/SL management on open positions (updateTp/updateSl contract calls) - Add partial close via decreasePositionSize with inline manage panel - Add market hours validation for stock trades (NYSE hours + holidays) - Add real-time fee estimation display in trade preview - Add liquidation price display on open positions and trade preview - Add /api/gtrade/market-status and /api/gtrade/estimate-fees endpoints - Include fee data in validate-trade response summary - Block stock trades when US market is closed (server + client) - Add 17 new tests covering all new features (69 total pass)
f49ec05 to
f5b9398
Compare
|
This PR inadvertently reverts critical trading execution fixes that were recently merged in PR #30. Specifically, it:
|
…UP_LIMITS, XAU fees - Fix GROUP_LIMITS: stocks max_leverage 150→50, min_leverage 2→1.1 - Add commodities_t1 group (XAU max_leverage 250) + fee tier in GROUP_FEES - Detect liquidations/TP/SL hits via disappeared-trade polling in loadOpenTrades - Persist _openTradesCache to sessionStorage for cross-refresh detection - Guard against false positives with _closingTradeIndices for user-initiated closes - Migrate fetch_trade_history to backend-global endpoint with legacy fallback - Add TP HIT / SL HIT / LIQUIDATED badge CSS styling - Add regression tests: leverage limits, XAU fees, badge CSS, backend URL (73/73 pass)
|
I addressed all your comments. can you review again? |
|
Thanks for the updates! It looks like you've successfully addressed the However, it appears that the Currently, var slippageP = Math.round(slippageInput * 1000);The gTrade contract actually expects this to be scaled in var slippageP = BigInt(Math.round((slippageInput / 100) * 1e10));Once this last precision fix is restored, this should be good to merge. Thanks! |
I checked the PR #30 diff ( |


Summary
Add advanced trade management features to Tide Chart's gTrade integration: TP/SL updates on open positions, partial closes, market hours validation for stocks, real-time fee estimation, and liquidation price display.
Related Issues
Closes #29
Type of Change
What's New
1. TP/SL Management on Open Positions
Traders can now update or remove Take Profit and Stop Loss directly on open positions through an inline manage panel. Each trade row has a Manage button that expands TP/SL inputs and a partial close control.
updateTp(uint32 _index, uint64 _newTp)andupdateSl(uint32 _index, uint64 _newSl)contract calls75 / leverage% from entry (gTradeMAX_SL_P)2. Partial Close (Decrease Position Size)
Reduce an open position's collateral without fully closing the trade. Useful for taking partial profits or reducing exposure.
decreasePositionSize(uint32 _index, uint120 _collateralDelta, uint24 _leverageDelta, uint64 _expectedPrice)contract call (4 params, gTrade v9)_expectedPricefetched from Chainlink on-chain feed with Synth API fallbackethers.parseUnits3. Market Hours Validation (Stocks)
Stock trades on gTrade can only execute during NYSE regular hours. The app now validates this at multiple layers to prevent wasted gas.
is_market_open()checks Mon-Fri 9:30 AM - 4:00 PM ET with 2025-2026 US holiday calendarPOST /api/gtrade/validate-tradereturns 400 for stock trades outside market hoursexecuteTrade()blocks submission with toast message before any wallet interaction4. Real-Time Fee Estimation
Trade preview now shows a breakdown of estimated protocol fees before execution.
estimate_trade_fees(asset, collateral_usd, leverage)calculates from group fee ratesPOST /api/gtrade/estimate-feesreturns{open_fee, close_fee, total_fee, fee_pct}validate-traderesponse summary5. Liquidation Price Display
Liquidation price is now visible on both open positions and the trade preview, giving traders immediate risk awareness.
entry * (1 - 0.9 / leverage), Shortentry * (1 + 0.9 / leverage)calculate_liquidation_price()with edge-case safety (returns 0 for invalid inputs)calculateLiquidationPrice()for instant UI feedbackLiq: $X.XXEst. Liq. PriceFiles Changed
gtrade.pyis_market_open(),estimate_trade_fees(),calculate_liquidation_price(), constants (GROUP_FEES,LIQ_THRESHOLD_PCT,_MARKET_HOLIDAYS)main.pyGET /api/gtrade/market-status,POST /api/gtrade/estimate-fees, market guard invalidate-tradestatic/trading.jsupdateTradeTP(),updateTradeSL(),decreasePosition(),toggleManagePanel(),checkMarketStatus(),calculateLiquidationPrice(), fee display in preview, market warning in preview + executeTrade, client-side validations (TP/SL direction + distance, partial close min position), optimistic UI updates with cache sync, anti-flicker polling (skip re-render when manage panel open), race-condition fix for async fee displaytests/test_tool.pyTesting
New Tests Added (17)
test_is_market_open_returns_tupleis_market_open()returns(bool, str)test_estimate_trade_fees_cryptotest_estimate_trade_fees_stockstest_estimate_trade_fees_unknown_assettest_calculate_liquidation_price_longtest_calculate_liquidation_price_shorttest_calculate_liquidation_price_high_leveragetest_calculate_liquidation_price_edge_casestest_flask_gtrade_market_statusGET /api/gtrade/market-statustest_flask_gtrade_estimate_fees_validPOST /api/gtrade/estimate-fees(valid)test_flask_gtrade_estimate_fees_invalidPOST /api/gtrade/estimate-fees(400)test_flask_validate_trade_includes_feesvalidate-tradeincludes fees in summarytest_group_fees_structureGROUP_FEEShas all groups with required keystest_liq_threshold_constantLIQ_THRESHOLD_PCT == 90test_dashboard_html_contains_new_cssDemo Video
https://www.loom.com/share/88d0915387514625b72d0cd381fe22f5?t=226
Checklist