Esempio n. 1
0
        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;
            }
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        private void UpdateAccountDetails()
        {
            DataAnalyzer dt = null;

            ExchangeObject.GetAccountChanges(this.CurrentAccount); //Service call 2 - Every Do-While loop delay thread sleep
            this.CurrentAccount.Type = GetAccountType(this.CurrentAccount.Id);
            if (((OandaAccount)this.CurrentAccount).OpenedTradesJson != null)
            {
                dt           = new DataAnalyzer(string.Empty, this.CurrentAccount.Id, this.CurrentAccount.Type);
                dt.NewTrades = ((OandaAccount)this.CurrentAccount).OpenedTradesJson;
                Thread newThread = new Thread(dt.SaveNewTrades);
                newThread.Name = "AnalyzerExpSave";
                newThread.Start();
            }
        }
Esempio n. 4
0
        private Account GetAccountDetails(string accountId)
        {
            Account accountInfo = null;

            accountInfo = ExchangeObject.GetAccountDetails(accountId); //Service call 1 - Once while starting app

            for (var iPos = ((OandaAccount)accountInfo).PositionList.Count - 1; iPos >= 0; --iPos)
            {
                DataAnalyzer dt        = new DataAnalyzer(((OandaAccount)accountInfo).PositionList[iPos].Symbol, accountId, GetAccountType(accountId));
                Thread       newThread = new Thread(dt.SaveActiveTicker);
                newThread.Name = "AnalyzerAdd";
                newThread.Start();

                Console.WriteLine("Bought " + ((OandaAccount)accountInfo).PositionList[iPos].TotalCount + " " +
                                  ((OandaAccount)accountInfo).PositionList[iPos].Symbol);
            }

            return(accountInfo);
        }