private void TriggerQuickBuy(string symbol) { DateTime triggerTime = DateTime.Now; Order orderObject = null; if (this.CurrentAccount.Type == "Long") { orderObject = new Order(this.StockObject.Symbol, 10000, this.CurrentPrice, Constants.OrderAction.BUY, this.OrderType); } else if (this.CurrentAccount.Type == "Short") { orderObject = new Order(this.StockObject.Symbol, 10000, this.CurrentPrice, Constants.OrderAction.SELL, this.OrderType); } if (ExchangeObject.PlaceOrder(orderObject, this.CurrentAccount.Id)) //Service call 7 { if (orderObject.Status == "FILLED") { Console.WriteLine("Quick Buy " + this.CurrentAccount.Type + " -------- " + this.StockObject.Symbol + " at " + this.CurrentPrice.ToString()); } else if (orderObject.Status == "PENDING") { Console.WriteLine(DateTime.Now + " : Order status " + Constants.OrderState.PENDING); } else if (orderObject.Status == "CANCELLED") { Console.WriteLine(DateTime.Now + " : Order status " + Constants.OrderState.CANCELLED); } } else { Console.WriteLine("Buy more stocks failed - Please check"); } }
private void AddEnoughPositions() { Order orderObject = null; PositionConfig pConfig = null; try { IEnumerable <Quote> quoteList = ExchangeObject.GetQuoteList(null, this.CurrentAccount.Id); //Service call 3 - Initially once and whenever new position added to account based on Max pos count foreach (Quote q in quoteList) { if (!IsSymbolAlreadyAvailable(q.Symbol)) { pConfig = this.GetPositionConfig(q.Symbol); if (pConfig != null) { if (pConfig.InitialUnits > pConfig.MinQuantity) { if (this.CurrentAccount.Type == "Long") { orderObject = new Order(q.Symbol, pConfig.InitialUnits, q.LastBuyTradePrice, Constants.OrderAction.BUY, this.OrderType); } else if (this.CurrentAccount.Type == "Short") { orderObject = new Order(q.Symbol, pConfig.InitialUnits, q.LastSellTradePrice, Constants.OrderAction.SELL, this.OrderType); } if (ExchangeObject.PlaceOrder(orderObject, this.CurrentAccount.Id)) //Service call 4 { if (orderObject.Status == Constants.OrderState.FILLED) { DataAnalyzer dt = new DataAnalyzer(orderObject.Symbol, this.CurrentAccount.Id, this.CurrentAccount.Type); Thread newThread = new Thread(dt.SaveActiveTicker); newThread.Name = "AnalyzerAdd"; newThread.Start(); } else if (orderObject.Status == Constants.OrderState.PENDING) { Console.WriteLine(DateTime.Now + " : Order status " + Constants.OrderState.PENDING); } else if (orderObject.Status == Constants.OrderState.CANCELLED) { Console.WriteLine(DateTime.Now + " : Order status " + Constants.OrderState.CANCELLED); } if (((OandaAccount)this.CurrentAccount).PositionList.Count >= this.MaxPositionCount) { return; } } } } } } return; } catch (Exception ex) { throw; } }
private bool PlaceSellOrder() { bool rtnVal = false; Order orderObject = null; //Need short buy change /*if (this.OrderType == Constants.OrderType.MARKET) * orderObject = new Order(this.StockObject.Symbol, this.StockObject.TotalCount, this.CurrentPrice, Constants.OrderAction.SELL, this.OrderType); * else if (this.StockObject.StrategyData.OrderType == Constants.OrderType.LIMIT) * orderObject = new Order(this.StockObject.Symbol, this.StockObject.TotalCount, this.CurrentPrice, Constants.OrderAction.SELL, this.OrderType);*/ if (this.CurrentAccount.Type == "Long") { orderObject = new Order(this.StockObject.Symbol, this.StockObject.TotalCount, this.CurrentPrice, Constants.OrderAction.SELL, this.OrderType); } else if (this.CurrentAccount.Type == "Short") { orderObject = new Order(this.StockObject.Symbol, (this.StockObject.TotalCount * -1), this.CurrentPrice, Constants.OrderAction.BUY, this.OrderType); } if (ExchangeObject.PlaceOrder(orderObject, this.CurrentAccount.Id)) //Service call 5 { if (orderObject.Status == Constants.OrderState.FILLED) { //Do this in new thread.. this.StockObject.TotalCount = orderObject.Count; this.StockObject.SellPrice = orderObject.Price; this.StockObject.TotalRevenue = (orderObject.Count * orderObject.Price) + orderObject.Brokerage; this.StockObject.TotalSellFee = orderObject.Brokerage; this.StockObject.ProfitnLoss = orderObject.PnL; this.StockObject.ModifiedOn = orderObject.DoneAt; this.StockObject.UserName = GetUserConfig(this.StockObject.Symbol).Name; this.StockObject.Status = Constants.PositionState.CLOSED; if (UpdatePositionsForDataAnalysis()) { DataAnalyzer dt = new DataAnalyzer(orderObject.Symbol, this.CurrentAccount.Id, this.CurrentAccount.Type); Thread newThread = new Thread(dt.CloseActiveTicker); newThread.Name = "AnalyzerAdd"; newThread.Start(); } else { Console.WriteLine("Error while writing to DB"); } rtnVal = true; } else if (orderObject.Status == Constants.OrderState.PENDING) { Console.WriteLine(DateTime.Now + " : Order status " + Constants.OrderState.PENDING); } else if (orderObject.Status == Constants.OrderState.CANCELLED) { Console.WriteLine(DateTime.Now + " : Order status " + Constants.OrderState.CANCELLED); } } return(rtnVal); }
private void AddMoreStocks() { int count = 0; Order orderObject = null; if (((ForexPosition)this.StockObject).TradeList.Count < this.posConfig.ExtrapolateLevel) { count = Math.Abs(Convert.ToInt32(this.StockObject.TradeList[this.StockObject.TradeList.Count - 1].Count)) + 1; //*************************************************************************************** //Check enough margin available to buy this units decimal margin = ((OandaAccount)this.CurrentAccount).MarginAvailable; decimal expPrice = count * this.CurrentPrice; if (expPrice < margin) { //Need short buy change /* if (this.OrderType == Constants.OrderType.MARKET) * orderObject = new Order(this.StockObject.Symbol, count, this.CurrentPrice, Constants.OrderAction.BUY, this.OrderType); * else if (this.StockObject.StrategyData.OrderType == Constants.OrderType.LIMIT) * orderObject = new Order(this.StockObject.Symbol, count, this.CurrentPrice, Constants.OrderAction.BUY, this.OrderType);*/ if (this.CurrentAccount.Type == "Long") { orderObject = new Order(this.StockObject.Symbol, count, this.CurrentPrice, Constants.OrderAction.BUY, this.OrderType); } else if (this.CurrentAccount.Type == "Short") { orderObject = new Order(this.StockObject.Symbol, count, this.CurrentPrice, Constants.OrderAction.SELL, this.OrderType); } if (ExchangeObject.PlaceOrder(orderObject, CurrentAccount.Id)) //Service call 7 { if (orderObject.Status == "FILLED") { Console.WriteLine("Added More " + this.CurrentAccount.Type + " " + this.StockObject.Symbol + " at " + this.CurrentPrice.ToString()); } else if (orderObject.Status == "PENDING") { Console.WriteLine(DateTime.Now + " : Order status " + Constants.OrderState.PENDING); } else if (orderObject.Status == "CANCELLED") { Console.WriteLine(DateTime.Now + " : Order status " + Constants.OrderState.CANCELLED); } } else { Console.WriteLine("Buy more stocks failed - Please check"); } } } }