private bool PlaceBracketOrder(RangeBreakOutStockConfig stock, OrderMode mode, int quantity)
        {
            var stopLoss = Math.Abs(stock.SellBreakOutPrice - stock.BuyBreakOutPrice);
            Dictionary <string, dynamic> orderResponse = new Dictionary <string, dynamic>();
            var triggerPrice = mode == OrderMode.BUY ? stock.BuyBreakOutPrice : stock.SellBreakOutPrice;
            var targetPrice  = mode == OrderMode.BUY ?
                               Math.Round((stock.BuyBreakOutPrice + (stock.BuyBreakOutPrice * (stock.ProfitMargin / 100.0m))), 2).GetNextValidPrice(true) :
                               Math.Round((stock.SellBreakOutPrice - (stock.SellBreakOutPrice * (stock.ProfitMargin / 100.0m))), 2).GetNextValidPrice(true);

            var targetPoint = mode == OrderMode.BUY ?
                              (targetPrice - stock.BuyBreakOutPrice) :
                              (stock.SellBreakOutPrice - targetPrice);

            orderResponse = _kite.PlaceOrder(stock.Exchange, stock.Symbol, mode.ToString(), quantity, Product: Constants.PRODUCT_MIS, OrderType: Constants.ORDER_TYPE_LIMIT, Price: triggerPrice, Variety: Constants.VARIETY_BO, Validity: Constants.VALIDITY_DAY, SquareOffValue: targetPoint, StoplossValue: stopLoss);
            if (orderResponse.Any(s => s.Key.ToLower() == "status" && s.Value.ToLower() == "success"))
            {
                string orderId = GetOrderId(orderResponse);
                stock.AllOrderStatus[OrderType.BracketOrder]      = new KeyValuePair <string, OrderStatus>(orderId, OrderStatus.Ordered);
                stock.AllOrderTargetPrice[OrderType.BracketOrder] = new KeyValuePair <OrderMode, decimal>(mode, targetPrice);
                Events.RaiseAskForOrderSubscriptionEvent(orderId, true);
                return(true);
            }
            else
            {
                return(false);
            }
        }
        private bool PlaceOrder(RangeBreakOutStockConfig stock, OrderMode mode)
        {
            var quantity = stock.GetQuantity();

            try
            {
                if (stock.OrderType == OrderType.CoverOrder)
                {
                    return(PlaceCoverOrder(stock, mode, quantity));
                }
                else if (stock.OrderType == OrderType.BracketOrder)
                {
                    return(PlaceCoverOrder(stock, mode, quantity));
                }
                else if (stock.OrderType == OrderType.CoverAndBracketOrder)
                {
                    Task task1 = Task.Factory.StartNew(() => PlaceCoverOrder(stock, mode, quantity));
                    Task task2 = Task.Factory.StartNew(() => PlaceBracketOrder(stock, mode, quantity));
                    Task.WaitAll(task1, task2);
                    return(true);
                }
                return(false);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
        private OrderMode?IsEligible(RangeBreakOutStockConfig stockConfiguration, decimal ltp)

        {
            if (stockConfiguration.OrderStatus != OrderStatus.BuyOrderInProgress && stockConfiguration.OrderStatus != OrderStatus.SellOrderInProgress)
            {
                if (ltp > stockConfiguration.BuyBreakOutPrice)
                {
                    return(OrderMode.BUY);
                }
                else if (ltp < stockConfiguration.SellBreakOutPrice)
                {
                    return(OrderMode.SELL);
                }
            }
            else if (stockConfiguration.TargetPrice != null && (stockConfiguration.OrderStatus == OrderStatus.BuyOrderInProgress || stockConfiguration.OrderStatus == OrderStatus.SellOrderInProgress))
            {
                switch (stockConfiguration.OrderStatus)
                {
                case OrderStatus.BuyOrderInProgress:
                    if (ltp >= stockConfiguration.TargetPrice)
                    {
                        stockConfiguration.OrderStatus = OrderStatus.TargetHit;
                        ExitOrder(stockConfiguration);
                    }
                    else if (ltp <= stockConfiguration.StopLoss)
                    {
                        stockConfiguration.TargetPrice = null;
                        stockConfiguration.OrderStatus = OrderStatus.SellOrderInProgress;
                        return(OrderMode.SELL);
                    }
                    break;

                case OrderStatus.SellOrderInProgress:
                    if (ltp <= stockConfiguration.TargetPrice)
                    {
                        stockConfiguration.OrderStatus = OrderStatus.TargetHit;
                        ExitOrder(stockConfiguration);
                    }
                    else if (ltp >= stockConfiguration.StopLoss)
                    {
                        stockConfiguration.TargetPrice = null;
                        stockConfiguration.OrderStatus = OrderStatus.BuyOrderInProgress;
                        return(OrderMode.BUY);
                    }
                    break;
                }
            }

            return(null);
        }
        private bool IsEligibleToCancelBracketOrder(RangeBreakOutStockConfig stock, decimal lastPrice)
        {
            var targetInfo = stock.AllOrderTargetPrice.FirstOrDefault(s => s.Key == OrderType.BracketOrder);

            if (targetInfo.Key != null)
            {
                if (targetInfo.Value.Key == OrderMode.BUY && lastPrice >= targetInfo.Value.Value)
                {
                    return(true);
                }
                else if (targetInfo.Value.Key == OrderMode.SELL && lastPrice <= targetInfo.Value.Value)
                {
                    return(true);
                }
            }
            return(false);
        }
 //Exiting order for profit booking
 private void ExitOrder(RangeBreakOutStockConfig stock)
 {
     try
     {
         if (!string.IsNullOrEmpty(stock.TargetOrderId) && !string.IsNullOrEmpty(stock.ParentOrderId))
         {
             var orderResponse = _kite.CancelOrder(stock.TargetOrderId, Variety: "co", ParentOrderId: stock.ParentOrderId);
             RaiseStatusChangeEvent(stock);
             if (orderResponse.Any(s => s.Key.ToLower() == "status" && s.Value.ToLower() == "success"))
             {
                 RangeBreakOutStockConfig stockOut;
                 _configStockDictionary.TryRemove(stock.TradingSymbol, out stock);
             }
         }
     }
     catch (Exception)
     {
     }
 }
        private bool PlaceCoverOrder(RangeBreakOutStockConfig stock, OrderMode mode, int quantity)
        {
            stock.StopLoss = mode == OrderMode.BUY ? stock.SellBreakOutPrice : stock.BuyBreakOutPrice;
            Dictionary <string, dynamic> orderResponse = new Dictionary <string, dynamic>();

            orderResponse = _kite.PlaceOrder(stock.Exchange, stock.Symbol, mode.ToString(), quantity, Product: Constants.PRODUCT_MIS, OrderType: Constants.ORDER_TYPE_MARKET, TriggerPrice: stock.StopLoss, Variety: stock.Variety, Validity: Constants.VALIDITY_DAY);
            if (orderResponse.Any(s => s.Key.ToLower() == "status" && s.Value.ToLower() == "success"))
            {
                stock.TargetPrice = null;
                string orderId = GetOrderId(orderResponse);
                Events.RaiseAskForOrderSubscriptionEvent(orderId, true);
                stock.ParentOrderId       = orderId;
                stock.LastOrderedQuantity = quantity;
                stock.ReversalNumber++;
                stock.OrderedQuantity = quantity;
                return(true);
            }
            else
            {
                return(true);
            }
        }
        private static bool ManipulateCoverOrderInfo(IEnumerable <Order> orderHistory, RangeBreakOutStockConfig stock)
        {
            bool hasBothEntered;

            if (orderHistory.Any(s => s.ParentOrderId == stock.ParentOrderId))
            {
                var order = orderHistory.FirstOrDefault(s => s.ParentOrderId == stock.ParentOrderId);
                stock.TargetOrderId = order.OrderId;
                hasBothEntered      = true;
            }

            if (orderHistory.Any(s => s.OrderId == stock.ParentOrderId))
            {
                try
                {
                    var order = orderHistory.FirstOrDefault(s => s.AveragePrice != 0 && s.OrderId == stock.ParentOrderId);
                    {
                        stock.TargetPrice = order.TransactionType.Equals("buy", StringComparison.InvariantCultureIgnoreCase) ?
                                            order.AveragePrice + (order.AveragePrice * (stock.ProfitMargin / 100.0m)) :
                                            order.AveragePrice - (order.AveragePrice * (stock.ProfitMargin / 100.0m));
                    }
                    if (stock.TargetPrice != null)
                    {
                        hasBothEntered = true;
                    }
                    else
                    {
                        hasBothEntered = false;
                    }
                }
                catch (Exception)
                {
                    hasBothEntered = false;
                }
            }
            else
            {
                hasBothEntered = false;
            }

            if (hasBothEntered)
            {
                Events.RaiseAskForOrderSubscriptionEvent(stock.ParentOrderId, false);
            }
            return(hasBothEntered);
        }
 private void RaiseStatusChangeEvent(RangeBreakOutStockConfig stock)
 {
     Events.RaiseUpdateObject(stock);
 }