try to fix shorts/bankruptcy
Build Images and Deploy / Update-PROD-Stack (push) Successful in 1m22s

This commit is contained in:
2026-03-20 13:26:22 -04:00
parent c28720be5a
commit 621d3a9120
3 changed files with 18 additions and 4 deletions
@@ -84,7 +84,12 @@ export async function POST(
data: { userId: params.userId, type: 'ACCOUNT_OPEN', shares: 0, price: 0, total: STARTING_BALANCE, profit: STARTING_BALANCE }, data: { userId: params.userId, type: 'ACCOUNT_OPEN', shares: 0, price: 0, total: STARTING_BALANCE, profit: STARTING_BALANCE },
}), }),
] ]
: [prisma.trade.deleteMany({ where: { userId: params.userId } })] : [
prisma.trade.deleteMany({ where: { userId: params.userId } }),
prisma.trade.create({
data: { userId: params.userId, type: 'ACCOUNT_OPEN', shares: 0, price: 0, total: STARTING_BALANCE, profit: STARTING_BALANCE },
}),
]
await prisma.$transaction([ await prisma.$transaction([
...fundUpdates, ...fundUpdates,
+6 -1
View File
@@ -86,7 +86,12 @@ export async function POST(req: NextRequest) {
data: { userId, type: 'ACCOUNT_OPEN', shares: 0, price: 0, total: STARTING_BALANCE, profit: STARTING_BALANCE }, data: { userId, type: 'ACCOUNT_OPEN', shares: 0, price: 0, total: STARTING_BALANCE, profit: STARTING_BALANCE },
}), }),
] ]
: [prisma.trade.deleteMany({ where: { userId } })] : [
prisma.trade.deleteMany({ where: { userId } }),
prisma.trade.create({
data: { userId, type: 'ACCOUNT_OPEN', shares: 0, price: 0, total: STARTING_BALANCE, profit: STARTING_BALANCE },
}),
]
await prisma.$transaction([ await prisma.$transaction([
...fundUpdates, ...fundUpdates,
+6 -2
View File
@@ -78,8 +78,12 @@ export function calcTrade(
return { total, balanceDelta: -total, profit: 0 } return { total, balanceDelta: -total, profit: 0 }
} }
case 'SELL_SHORT': { case 'SELL_SHORT': {
const returned = Math.max(0, (2 * avgBuyPrice - price) * shares) // The collateral model: BUY_SHORT debited avgBuyPrice*shares. On close the
const profit = returned - avgBuyPrice * shares // formula (2*avgBuyPrice - price)*shares returns the net credit/debit so that
// the combined two-leg P&L equals (avgBuyPrice - price)*shares.
// No cap: when price > 2*avgBuyPrice the balance goes negative (realistic loss).
const returned = (2 * avgBuyPrice - price) * shares
const profit = returned - avgBuyPrice * shares // = (avgBuyPrice - price) * shares
return { total: returned, balanceDelta: returned, profit } return { total: returned, balanceDelta: returned, profit }
} }
} }