private void MainForm_Load(object sender, EventArgs e)
        {
            WebSettings.GetSettings();
            var start = new LoadingStartupEvent();
            start.Progress = 10;
            onStartupLoad(this, start);

            CheckForIllegalCrossThreadCalls = false;
           //  Debug.WriteLine("Time Synched " + DoStuff.SynchronizeTime());
            _Interval = GlobalObjects.TimeInterval.Minute_5;

            start.Progress = 10;
            onStartupLoad(this, start);
            PopulateControls();

            start.Progress = 20;
            onStartupLoad(this, start);

            U5 = new UpdateTimer(_Interval);
            p = new PrepareForTrade(_Interval, WebSettings.General.HISAT_INST, GetParameters(), WebSettings.General.LIVE_START_DATE);
            marketOrder = new MarketOrder();


            p.onPriceSync += new PrepareForTrade.PricesSynced(p_onPriceSync);
            U5.onStartUpdate += new UpdateTimer.StartUpdate(U5_onStartUpdate);
            U5.OnManualCloseTrigger += new UpdateTimer.ManualCloseTrigger(U5_OnManualCloseTrigger);
            U5.OnConnectionExpired += new UpdateTimer.ConnectionExpired(U5_OnConnectionExpired);
            marketOrder.onOrderSend += new MarketOrder.OrderSend(marketOrder_onOrderSend);
            marketOrder.onOrderMatch += new MarketOrder.OrderMatch(marketOrder_onOrderMatch);
            _Stats.OnStatsCaculated += new Statistics.StatsCalculated(_Stats_OnStatsCaculated);


            start.Progress = 30;
            onStartupLoad(this, start);

            feed = new AlsiTrade_Backend.HiSat.LiveFeed(WebSettings.General.HISAT_INST);

            start.Progress = 60;
            onStartupLoad(this, start);

            BuildListViewColumns();

            start.Progress = 70;
            onStartupLoad(this, start);


            p._LastTrade = DoStuff.GetLastTrade(GetParameters(), _Interval);

            start.Progress = 80;
            onStartupLoad(this, start);


          
            Debug.WriteLine("LAST TRADE : " + p._LastTrade.TimeStamp + "   " + p._LastTrade);
            service = new WebUpdate();


            start.Progress = 100;
            onStartupLoad(this, start);

            WebUpdate.SetManualTradeTrigger(false);


            //test
            comboBox1.Items.Add(Trade.Trigger.None);
            comboBox1.Items.Add(Trade.Trigger.OpenLong);
            comboBox1.Items.Add(Trade.Trigger.OpenShort);
            comboBox1.Items.Add(Trade.Trigger.CloseLong);
            comboBox1.Items.Add(Trade.Trigger.CloseShort);

            comboBox2.Items.Add(Trade.BuySell.None);
            comboBox2.Items.Add(Trade.BuySell.Buy);
            comboBox2.Items.Add(Trade.BuySell.Sell);

           //Genetic plugin
            gb = GeneticBackend.GetSingletonGB();
            gb.Mf = this;
            gb.onUpdate += gb_onUpdate;
            gf.Show();
        }
        void p_onPriceSync(object sender, PrepareForTrade.PricesSyncedEvent e)
        {
            Debug.WriteLine("Prices Synced : " + e.ReadyForTradeCalcs);
            if (e.ReadyForTradeCalcs)
            {
                var trades = DoStuff.GetDataFromTick.DoYourThing(WebSettings.General.HISAT_INST, GetParameters(), WebSettings.General.LIVE_START_DATE);
                var NewTrades = AlsiUtils.Strategies.TradeStrategy.Expansion.ApplyRegressionFilter(11, trades);
                NewTrades = _Stats.CalcExpandedTradeStats(NewTrades);
                var Final = CompletedTrade.CreateList(NewTrades);

                foreach (var t in trades.Where(z => z.TimeStamp > DateTime.Now.AddDays(-5)))
                {
                    Debug.WriteLine(t.InstrumentName + "   " + t.TimeStamp + "  reason " + t.Reason + " " + t.TradedPrice + "  " + t.IndicatorNotes + "  " + t.CurrentDirection);
                }
                Debug.WriteLine("##############################################################");
                Debug.WriteLine("##############################################################");
                foreach (var ct in Final.Where(z => z.TimeStamp > DateTime.Now.AddDays(-5)))
                {
                   // Debug.WriteLine(ct.InstrumentName + "  " + ct.TimeStamp + "  reason " + ct.Reason + " " + ct.TradedPrice + "  " + ct.IndicatorNotes + "  " + ct.CurrentDirection + " Vol " + ct.TradeVolume);
                }

                var algotime = DoStuff.GetAlgoTime();
                var check = trades.Any(z => z.TimeStamp.Hour >= algotime.Hour && z.TimeStamp.Minute >= algotime.Minute && z.TimeStamp.Date == algotime.Date);
                if (!check && timeout < 5)
                {
                    timeout++;
                    Debug.WriteLine("Update Failed...Trying Again");
                    p.GetPricesFromTick();
                    return;
                }
                Trade currentOrder;
                if (!AlsiTrade_Backend.HiSat.LivePrice.EndOfDay)
                    currentOrder = trades.Where(z => z.TimeStamp.Hour >= algotime.Hour && z.TimeStamp.Minute >= algotime.Minute && z.TimeStamp.Date == algotime.Date).First();
                else
                    currentOrder = trades.Last();

                //Manual Trade
               
                MT.LastTrade = trades.Where(z => z.TimeStamp <= currentOrder.TimeStamp && z.Reason != Trade.Trigger.None).Last();
                MT.LastTrade.TradedPrice = AlsiTrade_Backend.HiSat.LivePrice.Last;
               
               

                currentOrder.TradeVolume = Final.Last().TradeVolume * WebSettings.General.VOL;
                currentOrder.InstrumentName = WebSettings.General.OTS_INST;
                Debug.WriteLine("Sending " + currentOrder);
                //original model entry
                //marketOrder.SendOrderToMarket(currentOrder);
                //algo order entry

                UpdateTradeLog(SetTradeLogColor(currentOrder), true);
            }
            else
            {
                timeout++;
                if (timeout == 3)
                {
                    timeout = 0;
                    p.GetPricesFromTick();
                }
                else
                {
                    Debug.WriteLine(timeout);
                    System.Threading.Thread.Sleep(1000);
                    p.GetPricesFromWeb();
                }
            }

            var ps = new NotifyOnPriceSynch();
            OnGeneticNotified(this, ps);

        }