This commit is contained in:
@@ -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,
|
||||||
|
|||||||
@@ -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
@@ -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 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user