Example #1
0
        public void TestPowerService()
        {
            DateTime      dt      = new DateTime(2019, 12, 24, 13, 44, 33);
            IPowerService ps      = new PowerService();
            var           results = ps.GetTrades(dt);

            Assert.IsNotNull(results);
        }
Example #2
0
        public void TestGetTradesError()
        {
            TestSetupHelpers.SetErrorMode();

            var ps   = new PowerService();
            var date = new DateTime(2017, 12, 13);

            Assert.Throws <PowerServiceException>(() => ps.GetTrades(date));
        }
        public void Gettrades_method_returns_one_hundred_and_eighty_days_of_historical_data()
        {
            var powerService = new PowerService();

            for (int i = 0; i < 180; i++)
            {
                var checkDate     = _tradesDate.AddDays(i * -1);
                var currentTrades = powerService.GetTrades(_tradesDate);
            }
        }
Example #4
0
        public void TestGetTrades()
        {
            TestSetupHelpers.SetTestMode();

            var ps   = new PowerService();
            var date = new DateTime(2017, 12, 13);

            var trades = ps.GetTrades(date);

            Assert.AreEqual(2, trades.Count());
        }
        public void GetTrades_method_returns_data_in_under_a_minute()
        {
            var powerService = new PowerService();
            var maxRunTime   = new TimeSpan(0, 0, 60);
            var stopWatch    = new System.Diagnostics.Stopwatch();

            stopWatch.Start();
            var currentTrades = powerService.GetTrades(_tradesDate);

            stopWatch.Stop();
            var runTime = stopWatch.Elapsed;

            Assert.GreaterOrEqual(maxRunTime, runTime);
        }
Example #6
0
        public void TestPowerPeriodAggregation()
        {
            DateTime      dt      = new DateTime(2019, 12, 24, 13, 44, 33);
            IPowerService ps      = new PowerService();
            var           results = ps.GetTrades(dt);
            double        totalP1 = 0;

            foreach (var trade in results)
            {
                totalP1 += trade.Periods[0].Volume;
            }
            Aggregate aggregate  = new Aggregate();
            var       aggresults = aggregate.GetAggregatedVolumes(results);

            Assert.AreEqual(totalP1, aggresults[1]);
        }
Example #7
0
        /// <summary>
        /// Fetch trades and generate CSV file for captured trades
        /// </summary>
        /// <param name="pTradeDate"></param>
        public static void RunTrades(DateTime pTradeDate)
        {
            try
            {
                string       FilePath      = string.Format("{0}PowerPosition_{1}.csv", ConfigurationManager.AppSettings["csvFilePath"], pTradeDate.ToString("yyyyMMdd_HHmm"));
                PowerService _powerService = new PowerService();

                List <PowerTrade> powerTrades = new List <PowerTrade>();
                powerTrades.AddRange(_powerService.GetTrades(pTradeDate));

                PowerTrade result = PowerTrade.Create(pTradeDate, 24);// creating 24 power period for 24 hrs trades

                foreach (var item in powerTrades)
                {
                    foreach (PowerPeriod power in item.Periods)
                    {
                        var pow = result.Periods.FirstOrDefault(x => x.Period == power.Period);
                        if (pow != null)
                        {
                            pow.Volume += power.Volume;
                        }
                    }
                }


                //Sample code for verifying the total result
                int _counter = 1;
                foreach (PowerTrade p in powerTrades)
                {
                    ExportTradeToCSV(p, FilePath.Replace(".csv", string.Format("_{0}.csv", _counter)), false);
                    _counter++;
                }



                ExportTradeToCSV(result, FilePath, false);
            }
            catch (Exception ex)
            {
                EventLogger.WriteEntry(string.Format("Trade capturing failed at {0}.\n Failure Reason : {1}", pTradeDate.ToString(), ex.Message));
            }
        }
        public void GetTrades_method__returns_same_trades_for_same_query_datetime()
        {
            var powerService      = new PowerService();
            var currentTrades     = powerService.GetTrades(_tradesDate);
            var matchTrades       = GetTestTrades();
            var matchTradeIndex   = 0;
            var currentTradecount = 0;

            //Assume ordering is always the same then trades queried for same date should be the exact same and in the same order
            foreach (PowerTrade currentTrade in currentTrades)
            {
                var currentTradeText = JsonConvert.SerializeObject(currentTrade);
                var matchTradeText   = JsonConvert.SerializeObject(matchTrades[matchTradeIndex]);
                Assert.AreEqual(currentTradeText, matchTradeText);
                matchTradeIndex++;
                currentTradecount++;
            }
            //confirm count is the same
            Assert.AreEqual(currentTradecount, matchTrades.Count);
        }
