/// <summary> /// Places a new order /// </summary> /// <param name="type">The type of the order</param> /// <param name="symbol">The symbol the order is for</param> /// <param name="amount">The amount of the order, positive for buying, negative for selling</param> /// <param name="groupId">Group id to assign to the order</param> /// <param name="clientOrderId">Client order id to assign to the order</param> /// <param name="price">Price of the order</param> /// <param name="priceTrailing">Trailing price of the order</param> /// <param name="priceAuxiliaryLimit">Auxiliary limit price of the order</param> /// <param name="priceOcoStop">Oco stop price of ther order</param> /// <param name="flags">Additional flags</param> /// <returns></returns> public async Task <CallResult <BitfinexOrder> > PlaceOrderAsync(OrderType type, string symbol, decimal amount, int?groupId = null, int?clientOrderId = null, decimal?price = null, decimal?priceTrailing = null, decimal?priceAuxiliaryLimit = null, decimal?priceOcoStop = null, OrderFlags?flags = null) { if (!CheckConnection()) { return(new CallResult <BitfinexOrder>(null, new WebError("Socket needs to be started before placing an order"))); } if (!authenticated) { return(new CallResult <BitfinexOrder>(null, new NoApiCredentialsError())); } log.Write(LogVerbosity.Info, "Going to place order"); var order = new BitfinexNewOrder() { Amount = amount, OrderType = type, Symbol = symbol, Price = price, ClientOrderId = clientOrderId, Flags = flags, GroupId = groupId, PriceAuxiliaryLimit = priceAuxiliaryLimit, PriceOCOStop = priceOcoStop, PriceTrailing = priceTrailing }; var wrapper = new object[] { 0, "on", null, order }; var data = JsonConvert.SerializeObject(wrapper, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Culture = CultureInfo.InvariantCulture }); BitfinexOrder orderConfirm = null; await Task.Run(() => { var waitAction = new WaitAction <BitfinexOrder>(); pendingOrders.Add(order, waitAction); Send(data); orderConfirm = waitAction.Wait(20000); pendingOrders.Remove(order); }).ConfigureAwait(false); if (orderConfirm != null) { log.Write(LogVerbosity.Info, "Order canceled"); } return(new CallResult <BitfinexOrder>(orderConfirm, orderConfirm != null ? null : new ServerError("No confirmation received for placed order"))); }
/// <summary> /// Cancels an order /// </summary> /// <param name="orderId">The id of the order to cancel</param> /// <returns></returns> public async Task <CallResult <bool> > CancelOrderAsync(long orderId) { if (!CheckConnection()) { return(new CallResult <bool>(false, new WebError("Socket needs to be started before canceling an order"))); } if (!authenticated) { return(new CallResult <bool>(false, new NoApiCredentialsError())); } log.Write(LogVerbosity.Info, "Going to cancel order " + orderId); var obj = new JObject { ["id"] = orderId }; var wrapper = new JArray(0, "oc", null, obj); var data = JsonConvert.SerializeObject(wrapper); bool done = false; await Task.Run(() => { var waitAction = new WaitAction(); pendingCancels.Add(orderId, waitAction); Send(data); done = waitAction.Wait(20000); pendingCancels.Remove(orderId); }).ConfigureAwait(false); if (done) { log.Write(LogVerbosity.Info, "Order canceled"); } return(new CallResult <bool>(done, done ? null : new ServerError("No confirmation received for canceling order"))); }