Khanna, Videh Rakesh Rakesh commited on
Commit
cac64bb
·
1 Parent(s): 1da9745

Add manual buy and short trade actions

Browse files
Files changed (2) hide show
  1. static/app.js +34 -19
  2. templates/index.html +2 -2
static/app.js CHANGED
@@ -550,6 +550,8 @@ function renderPredCard(pred, showChart = true) {
550
 
551
  const predBareSym = pred.ticker.replace(/\.(NS|BO)$/i, '');
552
  const predExchange = pred.ticker.endsWith('.BO') ? 'BSE' : 'NSE';
 
 
553
 
554
  const html = `
555
  <div class="pred-card ${cls}" id="card-${pred.ticker.replace(/[^a-zA-Z0-9]/g,'_')}">
@@ -566,7 +568,8 @@ function renderPredCard(pred, showChart = true) {
566
  <span class="conf-pill ${confClass(pred.confidence)}">${pred.confidence || 'LOW'}</span>
567
  <div class="pred-actions">
568
  <button class="btn-ghost btn-sm" onclick="addToWatchlist('${pred.ticker}','${(pred.company||'').replace(/'/g,"\\'")}')">+ Watch</button>
569
- <button class="btn-primary btn-sm" onclick='openTradeModal(${JSON.stringify(pred.ticker)},${JSON.stringify(pred.company||'')},${pred.price || 0},${risk.stop_loss||0},${tradeTarget||0},${JSON.stringify(plan)})'>Trade</button>
 
570
  </div>
571
  </div>
572
  <div class="pred-body">
@@ -1411,7 +1414,7 @@ function _refreshWatchlistRecommendedAction(ticker) {
1411
  if (!pick) return;
1412
 
1413
  const card = document.querySelector(`#wl-list [data-ticker="${ticker}"]`);
1414
- const btn = card ? card.querySelector('.pick-actions .btn-primary') : null;
1415
  if (!btn) return;
1416
 
1417
  const tfs = pick.timeframes || {};
@@ -1563,23 +1566,17 @@ function renderPickCard(pick, idx, idPrefix = 'pick', mode = 'top5') {
1563
  const actionLabel = actionMeta.label;
1564
  const actionTitle = actionMeta.title;
1565
  const actionClass = actionMeta.isShort ? 'btn-short' : '';
1566
- const intradayPlan = Object.assign({}, tfs.INTRADAY || {}, {
1567
- timeframe: 'INTRADAY',
1568
- direction: 'BEARISH',
1569
- });
1570
- const intradayIsBearish = _inferTradeDirection(tfs.INTRADAY) === 'SHORT';
1571
- const intradayStop = (tfs.INTRADAY || {}).stop_loss || 0;
1572
- const intradayTarget = (tfs.INTRADAY || {}).expected_target_price || (tfs.INTRADAY || {}).min_target || 0;
1573
- const intradayPlanJSON = JSON.stringify(intradayPlan).replace(/'/g, "&#39;");
1574
- const intradayShortBtn = mode === 'watchlist' && intradayIsBearish
1575
- ? `<button class="btn-primary btn-sm btn-short" title="Open a bearish intraday short trade" onclick='openTradeModal(${JSON.stringify(pick.ticker)},${JSON.stringify(safeCompany)},${pick.price||0},${intradayStop},${intradayTarget},${intradayPlanJSON})'>Short Today</button>`
1576
- : '';
1577
  const actionBtns = mode === 'watchlist'
1578
  ? `<button class="btn-danger btn-sm" onclick="removeFromWatchlist('${pick.ticker}')">✕ Remove</button>
1579
- <button class="btn-primary btn-sm ${actionClass}" title="${actionTitle}" onclick='openTradeModal(${JSON.stringify(pick.ticker)},${JSON.stringify(safeCompany)},${pick.price||0},${slBest},${tgtBest},${planDataBestJSON})'>${actionLabel}</button>
1580
- ${intradayShortBtn}`
1581
  : `<button class="btn-ghost btn-sm" onclick="addToWatchlist('${pick.ticker}','${safeCompany}')">+ Watch</button>
1582
- <button class="btn-primary btn-sm ${actionClass}" title="${actionTitle}" onclick='openTradeModal(${JSON.stringify(pick.ticker)},${JSON.stringify(safeCompany)},${pick.price||0},${slBest},${tgtBest},${planDataBestJSON})'>${actionLabel}</button>`;
 
1583
 
1584
  const bareSym = pick.ticker.replace(/\.(NS|BO)$/i, '');
1585
  const exchange = pick.ticker.endsWith('.BO') ? 'BSE' : 'NSE';
@@ -1698,7 +1695,8 @@ function renderWatchlistShellCard(item) {
1698
  const cardId = 'wl-card-' + ticker.replace(/[^a-zA-Z0-9]/g, '_');
1699
  const chartId = 'tv-wl-' + ticker.replace(/[^a-zA-Z0-9]/g, '_');
1700
  const safeId = ticker.replace(/[^a-zA-Z0-9]/g, '_');
1701
- const safeName = (fallbackName || '').replace(/'/g, "\\'");
 
1702
  // Shell TF grid: ML slots are painted immediately so the instant, local ML forecast
1703
  // can fill in without waiting for the slow per-card AI (/api/watchlist-pick) call.
1704
  const shellTfHtml = ['INTRADAY', '1D'].map(tf => `
@@ -1719,7 +1717,8 @@ function renderWatchlistShellCard(item) {
1719
  </div>
1720
  <div class="pick-actions">
1721
  <button class="btn-danger btn-sm" onclick="removeFromWatchlist('${ticker}')">✕ Remove</button>
1722
- <button class="btn-primary btn-sm" onclick="openTradeModal('${ticker}','${safeName}',0,0,0)">Trade</button>
 
1723
  </div>
1724
  </div>
1725
  <div class="pick-body">
@@ -1760,6 +1759,20 @@ function renderRankTable(data) {
1760
  const allocCell = capital && r.suggested_allocation
1761
  ? `₹${num(r.suggested_allocation)} · ${r.suggested_shares}sh`
1762
  : '—';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1763
  return `<tr>
1764
  <td class="rank-num">${r.rank}</td>
1765
  <td>${stockCell(r.ticker)}</td>
@@ -1774,7 +1787,8 @@ function renderRankTable(data) {
1774
  <td><span class="dir-badge ${dirClass(news)}">${news}</span> ${earnBadge}</td>
1775
  ${capital ? `<td style="font-size:11px;color:var(--text-muted)">${allocCell}</td>` : ''}
1776
  <td>
1777
- <button class="btn-primary btn-sm" onclick='openTradeModal(${JSON.stringify(r.ticker)},${JSON.stringify(r.company||"")},${r.price||0},${(r.risk||{}).stop_loss||0},${r.expected_target_price||0},${JSON.stringify({direction:r.direction,strategy:(r.active_strategies||[])[0]||null,timeframe:r.timeframe||null,max_chase_pct:(r.trade_plan||{}).max_chase_pct||null,prediction_data:{ml:r.ml||{},news:r.news||{},ai:r.ai_forecast?{direction:r.ai_forecast.direction,confidence:r.ai_forecast.confidence}:{},market:r.market||{}}})})'>${['BEARISH','SLIGHTLY BEARISH'].includes((r.direction||'').toUpperCase()) ? 'Short' : 'Trade'}</button>
 
1778
  </td>
1779
  </tr>`;
1780
  }).join('');
@@ -3065,6 +3079,7 @@ function openTradeModal(ticker, name, price, stopLoss = 0, target = 0, planData
3065
  entryEl.value = d.price;
3066
  entryEl.placeholder = '';
3067
  entryEl.title = `Live price from ${d.source || 'market'}`;
 
3068
  _autoFillShares();
3069
  } else {
3070
  entryEl.placeholder = 'Enter price manually';
 
550
 
551
  const predBareSym = pred.ticker.replace(/\.(NS|BO)$/i, '');
552
  const predExchange = pred.ticker.endsWith('.BO') ? 'BSE' : 'NSE';
553
+ const buyPlan = JSON.stringify(Object.assign({}, plan, { direction: 'LONG' })).replace(/'/g, "&#39;");
554
+ const shortPlan = JSON.stringify(Object.assign({}, plan, { direction: 'SHORT' })).replace(/'/g, "&#39;");
555
 
556
  const html = `
557
  <div class="pred-card ${cls}" id="card-${pred.ticker.replace(/[^a-zA-Z0-9]/g,'_')}">
 
568
  <span class="conf-pill ${confClass(pred.confidence)}">${pred.confidence || 'LOW'}</span>
569
  <div class="pred-actions">
570
  <button class="btn-ghost btn-sm" onclick="addToWatchlist('${pred.ticker}','${(pred.company||'').replace(/'/g,"\\'")}')">+ Watch</button>
571
+ <button class="btn-primary btn-sm" title="Open a long paper trade" onclick='openTradeModal(${JSON.stringify(pred.ticker)},${JSON.stringify(pred.company||'')},${pred.price || 0},${risk.stop_loss||0},${tradeTarget||0},${buyPlan})'>Buy</button>
572
+ <button class="btn-primary btn-sm btn-short" title="Open a short paper trade" onclick='openTradeModal(${JSON.stringify(pred.ticker)},${JSON.stringify(pred.company||'')},${pred.price || 0},${risk.stop_loss||0},${tradeTarget||0},${shortPlan})'>Short</button>
573
  </div>
574
  </div>
575
  <div class="pred-body">
 
1414
  if (!pick) return;
1415
 
1416
  const card = document.querySelector(`#wl-list [data-ticker="${ticker}"]`);
1417
+ const btn = card ? card.querySelector('.pick-actions .btn-recommended-action') : null;
1418
  if (!btn) return;
1419
 
1420
  const tfs = pick.timeframes || {};
 
1566
  const actionLabel = actionMeta.label;
1567
  const actionTitle = actionMeta.title;
1568
  const actionClass = actionMeta.isShort ? 'btn-short' : '';
1569
+ const buyPlanJSON = JSON.stringify(Object.assign({}, planDataBest || {}, { direction: 'LONG' })).replace(/'/g, "&#39;");
1570
+ const shortPlanJSON = JSON.stringify(Object.assign({}, planDataBest || {}, { direction: 'SHORT' })).replace(/'/g, "&#39;");
1571
+ const manualTradeBtns = `<button class="btn-primary btn-sm" title="Open a long paper trade" onclick='openTradeModal(${JSON.stringify(pick.ticker)},${JSON.stringify(safeCompany)},${pick.price||0},${slBest},${tgtBest},${buyPlanJSON})'>Buy</button>
1572
+ <button class="btn-primary btn-sm btn-short" title="Open a short paper trade" onclick='openTradeModal(${JSON.stringify(pick.ticker)},${JSON.stringify(safeCompany)},${pick.price||0},${slBest},${tgtBest},${shortPlanJSON})'>Short</button>`;
 
 
 
 
 
 
 
1573
  const actionBtns = mode === 'watchlist'
1574
  ? `<button class="btn-danger btn-sm" onclick="removeFromWatchlist('${pick.ticker}')">✕ Remove</button>
1575
+ <button class="btn-primary btn-sm btn-recommended-action ${actionClass}" title="${actionTitle}" onclick='openTradeModal(${JSON.stringify(pick.ticker)},${JSON.stringify(safeCompany)},${pick.price||0},${slBest},${tgtBest},${planDataBestJSON})'>${actionLabel}</button>
1576
+ ${manualTradeBtns}`
1577
  : `<button class="btn-ghost btn-sm" onclick="addToWatchlist('${pick.ticker}','${safeCompany}')">+ Watch</button>
1578
+ <button class="btn-primary btn-sm btn-recommended-action ${actionClass}" title="${actionTitle}" onclick='openTradeModal(${JSON.stringify(pick.ticker)},${JSON.stringify(safeCompany)},${pick.price||0},${slBest},${tgtBest},${planDataBestJSON})'>${actionLabel}</button>
1579
+ ${manualTradeBtns}`;
1580
 
1581
  const bareSym = pick.ticker.replace(/\.(NS|BO)$/i, '');
1582
  const exchange = pick.ticker.endsWith('.BO') ? 'BSE' : 'NSE';
 
1695
  const cardId = 'wl-card-' + ticker.replace(/[^a-zA-Z0-9]/g, '_');
1696
  const chartId = 'tv-wl-' + ticker.replace(/[^a-zA-Z0-9]/g, '_');
1697
  const safeId = ticker.replace(/[^a-zA-Z0-9]/g, '_');
1698
+ const buyPlan = JSON.stringify({ direction: 'LONG' }).replace(/'/g, "&#39;");
1699
+ const shortPlan = JSON.stringify({ direction: 'SHORT' }).replace(/'/g, "&#39;");
1700
  // Shell TF grid: ML slots are painted immediately so the instant, local ML forecast
1701
  // can fill in without waiting for the slow per-card AI (/api/watchlist-pick) call.
1702
  const shellTfHtml = ['INTRADAY', '1D'].map(tf => `
 
1717
  </div>
1718
  <div class="pick-actions">
1719
  <button class="btn-danger btn-sm" onclick="removeFromWatchlist('${ticker}')">✕ Remove</button>
1720
+ <button class="btn-primary btn-sm" title="Open a long paper trade" onclick='openTradeModal(${JSON.stringify(ticker)},${JSON.stringify(fallbackName)},0,0,0,${buyPlan})'>Buy</button>
1721
+ <button class="btn-primary btn-sm btn-short" title="Open a short paper trade" onclick='openTradeModal(${JSON.stringify(ticker)},${JSON.stringify(fallbackName)},0,0,0,${shortPlan})'>Short</button>
1722
  </div>
1723
  </div>
1724
  <div class="pick-body">
 
1759
  const allocCell = capital && r.suggested_allocation
1760
  ? `₹${num(r.suggested_allocation)} · ${r.suggested_shares}sh`
1761
  : '—';
1762
+ const tradePlan = {
1763
+ direction: r.direction,
1764
+ strategy: (r.active_strategies || [])[0] || null,
1765
+ timeframe: r.timeframe || null,
1766
+ max_chase_pct: (r.trade_plan || {}).max_chase_pct || null,
1767
+ prediction_data: {
1768
+ ml: r.ml || {},
1769
+ news: r.news || {},
1770
+ ai: r.ai_forecast ? { direction: r.ai_forecast.direction, confidence: r.ai_forecast.confidence } : {},
1771
+ market: r.market || {},
1772
+ },
1773
+ };
1774
+ const buyPlan = JSON.stringify(Object.assign({}, tradePlan, { direction: 'LONG' })).replace(/'/g, "&#39;");
1775
+ const shortPlan = JSON.stringify(Object.assign({}, tradePlan, { direction: 'SHORT' })).replace(/'/g, "&#39;");
1776
  return `<tr>
1777
  <td class="rank-num">${r.rank}</td>
1778
  <td>${stockCell(r.ticker)}</td>
 
1787
  <td><span class="dir-badge ${dirClass(news)}">${news}</span> ${earnBadge}</td>
1788
  ${capital ? `<td style="font-size:11px;color:var(--text-muted)">${allocCell}</td>` : ''}
1789
  <td>
1790
+ <button class="btn-primary btn-sm" title="Open a long paper trade" onclick='openTradeModal(${JSON.stringify(r.ticker)},${JSON.stringify(r.company||"")},${r.price||0},${(r.risk||{}).stop_loss||0},${r.expected_target_price||0},${buyPlan})'>Buy</button>
1791
+ <button class="btn-primary btn-sm btn-short" title="Open a short paper trade" onclick='openTradeModal(${JSON.stringify(r.ticker)},${JSON.stringify(r.company||"")},${r.price||0},${(r.risk||{}).stop_loss||0},${r.expected_target_price||0},${shortPlan})'>Short</button>
1792
  </td>
1793
  </tr>`;
1794
  }).join('');
 
3079
  entryEl.value = d.price;
3080
  entryEl.placeholder = '';
3081
  entryEl.title = `Live price from ${d.source || 'market'}`;
3082
+ _normalizeTradeLevelsForDirection();
3083
  _autoFillShares();
3084
  } else {
3085
  entryEl.placeholder = 'Enter price manually';
templates/index.html CHANGED
@@ -33,7 +33,7 @@
33
  @keyframes apl-spin { to { transform: rotate(360deg); } }
34
  .apl-hint { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; font-size: 11px; color: #5a6278; }
35
  </style>
36
- <link rel="stylesheet" href="/static/style.css?v=20260915b">
37
  <script src="https://unpkg.com/lightweight-charts@4.2.0/dist/lightweight-charts.standalone.production.js"></script>
38
  </head>
39
  <body>
@@ -449,6 +449,6 @@
449
  </a>
450
  </nav>
451
 
452
- <script src="/static/app.js?v=20260915b"></script>
453
  </body>
454
  </html>
 
33
  @keyframes apl-spin { to { transform: rotate(360deg); } }
34
  .apl-hint { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; font-size: 11px; color: #5a6278; }
35
  </style>
36
+ <link rel="stylesheet" href="/static/style.css?v=20260915c">
37
  <script src="https://unpkg.com/lightweight-charts@4.2.0/dist/lightweight-charts.standalone.production.js"></script>
38
  </head>
39
  <body>
 
449
  </a>
450
  </nav>
451
 
452
+ <script src="/static/app.js?v=20260915c"></script>
453
  </body>
454
  </html>