Spaces:
Sleeping
Sleeping
Naufal Arhab
feat(sniper): implement dual-horizon (15m-1D) sniper engine, forward outcome evaluator, auto-breakeven SL, and UI surfacing
740fcec Download frontend/src/api/execution.ts from agus1111/Perps-Terminal: direct link, hf CLI and curl.
- Browser
- Download file 4.89 kB
-
https://huggingface.co/spaces/agus1111/Perps-Terminal/resolve/main/frontend/src/api/execution.ts
- Command line
-
hf download hf://spaces/agus1111/Perps-Terminal/frontend/src/api/execution.ts
-
curl -L -o execution.ts https://huggingface.co/spaces/agus1111/Perps-Terminal/resolve/main/frontend/src/api/execution.ts
4.89 kB
| import { apiClient } from './client'; | |
| export const BYBIT_SESSION_CREDS_KEY = 'perps_bybit_session_creds'; | |
| export interface BybitSessionCredentials { | |
| api_key: string; | |
| api_secret: string; | |
| mode: 'demo' | 'testnet' | 'real'; | |
| } | |
| export function getStoredBybitCredentials(): BybitSessionCredentials | null { | |
| if (typeof window === 'undefined') return null; | |
| try { | |
| const raw = window.sessionStorage.getItem(BYBIT_SESSION_CREDS_KEY); | |
| if (!raw) return null; | |
| const parsed = JSON.parse(raw); | |
| if (parsed?.api_key && parsed?.api_secret) { | |
| return { | |
| api_key: String(parsed.api_key).trim(), | |
| api_secret: String(parsed.api_secret).trim(), | |
| mode: (parsed.mode || 'demo') as 'demo' | 'testnet' | 'real', | |
| }; | |
| } | |
| } catch { | |
| // ignore malformed session storage | |
| } | |
| return null; | |
| } | |
| export function saveStoredBybitCredentials(creds: BybitSessionCredentials): void { | |
| if (typeof window === 'undefined') return; | |
| window.sessionStorage.setItem(BYBIT_SESSION_CREDS_KEY, JSON.stringify({ | |
| api_key: creds.api_key.trim(), | |
| api_secret: creds.api_secret.trim(), | |
| mode: creds.mode, | |
| })); | |
| } | |
| export function clearStoredBybitCredentials(): void { | |
| if (typeof window === 'undefined') return; | |
| window.sessionStorage.removeItem(BYBIT_SESSION_CREDS_KEY); | |
| } | |
| function buildBybitHeaders(overrideMode?: string): Record<string, string> { | |
| const creds = getStoredBybitCredentials(); | |
| if (!creds) { | |
| return overrideMode ? { 'X-Bybit-Mode': overrideMode } : {}; | |
| } | |
| return { | |
| 'X-Bybit-Api-Key': creds.api_key, | |
| 'X-Bybit-Api-Secret': creds.api_secret, | |
| 'X-Bybit-Mode': overrideMode || creds.mode, | |
| }; | |
| } | |
| export interface BybitSettings { | |
| configured: boolean; | |
| masked_key: string; | |
| mode: 'demo' | 'testnet' | 'real'; | |
| source?: string; | |
| stateless?: boolean; | |
| } | |
| export interface BybitWalletBalance { | |
| coin: string; | |
| available_balance: number; | |
| total_equity: number; | |
| mode: string; | |
| raw?: any; | |
| } | |
| export interface ExecuteOrderPayload { | |
| symbol: string; | |
| side: 'Buy' | 'Sell' | 'Long' | 'Short'; | |
| order_type: 'Market' | 'Limit'; | |
| margin_usd: number; | |
| margin_percent?: number | null; | |
| leverage: number; | |
| stop_loss: number; | |
| take_profit_1: number; | |
| take_profit_2?: number | null; | |
| tp1_percent: number; | |
| auto_breakeven?: boolean; | |
| entry_price?: number | null; | |
| mark_price?: number | null; | |
| api_key?: string; | |
| api_secret?: string; | |
| mode?: string; | |
| log_to_journal?: boolean; | |
| summary_tldr?: string; | |
| model_used?: string; | |
| } | |
| export interface ExecutionResult { | |
| success: boolean; | |
| message: string; | |
| execution: { | |
| symbol: string; | |
| side: string; | |
| order_type: string; | |
| margin_usd: number; | |
| leverage: number; | |
| notional_usd: number; | |
| total_qty: number; | |
| entry_order_id: string; | |
| stop_loss: number; | |
| tp_orders: Array<{ | |
| tp_level: string; | |
| percent: number; | |
| order_id?: string; | |
| price: number; | |
| qty: number; | |
| }>; | |
| tp2_watcher_scheduled?: boolean; | |
| breakeven_watcher_scheduled?: boolean; | |
| breakeven_status?: string; | |
| mode: string; | |
| timestamp: number; | |
| }; | |
| journal_entry?: any; | |
| } | |
| export const executionApi = { | |
| getSettings: async (): Promise<BybitSettings> => { | |
| const { data } = await apiClient.get<BybitSettings>('/api/execution/bybit/settings', { | |
| headers: buildBybitHeaders(), | |
| }); | |
| return data; | |
| }, | |
| saveSettings: async (payload: { | |
| api_key?: string; | |
| api_secret?: string; | |
| mode?: string; | |
| }): Promise<{ success: boolean; message: string; balance?: BybitWalletBalance }> => { | |
| const mode = (payload.mode || 'demo') as 'demo' | 'testnet' | 'real'; | |
| const { data } = await apiClient.post<{ success: boolean; message: string; balance?: BybitWalletBalance }>( | |
| '/api/execution/bybit/settings', | |
| payload, | |
| { headers: buildBybitHeaders(mode) } | |
| ); | |
| if (data.success && payload.api_key && payload.api_secret) { | |
| saveStoredBybitCredentials({ | |
| api_key: payload.api_key, | |
| api_secret: payload.api_secret, | |
| mode, | |
| }); | |
| } | |
| return data; | |
| }, | |
| getBalance: async (mode?: string): Promise<BybitWalletBalance> => { | |
| const { data } = await apiClient.get<BybitWalletBalance>('/api/execution/bybit/balance', { | |
| params: mode ? { mode } : {}, | |
| headers: buildBybitHeaders(mode), | |
| }); | |
| return data; | |
| }, | |
| executeOrder: async (payload: ExecuteOrderPayload): Promise<ExecutionResult> => { | |
| const creds = getStoredBybitCredentials(); | |
| const enrichedPayload: ExecuteOrderPayload = { | |
| ...payload, | |
| api_key: payload.api_key || creds?.api_key, | |
| api_secret: payload.api_secret || creds?.api_secret, | |
| mode: payload.mode || creds?.mode, | |
| }; | |
| const { data } = await apiClient.post<ExecutionResult>('/api/execution/bybit/order', enrichedPayload, { | |
| headers: buildBybitHeaders(enrichedPayload.mode), | |
| }); | |
| return data; | |
| }, | |
| }; | |