/** * 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); }
public override decimal Calculate(ITimeSeries series, ITradingRecord tradingRecord) { int numberOfProfitable = 0; foreach (Trade trade in tradingRecord.Trades) { int entryIndex = trade.GetEntry().getIndex(); int exitIndex = trade.GetExit().getIndex(); decimal result; if (trade.GetEntry().isBuy()) { // buy-then-sell trade result = series.GetBar(exitIndex).ClosePrice.DividedBy(series.GetBar(entryIndex).ClosePrice); } else { // sell-then-buy trade result = series.GetBar(entryIndex).ClosePrice.DividedBy(series.GetBar(exitIndex).ClosePrice); } if (result.IsGreaterThan(Decimals.ONE)) { numberOfProfitable++; } } return(((decimal)numberOfProfitable).DividedBy(tradingRecord.GetTradeCount())); }
protected override decimal Calculate(int index) { decimal previousBarClosePrice = _series.GetBar(Math.Max(0, index - 1)).ClosePrice; decimal currentBarClosePrice = _series.GetBar(index).ClosePrice; return(currentBarClosePrice.DividedBy(previousBarClosePrice)); }
/** * Calculates the cash flow for a single trade. * @param trade a single trade */ private void Calculate(Trade trade) { int entryIndex = trade.GetEntry().getIndex(); int begin = entryIndex + 1; if (begin > _values.Count) { decimal lastValue = _values[_values.Count - 1]; // _values.AddRange(Collections.nCopies(begin - _values.Count, lastValue)); _values.AddRange(Enumerable.Range(0, begin - _values.Count).Select(_ => lastValue)); } int end = trade.GetExit().getIndex(); for (int i = Math.Max(begin, 1); i <= end; i++) { decimal ratio; if (trade.GetEntry().isBuy()) { ratio = _timeSeries.GetBar(i).ClosePrice.DividedBy(_timeSeries.GetBar(entryIndex).ClosePrice); } else { ratio = _timeSeries.GetBar(entryIndex).ClosePrice.DividedBy(_timeSeries.GetBar(i).ClosePrice); } _values.Add(_values[entryIndex].MultipliedBy(ratio)); } }
protected override decimal Calculate(int index) { decimal MaxPrice = _series.GetBar(index).MaxPrice; decimal MinPrice = _series.GetBar(index).MinPrice; decimal closePrice = _series.GetBar(index).ClosePrice; return(MaxPrice.Plus(MinPrice).Plus(closePrice).DividedBy(Decimals.THREE)); }
protected override decimal Calculate(int index) { decimal ts = _series.GetBar(index).MaxPrice.Minus(_series.GetBar(index).MinPrice); decimal ys = index == 0 ? Decimals.Zero : _series.GetBar(index).MaxPrice.Minus(_series.GetBar(index - 1).ClosePrice); decimal yst = index == 0 ? Decimals.Zero : _series.GetBar(index - 1).ClosePrice.Minus(_series.GetBar(index).MinPrice); return(ts.Abs().Max(ys.Abs()).Max(yst.Abs())); }
protected override decimal Calculate(int index) { int nIndex = Math.Max(index - _timeFrame, 0); decimal nPeriodsAgoValue = _series.GetBar(nIndex).Volume; decimal currentValue = _series.GetBar(index).Volume; return(currentValue.Minus(nPeriodsAgoValue) .DividedBy(nPeriodsAgoValue) .MultipliedBy(Decimals.HUNDRED)); }
public void indicatorShouldRetrieveBarVariation() { Assert.AreEqual(variationIndicator.GetValue(0), 1); for (int i = 1; i < 10; i++) { decimal previousBarClosePrice = timeSeries.GetBar(i - 1).ClosePrice; decimal currentBarClosePrice = timeSeries.GetBar(i).ClosePrice; Assert.AreEqual(variationIndicator.GetValue(i), currentBarClosePrice.DividedBy(previousBarClosePrice)); } }
public void indicatorShouldRetrieveBarClosePrice() { decimal result; for (int i = 0; i < 10; i++) { result = timeSeries.GetBar(i).MaxPrice.Plus(timeSeries.GetBar(i).MinPrice) .DividedBy(Decimals.TWO); Assert.AreEqual(average.GetValue(i), result); } }
protected override bool Calculate(int index) { if (index < 3) { // We need 4 candles: 1 black, 3 white return(false); } blackCandleIndex = index - 3; return(_series.GetBar(blackCandleIndex).IsBearish() && isWhiteSoldier(index - 2) && isWhiteSoldier(index - 1) && isWhiteSoldier(index)); }
protected override bool Calculate(int index) { if (index < 3) { // We need 4 candles: 1 white, 3 black return(false); } whiteCandleIndex = index - 3; return(_series.GetBar(whiteCandleIndex).IsBullish() && isBlackCrow(index - 2) && isBlackCrow(index - 1) && isBlackCrow(index)); }
public override decimal Calculate(ITimeSeries series, Trade trade) { int entryIndex = trade.GetEntry().getIndex(); int exitIndex = trade.GetExit().getIndex(); if (trade.GetEntry().isBuy()) { return(series.GetBar(exitIndex).ClosePrice.DividedBy(series.GetBar(entryIndex).ClosePrice)); } else { return(series.GetBar(entryIndex).ClosePrice.DividedBy(series.GetBar(exitIndex).ClosePrice)); } }
public void indicatorShouldRetrieveBarClosePrice() { for (int i = 0; i < 10; i++) { Assert.AreEqual(closePrice.GetValue(i), timeSeries.GetBar(i).ClosePrice); } }
public void indicatorShouldRetrieveBarAmountPrice() { for (int i = 0; i < 10; i++) { Assert.AreEqual(amountIndicator.GetValue(i), timeSeries.GetBar(i).Amount); } }
public void indicatorShouldRetrieveBarTrade() { for (int i = 0; i < 10; i++) { Assert.AreEqual((int)tradeIndicator.GetValue(i), timeSeries.GetBar(i).NumberOfTrades); } }
protected override decimal Calculate(int index) { IBar bar = _series.GetBar(index); return(((bar.ClosePrice.Minus(bar.MinPrice)).Minus(bar.MaxPrice.Minus(bar.ClosePrice))) .DividedBy(bar.MaxPrice.Minus(bar.MinPrice))); }
/** * Constructor. * @param index the index the order is executed * @param series the time series * @param type the type of the order */ public Order(int index, ITimeSeries series, OrderType type) { _type = type; _index = index; _amount = 1; _price = series.GetBar(index).ClosePrice; }
public void indicatorShouldRetrieveBarMaxPrice() { for (int i = 0; i < 10; i++) { Assert.AreEqual(maxPriceIndicator.GetValue(i), timeSeries.GetBar(i).MaxPrice); } }
protected override decimal Calculate(int index) { if (index == 0) { return(Decimals.Zero); } decimal upMove = _series.GetBar(index).MaxPrice.Minus(_series.GetBar(index - 1).MaxPrice); decimal downMove = _series.GetBar(index - 1).MinPrice.Minus(_series.GetBar(index).MinPrice); if (downMove.IsGreaterThan(upMove) && downMove.IsGreaterThan(Decimals.Zero)) { return(downMove); } else { return(Decimals.Zero); } }
public void whenTimeFrameIs1ResultShouldBeIndicatorValue() { // throws exception IIndicator <decimal> indicator = getIndicator(new ClosePriceIndicator(data), 1); for (int i = 0; i < data.GetBarCount(); i++) { Assert.AreEqual(data.GetBar(i).ClosePrice, indicator.GetValue(i)); } }
public void indicatorShouldRetrieveBarMaxPrice() { for (int i = 0; i < 10; i++) { IBar bar = timeSeries.GetBar(i); decimal typicalPrice = bar.MaxPrice.Plus(bar.MinPrice).Plus(bar.ClosePrice) .DividedBy(Decimals.THREE); Assert.AreEqual(typicalPrice, typicalPriceIndicator.GetValue(i)); } }
protected override decimal Calculate(int index) { if (index == 0) { return(Decimals.Zero); } decimal yesterdayClose = _series.GetBar(index - 1).ClosePrice; decimal todayClose = _series.GetBar(index).ClosePrice; if (yesterdayClose.IsGreaterThan(todayClose)) { return(GetValue(index - 1).Minus(_series.GetBar(index).Volume)); } else if (yesterdayClose.IsLessThan(todayClose)) { return(GetValue(index - 1).Plus(_series.GetBar(index).Volume)); } return(GetValue(index - 1)); }
public override decimal Calculate(ITimeSeries series, Trade trade) { int entryIndex = trade.GetEntry().getIndex(); int exitIndex = trade.GetExit().getIndex(); decimal result; if (trade.GetEntry().isBuy()) { // buy-then-sell trade result = series.GetBar(exitIndex).ClosePrice.DividedBy(series.GetBar(entryIndex).ClosePrice); } else { // sell-then-buy trade result = series.GetBar(entryIndex).ClosePrice.DividedBy(series.GetBar(exitIndex).ClosePrice); } return((result.IsGreaterThan(Decimals.ONE)) ? 1M : 0M); }
protected override decimal Calculate(int index) { if (index == 0) { return(Decimals.THOUSAND); } IBar currentBar = _series.GetBar(index); IBar previousBar = _series.GetBar(index - 1); decimal previousValue = GetValue(index - 1); if (currentBar.Volume.IsLessThan(previousBar.Volume)) { decimal currentPrice = currentBar.ClosePrice; decimal previousPrice = previousBar.ClosePrice; decimal priceChangeRatio = currentPrice.Minus(previousPrice).DividedBy(previousPrice); return(previousValue.Plus(priceChangeRatio.MultipliedBy(previousValue))); } return(previousValue); }
protected override decimal Calculate(int index) { int startIndex = Math.Max(0, index - _timeFrame + 1); decimal sumOfVolume = Decimals.Zero; for (int i = startIndex; i <= index; i++) { sumOfVolume = sumOfVolume.Plus(_series.GetBar(i).Volume); } return(sumOfVolume); }
protected override bool Calculate(int index) { if (index < 1) { // Harami is a 2-candle pattern return(false); } IBar prevBar = _series.GetBar(index - 1); IBar currBar = _series.GetBar(index); if (prevBar.IsBearish() && currBar.IsBullish()) { decimal prevOpenPrice = prevBar.OpenPrice; decimal prevClosePrice = prevBar.ClosePrice; decimal currOpenPrice = currBar.OpenPrice; decimal currClosePrice = currBar.ClosePrice; return(currOpenPrice.IsLessThan(prevOpenPrice) && currOpenPrice.IsGreaterThan(prevClosePrice) && currClosePrice.IsLessThan(prevOpenPrice) && currClosePrice.IsGreaterThan(prevClosePrice)); } return(false); }
/** * Calculates the profit of a trade (Buy and sell). * @param series a time series * @param trade a trade * @return the profit of the trade */ private decimal CalculateProfit(ITimeSeries series, Trade trade) { decimal profit = Decimals.ONE; if (trade.IsClosed()) { // use price of entry/exit order, if NaN use close price of underlying time series decimal exitClosePrice = trade.GetExit().getPrice().IsNaN() ? series.GetBar(trade.GetExit().getIndex()).ClosePrice : trade.GetExit().getPrice(); decimal entryClosePrice = trade.GetEntry().getPrice().IsNaN() ? series.GetBar(trade.GetEntry().getIndex()).ClosePrice : trade.GetEntry().getPrice(); if (trade.GetEntry().isBuy()) { profit = exitClosePrice.DividedBy(entryClosePrice); } else { profit = entryClosePrice.DividedBy(exitClosePrice); } } return(profit); }
protected override decimal Calculate(int index) { if (index == 0) { return(Decimals.Zero); } // Calculating the money flow multiplier decimal moneyFlowMultiplier = _clvIndicator.GetValue(index); // Calculating the money flow volume decimal moneyFlowVolume = moneyFlowMultiplier.MultipliedBy(_series.GetBar(index).Volume); return(moneyFlowVolume.Plus(GetValue(index - 1))); }
protected override decimal Calculate(int index) { IBar t = _series.GetBar(index); decimal openPrice = t.OpenPrice; decimal closePrice = t.ClosePrice; if (closePrice.IsGreaterThan(openPrice)) { // Bullish return(openPrice.Minus(t.MinPrice)); } else { // Bearish return(closePrice.Minus(t.MinPrice)); } }
public void getBarWithRemovedIndexOnMovingSeriesShouldReturnFirstRemainingBar() { IBar bar = defaultSeries.GetBar(4); defaultSeries.SetMaximumBarCount(2); Assert.AreEqual(bar, defaultSeries.GetBar(0)); Assert.AreEqual(bar, defaultSeries.GetBar(1)); Assert.AreEqual(bar, defaultSeries.GetBar(2)); Assert.AreEqual(bar, defaultSeries.GetBar(3)); Assert.AreEqual(bar, defaultSeries.GetBar(4)); Assert.AreNotEqual(bar, defaultSeries.GetBar(5)); }