Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Minor fixes and start param support #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
295 changes: 150 additions & 145 deletions Trady.Analysis/Indicator/ParabolicStopAndReverse.cs
Original file line number Diff line number Diff line change
@@ -1,145 +1,150 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Trady.Analysis.Candlestick;
using Trady.Analysis.Infrastructure;
using Trady.Core;
using Trady.Core.Infrastructure;

namespace Trady.Analysis.Indicator
{
public class ParabolicStopAndReverse<TInput, TOutput> : CumulativeNumericAnalyzableBase<TInput, (decimal High, decimal Low), TOutput>
{
const int LongShortDeterminationPeriod = 4;

public decimal Step { get; }

public decimal MaximumStep { get; }

bool _isUptrend;
decimal _extremePoint;
decimal _acceleratingFactor;

public ParabolicStopAndReverse(IEnumerable<TInput> inputs, Func<TInput, (decimal High, decimal Low)> inputMapper, decimal step = 0.02m, decimal maximumStep = 0.2m) : base(inputs, inputMapper)
{
if (step > maximumStep)
throw new ArgumentException($"{nameof(step)} must be smaller than {nameof(maximumStep)}");

MaximumStep = maximumStep;
Step = step;
}

protected override decimal? ComputeCumulativeValue(IReadOnlyList<(decimal High, decimal Low)> mappedInputs, int index, decimal? prevOutputToMap)
=> _isUptrend ? ComputeUptrend(mappedInputs, index, prevOutputToMap) : ComputeDowntrend(mappedInputs, index, prevOutputToMap);

decimal? ComputeDowntrend(IReadOnlyList<(decimal High, decimal Low)> mappedInputs, int index, decimal? prevOutputToMap)
{
decimal? sar;
var isTrendUnchanged = mappedInputs[index - 1].High < prevOutputToMap;
if (isTrendUnchanged)
{
// Current downtrend
sar = prevOutputToMap + _acceleratingFactor * (_extremePoint - prevOutputToMap);
sar = new decimal[] { sar.Value, mappedInputs[index - 1].High, mappedInputs[index - 2].High }.Max();

var isNewExtreme = mappedInputs[index].Low < _extremePoint;
_extremePoint = Math.Min(_extremePoint, mappedInputs[index].Low);
if (mappedInputs[index].High < sar && isNewExtreme)
_acceleratingFactor += Math.Min(Step, MaximumStep - _acceleratingFactor);
}
else
{
// Current uptrend
sar = _extremePoint;
_extremePoint = Math.Max(_extremePoint, mappedInputs[index].High);
_acceleratingFactor = Step;
}

_isUptrend = !isTrendUnchanged;
return sar;
}

decimal? ComputeUptrend(IReadOnlyList<(decimal High, decimal Low)> mappedInputs, int index, decimal? prevOutputToMap)
{
decimal? sar;
var isTrendUnchanged = mappedInputs[index - 1].Low > prevOutputToMap;
if (isTrendUnchanged)
{
// Current uptrend
sar = prevOutputToMap + _acceleratingFactor * (_extremePoint - prevOutputToMap);
sar = new decimal[] { sar.Value, mappedInputs[index - 1].Low, mappedInputs[index - 2].Low }.Min();

var isNewExtreme = mappedInputs[index].High > _extremePoint;
_extremePoint = Math.Max(_extremePoint, mappedInputs[index].High);
if (mappedInputs[index].Low > sar && isNewExtreme)
_acceleratingFactor += Math.Min(Step, MaximumStep - _acceleratingFactor);
}
else
{
// Current downtrend
sar = _extremePoint;
_extremePoint = Math.Min(_extremePoint, mappedInputs[index].Low);
_acceleratingFactor = Step;
}

_isUptrend = isTrendUnchanged;
return sar;
}

protected override decimal? ComputeInitialValue(IReadOnlyList<(decimal High, decimal Low)> mappedInputs, int index)
{
decimal sar;

var subsetInputs = mappedInputs.Take(index + 1).ToList().AsReadOnly();
_isUptrend = IsUptrend(subsetInputs, index);
if (_isUptrend)
{
sar = subsetInputs.Min(i => i.Low);
_extremePoint = subsetInputs.Max(i => i.High);
}
else
{
sar = subsetInputs.Max(i => i.High);
_extremePoint = subsetInputs.Min(i => i.Low);
}

_acceleratingFactor = Step;
return sar;
}

static bool IsUptrend(ReadOnlyCollection<(decimal High, decimal Low)> inputs, int index)
{
const int Strictness = 1;

var isUptrend = new UpTrendByTuple(inputs, Strictness)[index].GetValueOrDefault(false);
if (isUptrend)
return true;

var isDowntrend = new DownTrendByTuple(inputs, Strictness)[index].GetValueOrDefault(false);
if (isDowntrend)
return false;

decimal highLowMean(int i) => (inputs[i].High + inputs[i].Low) / 2;
return highLowMean(index) > highLowMean(index - 1);
}

protected override int InitialValueIndex => LongShortDeterminationPeriod;
}

public class ParabolicStopAndReverseByTuple : ParabolicStopAndReverse<(decimal High, decimal Low), decimal?>
{
public ParabolicStopAndReverseByTuple(IEnumerable<(decimal High, decimal Low)> inputs, decimal step = 0.02M, decimal maximumStep = 0.2M)
: base(inputs, i => i, step, maximumStep)
{
}
}

public class ParabolicStopAndReverse : ParabolicStopAndReverse<IOhlcv, AnalyzableTick<decimal?>>
{
public ParabolicStopAndReverse(IEnumerable<IOhlcv> inputs, decimal step = 0.02M, decimal maximumStep = 0.2M)
: base(inputs, i => (i.High, i.Low), step, maximumStep)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Trady.Analysis.Candlestick;
using Trady.Analysis.Infrastructure;
using Trady.Core;
using Trady.Core.Infrastructure;

namespace Trady.Analysis.Indicator
{
public class ParabolicStopAndReverse<TInput, TOutput> : CumulativeNumericAnalyzableBase<TInput, (decimal High, decimal Low), TOutput>
{
const int LongShortDeterminationPeriod = 4;

public decimal Start { get; }

public decimal Step { get; }

public decimal MaximumStep { get; }

bool _isUptrend;
decimal _extremePoint;
decimal _acceleratingFactor;

public ParabolicStopAndReverse(IEnumerable<TInput> inputs, Func<TInput, (decimal High, decimal Low)> inputMapper,
decimal start = 0.00252m, decimal step = 0.00133m, decimal maximumStep = 0.22m) : base(inputs, inputMapper)
{
if (step > maximumStep)
throw new ArgumentException($"{nameof(step)} must be smaller than {nameof(maximumStep)}");

Start = start;
MaximumStep = maximumStep;
Step = step;
}

protected override decimal? ComputeCumulativeValue(IReadOnlyList<(decimal High, decimal Low)> mappedInputs, int index, decimal? prevOutputToMap)
=> _isUptrend ? ComputeUptrend(mappedInputs, index, prevOutputToMap) : ComputeDowntrend(mappedInputs, index, prevOutputToMap);

decimal? ComputeDowntrend(IReadOnlyList<(decimal High, decimal Low)> mappedInputs, int index, decimal? prevOutputToMap)
{
decimal? sar;
var isTrendUnchanged = mappedInputs[index].High < prevOutputToMap;
if (isTrendUnchanged)
{
// Current downtrend
sar = prevOutputToMap + _acceleratingFactor * (_extremePoint - prevOutputToMap);
sar = new decimal[] { sar.Value, mappedInputs[index - 1].High, mappedInputs[index - 2].High }.Max();

var isNewExtreme = mappedInputs[index].Low < _extremePoint;
_extremePoint = Math.Min(_extremePoint, mappedInputs[index].Low);
if (mappedInputs[index].High < sar && isNewExtreme)
_acceleratingFactor += Math.Min(Step, MaximumStep - _acceleratingFactor);
}
else
{
// Current uptrend
sar = _extremePoint;
_extremePoint = Math.Max(_extremePoint, mappedInputs[index].High);
_acceleratingFactor = Start;
}

_isUptrend = !isTrendUnchanged;
return sar;
}

decimal? ComputeUptrend(IReadOnlyList<(decimal High, decimal Low)> mappedInputs, int index, decimal? prevOutputToMap)
{
decimal? sar;
var isTrendUnchanged = mappedInputs[index].Low > prevOutputToMap;
if (isTrendUnchanged)
{
// Current uptrend
sar = prevOutputToMap + _acceleratingFactor * (_extremePoint - prevOutputToMap);
sar = new decimal[] { sar.Value, mappedInputs[index - 1].Low, mappedInputs[index - 2].Low }.Min();

var isNewExtreme = mappedInputs[index].High > _extremePoint;
_extremePoint = Math.Max(_extremePoint, mappedInputs[index].High);
if (mappedInputs[index].Low > sar && isNewExtreme)
_acceleratingFactor += Math.Min(Step, MaximumStep - _acceleratingFactor);
}
else
{
// Current downtrend
sar = _extremePoint;
_extremePoint = Math.Min(_extremePoint, mappedInputs[index].Low);
_acceleratingFactor = Start;
}

_isUptrend = isTrendUnchanged;
return sar;
}

protected override decimal? ComputeInitialValue(IReadOnlyList<(decimal High, decimal Low)> mappedInputs, int index)
{
decimal sar;

var subsetInputs = mappedInputs.Take(index + 1).ToList().AsReadOnly();
_isUptrend = IsUptrend(subsetInputs, index);
if (_isUptrend)
{
sar = subsetInputs.Min(i => i.Low);
_extremePoint = subsetInputs.Max(i => i.High);
}
else
{
sar = subsetInputs.Max(i => i.High);
_extremePoint = subsetInputs.Min(i => i.Low);
}

_acceleratingFactor = Step;
return sar;
}

static bool IsUptrend(ReadOnlyCollection<(decimal High, decimal Low)> inputs, int index)
{
const int Strictness = 1;

var isUptrend = new UpTrendByTuple(inputs, Strictness)[index].GetValueOrDefault(false);
if (isUptrend)
return true;

var isDowntrend = new DownTrendByTuple(inputs, Strictness)[index].GetValueOrDefault(false);
if (isDowntrend)
return false;

decimal highLowMean(int i) => (inputs[i].High + inputs[i].Low) / 2;
return highLowMean(index) > highLowMean(index - 1);
}

protected override int InitialValueIndex => LongShortDeterminationPeriod;
}

public class ParabolicStopAndReverseByTuple : ParabolicStopAndReverse<(decimal High, decimal Low), decimal?>
{
public ParabolicStopAndReverseByTuple(IEnumerable<(decimal High, decimal Low)> inputs, decimal start = 0.02M,
decimal step = 0.02M, decimal maximumStep = 0.2M)
: base(inputs, i => i, start, step, maximumStep)
{
}
}

public class ParabolicStopAndReverse : ParabolicStopAndReverse<IOhlcv, AnalyzableTick<decimal?>>
{
public ParabolicStopAndReverse(IEnumerable<IOhlcv> inputs, decimal start = 0.02M, decimal step = 0.02M, decimal maximumStep = 0.2M)
: base(inputs, i => (i.High, i.Low), start, step, maximumStep)
{
}
}
}