protected void AddMyTrade(Trade mynewtrade)
 {
     lock (myTrades_Lock)
     {
         trade_counter++;
         mynewtrade.Id = trade_counter;
         myTrades.AddOrUpdate(mynewtrade);
     }
 }
        /// <summary>
        /// trigger if balance is smaller then 50
        /// </summary>
        public void ManageStopOuts()
        {
            cleanUp = clientBalances.Connect(dssds => dssds.BalanceLeft < 80)
                   //  .Synchronize(myTrade_locker)
                     .WhereReasonsAre(ChangeReason.Add, ChangeReason.Update)
                     //TODO ADD FILTER - total possition > 0 - to go hear agregated possition of the customer should be > then 0
                     //.Filter(f => f.BalanceLeft < 80)
                     .Subscribe(
                             c =>
                             {
                                 foreach (var item in c)
                                 {
                                     LogInfo("Check if we can clos POSITION check - BALANCE IS BELOW 50 for client: " + item.Current.ClientID + " item.Current.BalanceLeft:" + item.Current.BalanceLeft + " reson" + item.Reason.ToString());

                                     var positionSToclose = curPairPositionPerClient.Items.Where(r => (r.ClientId == item.Current.ClientID && r.Amount1 != 0)).OrderByDescending(d => ConvertToBaseCcy(d.Amount1, d.Cur1));//;

                                    if (positionSToclose.Count() > 0)
                                     {
                                         var positionToclose = positionSToclose.First();

                                         LogInfo("CLOSE THE POSITION - " + positionToclose);

                                         double lastquote = 0;
                                         if (positionToclose.Amount1 > 0)
                                             lastquote = quotes.Items.Where(r => r.Pair == positionToclose.Pair).First().Bid;
                                         else
                                             lastquote = quotes.Items.Where(r => r.Pair == positionToclose.Pair).First().Ask;

                                         Trade GenerateClosingTrade = new Trade
                                         {
                                             //Id = myTrades.Count,
                                             ClientId = positionToclose.ClientId,
                                             Pair = positionToclose.Pair,
                                             Amount1 = positionToclose.Amount1 * (-1),
                                             Status = Status.Pending,
                                            //Status = Status.Open,
                                            OpenPrice = lastquote,
                                             CurrentPrice = lastquote,
                                             Amount2 = positionToclose.Amount1 * (-1) * lastquote,
                                             Cur1 = positionToclose.Pair.Substring(0, 3),
                                             Cur2 = positionToclose.Pair.Substring(positionToclose.Pair.Length - 3, 3),
                                         };

                                         AddMyTrade(GenerateClosingTrade);

                                    }

                                }
                             }
                     );
        }
        public void ExtractData()
        {
            Random rand = new Random();

            var pairs = new[] { "EUR/GBP", "EUR/USD", "EUR/CHF", "GBP/USD", "USD/CHF", "USD/HUF", "EUR/HUF" };
            var customers = new[] { "CUST1", "CUST2", "CUST3", "CUST4", "CUST5", "CUST6", "CUST7" };

            long TradeIdnum = 0;

            for (int i = 0; i < 7; i++)
            {
                var newquote = new Quote
                {
                    Pair = pairs[i],
                    Ask = Math.Round(rand.NextDouble(), 5),
                    Bid = Math.Round(rand.NextDouble(), 5),
                    Cur1 = pairs[i].Substring(0, 3),
                    Cur2 = pairs[i].Substring(pairs[i].Length - 3, 3),
                };
                ////Console.WriteLine(newquote);
                LogInfo(newquote.ToString());
                quotes.AddOrUpdate(newquote);
            }

            for (int i = 0; i < 6; i++)
            {
                //var newcustomer = new Balance(i, rand.Next(100), rand.Next(100));
                var newcustomer = new BalancePerClient(i, 100, 0, 0);

                clientBalances.AddOrUpdate(newcustomer);
                /////Console.WriteLine("added customer: " + newcustomer);
                //appendlog("added customer: " + newcustomer);
            }

            while (true)
            {

                var pair = pairs[rand.Next(6)];

                var newquote = new Quote
                {
                    Pair = pair,
                    Ask = Math.Round(rand.NextDouble(), 5),
                    Bid = Math.Round(rand.NextDouble(), 5),
                    Cur1 = pair.Substring(0, 3),
                    Cur2 = pair.Substring(pair.Length - 3, 3)
                };
                ////Console.WriteLine(newquote);
                LogInfo(newquote.ToString());
                quotes.AddOrUpdate(newquote);

                var currentprice = Math.Round(rand.NextDouble(), 5);
                var amount = rand.Next(-100, 100);
                if (amount == 0)
                {
                    amount = 1;
                }
                var Pair = pairs[rand.Next(6)];

                var newtrade = new Trade
                {
                    Id = myTrades.Count,
                    ClientId = rand.Next(6),
                    Pair = Pair,
                    Status = Status.Open,
                    Amount1 = amount,
                    Amount2 = amount * currentprice * (-1),
                    Cur1 = Pair.Substring(0, 3),
                    Cur2 = Pair.Substring(Pair.Length - 3, 3),
                };

                if (newtrade.Amount1 > 0)
                {
                    newtrade.OpenPrice = quotes.Items.Where(r => r.Pair == newtrade.Pair).First().Ask;
                    newtrade.CurrentPrice = quotes.Items.Where(r => r.Pair == newtrade.Pair).First().Bid;
                }
                else
                {
                    newtrade.OpenPrice = quotes.Items.Where(r => r.Pair == newtrade.Pair).First().Bid;
                    newtrade.CurrentPrice = quotes.Items.Where(r => r.Pair == newtrade.Pair).First().Ask;
                }

                if (TradeIdnum < 20)
                {

                    TradeIdnum = TradeIdnum + 1;
                    string printtext = newtrade.ToString();

                    LogInfo(printtext);
                    AddMyTrade(newtrade);

                }
                Thread.Sleep(500);
            }
        }