public void IfCacheWorks() { var sma = new SmaIndicator(new ClosePriceIndicator(_series), 3); var firstTime = sma.GetValue(4); var secondTime = sma.GetValue(4); Assert.AreEqual(firstTime, secondTime); }
public void StochasticOscilatorDParam14UsingSma3AndGenericConstructer() { var sof = new StochasticOscillatorKIndicator(_data, 14); var sma = new SmaIndicator(sof, 3); var sos = new StochasticOscillatorDIndicator(sma); Assert.AreEqual(sma.GetValue(0), sos.GetValue(0)); Assert.AreEqual(sma.GetValue(1), sos.GetValue(1)); Assert.AreEqual(sma.GetValue(2), sos.GetValue(2)); }
public void GetValueWithOldResultsRemoval() { var data = new double[20]; TaTestsUtils.ArraysFill(data, 1); TimeSeries timeSeries = GenerateTimeSeries.From(data); var sma = new SmaIndicator(new ClosePriceIndicator(timeSeries), 10); TaTestsUtils.AssertDecimalEquals(sma.GetValue(5), 1); TaTestsUtils.AssertDecimalEquals(sma.GetValue(10), 1); timeSeries.MaximumTickCount = 12; TaTestsUtils.AssertDecimalEquals(sma.GetValue(19), 1); }
public void GetValueWithNullTimeSeries() { var constant = new ConstantIndicator <Decimal>(Decimal.Ten); Assert.AreEqual(Decimal.Ten, constant.GetValue(0)); Assert.AreEqual(Decimal.Ten, constant.GetValue(100)); Assert.IsNull(constant.TimeSeries); var sma = new SmaIndicator(constant, 10); Assert.AreEqual(Decimal.Ten, sma.GetValue(0)); Assert.AreEqual(Decimal.Ten, sma.GetValue(100)); Assert.IsNull(sma.TimeSeries); }
protected override Decimal Calculate(int index) { var shortMa = _shortSma.GetValue(index); var longMa = _longSma.GetValue(index); return(shortMa.Minus(longMa).DividedBy(longMa).MultipliedBy(Decimal.Hundred)); }
/// <param name="index"> the tick/candle index </param> /// <returns> true if the tick/candle has a very short lower shadow, false otherwise </returns> private bool HasVeryShortLowerShadow(int index) { var currentLowerShadow = _lowerShadowInd.GetValue(index); // We use the white candle index to remove to bias of the previous crows var averageLowerShadow = _averageLowerShadowInd.GetValue(_whiteCandleIndex); return(currentLowerShadow.IsLessThan(averageLowerShadow.MultipliedBy(_factor))); }
/// <param name="index"> the tick/candle index </param> /// <returns> true if the tick/candle has a very short upper shadow, false otherwise </returns> private bool HasVeryShortUpperShadow(int index) { var currentUpperShadow = _upperShadowInd.GetValue(index); // We use the black candle index to remove to bias of the previous soldiers var averageUpperShadow = _averageUpperShadowInd.GetValue(_blackCandleIndex); return(currentUpperShadow.IsLessThan(averageUpperShadow.MultipliedBy(_factor))); }
public void SmaWhenTimeFrameIs1ResultShouldBeIndicatorValue() { var quoteSma = new SmaIndicator(new ClosePriceIndicator(_data), 1); for (var i = 0; i < _data.TickCount; i++) { Assert.AreEqual(_data.GetTick(i).ClosePrice, quoteSma.GetValue(i)); } }
public void GetValueWithCacheLengthIncrease() { var data = new double[200]; TaTestsUtils.ArraysFill(data, 10); var sma = new SmaIndicator(new ClosePriceIndicator(GenerateTimeSeries.From(data)), 100); TaTestsUtils.AssertDecimalEquals(sma.GetValue(105), 10); }
public void ValuesLessThanTimeFrameMustBeEqualsToSmaValues() { var zlema = new ZlemaIndicator(new ClosePriceIndicator(_data), 10); var sma = new SmaIndicator(new ClosePriceIndicator(_data), 10); for (var i = 0; i < 9; i++) { Assert.AreEqual(sma.GetValue(i), zlema.GetValue(i)); } }
public void BollingerBandsMiddleUsingSma() { var sma = new SmaIndicator(new ClosePriceIndicator(_data), 3); var bbmSma = new BollingerBandsMiddleIndicator(sma); for (var i = 0; i < _data.TickCount; i++) { Assert.AreEqual(sma.GetValue(i), bbmSma.GetValue(i)); } }
protected override bool Calculate(int index) { if (index < 1) { return(_bodyHeightInd.GetValue(index).IsZero); } var averageBodyHeight = _averageBodyHeightInd.GetValue(index - 1); var currentBodyHeight = _bodyHeightInd.GetValue(index); return(currentBodyHeight.IsLessThan(averageBodyHeight.MultipliedBy(_factor))); }
protected override Decimal Calculate(int index) { var typicalPrice = _typicalPriceInd.GetValue(index); var typicalPriceAvg = _smaInd.GetValue(index); var meanDeviation = _meanDeviationInd.GetValue(index); if (meanDeviation.IsZero) { return(Decimal.Zero); } return((typicalPrice.Minus(typicalPriceAvg)).DividedBy(meanDeviation.MultipliedBy(Factor))); }
public void GetValueOnResultsCalculatedFromRemovedTicksShouldReturnFirstRemainingResult() { TimeSeries timeSeries = GenerateTimeSeries.From(1, 1, 1, 1, 1); timeSeries.MaximumTickCount = 3; Assert.AreEqual(2, timeSeries.RemovedTicksCount); var sma = new SmaIndicator(new ClosePriceIndicator(timeSeries), 2); for (var i = 0; i < 5; i++) { TaTestsUtils.AssertDecimalEquals(sma.GetValue(i), 1); } }
protected override Decimal Calculate(int index) { var startIndex = Math.Max(0, index - _timeFrame + 1); var numberOfObservations = index - startIndex + 1; var variance = Decimal.Zero; var average = _sma.GetValue(index); for (var i = startIndex; i <= index; i++) { var pow = _indicator.GetValue(i).Minus(average).Pow(2); variance = variance.Plus(pow); } variance = variance.DividedBy(Decimal.ValueOf(numberOfObservations)); return(variance); }
protected override Decimal Calculate(int index) { var absoluteDeviations = Decimal.Zero; var average = _sma.GetValue(index); var startIndex = Math.Max(0, index - _timeFrame + 1); var nbValues = index - startIndex + 1; for (var i = startIndex; i <= index; i++) { // For each period... absoluteDeviations = absoluteDeviations.Plus(_indicator.GetValue(i).Minus(average).Abs()); } return(absoluteDeviations.DividedBy(Decimal.ValueOf(nbValues))); }
protected override Decimal Calculate(int index) { var startIndex = Math.Max(0, index - _timeFrame + 1); var numberOfObservations = index - startIndex + 1; var covariance = Decimal.Zero; var average1 = _sma1.GetValue(index); var average2 = _sma2.GetValue(index); for (var i = startIndex; i <= index; i++) { var mul = _indicator1.GetValue(i).Minus(average1).MultipliedBy(_indicator2.GetValue(i).Minus(average2)); covariance = covariance.Plus(mul); } covariance = covariance.DividedBy(Decimal.ValueOf(numberOfObservations)); return(covariance); }
public void QuickStart() { // Getting a time series (from any provider: CSV, web service, etc.) var series = CsvTradesLoader.LoadBitstampSeries(); // Getting the close price of the ticks var firstClosePrice = series.GetTick(0).ClosePrice; Console.WriteLine("First close price: " + firstClosePrice.ToDouble()); // Or within an indicator: var closePrice = new ClosePriceIndicator(series); // Here is the same close price: Console.WriteLine(firstClosePrice.IsEqual(closePrice.GetValue(0))); // equal to firstClosePrice // Getting the simple moving average (SMA) of the close price over the last 5 ticks var shortSma = new SmaIndicator(closePrice, 5); // Here is the 5-ticks-SMA value at the 42nd index Console.WriteLine("5-ticks-SMA value at the 42nd index: " + shortSma.GetValue(42).ToDouble()); // Getting a longer SMA (e.g. over the 30 last ticks) var longSma = new SmaIndicator(closePrice, 30); // Ok, now let's building our trading rules! // Buying rules // We want to buy: // - if the 5-ticks SMA crosses over 30-ticks SMA // - or if the price goes below a defined price (e.g $800.00) var buyingRule = (new CrossedUpIndicatorRule(shortSma, longSma)) .Or(new CrossedDownIndicatorRule(closePrice, Decimal.ValueOf("800"))); // Selling rules // We want to sell: // - if the 5-ticks SMA crosses under 30-ticks SMA // - or if if the price looses more than 3% // - or if the price earns more than 2% var sellingRule = (new CrossedDownIndicatorRule(shortSma, longSma)) //.Or(new CrossedDownIndicatorRule(new TrailingStopLossIndicator(closePrice, Decimal.ValueOf("30")), closePrice )) .Or(new StopLossRule(closePrice, Decimal.ValueOf("3"))) .Or(new StopGainRule(closePrice, Decimal.ValueOf("2"))); // Running our juicy trading strategy... var tradingRecord = series.Run(new Strategy(buyingRule, sellingRule)); Console.WriteLine("Number of trades for our strategy: " + tradingRecord.TradeCount); // Analysis // Getting the cash flow of the resulting trades var cashFlow = new CashFlow(series, tradingRecord); // Getting the profitable trades ratio IAnalysisCriterion profitTradesRatio = new AverageProfitableTradesCriterion(); Console.WriteLine("Profitable trades ratio: " + profitTradesRatio.Calculate(series, tradingRecord)); // Getting the reward-risk ratio IAnalysisCriterion rewardRiskRatio = new RewardRiskRatioCriterion(); Console.WriteLine("Reward-risk ratio: " + rewardRiskRatio.Calculate(series, tradingRecord)); // Total profit of our strategy // vs total profit of a buy-and-hold strategy IAnalysisCriterion vsBuyAndHold = new VersusBuyAndHoldCriterion(new TotalProfitCriterion()); Console.WriteLine("Our profit vs buy-and-hold profit: " + vsBuyAndHold.Calculate(series, tradingRecord)); // Your turn! }
protected override Decimal Calculate(int index) { return(_sma5.GetValue(index).Minus(_sma34.GetValue(index))); }
public void Dpo() { var dpo = new DpoIndicator(_series, 9); var cp = new ClosePriceIndicator(_series); var sma = new SmaIndicator(cp, 9); int timeShift = 9 / 2 + 1; for (int i = _series.Begin; i <= _series.End; i++) { TaTestsUtils.AssertDecimalEquals(dpo.GetValue(i), cp.GetValue(i).Minus(sma.GetValue(i - timeShift))); } TaTestsUtils.AssertDecimalEquals(dpo.GetValue(9), 0.111999); TaTestsUtils.AssertDecimalEquals(dpo.GetValue(10), -0.02); TaTestsUtils.AssertDecimalEquals(dpo.GetValue(11), 0.21142857142); TaTestsUtils.AssertDecimalEquals(dpo.GetValue(12), 0.169999999999999); }
protected override Decimal Calculate(int index) { return(_price.GetValue(index).Minus(_sma.GetValue(index - _timeShift))); }
public void SmaUsingTimeFrame3UsingClosePrice() { var sma = new SmaIndicator(new ClosePriceIndicator(_data), 3); TaTestsUtils.AssertDecimalEquals(sma.GetValue(0), 1); TaTestsUtils.AssertDecimalEquals(sma.GetValue(1), 1.5); TaTestsUtils.AssertDecimalEquals(sma.GetValue(2), 2); TaTestsUtils.AssertDecimalEquals(sma.GetValue(3), 3); TaTestsUtils.AssertDecimalEquals(sma.GetValue(4), 10d / 3); TaTestsUtils.AssertDecimalEquals(sma.GetValue(5), 11d / 3); TaTestsUtils.AssertDecimalEquals(sma.GetValue(6), 4); TaTestsUtils.AssertDecimalEquals(sma.GetValue(7), 13d / 3); TaTestsUtils.AssertDecimalEquals(sma.GetValue(8), 4); TaTestsUtils.AssertDecimalEquals(sma.GetValue(9), 10d / 3); TaTestsUtils.AssertDecimalEquals(sma.GetValue(10), 10d / 3); TaTestsUtils.AssertDecimalEquals(sma.GetValue(11), 10d / 3); TaTestsUtils.AssertDecimalEquals(sma.GetValue(12), 3); }