public static IBfxOrder Trailing(BfTradeSide side, decimal trailingOffset, decimal size) { var request = BfParentOrderRequest.Trail(BfProductCode.Unknown, side, trailingOffset, size, 0, BfTimeInForce.NotSpecified); var order = new BfxParentOrder(request); return(order); }
public static IBfxOrder StopLimit(BfTradeSide side, decimal triggerPrice, decimal orderPrice, decimal size) { var request = BfParentOrderRequest.StopLimit(BfProductCode.Unknown, side, triggerPrice, orderPrice, size, 0, BfTimeInForce.NotSpecified); var order = new BfxParentOrder(request); return(order); }
//====================================================================== // From element of parent order // public DbChildOrder(BfParentOrderRequest req, BfParentOrderResponse resp, int childOrderIndex) { ProductCode = req.Parameters[childOrderIndex].ProductCode; Side = req.Parameters[childOrderIndex].Side; OrderType = req.Parameters[childOrderIndex].ConditionType; OrderSize = req.Parameters[childOrderIndex].Size; MinuteToExpire = req.MinuteToExpire; TimeInForce = req.TimeInForce; if (OrderType is BfOrderType.Limit or BfOrderType.StopLimit) { OrderPrice = req.Parameters[childOrderIndex].Price; } if (OrderType is BfOrderType.Stop or BfOrderType.StopLimit) { TriggerPrice = req.Parameters[childOrderIndex].TriggerPrice; } if (OrderType == BfOrderType.Trail) { Offset = req.Parameters[childOrderIndex].Offset; } ParentOrderAcceptanceId = resp.ParentOrderAcceptanceId; ChildOrderIndex = childOrderIndex; }
public static BfParentOrderRequestParameter ToParameter(this BfParentOrderRequest parent) { if (parent.Paremters.Count != 1) { throw new ArgumentException(); } return(parent.Paremters[0]); }
// Parent orders public void Insert(BfProductCode productCode, BfParentOrderRequest req, BfParentOrderResponse resp) { ParentOrders.Add(new DbParentOrder(productCode, req, resp)); for (int childOrderIndex = 0; childOrderIndex < req.Parameters.Count; childOrderIndex++) { ChildOrders.Add(new DbChildOrder(req, resp, childOrderIndex)); } }
public void SendParentOrder() { EnableSendOrder(false); var order = BfParentOrderRequest.Stop(_productCode, BfTradeSide.Sell, 100000m, 0.1m); var resp = _client.SendParentOrder(order); Assert.IsTrue(resp.IsOk); Console.WriteLine(GetRequestJson()); }
public static IBfxOrder IFD(IBfxOrder ifOrder, IBfxOrder doneOrder) { var request = BfParentOrderRequest.IFD( GetParentOrderRequestParameter(ifOrder), GetParentOrderRequestParameter(doneOrder), 0, BfTimeInForce.NotSpecified ); return(new BfxParentOrder(request)); }
public static IBfxOrder OCO(IBfxOrder firstOrder, IBfxOrder secondOrder) { var request = BfParentOrderRequest.OCO( GetParentOrderRequestParameter(firstOrder), GetParentOrderRequestParameter(secondOrder), 0, BfTimeInForce.NotSpecified ); return(new BfxParentOrder(request)); }
public BfxParentOrder(BfParentOrderRequest request) { Request = request; ProductCode = request.Paremters[0].ProductCode; foreach (var childRequest in request.Paremters) { ChildOrders.Add(new BfxChildOrder(childRequest)); } }
public DbParentOrder(BfProductCode productCode, BfParentOrderRequest req, BfParentOrderResponse resp) { ProductCode = productCode; OrderType = req.OrderMethod; TimeInForce = req.TimeInForce; if (TimeInForce == BfTimeInForce.NotSpecified) { TimeInForce = BfTimeInForce.GTC; } AcceptanceId = resp.ParentOrderAcceptanceId; }
public void SendParentOrder() { var order = new BfParentOrderRequest(); var resp = _client.SendParentOrder(order); if (CheckUnauthorized(resp)) { return; } Assert.IsFalse(resp.IsError); DumpJson(resp); }
public void CheckParentOrderRequestValid(BfParentOrderRequest request) { foreach (var childRequest in request.Paremters) { if (request.Paremters[0].ProductCode != childRequest.ProductCode) { throw new ArgumentException("Different product code is set to child order"); } CheckChildOrderRequestValid(childRequest); switch (request.OrderMethod) { case BfOrderType.Simple: switch (childRequest.ConditionType) { case BfOrderType.Stop: case BfOrderType.StopLimit: case BfOrderType.Trail: break; default: throw new ArgumentException("Illegal child order type"); } break; case BfOrderType.IFD: case BfOrderType.OCO: case BfOrderType.IFDOCO: switch (childRequest.ConditionType) { case BfOrderType.Limit: case BfOrderType.Market: case BfOrderType.Stop: case BfOrderType.StopLimit: case BfOrderType.Trail: break; default: throw new ArgumentException("Illegal child order type"); } break; default: throw new ArgumentException("Illegal parent order type"); } } }
public BfParentOrderRequest CreateStopLimitOrder(BfTradeSide side, decimal size, decimal price, decimal stopTriggerPrice) { if (size > price || size > stopTriggerPrice) { throw new ArgumentException(); } var request = new BfParentOrderRequest { OrderMethod = BfOrderType.Simple }; request.Paremters.Add(new BfParentOrderRequestParameter { ProductCode = _market.ProductCode, ConditionType = BfOrderType.StopLimit, Side = side, Size = size, Price = price, TriggerPrice = stopTriggerPrice }); CheckParentOrderRequestValid(request); return(request); }
public BfParentOrderRequest CreateTrailOrder(BfTradeSide side, decimal size, decimal trailingStopPriceOffset) { if (size > trailingStopPriceOffset) { throw new ArgumentException(); } var request = new BfParentOrderRequest { OrderMethod = BfOrderType.Simple }; request.Paremters.Add(new BfParentOrderRequestParameter { ProductCode = _market.ProductCode, ConditionType = BfOrderType.Trail, Side = side, Size = size, Offset = trailingStopPriceOffset }); CheckParentOrderRequestValid(request); return(request); }
public BfParentOrderRequest CreateOCO(BfParentOrderRequestParameter first, BfParentOrderRequestParameter second, TimeSpan?minuteToExpire = null, BfTimeInForce timeInForce = BfTimeInForce.NotSpecified) { var request = new BfParentOrderRequest { OrderMethod = BfOrderType.OCO }; if (minuteToExpire.HasValue) { request.MinuteToExpire = (int)minuteToExpire.Value.TotalMinutes; } request.TimeInForce = timeInForce; request.Paremters.Add(first); request.Paremters.Add(second); CheckParentOrderRequestValid(request); return(request); }
public BfxParentOrder(BfParentOrderRequest request) : this(request.OrderMethod) { Request = request; _orderMethod = request.OrderMethod; _childOrders = request.Parameters.Select(e => new BfxChildOrder(e)).ToArray(); ProductCode = _childOrders[0].ProductCode; if (request.OrderMethod == BfOrderType.Simple) { var childOrder = _childOrders[0]; OrderType = childOrder.OrderType; Side = childOrder.Side; OrderSize = childOrder.OrderSize; OrderPrice = childOrder.OrderPrice; TriggerPrice = childOrder.TriggerPrice; TrailOffset = childOrder.TrailOffset; } else { OrderType = request.OrderMethod; } }
public Task <BfxParentOrderTransaction> PlaceOrder(BfParentOrderRequest request, object tag = null) { DebugEx.Trace(); return(Task.Run(() => { DebugEx.Trace(); var trans = new BfxParentOrderTransaction(this, request); trans.ChildOrderChanged += OnChildOrderChanged; trans.ParentOrderStateChanged += OnParentOrderTransactionStateChanged; trans.ParentOrderChanged += OnParentOrderChanged; trans.Tag = tag; for (var retry = 0; retry <= Config.OrderRetryMax; retry++) { DebugEx.Trace(); if (!trans.IsOrderable()) { DebugEx.Trace(); trans.Dispose(); return null; // order is unacceptable } if (trans.SendOrder()) { DebugEx.Trace(); return trans; } DebugEx.Trace("Trying retry..."); Thread.Sleep(Config.OrderRetryInterval); } DebugEx.Trace(); trans.Dispose(); return null; // Retried out })); }
void IBfOrderSource.OpenParentOrder(BfParentOrderRequest req, BfParentOrderResponse resp) { }
public BfxParentOrderTransaction(BfTradingMarket market, BfParentOrderRequest request) { DebugEx.EnterMethod(); _market = market; State = new BfxParentOrderTransactionState(_market, request); }
public BfxParentOrderTransactionState(BfTradingMarket market, BfParentOrderRequest request) : base(request) { _market = market; }
static void ParentOrderMain() { while (true) { Console.WriteLine("======== Parent order operations"); Console.WriteLine("S)top Stop L)imit T)rail"); Console.WriteLine("I)FD O)CO IFD)OCO"); Console.WriteLine(""); Console.WriteLine("Time in F)orce"); Console.WriteLine("M)inutes to expire"); Console.WriteLine(""); Console.WriteLine("C) Cancel parent order"); Console.WriteLine("G)et parent orders"); Console.WriteLine(""); Console.WriteLine("R) Return to main"); try { switch (GetCh()) { case 'S': { var request = BfParentOrderRequest.Stop(ProductCode, BfTradeSide.Buy, _ticker.BestAsk + UnexecuteGap, OrderSize); var content = _client.SendParentOrder(request).GetContent(); _parentOrderAcceptanceIds.Enqueue(content.ParentOrderAcceptanceId); } break; case 'L': { var request = BfParentOrderRequest.StopLimit(ProductCode, BfTradeSide.Buy, _ticker.BestAsk + UnexecuteGap, _ticker.BestAsk + UnexecuteGap, OrderSize); var content = _client.SendParentOrder(request).GetContent(); _parentOrderAcceptanceIds.Enqueue(content.ParentOrderAcceptanceId); } break; case 'T': { var request = BfParentOrderRequest.Trail(ProductCode, BfTradeSide.Buy, UnexecuteGap, OrderSize); var content = _client.SendParentOrder(request).GetContent(); _parentOrderAcceptanceIds.Enqueue(content.ParentOrderAcceptanceId); } break; case 'I': { var request = BfParentOrderRequest.IFD( BfParentOrderRequestParameter.Limit(ProductCode, BfTradeSide.Sell, _ticker.BestAsk + UnexecuteGap, OrderSize), BfParentOrderRequestParameter.Limit(ProductCode, BfTradeSide.Buy, _ticker.BestBid - UnexecuteGap, OrderSize) ); var content = _client.SendParentOrder(request).GetContent(); _parentOrderAcceptanceIds.Enqueue(content.ParentOrderAcceptanceId); } break; case 'O': { var request = BfParentOrderRequest.OCO( BfParentOrderRequestParameter.Limit(ProductCode, BfTradeSide.Sell, _ticker.BestAsk + UnexecuteGap, OrderSize), BfParentOrderRequestParameter.Limit(ProductCode, BfTradeSide.Buy, _ticker.BestBid - UnexecuteGap, OrderSize) ); var content = _client.SendParentOrder(request).GetContent(); _parentOrderAcceptanceIds.Enqueue(content.ParentOrderAcceptanceId); } break; case 'D': { var request = BfParentOrderRequest.IFDOCO( BfParentOrderRequestParameter.Limit(ProductCode, BfTradeSide.Sell, _ticker.BestAsk + UnexecuteGap, OrderSize), BfParentOrderRequestParameter.Limit(ProductCode, BfTradeSide.Sell, _ticker.BestAsk + UnexecuteGap, OrderSize), BfParentOrderRequestParameter.Limit(ProductCode, BfTradeSide.Buy, _ticker.BestBid - UnexecuteGap, OrderSize) ); var content = _client.SendParentOrder(request).GetContent(); _parentOrderAcceptanceIds.Enqueue(content.ParentOrderAcceptanceId); } break; case 'F': break; case 'M': break; case 'C': { var resp = _client.CancelParentOrder(ProductCode, parentOrderAcceptanceId: _parentOrderAcceptanceIds.Dequeue()); if (!resp.IsErrorOrEmpty) { // Cancel order sent } } break; case 'G': { var resp = _client.GetParentOrders(ProductCode); if (resp.IsOk) { var jobj = JsonConvert.DeserializeObject(resp.Json); var json = JsonConvert.SerializeObject(jobj, Formatting.Indented); Console.WriteLine(json); } } break; case 'R': return; } } catch (Exception ex) { Console.WriteLine($"{ex.Message}"); } } }