repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | export function createState(startingCash = 10_000_000) { |
| #2 | return { |
| #3 | tick: 0, |
| #4 | book: { positions: [], cash_lamports: startingCash }, |
| #5 | candles: [], |
| #6 | consecutive_losses: 0, |
| #7 | total_pnl_lamports: 0, |
| #8 | total_trades: 0, |
| #9 | }; |
| #10 | } |
| #11 | export function openPosition(state, side, size_lamports, currentPrice) { |
| #12 | const pos = { |
| #13 | id: `pos-${state.tick}-${Date.now()}`, |
| #14 | side, |
| #15 | entry_price: currentPrice, |
| #16 | size_lamports, |
| #17 | opened_at_tick: state.tick, |
| #18 | opened_at: new Date().toISOString(), |
| #19 | }; |
| #20 | state.book.positions.push(pos); |
| #21 | state.book.cash_lamports -= size_lamports; |
| #22 | return pos; |
| #23 | } |
| #24 | export function closePosition(state, positionId, currentPrice) { |
| #25 | const idx = state.book.positions.findIndex((position) => position.id === positionId); |
| #26 | if (idx === -1) |
| #27 | throw new Error(`position ${positionId} not found`); |
| #28 | const pos = state.book.positions[idx]; |
| #29 | const priceDelta = currentPrice - pos.entry_price; |
| #30 | const units = pos.size_lamports / pos.entry_price; |
| #31 | const rawPnl = pos.side === "long" ? units * priceDelta : units * -priceDelta; |
| #32 | const pnl = Math.round(rawPnl); |
| #33 | state.book.positions.splice(idx, 1); |
| #34 | state.book.cash_lamports += pos.size_lamports + pnl; |
| #35 | state.total_pnl_lamports += pnl; |
| #36 | state.total_trades += 1; |
| #37 | state.consecutive_losses = pnl < 0 ? state.consecutive_losses + 1 : 0; |
| #38 | return pnl; |
| #39 | } |
| #40 | export function unrealisedPnl(state, currentPrice) { |
| #41 | return state.book.positions.reduce((sum, pos) => { |
| #42 | const units = pos.size_lamports / pos.entry_price; |
| #43 | const delta = currentPrice - pos.entry_price; |
| #44 | const pnl = pos.side === "long" ? units * delta : units * -delta; |
| #45 | return sum + pnl; |
| #46 | }, 0); |
| #47 | } |
| #48 | //# sourceMappingURL=state.js.map |