public PrepareForTrade(GlobalObjects.TimeInterval Interval, string ContractName, Parameter_EMA_Scalp Parameter, DateTime StartPeriod)
 {
     _Interval = Interval;
     _ContractName = ContractName;
     _Start = StartPeriod;
     _Param = Parameter;
 }
        public static List<Trade> RunEMAScalpLiveTrade(Parameter_EMA_Scalp Parameter, DateTime StartDate, GlobalObjects.TimeInterval Interval)
        {
            //  DateTime START = new DateTime(2012,06,02);//NB must be set periodically           
            DateTime e = DateTime.UtcNow.AddHours(5);
            UpdateDB.MergeTempWithHisto(Interval);
            GlobalObjects.Points = AlsiUtils.DataBase.readDataFromDataBase(Interval, AlsiUtils.DataBase.dataTable.Temp, StartDate, e, false);
            var sp = GlobalObjects.Points.First();
            var l = GlobalObjects.Points.Last();
            return AlsiUtils.Strategies.EMA_Scalp.EmaScalp(Parameter, GlobalObjects.Points, false);

        }
 public UpdateTimer(GlobalObjects.TimeInterval interval)
 {
     _Businessday = CheckBusinessDay();
     _interval = interval;
     _timer.Interval = 500;
     _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
     _UpdateStatus.Elapsed += new ElapsedEventHandler(_UpdateStatus_Elapsed);
     _UpdateStatus.Interval = 5000;
     _UpdateStatus.Start();
     if (_Businessday) _timer.Start();
     service = new WebUpdate();
 }
        public static void MergeTempWithHisto(GlobalObjects.TimeInterval T)
        {


            var dc = new AlsiDBDataContext();
            switch (T)
            {
                case (GlobalObjects.TimeInterval.Minute_5):
                    //Must be 2000 prices in db for accurate calcs
                    if (dc.OHLC_5_Minutes.Count() < 20100) throw new IndexOutOfRangeException();
                    var last1000Prices = dc.OHLC_5_Minutes.Skip(Math.Max(0, dc.OHLC_5_Minutes.Count() - 20000)).Take(20000);
                    dc.Clean_OHLC_Temp_2();


                    DataTable MinData = new DataTable("MinData");
                    MinData.Columns.Add("Stamp", typeof(DateTime));
                    MinData.Columns.Add("O", typeof(int));
                    MinData.Columns.Add("L", typeof(int));
                    MinData.Columns.Add("H", typeof(int));
                    MinData.Columns.Add("C", typeof(int));
                

                    foreach (var p in last1000Prices) MinData.Rows.Add(p.Stamp, p.O, p.H, p.L, p.C);


                    #region BulkCopy

                    DataSet minuteDataSet = new DataSet("minuteDataset");
                    minuteDataSet.Tables.Add(MinData);
                    SqlConnection myConnection = new SqlConnection(AlsiUtils.Data_Objects.GlobalObjects.CustomConnectionString);
                    myConnection.Open();
                    SqlBulkCopy bulkcopy = new SqlBulkCopy(myConnection);
                    bulkcopy.DestinationTableName = "OHLC_Temp_2";
                    bulkcopy.WriteToServer(MinData);
                    Debug.WriteLine("Tick Bulk Copy Complete");
                    MinData.Dispose();
                    myConnection.Close();

                    #endregion

                    dc.MergeTemp();
                    Debug.WriteLine("Merged Data");
                    break;
            }


        }
        public static Trade GetLastTrade(Parameter_EMA_Scalp Parameter, GlobalObjects.TimeInterval t)
        {
            var _Stats = new AlsiUtils.Statistics();
            var dt = DataBase.dataTable.MasterMinute;

            var _tempTradeList = AlsiTrade_Backend.RunCalcs.RunEMAScalp(Parameter, t, true, DateTime.Now.AddMonths(-1), DateTime.Now.AddHours(12), dt);
            var _trades = _Stats.CalcBasicTradeStats_old(_tempTradeList);
            _trades.Reverse();

            return _trades[0];
        }
 public static List<Trade> RunMAMAScalp(Parameter_MAMA Parameter, GlobalObjects.TimeInterval Interval, bool TradesOnly, DateTime Start, DateTime End, DataBase.dataTable Table)
 {
     GlobalObjects.Points = AlsiUtils.DataBase.readDataFromDataBase(Interval, Table, Start, End, false);
     return AlsiUtils.Strategies.MAMA_Scalp.MAMAScalp(Parameter, GlobalObjects.Points, TradesOnly);
 }
        private DateTime ConvertTime(GlobalObjects.TimeInterval Interval)
        {
            DateTime _Now = DateTime.UtcNow.AddHours(2);
            DateTime t = new DateTime();
            // int tenmin = (_Now.Minute / 10) * 10;
            //int onemin = _Now.Minute - tenmin;

            switch (Interval)
            {
                case GlobalObjects.TimeInterval.Minute_1:
                    break;

                case GlobalObjects.TimeInterval.Minute_2:
                    t = _Now.AddMinutes(-2);
                    break;

                case GlobalObjects.TimeInterval.Minute_5:
                    t = _Now.AddMinutes(-5);
                    break;

                case GlobalObjects.TimeInterval.Minute_10:
                    break;
            }

            return t;
        }
        static public List<Price> readDataFromDataBase(GlobalObjects.TimeInterval T, dataTable TD, DateTime Start, DateTime End, bool reverseList)
        {

            List<Price> prices = new List<Price>();

            AlsiDBDataContext dc = new AlsiDBDataContext();


            if (TD == dataTable.Temp)
            {
                var result = from q in dc.OHLC_Temps
                             where q.Stamp > Start && q.Stamp < End
                             select new { q.Stamp, q.O, q.H, q.L, q.C, };

                foreach (var v in result)
                {
                    Price p = new Price();
                    p.Close = v.C;
                    p.Open = v.O;
                    p.High = v.H;
                    p.Low = v.L;
                    p.TimeStamp = v.Stamp;
                    prices.Add(p);

                }
                dc.Clean_OHLC_Temp();
                if (reverseList) prices.Reverse();
                return prices;
            }

            if (TD == dataTable.AllHistory)
            {
                if (T == GlobalObjects.TimeInterval.Minute_2)
                {
                    var firstinDB = dc.OHLC_2_Minutes.AsEnumerable().First().Stamp;
                    var lastinDB = dc.OHLC_2_Minutes.AsEnumerable().Last().Stamp;
                    if (firstinDB > Start) dc.OHLC_2_AllHistory();
                    if (lastinDB > End) dc.OHLC_2_AllHistory();
                }
                if (T == GlobalObjects.TimeInterval.Minute_5)
                {
                    try
                    {
                        var firstinDB = dc.OHLC_5_Minutes.AsEnumerable().First().Stamp;
                        var lastinDB = dc.OHLC_5_Minutes.AsEnumerable().Last().Stamp;
                        if (firstinDB > Start) dc.OHLC_5_AllHistory();
                        if (lastinDB < End) dc.OHLC_5_AllHistory();
                    }
                    catch { dc.OHLC_5_AllHistory(); }

                }
                if (T == GlobalObjects.TimeInterval.Minute_10)
                {
                    var firstinDB = dc.OHLC_10_Minutes.AsEnumerable().First().Stamp;
                    var lastinDB = dc.OHLC_10_Minutes.AsEnumerable().Last().Stamp;
                    if (firstinDB > Start) dc.OHLC_10_AllHistory();
                    if (lastinDB < End) dc.OHLC_10_AllHistory();
                }
            }
            if (TD == dataTable.MasterMinute)
            {
                if (T == GlobalObjects.TimeInterval.Minute_2)
                {
                    var min2 = dc.OHLC_2_Minutes;
                    if (min2.Count() == 0)
                    {
                        dc.OHLC_2();
                    }
                    else
                    {
                        var firstinDB = dc.OHLC_2_Minutes.AsEnumerable().First().Stamp;
                        var lastinDB = dc.OHLC_2_Minutes.AsEnumerable().Last().Stamp;
                        if (firstinDB > Start) dc.OHLC_2();
                        if (lastinDB < End) dc.OHLC_2();
                    }
                }

                if (T == GlobalObjects.TimeInterval.Minute_5)
                {
                    var min5 = dc.OHLC_5_Minutes;
                    if (min5.Count() == 0)
                    {
                        dc.OHLC_5();
                    }
                    else
                    {
                        var firstinDB = min5.AsEnumerable().First().Stamp;
                        var lastinDB = min5.AsEnumerable().Last().Stamp;
                        if (firstinDB > Start) dc.OHLC_5();
                        if (lastinDB < End) dc.OHLC_5();
                    }
                }

                if (T == GlobalObjects.TimeInterval.Minute_10)
                {
                    var min10 = dc.OHLC_10_Minutes;
                    if (min10.Count() == 0)
                    {
                        dc.OHLC_10();
                    }
                    else
                    {
                        var firstinDB = dc.OHLC_10_Minutes.AsEnumerable().First().Stamp;
                        var lastinDB = dc.OHLC_10_Minutes.AsEnumerable().Last().Stamp;
                        if (firstinDB > Start) dc.OHLC_10();
                        if (lastinDB < End) dc.OHLC_10();
                    }
                }

                if (T == GlobalObjects.TimeInterval.Hour_1)
                {
                   dc.OHLC_Hour_1();
                }
            }





            if (T == GlobalObjects.TimeInterval.Minute_2)
            {
                var result = from q in dc.OHLC_2_Minutes
                             where q.Stamp > Start && q.Stamp < End
                             select new { q.Stamp, q.O, q.H, q.L, q.C, q.Instrument };

                foreach (var v in result)
                {
                    Price p = new Price();
                    p.Close = v.C;
                    p.Open = v.O;
                    p.High = v.H;
                    p.Low = v.L;
                    p.TimeStamp = v.Stamp;
                    p.InstrumentName = v.Instrument;
                    prices.Add(p);

                }
            }

            if (T == GlobalObjects.TimeInterval.Minute_5)
            {
                var result = from q in dc.OHLC_5_Minutes
                             where q.Stamp > Start && q.Stamp < End
                             select new { q.Stamp, q.O, q.H, q.L, q.C, q.Instrument };

                foreach (var v in result)
                {
                    Price p = new Price();
                    p.Close = v.C;
                    p.Open = v.O;
                    p.High = v.H;
                    p.Low = v.L;
                    p.TimeStamp = v.Stamp;
                    p.InstrumentName = v.Instrument;
                    prices.Add(p);

                }
            }
            if (T == GlobalObjects.TimeInterval.Minute_10)
            {
                var result = from q in dc.OHLC_10_Minutes
                             where q.Stamp > Start && q.Stamp < End
                             select new { q.Stamp, q.O, q.H, q.L, q.C, q.Instrument };

                foreach (var v in result)
                {
                    Price p = new Price();
                    p.Close = v.C;
                    p.Open = v.O;
                    p.High = v.H;
                    p.Low = v.L;
                    p.TimeStamp = v.Stamp;
                    p.InstrumentName = v.Instrument;
                    prices.Add(p);

                }
            }

            if (T == GlobalObjects.TimeInterval.Hour_1)
            {
                var result = from q in dc.OHLC_1_Hours 
                             where q.Stamp > Start && q.Stamp < End
                             select new { q.Stamp, q.O, q.H, q.L, q.C, q.Instrument };

                foreach (var v in result)
                {
                    Price p = new Price();
                    p.Close = v.C;
                    p.Open = v.O;
                    p.High = v.H;
                    p.Low = v.L;
                    p.TimeStamp = v.Stamp;
                    p.InstrumentName = v.Instrument;
                    prices.Add(p);

                }
            }




            if (reverseList) prices.Reverse();
            return prices;


        }