Exemplo n.º 1
0
        public static void Execute()
        {
            List <ExternalBlock> blocks = new List <ExternalBlock>();

            blocks = context.ExternalBlocks.ToList();

            CompareByTime sortBlock = new CompareByTime();

            blocks.Sort(sortBlock);

            Random r1 = new Random();

            for (int i = 0; i < blocks.Count; i++) // 10000 ->  1-5000 -> 2222
            {
                // fetching max execution limit for a block

                // BrokerSecurity bs = new BrokerSecurity();
                string symbol = blocks[i].symbol;

                var x = (from n in context.BrokerSecurities
                         where n.securitySymbol.Equals(symbol)
                         select n).FirstOrDefault();
                //fetching market data for the symbol

                while (blocks[i].openQuantity > 0 && !(blocks[i].blockStatus.Equals("Expired")))
                {
                    int randomMaxOrderPerIteration = r1.Next(1, x.maxExeOrder);  //1-5000 -> 2222

                    if (randomMaxOrderPerIteration > blocks[i].openQuantity)
                    {
                        randomMaxOrderPerIteration = (int)blocks[i].openQuantity; //make sure that open quantity is not null
                    }
                    //while(true) //it's looping only once

                    int randomOrderToExecute = r1.Next(1, randomMaxOrderPerIteration);     // 1-2222 -> 1111

                    //entering new  data in trade execution

                    decimal tempTradePrice = GetTradePrice(blocks[i]);

                    //check for price match
                    int priceMatchStatus = CheckPriceMatch(tempTradePrice, blocks[i]);

                    if (priceMatchStatus == -1)
                    {
                        continue;
                    }

                    TradeExecution te = new TradeExecution();
                    te.symbol                 = blocks[i].symbol;
                    te.tradedQuantity         = 0;
                    te.remainingOrderQuantity = randomMaxOrderPerIteration;



                    te.tradePrice = tempTradePrice;
                    te.blockId    = blocks[i].blockId;
                    te.status     = "Execution";
                    te.timestamp  = DateTime.Now;

                    blocks[i].updatedTStamp = te.timestamp;
                    x.tradePrice            = tempTradePrice;

                    context.TradeExecutions.Add(te);
                    context.SaveChanges();



                    AutoAllocate(te, randomOrderToExecute);       //call auto alocate function with randomOrderToExecute as parameter


                    int randomContinueExecution = r1.Next(0, 100);     // random value to determine whether to continue
                    if (randomContinueExecution > (100 - x.perFullyExe))
                    {
                        TradeExecution Te = context.TradeExecutions.Find(te.tradeId);
                        Te.tradedQuantity         = randomMaxOrderPerIteration;
                        Te.remainingOrderQuantity = 0;

                        blocks[i].executedQuantity += randomMaxOrderPerIteration;

                        Te.remainingOrderQuantity = 0;
                        Te.status = "Fully";
                        // context.SaveChanges();
                        //break;
                    }
                    else
                    {
                        TradeExecution Te = context.TradeExecutions.Find(te.tradeId);
                        Te.status = "Partially";

                        blocks[i].executedQuantity += randomOrderToExecute;

                        // context.SaveChanges();
                        //break;
                    }



                    blocks[i].openQuantity -= randomMaxOrderPerIteration;
                    if (blocks[i].executedQuantity == blocks[i].totalQuantity)
                    {
                        blocks[i].blockStatus = "Fully";
                    }
                    else
                    {
                        blocks[i].blockStatus = "Partial";
                    }
                    context.SaveChanges();
                }

                blocks[i].openQuantity = blocks[i].totalQuantity - blocks[i].executedQuantity;
                context.SaveChanges();
            }

            //return 1; //return status
        }
        public void Execute()
        {
            List <ExternalBlock> blocks = new List <ExternalBlock>();

            blocks = context.ExternalBlocks.ToList();

            CompareByTime sortBlock = new CompareByTime();

            blocks.Sort(sortBlock);

            Random r1 = new Random();

            for (int i = 0; i < blocks.Count; i++) // 10000 ->  1-5000 -> 2222
            {
                // fetching max execution limit for a block

                // BrokerSecurity bs = new BrokerSecurity();

                var x = (from n in context.BrokerSecurities
                         where n.securitySymbol.Equals(blocks[i].symbol)
                         select n).FirstOrDefault();
                //fetching market data for the symbol

                while (blocks[i].openQuantity > 0)
                {
                    int randomMaxOrderPerIteration = r1.Next(1, x.maxExeOrder);  //1-5000 -> 2222

                    if (randomMaxOrderPerIteration > blocks[i].openQuantity)
                    {
                        randomMaxOrderPerIteration = (int)blocks[i].openQuantity;          //make sure that open quantity is not null
                    }
                    while (true)                                                           //it's looping only once
                    {
                        int randomOrderToExecute = r1.Next(1, randomMaxOrderPerIteration); // 1-2222 -> 1111

                        //entering data in trade execution

                        TradeExecution te = new TradeExecution();
                        te.symbol                 = blocks[i].symbol;
                        te.tradedQuantity         = 0;
                        te.remainingOrderQuantity = randomMaxOrderPerIteration;
                        te.tradePrice             = GetTradePrice(blocks[i]);
                        te.blockId                = blocks[i].blockId;
                        te.status                 = "Execution";
                        te.timestamp              = DateTime.Now;
                        //if (blocks[i].createTStamp == null)
                        //{
                        //    te.timestamp = DateTime.Now;
                        //}
                        //else
                        //{
                        //    te.timestamp = (DateTime)blocks[i].createTStamp;
                        //}

                        context.TradeExecutions.Add(te);

                        AutoAllocate(te, randomOrderToExecute);   //call auto alocate function with randomOrderToExecute as parameter

                        blocks[i].executedQuantity = randomOrderToExecute;

                        int randomContinueExecution = r1.Next(0, 100); // random value to determine whether to continue
                        if (randomContinueExecution > (100 - x.perFullyExe))
                        {
                            te.tradedQuantity          = randomMaxOrderPerIteration;
                            blocks[i].executedQuantity = randomMaxOrderPerIteration;
                            te.remainingOrderQuantity  = 0;
                            te.status = "Fully Executed";
                            context.SaveChanges();
                            break;
                        }
                        else
                        {
                            te.status = "Partially Executed";
                            context.SaveChanges();
                            break;
                        }
                    }

                    blocks[i].openQuantity -= randomMaxOrderPerIteration;
                }
            }
        }