diff --git a/roboquant-alpaca/src/main/kotlin/org/roboquant/alpaca/AlpacaBroker.kt b/roboquant-alpaca/src/main/kotlin/org/roboquant/alpaca/AlpacaBroker.kt index 18b83d02..a98cdf63 100644 --- a/roboquant-alpaca/src/main/kotlin/org/roboquant/alpaca/AlpacaBroker.kt +++ b/roboquant-alpaca/src/main/kotlin/org/roboquant/alpaca/AlpacaBroker.kt @@ -127,12 +127,10 @@ class AlpacaBroker( private fun syncOrders() { _account.orders.forEach { if (it.open) { - val aOrderId = it.order.id - logger.info { "open order id=$aOrderId" } - val alpacaOrder = alpacaAPI.trader().orders().getOrderByOrderID(UUID.fromString(aOrderId), false) + val orderId = UUID.fromString(it.order.id) + val alpacaOrder = alpacaAPI.trader().orders().getOrderByOrderID(orderId, false) + logger.info { "open order id=$orderId alpaca-order=$alpacaOrder" } updateIAccountOrder(it.order, alpacaOrder) - } else { - logger.warn("cannot find order=${it.order} in orderMap") } } } @@ -147,7 +145,6 @@ class AlpacaBroker( logger.debug { "received open $order" } val rqOrder = toOrder(order) _account.initializeOrders(listOf(rqOrder)) - orderPlacer.addExistingOrder(rqOrder, order.id!!) updateIAccountOrder(rqOrder, order) } } @@ -280,21 +277,16 @@ class AlpacaBroker( * @return the updated account that reflects the latest state */ override fun place(orders: List) { - val now = Instant.now() - _account.initializeOrders(orders) for (order in orders) { when (order) { - is SingleOrder -> orderPlacer.placeSingleOrder(order) - is CancelOrder -> { - val sucess = orderPlacer.cancelOrder(order) - val status = if (sucess) OrderStatus.COMPLETED else OrderStatus.REJECTED - _account.updateOrder(order, now, status) + is SingleOrder -> { + orderPlacer.placeSingleOrder(order) + _account.initializeOrders(listOf(order)) } - + is CancelOrder -> orderPlacer.cancelOrder(order) else -> { - logger.warn { "unsupported order type order=$order" } - _account.updateOrder(order, now, OrderStatus.REJECTED) + logger.warn { "ignoring unsupported order type order=$order" } } } } diff --git a/roboquant-alpaca/src/main/kotlin/org/roboquant/alpaca/AlpaceOrderPlacer.kt b/roboquant-alpaca/src/main/kotlin/org/roboquant/alpaca/AlpaceOrderPlacer.kt index d68dc140..a8605de0 100644 --- a/roboquant-alpaca/src/main/kotlin/org/roboquant/alpaca/AlpaceOrderPlacer.kt +++ b/roboquant-alpaca/src/main/kotlin/org/roboquant/alpaca/AlpaceOrderPlacer.kt @@ -31,18 +31,12 @@ import net.jacobpeterson.alpaca.openapi.trader.model.TimeInForce as OrderTimeInF */ internal class AlpaceOrderPlacer(private val alpacaAPI: AlpacaAPI, private val extendedHours: Boolean = false) { - private val orders = mutableMapOf() - - fun cancelOrder(cancellation: CancelOrder): Boolean { - if (cancellation !in orders) return false - - val orderId = orders[cancellation.order] - alpacaAPI.trader().orders().deleteOrderByOrderID(UUID.fromString(orderId)) - return true + fun cancelOrder(cancellation: CancelOrder) { + val orderId = UUID.fromString(cancellation.id) + alpacaAPI.trader().orders().deleteOrderByOrderID(orderId) } - fun get(order: Order) = orders[order] private fun TimeInForce.toOrderTimeInForce(): OrderTimeInForce { @@ -72,18 +66,18 @@ internal class AlpaceOrderPlacer(private val alpacaAPI: AlpacaAPI, private val e .timeInForce(tif) .extendedHours(extendedHours) - when (order) { + when (order) { - is LimitOrder -> { - result.type(OrderType.LIMIT) - result.limitPrice(order.limit.toString()) - } + is LimitOrder -> { + result.type(OrderType.LIMIT) + result.limitPrice(order.limit.toString()) + } - is MarketOrder -> result.type(OrderType.MARKET) + is MarketOrder -> result.type(OrderType.MARKET) - else -> throw UnsupportedException("unsupported ordertype $order") + else -> throw UnsupportedException("unsupported ordertype $order") - } + } return result } @@ -109,12 +103,7 @@ internal class AlpaceOrderPlacer(private val alpacaAPI: AlpacaAPI, private val e } - /** - * Add an order that already existed when starting the broker implementation - */ - fun addExistingOrder(rqOrder: Order, id: String) { - orders[rqOrder] = id - } + } diff --git a/roboquant-questdb/src/main/kotlin/org/roboquant/questdb/QuestDBJournal.kt b/roboquant-questdb/src/main/kotlin/org/roboquant/questdb/QuestDBJournal.kt index f4e81f18..49bdc938 100644 --- a/roboquant-questdb/src/main/kotlin/org/roboquant/questdb/QuestDBJournal.kt +++ b/roboquant-questdb/src/main/kotlin/org/roboquant/questdb/QuestDBJournal.kt @@ -92,9 +92,9 @@ class QuestDBJournal( } - private inline fun CairoEngine.appendRows(tableName: String, block: TableWriter.() -> Unit) { - val token = ctx.getTableToken(tableName) - getWriter(token, tableName).use { + private inline fun appendRows(block: TableWriter.() -> Unit) { + val token = ctx.getTableToken(table) + ctx.cairoEngine.getWriter(token, table).use { it.block() it.commit() } @@ -174,7 +174,7 @@ class QuestDBJournal( result.putAll(values) } - engine.appendRows(table) { + appendRows { val t = event.time.epochMicro for ((k, v) in result) { val row = newRow(t)