|
1 | 1 | import * as ccxt from 'ccxt'; |
2 | | -import { MarketData, OrderParams, OrderResult, OrderInfo, OrderSide, PositionInfo } from './types'; |
| 2 | +import { MarketData, OrderParams, OrderResult, OrderInfo, PositionInfo } from './types'; |
| 3 | +import { Logger } from '../modules/services'; |
3 | 4 |
|
4 | 5 | /** |
5 | 6 | * Fetches current bid/ask prices for a trading pair |
@@ -41,126 +42,132 @@ export function roundAmountDown(value: number, precision: number | undefined): n |
41 | 42 | } |
42 | 43 |
|
43 | 44 | /** |
44 | | - * Places a limit order on the exchange |
| 45 | + * Service for placing orders on exchanges. |
| 46 | + * Logger is injected via constructor. |
45 | 47 | */ |
46 | | -export async function placeLimitOrder( |
47 | | - exchange: ccxt.Exchange, |
48 | | - params: OrderParams |
49 | | -): Promise<OrderResult> { |
50 | | - if (!params.price) { |
51 | | - throw new Error('Price is required for limit orders'); |
52 | | - } |
| 48 | +export class ProfileOrderService { |
| 49 | + constructor(private readonly logger: Logger) {} |
| 50 | + |
| 51 | + /** |
| 52 | + * Places a limit order on the exchange |
| 53 | + */ |
| 54 | + async placeLimitOrder(exchange: ccxt.Exchange, params: OrderParams): Promise<OrderResult> { |
| 55 | + if (!params.price) { |
| 56 | + throw new Error('Price is required for limit orders'); |
| 57 | + } |
53 | 58 |
|
54 | | - // Load markets for precision info |
55 | | - const markets = await exchange.loadMarkets(); |
56 | | - const market = markets[params.pair]; |
| 59 | + // Load markets for precision info |
| 60 | + const markets = await exchange.loadMarkets(); |
| 61 | + const market = markets[params.pair]; |
57 | 62 |
|
58 | | - if (!market) { |
59 | | - throw new Error(`Market ${params.pair} not found`); |
60 | | - } |
| 63 | + if (!market) { |
| 64 | + throw new Error(`Market ${params.pair} not found`); |
| 65 | + } |
61 | 66 |
|
62 | | - // Calculate base currency amount |
63 | | - let baseAmount: number; |
64 | | - if (params.isQuoteCurrency) { |
65 | | - baseAmount = params.amount / params.price; |
66 | | - } else { |
67 | | - baseAmount = params.amount; |
68 | | - } |
| 67 | + // Calculate base currency amount |
| 68 | + let baseAmount: number; |
| 69 | + if (params.isQuoteCurrency) { |
| 70 | + baseAmount = params.amount / params.price; |
| 71 | + } else { |
| 72 | + baseAmount = params.amount; |
| 73 | + } |
69 | 74 |
|
70 | | - // Round amount to precision |
71 | | - const roundedBaseAmount = roundAmountDown(baseAmount, market.precision?.amount); |
72 | | - const roundedPrice = roundToPrecision(params.price, market.precision?.price); |
| 75 | + // Round amount to precision |
| 76 | + const roundedBaseAmount = roundAmountDown(baseAmount, market.precision?.amount); |
| 77 | + const roundedPrice = roundToPrecision(params.price, market.precision?.price); |
73 | 78 |
|
74 | | - if (!roundedBaseAmount || roundedBaseAmount <= 0) { |
75 | | - const minAmount = market.limits?.amount?.min; |
76 | | - throw new Error( |
77 | | - `Order amount too small for ${params.pair}. Minimum is ${minAmount} ${market.base}` + |
78 | | - (params.isQuoteCurrency ? ` (increase your ${market.quote} amount)` : '') |
| 79 | + if (!roundedBaseAmount || roundedBaseAmount <= 0) { |
| 80 | + const minAmount = market.limits?.amount?.min; |
| 81 | + throw new Error( |
| 82 | + `Order amount too small for ${params.pair}. Minimum is ${minAmount} ${market.base}` + |
| 83 | + (params.isQuoteCurrency ? ` (increase your ${market.quote} amount)` : '') |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + const order = await exchange.createOrder( |
| 88 | + params.pair, |
| 89 | + 'limit', |
| 90 | + params.side, |
| 91 | + roundedBaseAmount, |
| 92 | + roundedPrice |
79 | 93 | ); |
80 | | - } |
81 | 94 |
|
82 | | - const order = await exchange.createOrder( |
83 | | - params.pair, |
84 | | - 'limit', |
85 | | - params.side, |
86 | | - roundedBaseAmount, |
87 | | - roundedPrice |
88 | | - ); |
| 95 | + this.logger.info(`Order: ${params.side} ${roundedBaseAmount} ${params.pair} @ ${roundedPrice} (limit)`); |
| 96 | + |
| 97 | + return { |
| 98 | + id: order.id, |
| 99 | + status: order.status, |
| 100 | + type: order.type, |
| 101 | + side: order.side, |
| 102 | + price: order.price, |
| 103 | + amount: order.amount, |
| 104 | + filled: order.filled, |
| 105 | + remaining: order.remaining, |
| 106 | + raw: order |
| 107 | + }; |
| 108 | + } |
89 | 109 |
|
90 | | - return { |
91 | | - id: order.id, |
92 | | - status: order.status, |
93 | | - type: order.type, |
94 | | - side: order.side, |
95 | | - price: order.price, |
96 | | - amount: order.amount, |
97 | | - filled: order.filled, |
98 | | - remaining: order.remaining, |
99 | | - raw: order |
100 | | - }; |
101 | | -} |
| 110 | + /** |
| 111 | + * Places a market order on the exchange |
| 112 | + * If isQuoteCurrency is true, converts quote amount to base amount first |
| 113 | + */ |
| 114 | + async placeMarketOrder(exchange: ccxt.Exchange, params: OrderParams): Promise<OrderResult> { |
| 115 | + // Load markets for precision info |
| 116 | + const markets = await exchange.loadMarkets(); |
| 117 | + const market = markets[params.pair]; |
| 118 | + |
| 119 | + if (!market) { |
| 120 | + throw new Error(`Market ${params.pair} not found`); |
| 121 | + } |
102 | 122 |
|
103 | | -/** |
104 | | - * Places a market order on the exchange |
105 | | - * If isQuoteCurrency is true, converts quote amount to base amount first |
106 | | - */ |
107 | | -export async function placeMarketOrder( |
108 | | - exchange: ccxt.Exchange, |
109 | | - params: OrderParams |
110 | | -): Promise<OrderResult> { |
111 | | - // Load markets for precision info |
112 | | - const markets = await exchange.loadMarkets(); |
113 | | - const market = markets[params.pair]; |
114 | | - |
115 | | - if (!market) { |
116 | | - throw new Error(`Market ${params.pair} not found`); |
117 | | - } |
| 123 | + let baseAmount: number; |
118 | 124 |
|
119 | | - let baseAmount: number; |
| 125 | + if (params.isQuoteCurrency) { |
| 126 | + // Convert quote currency amount to base amount using current price |
| 127 | + const ticker = await exchange.fetchTicker(params.pair); |
| 128 | + const price = ticker.last || ticker.close; |
120 | 129 |
|
121 | | - if (params.isQuoteCurrency) { |
122 | | - // Convert quote currency amount to base amount using current price |
123 | | - const ticker = await exchange.fetchTicker(params.pair); |
124 | | - const price = ticker.last || ticker.close; |
| 130 | + if (!price) { |
| 131 | + throw new Error('Could not fetch current price for market order conversion'); |
| 132 | + } |
125 | 133 |
|
126 | | - if (!price) { |
127 | | - throw new Error('Could not fetch current price for market order conversion'); |
| 134 | + baseAmount = params.amount / price; |
| 135 | + } else { |
| 136 | + baseAmount = params.amount; |
128 | 137 | } |
129 | 138 |
|
130 | | - baseAmount = params.amount / price; |
131 | | - } else { |
132 | | - baseAmount = params.amount; |
133 | | - } |
| 139 | + // Round amount to precision |
| 140 | + const roundedBaseAmount = roundAmountDown(baseAmount, market.precision?.amount); |
134 | 141 |
|
135 | | - // Round amount to precision |
136 | | - const roundedBaseAmount = roundAmountDown(baseAmount, market.precision?.amount); |
| 142 | + if (!roundedBaseAmount || roundedBaseAmount <= 0) { |
| 143 | + const minAmount = market.limits?.amount?.min; |
| 144 | + throw new Error( |
| 145 | + `Order amount too small for ${params.pair}. Minimum is ${minAmount} ${market.base}` + |
| 146 | + (params.isQuoteCurrency ? ` (increase your ${market.quote} amount)` : '') |
| 147 | + ); |
| 148 | + } |
137 | 149 |
|
138 | | - if (!roundedBaseAmount || roundedBaseAmount <= 0) { |
139 | | - const minAmount = market.limits?.amount?.min; |
140 | | - throw new Error( |
141 | | - `Order amount too small for ${params.pair}. Minimum is ${minAmount} ${market.base}` + |
142 | | - (params.isQuoteCurrency ? ` (increase your ${market.quote} amount)` : '') |
| 150 | + const order = await exchange.createOrder( |
| 151 | + params.pair, |
| 152 | + 'market', |
| 153 | + params.side, |
| 154 | + roundedBaseAmount |
143 | 155 | ); |
144 | | - } |
145 | | - |
146 | | - const order = await exchange.createOrder( |
147 | | - params.pair, |
148 | | - 'market', |
149 | | - params.side, |
150 | | - roundedBaseAmount |
151 | | - ); |
152 | 156 |
|
153 | | - return { |
154 | | - id: order.id, |
155 | | - status: order.status, |
156 | | - type: order.type, |
157 | | - side: order.side, |
158 | | - price: order.price, |
159 | | - amount: order.amount, |
160 | | - filled: order.filled, |
161 | | - remaining: order.remaining, |
162 | | - raw: order |
163 | | - }; |
| 157 | + this.logger.info(`Order: ${params.side} ${roundedBaseAmount} ${params.pair} @ market`); |
| 158 | + |
| 159 | + return { |
| 160 | + id: order.id, |
| 161 | + status: order.status, |
| 162 | + type: order.type, |
| 163 | + side: order.side, |
| 164 | + price: order.price, |
| 165 | + amount: order.amount, |
| 166 | + filled: order.filled, |
| 167 | + remaining: order.remaining, |
| 168 | + raw: order |
| 169 | + }; |
| 170 | + } |
164 | 171 | } |
165 | 172 |
|
166 | 173 | /** |
|
0 commit comments