public List <TradeOrder> CreateOrder(ParentOrder parentOrder) { List <TradeOrder> res = new List <TradeOrder>(); RollingAlgo algo = (RollingAlgo)parentOrder.Algo; if (algo.CurrentLevel <= 0) { return(res); } var entry = algo.TradeMap[algo.CurrentLevel]; //if(entry.PartialFilled && !entry.SellOnPartil) //{ // //on partial filled, if buy, then sell upper level // entry = algo.TradeMap[algo.CurrentLevel - 1]; //} if (entry.Level > 0 && entry.WasFilledSellOnPartial) { double price = Util.AdjustOrderPrice(TradeType.Sell, parentOrder.Symbol, entry.TargetSellPrice); double qty = entry.CurrentQty; var order = TradeManager.Instance.PlaceOrder(parentOrder.ID, TradeType.Sell, parentOrder.Symbol, price, qty); if (order != null) { order.Notes = algo.CurrentLevel.ToString(); res.Add(order); } } return(res); }
public bool RemoveParentOrderByID(int ID) { int idx = -1; ParentOrder parentOrder = GetParentOrderByParentID(ID); if (parentOrder == null) { return(false); } lock (locker) { //Cancel open orders for this parentOrder var openOrders = parentOrder.GetOpenOrders(); TradeManager.Instance.CancelOrders(openOrders); } Thread.Sleep(2000); lock (locker) { if (Parent_Child_Order_Map.ContainsKey(ID)) { Parent_Child_Order_Map.Remove(ID); } List <int> remove = new List <int>(); foreach (int childID in Child_Parent_Order_Map.Keys) { if (Child_Parent_Order_Map[childID] == parentOrder.ID) { remove.Add(childID); } } foreach (int item in remove) { Child_Parent_Order_Map.Remove(item); } for (int i = 0; i < ParentOrderList.Count; i++) { if (ParentOrderList[i].ID == ID) { idx = i; break; } } if (idx != -1) { ParentOrderList.RemoveAt(idx); } } StateManager.Save(); return(idx != -1); }
public void AddParentOrder(ParentOrder parentOrder) { lock (locker) { ParentOrderList.Add(parentOrder); if (!Parent_Child_Order_Map.ContainsKey(parentOrder.ID)) { Parent_Child_Order_Map[parentOrder.ID] = new List <int>(); } } }
public void CloseParentOrderByID(int ID) { ParentOrder parent = GetParentOrderByParentID(ID); ParentOrderManager.Instance.StopParentOrder(ID); Thread.Sleep(500); //Please mkt order if (parent != null) { Log.Info(string.Format("Close parent order {0}, qty: {1}, market order ", parent.ID, parent.Qty)); var res = TradeManager.Instance.PlaceOrder(ID, TradeType.Sell, parent.Symbol, 0, parent.Qty, null, OrderType.MKT); } }
public ParentOrder CreateParentOrder(string symbol, double openQty, Algo algo) { int id = -1; do { id = new Random().Next(1000 * 1000); } while (Parent_Child_Order_Map.ContainsKey(id)); ParentOrder parentOrder = new ParentOrder(id, symbol, openQty, algo); Log.Info(string.Format("ParentOrder {0} is created, symbol {1}", id, symbol)); return(parentOrder); }
private void HandleSellExecution(ParentOrder parentOrder, TradeExecution execution) { TradeOrder order = parentOrder.GetChildOrderByID(execution.OrderID); if (order == null) { Log.Error(string.Format("Cannot find trade order, orderID: {0}", execution.OrderID)); } else { if (string.IsNullOrWhiteSpace(order.Notes)) { Log.Warn("HandleSellExecution error. Cannot resolve execution level due to emtpy order notes"); } else { int exeLevel = int.Parse(order.Notes); TradeMap[exeLevel].CurrentQty -= execution.Shares; if (TradeMap[exeLevel].CurrentQty < 0) { //should not hit here Log.Error(string.Format("Negative CurrentQty detected. level: {0}, qty: {1}", exeLevel, TradeMap[exeLevel].CurrentQty)); } if (TradeMap[exeLevel].CurrentQty <= 0) { if (TradeMap.ContainsKey(CurrentLevel)) { TradeMap[CurrentLevel].WasFilledSellOnPartial = false; TradeMap[CurrentLevel].LastBuyPrice = 0; } CurrentLevel--; //TradeMap.Remove(CurrentLevel); //for (int i = CurrentLevel + 1; i <= ScaleLevel; i++) //{ // if (TradeMap.ContainsKey(i)) TradeMap.Remove(i); //} //if (CurrentLevel >= 0) //{ // GenerateTradeMapNextLevel(TradeMap[CurrentLevel].LastBuyPrice); //} } } Log.Info("After sld execution." + Util.PrintTradeMapCurrLvl(this)); } }
public override void HandleExecutionMsg(ParentOrder parentOrder, TradeExecution tradeExecution) { if (tradeExecution.Side.ToUpper() == Constant.ExecutionBuy) { HandleBuyExecution(parentOrder, tradeExecution); } else if (tradeExecution.Side.ToUpper() == Constant.ExecutionSell) { HandleSellExecution(parentOrder, tradeExecution); } else { Log.Error(string.Format("Unsupported execution. Side {0}", tradeExecution.Side)); } }
public void AddChildOrder(TradeOrder tradeOrder) { if (!Parent_Child_Order_Map.ContainsKey(tradeOrder.ParentOrderID)) { return; } lock (locker) { ParentOrder pOrder = GetParentOrderByParentID(tradeOrder.ParentOrderID); pOrder.AddChildOrder(tradeOrder); Parent_Child_Order_Map[tradeOrder.ParentOrderID].Add(tradeOrder.OrderID); Child_Parent_Order_Map[tradeOrder.OrderID] = tradeOrder.ParentOrderID; } }
public ParentOrder FindAssociatedParentOrderByChildID(int childOrderID) { ParentOrder res = null; lock (locker) { if (Child_Parent_Order_Map.ContainsKey(childOrderID)) { int pID = Child_Parent_Order_Map[childOrderID]; res = GetParentOrderByParentID(pID); } } return(res); }
public override List <TradeOrder> Eval(ParentOrder parentOrder) { List <TradeOrder> res = new List <TradeOrder>(); foreach (var rule in TradeRules) { var list = rule.CreateOrder(parentOrder); if (list != null && list.Count > 0) { res.AddRange(list); } } return(res); }
public ParentOrder GetParentOrderByParentID(int ID) { ParentOrder res = null; lock (locker) { foreach (ParentOrder p in ParentOrderList) { if (p.ID == ID) { res = p; break; } } } return(res); }
public Dictionary <int, TradeMapEntry> GetTradeMapByParentID(int parentOrderID) { Dictionary <int, TradeMapEntry> res = null; ParentOrder p = ParentOrderManager.Instance.GetParentOrderByParentID(parentOrderID); if (p != null) { res = ((RollingAlgo)p.Algo).TradeMap; } else { Log.Error(string.Format("Cannot find parent order ID: " + parentOrderID)); } return(res); }
public List <TradeOrder> CreateOrder(ParentOrder parentOrder) { List <TradeOrder> res = new List <TradeOrder>(); RollingAlgo algo = (RollingAlgo)parentOrder.Algo; if (algo.CurrentLevel != 0) { return(res); } var entry = algo.TradeMap[algo.CurrentLevel]; double lastPrice = MarketDataManager.Instance.GetLastPrice(parentOrder.Symbol); if (entry.WasFilledSellOnPartial) { double soldPct = 1 - entry.CurrentQty / entry.TargetQty; if (SellPct - soldPct > 0.001) { double qty = SellPct * entry.TargetQty - (entry.TargetQty - entry.CurrentQty); double price = Util.AdjustOrderPrice(TradeType.Sell, parentOrder.Symbol, entry.LastBuyPrice * (1 + priceUpPct)); if (Util.IsLimitPriceInMktRange(TradeType.Sell, parentOrder.Symbol, price, lastPrice)) { var order = TradeManager.Instance.PlaceOrder(parentOrder.ID, TradeType.Sell, parentOrder.Symbol, price, qty); if (order != null) { order.Notes = algo.CurrentLevel.ToString(); res.Add(order); } } else { Log.Info("Not place first level order due to outside last price range"); Log.Info(string.Format("Symbol:{0}, TradeType: {1}, Price: {2}, MarketPx: {3}", parentOrder.Symbol, TradeType.Sell.ToString(), price, lastPrice )); } } } return(res); }
public List <TradeOrder> CreateOrder(ParentOrder parentOrder) { List <TradeOrder> res = new List <TradeOrder>(); RollingAlgo algo = (RollingAlgo)parentOrder.Algo; TradeMapEntry entry = null; int orderLevel = -99; if (algo.CurrentLevel == -1) { //entry = algo.TradeMap[0]; orderLevel = 0; } else { TradeMapEntry currEntry = algo.TradeMap[algo.CurrentLevel]; if (currEntry.PartialFilled && !currEntry.WasFilledSellOnPartial) { //On partial filled, if buy, then buy current level orderLevel = algo.CurrentLevel; } else if (currEntry.Filled && algo.CurrentLevel <= algo.ScaleLevel - 1) { //buy next level orderLevel = algo.CurrentLevel + 1; } } entry = algo.TradeMap.ContainsKey(orderLevel) ? algo.TradeMap[orderLevel] : null; if (entry != null) { double price = Util.AdjustOrderPrice(TradeType.Buy, parentOrder.Symbol, entry.TargetBuyPrice); double qty = entry.TargetQty - entry.CurrentQty; var order = TradeManager.Instance.PlaceOrder(parentOrder.ID, TradeType.Buy, parentOrder.Symbol, price, qty); if (order != null) { order.Notes = orderLevel.ToString(); res.Add(order); } } return(res); }
private void HandleBuyExecution(ParentOrder parentOrder, TradeExecution execution) { /* * check if buy execution create new tradeMap entry * 1.curr level =-1; * 2. Curre level filled and next level exist * 3. curr level partial filled but wasfilled and next level exist * 4. curr level is zero and partial filled and buyback enabled * * */ TradeOrder order = parentOrder.GetChildOrderByID(execution.OrderID); if (order == null) { Log.Error(string.Format("Cannot find trade order, orderID: {0}", execution.OrderID)); } else { int exeLevel = int.Parse(order.Notes); TradeMap[exeLevel].CurrentQty += execution.Shares; if (TradeMap[exeLevel].CurrentQty > TradeMap[exeLevel].TargetQty) { //should not hit here Log.Error(string.Format("Overbot Qty detected. level: {0}, qty: {1}, target qty: {2}", exeLevel, TradeMap[exeLevel].CurrentQty, TradeMap[exeLevel].TargetQty)); } TradeMap[exeLevel].LastBuyPrice = execution.Price; //TradeMap[exeLevel].TargetSellPrice = (exeLevel == 0) ? int.MaxValue : TradeMap[exeLevel - 1].LastBuyPrice; CurrentLevel = Math.Max(CurrentLevel, exeLevel); if (TradeMap[CurrentLevel].Filled) { TradeMap[CurrentLevel].WasFilledSellOnPartial = true; //GenerateTradeMapNextLevel(TradeMap[CurrentLevel].LastBuyPrice); } Log.Info("After bot execution." + Util.PrintTradeMapCurrLvl(this)); } }
public bool UpdateTradeMapByParentID(int parentOrderID, Dictionary <int, TradeMapEntry> map, int currLvl) { bool res = false; ParentOrder p = ParentOrderManager.Instance.GetParentOrderByParentID(parentOrderID); if (p != null) { RollingAlgo algo = (RollingAlgo)p.Algo; algo.TradeMap = map; algo.CurrentLevel = currLvl; res = true; } else { Log.Error(string.Format("Cannot find parent order ID: " + parentOrderID)); } return(res); }
public List <TradeOrder> CreateOrder(ParentOrder parentOrder) { List <TradeOrder> res = new List <TradeOrder>(); RollingAlgo algo = (RollingAlgo)parentOrder.Algo; if (algo.BuyBackLvlZero && algo.TradeMap.ContainsKey(algo.CurrentLevel)) { TradeMapEntry entry = algo.TradeMap[algo.CurrentLevel]; if (algo.CurrentLevel == 0 && !entry.Filled && entry.WasFilledSellOnPartial) { double price = Util.AdjustOrderPrice(TradeType.Buy, parentOrder.Symbol, entry.TargetBuyPrice); double qty = entry.TargetQty - entry.CurrentQty; var order = TradeManager.Instance.PlaceOrder(parentOrder.ID, TradeType.Buy, parentOrder.Symbol, price, qty); if (order != null) { order.Notes = algo.CurrentLevel.ToString(); res.Add(order); } } } return(res); }
public abstract List <TradeOrder> Eval(ParentOrder parentOrder);
public abstract void HandleExecutionMsg(ParentOrder parentOrder, TradeExecution tradeExecution);