//public async Task Start(int jobId) //{ // //check indian standard time // TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"); // var indianTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE).TimeOfDay; // var start = new TimeSpan(10, 0, 0); //10 o'clock // var end = new TimeSpan(18, 30, 0); //12 o'clock // if ((indianTime > start) && (indianTime < end)) // { // _jobId = jobId; // var job = await _jobService.GetJob(jobId); // var strategy = await _strategyService.Get(job.StrategyId); // var symbols = await _watchlistService.GetSymbols(strategy.WatchlistId); // var userSession = await _userSessionService.GetCurrentSession(); // _zeropdhaService = new ZerodhaBroker(AutoMapper.Mapper.Map<UserSessionModel>(userSession)); // var positions = _zeropdhaService._kite.GetPositions(); // var orders = _zeropdhaService._kite.GetOrders(); // Parallel.ForEach(symbols, symbol => // { // var candles = _zeropdhaService.GetData(symbol, _HistoricalDataInDays, _HistoricalDataTimeframe); // if (candles != null && candles.Count() > 0) // { // var position = positions.Day.Where(x => x.TradingSymbol == symbol.TradingSymbol).FirstOrDefault(); // var order = orders.Where(x => x.Tradingsymbol == symbol.TradingSymbol && x.Status == "TRIGGER PENDING").FirstOrDefault(); // var parentOrder = orders.Where(x => x.OrderId == order.ParentOrderId).FirstOrDefault(); // if (position.Quantity != 0) // SquareOffOpenPosition(symbol, candles, position, order, parentOrder); // else if (positions.Day != null && positions.Day.Count < _MaxActivePositions) // { // if (!BullishScan(symbol, candles)) // BearishScan(symbol, candles); // } // } // }); // //Update Status after every round of scanning // job.Status = JobStatus.Running.ToString(); // job.ModifiedDate = DateTime.Now; // await _jobService.Update(job); // } //} public async Task Start(int jobId) { _jobId = jobId; var job = await _jobService.GetJob(jobId); var strategy = await _strategyService.Get(job.StrategyId); var symbols = await _watchlistService.GetSymbols(strategy.WatchlistId); var userSession = await _userSessionService.GetCurrentSession(); _zeropdhaService = new ZerodhaBroker(AutoMapper.Mapper.Map <UserSessionModel>(userSession)); var positions = _zeropdhaService._kite.GetPositions(); var orders = _zeropdhaService._kite.GetOrders(); Parallel.ForEach(symbols, symbol => { var candles = _zeropdhaService.GetData(symbol, _HistoricalDataInDays, _HistoricalDataTimeframe); if (candles != null && candles.Count() > 0) { var position = positions.Day.Where(x => x.TradingSymbol == symbol.TradingSymbol).FirstOrDefault(); var order = orders.Where(x => x.Tradingsymbol == symbol.TradingSymbol && x.Status == "TRIGGER PENDING").FirstOrDefault(); var parentOrder = orders.Where(x => x.OrderId == order.ParentOrderId).FirstOrDefault(); BullishScan(symbol, candles); } }); //Update Status after every round of scanning job.Status = JobStatus.Running.ToString(); job.ModifiedDate = DateTime.Now; await _jobService.Update(job); }
private async Task Start() { var userSession = await _userSessionService.GetCurrentSession(); _zeropdhaService = new ZerodhaBroker(AutoMapper.Mapper.Map <UserSessionModel>(userSession)); if (IsMarketClosed()) { //Copy All Orders to DB var orders = _zeropdhaService._kite.GetOrders(); if (orders != null) { await _brokerOrderService.SyncOrders(orders); } //Copy All Day Positions var positions = _zeropdhaService._kite.GetPositions(); if (positions.Day != null) { await _brokerPositionService.SyncPositions(positions.Day); } //Delete Cached Data DeleteCachedHistoricalData(); //Write code for following tasks -> //Kill Jobs //Shutdown VM } }
private async Task <bool> CacheHistoryData(DateTime indianTime, bool isDevelopment) { //Delete Previous data //Directory.Delete(GlobalConfigurations.CachedDataPath , recursive: true); var job = await _jobService.GetJob(_jobId); var strategy = await _strategyService.Get(job.StrategyId); var symbols = await _watchlistService.GetSymbols(strategy.WatchlistId); var userSession = await _userSessionService.GetCurrentSession(); if (userSession != null) { _zeropdhaService = new ZerodhaBroker(AutoMapper.Mapper.Map <UserSessionModel>(userSession)); await symbols.ParallelForEachAsync(async symbol => { var candles = await _zeropdhaService.GetCachedDataAsync(symbol, _HistoricalDataTimeframe, indianTime.AddDays(_HistoricalDataInDays), indianTime, isDevelopment: isDevelopment); if (candles != null && candles.Count() > 0) { ApplicationLogger.LogJob(_jobId, "Trading Data Cached :" + symbol.TradingSymbol); } else { ApplicationLogger.LogJob(_jobId, "Trading Data Not Cached :" + symbol.TradingSymbol); } }); } return(true); }
public async Task ScriptIsProfitableOrNot() { //Get All Candles according to MAStrategy mAStrategy = new MAStrategy(); var symbolService = new SymbolService(); var userSessionService = new UserSessionService(); var symbol = await symbolService.GetAsync(21108); var userSession = await userSessionService.GetCurrentSession(); PositionTestModel positionTestModel = null; if (userSession != null) { var zeropdhaService = new ZerodhaBroker(new UserSessionModel() { AccessToken = userSession.AccessToken, ApiKey = userSession.ApiKey, AppSecret = userSession.AppSecret, PublicToken = userSession.PublicToken, UserId = userSession.UserId }); var candles = zeropdhaService.GetData( symbol, mAStrategy._HistoricalDataTimeframe, DateTime.Now.AddDays(-7), DateTime.Now.AddDays(-2), false); positionTestModel = GetProfitOfDay(symbol, candles); } Assert.IsTrue(positionTestModel.Profit > 1, "Symbol is Not Profitable"); }
public async Task <ActionResult> Sync() { var userSessionService = new UserSessionService(); var userSession = await userSessionService.GetCurrentSession(); var _zerodhaBroker = new ZerodhaBroker(AutoMapper.Mapper.Map <UserSessionModel>(userSession)); var instruments = _zerodhaBroker._kite.GetInstruments().Where(x => x.Exchange == "NSE" || x.Exchange == "BSE").ToList(); await _symbolService.DataRefereshAsync(instruments); return(View()); }
public async Task WeeklyWatchlistIsProfitableOrNot() { List <PositionTestModel> positionWeekTestModels = new List <PositionTestModel>(); //Get All Candles according to MAStrategy mAStrategy = new MAStrategy(); var watchlistService = new WatchlistService(); var userSessionService = new UserSessionService(); var symbols = await watchlistService.GetSymbols(1); var userSession = await userSessionService.GetCurrentSession(); var weekStart = 5; if (userSession != null) { var zeropdhaService = new ZerodhaBroker(new UserSessionModel() { AccessToken = userSession.AccessToken, ApiKey = userSession.ApiKey, AppSecret = userSession.AppSecret, PublicToken = userSession.PublicToken, UserId = userSession.UserId }); for (int i = 0; i < 5; i++) { List <PositionTestModel> positionTestModels = new List <PositionTestModel>(); Parallel.ForEach(symbols.Take(50), symbol => { var candles = zeropdhaService.GetData( symbol, mAStrategy._HistoricalDataTimeframe, new DateTime(2018, 3, weekStart + i, 18, 0, 0).AddDays(mAStrategy._HistoricalDataInDays), //DateTime.Now new DateTime(2018, 3, weekStart + i, 18, 0, 0), false); positionTestModels.Add(GetProfitOfDay(symbol, candles)); }); positionWeekTestModels.Add(new PositionTestModel() { dateTime = new DateTime(2018, 3, weekStart + i, 0, 0, 0), NoOfPositions = positionTestModels.Where(x => x != null).Sum(x => x.NoOfPositions), NoOfProfitable = positionTestModels.Where(x => x != null).Sum(x => x.NoOfProfitable), NoOfStopHit = positionTestModels.Where(x => x != null).Sum(x => x.NoOfStopHit), Profit = positionTestModels.Where(x => x != null).Sum(x => x.Profit) }); } } Assert.IsTrue(positionWeekTestModels.Where(x => x != null).Sum(x => x.Profit) > 1, "Week is Not Profitable"); }
public async Task Start(int jobId) { var job = await _jobService.GetJob(jobId); _jobId = jobId; var symbols = await _watchlistService.GetSymbols(job.Strategy.WatchlistId); var userSession = await _userSessionService.GetCurrentSession(); _zeropdhaService = new ZerodhaBroker(AutoMapper.Mapper.Map <UserSessionModel>(userSession)); var positions = _zeropdhaService._kite.GetPositions(); var orders = _zeropdhaService._kite.GetOrders(); Parallel.ForEach(symbols, symbol => { var candles = _zeropdhaService.GetData(symbol, -2, Constants.INTERVAL_5MINUTE); if (candles.Count() > 0) { var position = positions.Day.Where(x => x.TradingSymbol == symbol.TradingSymbol).FirstOrDefault(); var order = orders.Where(x => x.Tradingsymbol == symbol.TradingSymbol && x.Status == "TRIGGER PENDING").FirstOrDefault(); if (position.Quantity != 0) { SquareOffOpenPosition(symbol, candles, position, order); } else { if (!BullishScan(symbol, candles)) { BearishScan(symbol, candles); } } } }); //Update Status after every round of scanning job.Status = JobStatus.Running.ToString(); job.ModifiedDate = DateTime.Now; await _jobService.Update(job); }
public async Task Start(int jobId, bool isDevelopment = false) { _jobId = jobId; StringBuilder sb = new StringBuilder(); Stopwatch sw = new Stopwatch(); sw.Start(); var indianTime = GlobalConfigurations.IndianTime; ApplicationLogger.LogJob(jobId, " job Started " + indianTime.ToString()); //check indian standard time if (((indianTime.TimeOfDay > preMarketStart) && (indianTime.TimeOfDay < preMarketEnd)) || isDevelopment) { //Load History Data await CacheHistoryData(indianTime, isDevelopment); } if (((indianTime.TimeOfDay > marketStart) && (indianTime.TimeOfDay < marketEnd)) || isDevelopment) { var job = await _jobService.GetJob(jobId); var strategy = await _strategyService.Get(job.StrategyId); var symbols = await _watchlistService.GetSymbols(strategy.WatchlistId); var userSession = await _userSessionService.GetCurrentSession(); if (userSession != null) { _zeropdhaService = new ZerodhaBroker(AutoMapper.Mapper.Map <UserSessionModel>(userSession)); var positions = _zeropdhaService._kite.GetPositions(); var orders = _zeropdhaService._kite.GetOrders(); if (positions.Day != null && positions.Day.Where(x => x.Quantity != 0).Count() < _MaxActivePositions) { await symbols.ParallelForEachAsync(async symbol => { var candles = await _zeropdhaService.GetCachedDataAsync(symbol, _HistoricalDataTimeframe, indianTime.AddDays(_HistoricalDataInDays), indianTime, isDevelopment: isDevelopment); if (candles != null && candles.Count() > 0) { var position = positions.Day.Where(x => x.TradingSymbol == symbol.TradingSymbol).FirstOrDefault(); var order = orders.Where(x => x.Tradingsymbol == symbol.TradingSymbol && x.Status == "OPEN"); //check whether orde is expired IsOrderExpired(position, order); if (position.Quantity == 0 && order.Count() == 0) { Scan(symbol, candles); } } }); } //Update Status after every round of scanning job.Status = JobStatus.Running.ToString(); job.ModifiedDate = DateTime.Now; await _jobService.Update(job); } else { ApplicationLogger.LogJob(jobId, " Not Authenticated !"); } } sw.Stop(); ApplicationLogger.LogJob(jobId, " job Completed in - " + sw.Elapsed.TotalSeconds + " (Seconds)"); }
public async Task RegressionMonthlyWatchlistIsProfitableOrNot() { List <PositionTestModel> positionWeekTestModels = new List <PositionTestModel>(); //Get All Candles according to MAStrategy mAStrategy = new MAStrategy(); var watchlistService = new WatchlistService(); var userSessionService = new UserSessionService(); var symbols = await watchlistService.GetSymbols(1); var userSession = await userSessionService.GetCurrentSession(); var weekStart = new DateTime(2018, 2, 5, 9, 0, 0).StartOfWeek(DayOfWeek.Monday); IList <PositionTestModel> positionTestModels = new List <PositionTestModel>(); for (int emaShortPeriodCounter = 3; emaShortPeriodCounter < 20; emaShortPeriodCounter++) { for (int emaLongPeriodCounter = emaShortPeriodCounter += 1; emaLongPeriodCounter < 25; emaLongPeriodCounter++) { mAStrategy._EmaShortPeriod = emaShortPeriodCounter; mAStrategy._EmaLongPeriod = emaLongPeriodCounter; for (int weekCounter = 0; weekCounter < 4; weekCounter++) { DateTime tempWeekStart = weekStart.AddDays(weekCounter * 7); if (userSession != null) { var zeropdhaService = new ZerodhaBroker(new UserSessionModel() { AccessToken = userSession.AccessToken, ApiKey = userSession.ApiKey, AppSecret = userSession.AppSecret, PublicToken = userSession.PublicToken, UserId = userSession.UserId }); for (int dayCounter = 0; dayCounter < 5; dayCounter++) { Parallel.ForEach(symbols.Take(50), symbol => { var candles = zeropdhaService.GetData( symbol, mAStrategy._HistoricalDataTimeframe, new DateTime(2018, 3, tempWeekStart.Day + dayCounter, 9, 0, 0).AddDays(mAStrategy._HistoricalDataInDays), //DateTime.Now new DateTime(2018, 3, tempWeekStart.Day + dayCounter, 18, 0, 0), false); positionTestModels.Add(GetProfitOfDay(symbol, candles, mAStrategy)); }); positionWeekTestModels.Add(new PositionTestModel() { EMA = emaShortPeriodCounter + ", " + emaLongPeriodCounter, Period = mAStrategy._HistoricalDataTimeframe, dateTime = new DateTime(2018, 3, tempWeekStart.Day + dayCounter, 0, 0, 0), NoOfPositions = positionTestModels.Where(x => x != null).Sum(x => x.NoOfPositions), NoOfProfitable = positionTestModels.Where(x => x != null).Sum(x => x.NoOfProfitable), NoOfStopHit = positionTestModels.Where(x => x != null).Sum(x => x.NoOfStopHit), Profit = positionTestModels.Where(x => x != null).Sum(x => x.Profit) }); } } } } } positionWeekTestModels.WriteCSV(@"D:\Work\Autotrading\Results\RegressionMonth-" + mAStrategy._HistoricalDataTimeframe + ".csv"); Assert.IsTrue(positionWeekTestModels.Where(x => x != null).Sum(x => x.Profit) > 1, "Week is Not Profitable"); }