public void getBarData() { // series Assert.AreEqual(bars, defaultSeries.GetBarData()); // Constrained series Assert.AreEqual(bars.Count, constrainedSeries.GetBarData().Count); Assert.AreEqual(bars[0], constrainedSeries.GetBarData()[0]); // Empty series Assert.AreEqual(0, emptySeries.GetBarData().Count); }
public BaseTimeSeries(ITimeSeries defaultSeries, int seriesBeginIndex, int seriesEndIndex) : this(defaultSeries.Name, defaultSeries.GetBarData().ToList(), seriesBeginIndex, seriesEndIndex, true) { if (defaultSeries.GetBarData() == null || defaultSeries.GetBarData().isEmpty()) { throw new ArgumentException("Cannot create a constrained series from a time series with a null/empty list of bars"); } if (defaultSeries.GetMaximumBarCount() != int.MaxValue) { throw new ArgumentException("Cannot create a constrained series from a time series for which a maximum bar count has been set"); } }
/** * Runs the provided strategy over the managed series (from startIndex to finishIndex). * <p> * @param strategy the trading strategy * @param orderType the {@link OrderType} used to open the trades * @param amount the amount used to open/close the trades * @param startIndex the start index for the run (included) * @param finishIndex the finish index for the run (included) * @return the trading record coMing from the run */ public ITradingRecord Run(IStrategy strategy, OrderType orderType, decimal amount, int startIndex, int finishIndex) { int runBeginIndex = Math.Max(startIndex, _timeSeries.GetBeginIndex()); int runEndIndex = Math.Min(finishIndex, _timeSeries.GetEndIndex()); // log.trace("Running strategy (indexes: {} -> {}): {} (starting with {})", runBeginIndex, runEndIndex, strategy, orderType); ITradingRecord tradingRecord = new BaseTradingRecord(orderType); for (int i = runBeginIndex; i <= runEndIndex; i++) { // For each bar between both indexes[] if (strategy.ShouldOperate(i, tradingRecord)) { tradingRecord.Operate(i, _timeSeries.GetBar(i).ClosePrice, amount); } } if (!tradingRecord.IsClosed()) { // If the last trade is still opened, we search out of the run end index. // May works if the end index for this run was inferior to the actual number of bars int seriesMaxSize = Math.Max(_timeSeries.GetEndIndex() + 1, _timeSeries.GetBarData().Count); for (int i = runEndIndex + 1; i < seriesMaxSize; i++) { // For each bar after the end index of this run[] // --> Trying to close the last trade if (strategy.ShouldOperate(i, tradingRecord)) { tradingRecord.Operate(i, _timeSeries.GetBar(i).ClosePrice, amount); break; } } } return(tradingRecord); }