Example #9
0
        public IEnumerable <HourlyVolume> GetPowerTrades()
        {
            PowerService powerService = new PowerService();
            var          powerTrades  = powerService.GetTrades(DateTime.Today);

            var hourlyVolume = powerTrades.SelectMany(x => x.Periods).Select(y => new HourlyVolume
            {
                Hour   = y.Period.ToString(),
                Volume = Decimal.Parse(y.Volume.ToString())
            }).ToList();


            var totalVolumesByPeriod =
                from volume in hourlyVolume
                group volume by volume.Hour into volumeGroup
                select new HourlyVolume
            {
                Hour   = volumeGroup.Key,
                Volume = Math.Round(volumeGroup.Sum(x => x.Volume), 0)
            };


            return(totalVolumesByPeriod);
        }
 public void Explore()
 {
     var service = new PowerService();
     var trades = service.GetTrades(new DateTime(2015, 10, 5, 23, 0, 0));
 }
Example #11
0
        /// <summary>
        /// Aggregate Power period volumes and extract to CSV file
        /// </summary>
        /// <parameter name></parameter>
        /// <returns></returns>
        private void PwrPositionAggCalc()
        {
            PwrServiceLog.WriteEventLog("");
            PwrServiceLog.WriteEventLog("$$$$$$ Power Trades Period Volume Aggregation Operation Starts. $$$$$$");

            double dblVol1 = 0.0, dblVol2 = 0.0, dblVol3 = 0.0, dblVol4 = 0.0, dblVol5 = 0.0, dblVol6 = 0.0, dblVol7 = 0.0, dblVol8 = 0.0,
                   dblVol9 = 0.0, dblVol10 = 0.0, dblVol11 = 0.0, dblVol12 = 0.0, dblVol13 = 0.0, dblVol14 = 0.0, dblVol15 = 0.0, dblVol16 = 0.0,
                   dblVol17 = 0.0, dblVol18 = 0.0, dblVol19 = 0.0, dblVol20 = 0.0, dblVol21 = 0.0, dblVol22 = 0.0, dblVol23 = 0.0, dblVol24 = 0.0;

            int    intCounter = 0, intTradePeriod = 0;
            string strExtractYear, strExtractMonth, strExtractDate, strEroorMsg, strFileLocation, strFileName, strPath;

            // DataTable Object Declaration
            DataTable tblPwrVolume = null;

            try
            {
                // Retrieving extract date from Application Configuration File
                PwrServiceLog.WriteEventLog("Retrieving extract date from Application Configuration File ");
                strExtractYear  = System.Configuration.ConfigurationManager.AppSettings["ExtractRunYear"];
                strExtractMonth = System.Configuration.ConfigurationManager.AppSettings["ExtractRunMonth"];
                strExtractDate  = System.Configuration.ConfigurationManager.AppSettings["ExtractRunDate"];

                DateTime dtExtractDate = new DateTime(Int32.Parse(strExtractYear), Int32.Parse(strExtractMonth), Int32.Parse(strExtractDate));
                PwrServiceLog.WriteEventLog("Service running for extract Date: " + dtExtractDate.ToString());

                // initialize object for Power Service
                PowerService pwrService = new PowerService();
                PwrServiceLog.WriteEventLog("PowerService DLL Object initialized.");

                // Calling PowerService DLL Method GetTrades()
                PwrServiceLog.WriteEventLog("PowerService DLL Method GetTrades() Call - To retrieve all day-ahead trades and their period/volumes.");
                var TradesAll = pwrService.GetTrades(dtExtractDate);

                foreach (var tradeCount in TradesAll)
                {
                    // setting trade counter
                    intCounter += 1;
                    PwrServiceLog.WriteEventLog("****** Power Trade Number: " + intCounter + " - Period & Volume Retrieval Starts. ******");

                    foreach (var powerTrade in tradeCount.Periods)
                    {
                        PwrServiceLog.WriteEventLog("Power Trade " + intCounter + " ,Period: " + powerTrade.Period.ToString() + " ,Volume: " + powerTrade.Volume.ToString());
                        // Aggregation of Power Trade Volumes as per the period respectively.
                        intTradePeriod = powerTrade.Period;
                        switch (intTradePeriod)
                        {
                        case 1:
                            dblVol1 += powerTrade.Volume;
                            break;

                        case 2:
                            dblVol2 += powerTrade.Volume;
                            break;

                        case 3:
                            dblVol3 += powerTrade.Volume;
                            break;

                        case 4:
                            dblVol4 += powerTrade.Volume;
                            break;

                        case 5:
                            dblVol5 += powerTrade.Volume;
                            break;

                        case 6:
                            dblVol6 += powerTrade.Volume;
                            break;

                        case 7:
                            dblVol7 += powerTrade.Volume;
                            break;

                        case 8:
                            dblVol8 += powerTrade.Volume;
                            break;

                        case 9:
                            dblVol9 += powerTrade.Volume;
                            break;

                        case 10:
                            dblVol10 += powerTrade.Volume;
                            break;

                        case 11:
                            dblVol11 += powerTrade.Volume;
                            break;

                        case 12:
                            dblVol12 += powerTrade.Volume;
                            break;

                        case 13:
                            dblVol13 += powerTrade.Volume;
                            break;

                        case 14:
                            dblVol14 += powerTrade.Volume;
                            break;

                        case 15:
                            dblVol15 += powerTrade.Volume;
                            break;

                        case 16:
                            dblVol16 += powerTrade.Volume;
                            break;

                        case 17:
                            dblVol17 += powerTrade.Volume;
                            break;

                        case 18:
                            dblVol18 += powerTrade.Volume;
                            break;

                        case 19:
                            dblVol19 += powerTrade.Volume;
                            break;

                        case 20:
                            dblVol20 += powerTrade.Volume;
                            break;

                        case 21:
                            dblVol21 += powerTrade.Volume;
                            break;

                        case 22:
                            dblVol22 += powerTrade.Volume;
                            break;

                        case 23:
                            dblVol23 += powerTrade.Volume;
                            break;

                        case 24:
                            dblVol24 += powerTrade.Volume;
                            break;
                        }
                    }
                    PwrServiceLog.WriteEventLog("****** Power Trade Number: " + intCounter + " - All Period & Respective Volume Retrieval Completed. ******");
                }

                // if PowerService DLL Method GetTrades() returns 0 trades then throw an exception.
                if (intCounter == 0)
                {
                    strEroorMsg = "%%% ERROR: %%% - There is no day ahead power trades retrieved by calling PowerService DLL Method GetTrades(), Trade Count = " + intCounter;
                    PwrServiceLog.WriteEventLog(strEroorMsg);
                    throw new Exception("An generic error occurred in PwrPositionAggCalc function. Exception Message: " + strEroorMsg);
                }
                else
                {
                    PwrServiceLog.WriteEventLog("@@@@@@ Day Ahead Power Trade's - Period wise Volume Aggregation Completed, Total Trade Count: " + intCounter + " @@@@@@");
                }

                // DataTable Initialization to store Power Periods & Aggregated Volumes
                tblPwrVolume = new DataTable();
                // Column Name Addition in DataTable tblPwrVolume
                tblPwrVolume.Columns.Add("Local Time", typeof(string));
                tblPwrVolume.Columns.Add("Volume", typeof(Double));
                PwrServiceLog.WriteEventLog("Data Table Created to hold Power Trade's Power Periods and respective aggregated volumes.");

                // Adding Power Periods & Aggregated Volumes into DataTable
                tblPwrVolume.Rows.Add("23:00", dblVol1);
                tblPwrVolume.Rows.Add("00:00", dblVol2);
                tblPwrVolume.Rows.Add("01:00", dblVol3);
                tblPwrVolume.Rows.Add("02:00", dblVol4);
                tblPwrVolume.Rows.Add("03:00", dblVol5);
                tblPwrVolume.Rows.Add("04:00", dblVol6);
                tblPwrVolume.Rows.Add("05:00", dblVol7);
                tblPwrVolume.Rows.Add("06:00", dblVol8);
                tblPwrVolume.Rows.Add("07:00", dblVol9);
                tblPwrVolume.Rows.Add("08:00", dblVol10);
                tblPwrVolume.Rows.Add("09:00", dblVol11);
                tblPwrVolume.Rows.Add("10:00", dblVol12);
                tblPwrVolume.Rows.Add("11:00", dblVol13);
                tblPwrVolume.Rows.Add("12:00", dblVol14);
                tblPwrVolume.Rows.Add("13:00", dblVol15);
                tblPwrVolume.Rows.Add("14:00", dblVol16);
                tblPwrVolume.Rows.Add("15:00", dblVol17);
                tblPwrVolume.Rows.Add("16:00", dblVol18);
                tblPwrVolume.Rows.Add("17:00", dblVol19);
                tblPwrVolume.Rows.Add("18:00", dblVol20);
                tblPwrVolume.Rows.Add("19:00", dblVol21);
                tblPwrVolume.Rows.Add("20:00", dblVol22);
                tblPwrVolume.Rows.Add("21:00", dblVol23);
                tblPwrVolume.Rows.Add("22:00", dblVol24);

                PwrServiceLog.WriteEventLog("Data Table filled with Day Ahead Power Periods and aggregated volumes.");

                // CSV File location retrieve from Application Configuration file
                strFileLocation = System.Configuration.ConfigurationManager.AppSettings["CsvFileLocation"];
                PwrServiceLog.WriteEventLog("CSV File location retrieved from Application Configuration file as: " + strFileLocation);
                strFileName = "\\PowerPosition_" + DateTime.Now.ToString("yyyyMMdd_HHmm") + ".csv";

                strPath = strFileLocation + strFileName;
                // Function Call to Create CSV File
                CsvExtract(strPath, tblPwrVolume, true);

                // Release DataTable
                tblPwrVolume.Dispose();

                PwrServiceLog.WriteEventLog("$$$$$$ Power Trades Period Volume Aggregation Operation completed Successfully.. $$$$$$");
                PwrServiceLog.WriteEventLog("");
            }
            catch (Exception objEx)
            {
                PwrServiceLog.WriteEventLog("%%% EXCEPTION ERROR %%% - Exception occurred in PwrPositionAggCalc Method. Exception Message: " + objEx.Message.ToString());
                throw new Exception("An generic error occurred in PwrPositionAggCalc function. Exception Message:" + objEx.Message.ToString());
            }
            finally
            {
                // Release DataTable
                tblPwrVolume.Dispose();
            }
        }