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; 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 GenerateTradeMapNextLevel(double currBuyPrice) { if (CurrentLevel < this.ScaleLevel - 1) { TradeMapEntry entry = new TradeMapEntry(); entry.Level = CurrentLevel + 1; double price = (IsPctScaleFactor) ? currBuyPrice * (1 - ScaleFactor) : currBuyPrice - ScaleFactor; entry.TargetBuyPrice = price <= 0 ? 0 : Util.NormalizePrice(price); if (this.IsShare) { entry.TargetQty = this.ShareOrDollarAmt; } else { entry.TargetQty = Math.Floor(this.ShareOrDollarAmt / entry.TargetBuyPrice); } double adj = 0d; if (this.IsAdjPct) { adj = entry.Level * entry.TargetQty * this.AdjQty / (double)100; } else { adj = this.AdjQty * entry.Level; } entry.TargetQty += adj; if (entry.TargetQty < 0) { entry.TargetQty = 0; } TradeMap[entry.Level] = entry; } }
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); }