94 lines
2.1 KiB
TypeScript
94 lines
2.1 KiB
TypeScript
import { Order, OrderResult, OrderStatus } from '@stock-bot/types';
|
|
|
|
export interface BrokerInterface {
|
|
/**
|
|
* Execute an order with the broker
|
|
*/
|
|
executeOrder(order: Order): Promise<OrderResult>;
|
|
|
|
/**
|
|
* Get order status from broker
|
|
*/
|
|
getOrderStatus(orderId: string): Promise<OrderStatus>;
|
|
|
|
/**
|
|
* Cancel an order
|
|
*/
|
|
cancelOrder(orderId: string): Promise<boolean>;
|
|
|
|
/**
|
|
* Get current positions
|
|
*/
|
|
getPositions(): Promise<Position[]>;
|
|
|
|
/**
|
|
* Get account balance
|
|
*/
|
|
getAccountBalance(): Promise<AccountBalance>;
|
|
}
|
|
|
|
export interface Position {
|
|
symbol: string;
|
|
quantity: number;
|
|
averagePrice: number;
|
|
currentPrice: number;
|
|
unrealizedPnL: number;
|
|
side: 'long' | 'short';
|
|
}
|
|
|
|
export interface AccountBalance {
|
|
totalValue: number;
|
|
availableCash: number;
|
|
buyingPower: number;
|
|
marginUsed: number;
|
|
}
|
|
|
|
export class MockBroker implements BrokerInterface {
|
|
private orders: Map<string, OrderResult> = new Map();
|
|
private positions: Position[] = [];
|
|
|
|
async executeOrder(order: Order): Promise<OrderResult> {
|
|
const orderId = `mock_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
|
|
const result: OrderResult = {
|
|
orderId,
|
|
symbol: order.symbol,
|
|
quantity: order.quantity,
|
|
side: order.side,
|
|
status: 'filled',
|
|
executedPrice: order.price || 100, // Mock price
|
|
executedAt: new Date(),
|
|
commission: 1.0,
|
|
};
|
|
|
|
this.orders.set(orderId, result);
|
|
return result;
|
|
}
|
|
|
|
async getOrderStatus(orderId: string): Promise<OrderStatus> {
|
|
const order = this.orders.get(orderId);
|
|
return order?.status || 'unknown';
|
|
}
|
|
|
|
async cancelOrder(orderId: string): Promise<boolean> {
|
|
const order = this.orders.get(orderId);
|
|
if (order && order.status === 'pending') {
|
|
order.status = 'cancelled';
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
async getPositions(): Promise<Position[]> {
|
|
return this.positions;
|
|
}
|
|
|
|
async getAccountBalance(): Promise<AccountBalance> {
|
|
return {
|
|
totalValue: 100000,
|
|
availableCash: 50000,
|
|
buyingPower: 200000,
|
|
marginUsed: 0,
|
|
};
|
|
}
|
|
}
|