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 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)"); }