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 static string PrintTradeMapCurrLvl(RollingAlgo algo) { var entry = algo.TradeMap[algo.CurrentLevel]; return(string.Format("CurrentLvl: {0}, Qty: {1}, IsFilled: {2}, LastBuyPx: {3}, TargetSellPx: {4} ", algo.CurrentLevel, entry.CurrentQty, entry.Filled, entry.LastBuyPrice, entry.TargetSellPrice)); }
public Dictionary <int, TradeMapEntry> InitializeTradeMap(RollingAlgo algo) { Dictionary <int, TradeMapEntry> res = new Dictionary <int, TradeMapEntry>(); double buyPrice = algo.BeginPrice; double sellPrice = int.MaxValue; double qty = (algo.IsShare) ? algo.ShareOrDollarAmt : Math.Floor(algo.ShareOrDollarAmt / algo.BeginPrice); for (int i = 0; i <= algo.ScaleLevel; i++) { TradeMapEntry entry = new TradeMapEntry(); entry.Level = i; if (i == 0) { entry.TargetBuyPrice = buyPrice; entry.TargetSellPrice = sellPrice; entry.TargetQty = qty; } else { double tmpprice = (algo.IsPctScaleFactor) ? buyPrice * (1 - algo.ScaleFactor) : buyPrice - algo.ScaleFactor; buyPrice = tmpprice <= 0 ? 0 : Util.NormalizePrice(tmpprice); entry.TargetBuyPrice = buyPrice; double tmpprice2 = (algo.IsPctScaleFactor) ? buyPrice / (1 - algo.ScaleFactor) : buyPrice + algo.ScaleFactor; sellPrice = tmpprice2 <= 0 ? 0 : Util.NormalizePrice(tmpprice2); entry.TargetSellPrice = sellPrice; qty = (algo.IsShare) ? algo.ShareOrDollarAmt : Math.Floor(algo.ShareOrDollarAmt / entry.TargetBuyPrice); entry.TargetQty = qty; double adj = 0d; if (algo.IsAdjPct) { adj = entry.Level * entry.TargetQty * algo.AdjQty / (double)100; } else { adj = algo.AdjQty * entry.Level; } entry.TargetQty += adj; if (entry.TargetQty < 0) { entry.TargetQty = 0; } } res[i] = entry; } 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); }
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); }