mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-06 17:42:13 +00:00
Instrument lookups, configuration, credentials, CSV entities, snapshots and the alert processing service are read and written asynchronously; the blocking synchronous forms are obsolete wrappers. IAsyncPersistable replaces IPersistable: every type that saves and loads its settings has LoadAsync and SaveAsync only, and nested objects are read with GetValueAsync, LoadEntireAsync and LoadAllAsync. The synchronous Load and Save are the obsolete Ecng extensions. The MultiConnect sample keeps IPersistable for two settings classes, because the settings window of the published StockSharp.Xaml package takes it. Copies that were made by saving and loading are made asynchronously. IMessageAdapter, IIndicator, the risk, commission, PnL, latency, slippage and position managers, IStorageBuffer, Strategy, DiagramElement, ICompositionModel and its nodes get CloneAsync and no longer implement ICloneable; Clone is an obsolete blocking wrapper. A strategy copy keeps every security and portfolio parameter the original holds. Messages, FieldMapping, BankDetails and WithdrawInfo are copied in memory. A CommissionRuleMessage shares its rule and the emulator takes a copy of it. Envelope gets a constructor that takes all three lines and CreateAsync. ToBruteForce and ToBruteForceRandom become ToBruteForceAsync and ToBruteForceRandomAsync and return IAsyncEnumerable; BruteForceOptimizer runs over it and takes the next strategy through an asynchronous delegate. Diagram elements keep their own state in SaveState, OnLoading and LoadState. Undo restores an in-memory snapshot; a chart element holds its series and axes in it by reference. An alert rule keeps its value in the form a message carries it. A diagram strategy applies composition settings that were loaded before the composition was set in SetCompositionAsync or at the start, and saves them while they wait. Strategies get an OnStartedAsync hook; diagram strategies resolve securities, underlyings and strikes before the start so value processing never looks up instruments. Paths.Serialize, SerializeInvariant, AppStartSettings.TrySave, ICredentialsProvider.Save, ISnapshotSerializer.Serialize and the composition registry gain async forms. ICredentialsProvider.Delete becomes DeleteAsync, so loading, saving and deleting the credentials wait for one another. Paths.InstalledVersion and AppNameWithVersion are obsolete in favour of GetInstalledVersionAsync and GetAppNameWithVersionAsync. A file is opened for writing only after the last cancellation check, because opening it empties it; a cancelled read reports the cancellation instead of an empty result. A snapshot flush that failed leaves what it took due for the next one. The indicator templates for C#, F# and Python override LoadAsync and SaveAsync.
247 lines
6.5 KiB
C#
247 lines
6.5 KiB
C#
namespace StockSharp.Algo.Indicators;
|
|
|
|
/// <summary>
|
|
/// Vortex Indicator.
|
|
/// </summary>
|
|
[Display(
|
|
ResourceType = typeof(LocalizedStrings),
|
|
Name = LocalizedStrings.VIKey,
|
|
Description = LocalizedStrings.VortexKey)]
|
|
[IndicatorIn(typeof(CandleIndicatorValue))]
|
|
[Doc("topics/api/indicators/list_of_indicators/vortex_indicator.html")]
|
|
[IndicatorOut(typeof(IVortexIndicatorValue))]
|
|
public class VortexIndicator : BaseComplexIndicator<IVortexIndicatorValue>
|
|
{
|
|
private readonly VortexPart _plusVi;
|
|
private readonly VortexPart _minusVi;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="VortexIndicator"/>.
|
|
/// </summary>
|
|
public VortexIndicator()
|
|
: this(14)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="VortexIndicator"/>.
|
|
/// </summary>
|
|
/// <param name="length">Period length.</param>
|
|
public VortexIndicator(int length)
|
|
: this(new(true) { Length = length }, new(false) { Length = length })
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="VortexIndicator"/>.
|
|
/// </summary>
|
|
/// <param name="plusVi">+VI part.</param>
|
|
/// <param name="minusVi">-VI part.</param>
|
|
public VortexIndicator(VortexPart plusVi, VortexPart minusVi)
|
|
: base(plusVi, minusVi)
|
|
{
|
|
_plusVi = plusVi ?? throw new ArgumentNullException(nameof(plusVi));
|
|
_minusVi = minusVi ?? throw new ArgumentNullException(nameof(minusVi));
|
|
|
|
_plusVi.Name = "+VI";
|
|
_minusVi.Name = "-VI";
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override IndicatorMeasures Measure => IndicatorMeasures.MinusOnePlusOne;
|
|
|
|
/// <summary>
|
|
/// +VI part.
|
|
/// </summary>
|
|
[Browsable(false)]
|
|
public VortexPart PlusVi => _plusVi;
|
|
|
|
/// <summary>
|
|
/// -VI part.
|
|
/// </summary>
|
|
[Browsable(false)]
|
|
public VortexPart MinusVi => _minusVi;
|
|
|
|
/// <summary>
|
|
/// Period length.
|
|
/// </summary>
|
|
[Display(
|
|
ResourceType = typeof(LocalizedStrings),
|
|
Name = LocalizedStrings.PeriodKey,
|
|
Description = LocalizedStrings.IndicatorPeriodKey,
|
|
GroupName = LocalizedStrings.GeneralKey)]
|
|
public int Length
|
|
{
|
|
get => _plusVi.Length;
|
|
set => _plusVi.Length = _minusVi.Length = value;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override async Task SaveAsync(SettingsStorage storage, CancellationToken cancellationToken)
|
|
{
|
|
await base.SaveAsync(storage, cancellationToken);
|
|
|
|
storage.SetValue(nameof(Length), Length);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override async Task LoadAsync(SettingsStorage storage, CancellationToken cancellationToken)
|
|
{
|
|
await base.LoadAsync(storage, cancellationToken);
|
|
|
|
Length = storage.GetValue<int>(nameof(Length));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override string ToString() => base.ToString() + " " + Length;
|
|
|
|
/// <inheritdoc />
|
|
protected override IVortexIndicatorValue CreateValue(DateTime time)
|
|
=> new VortexIndicatorValue(this, time);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The part of the Vortex indicator.
|
|
/// </summary>
|
|
[IndicatorHidden]
|
|
public class VortexPart : DecimalLengthIndicator
|
|
{
|
|
private readonly DecimalBuffer _trBuffer;
|
|
private readonly DecimalBuffer _vmBuffer;
|
|
|
|
private decimal _prevHigh;
|
|
private decimal _prevLow;
|
|
private decimal _prevClose;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="VortexPart"/>.
|
|
/// </summary>
|
|
/// <param name="isPositive"><see cref="IsPositive"/></param>
|
|
public VortexPart(bool isPositive)
|
|
{
|
|
_trBuffer = new(Length) { Stats = CircularBufferStats.Sum };
|
|
_vmBuffer = new(Length) { Stats = CircularBufferStats.Sum };
|
|
|
|
IsPositive = isPositive;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Is this the positive (+VI) or negative (-VI) part.
|
|
/// </summary>
|
|
public bool IsPositive { get; }
|
|
|
|
/// <inheritdoc />
|
|
public override void Reset()
|
|
{
|
|
_trBuffer.Clear();
|
|
_vmBuffer.Clear();
|
|
_trBuffer.Capacity = _vmBuffer.Capacity = Length;
|
|
_prevHigh = 0;
|
|
_prevLow = 0;
|
|
_prevClose = 0;
|
|
|
|
base.Reset();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override int NumValuesToInitialize => base.NumValuesToInitialize + 1;
|
|
|
|
/// <inheritdoc />
|
|
protected override decimal? OnProcessDecimal(IIndicatorValue input)
|
|
{
|
|
var candle = input.ToCandle();
|
|
|
|
if (_prevClose == 0)
|
|
{
|
|
if (input.IsFinal)
|
|
{
|
|
_prevHigh = candle.HighPrice;
|
|
_prevLow = candle.LowPrice;
|
|
_prevClose = candle.ClosePrice;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
var tr = (candle.HighPrice - candle.LowPrice).Max((candle.HighPrice - _prevClose).Abs()).Max((candle.LowPrice - _prevClose).Abs());
|
|
var vm = IsPositive ? (candle.HighPrice - _prevLow).Abs() : (candle.LowPrice - _prevHigh).Abs();
|
|
|
|
decimal result;
|
|
|
|
if (input.IsFinal)
|
|
{
|
|
_trBuffer.PushBack(tr);
|
|
_vmBuffer.PushBack(vm);
|
|
|
|
_prevHigh = candle.HighPrice;
|
|
_prevLow = candle.LowPrice;
|
|
_prevClose = candle.ClosePrice;
|
|
|
|
result = _trBuffer.Sum != 0 ? _vmBuffer.Sum / _trBuffer.Sum : 0;
|
|
}
|
|
else
|
|
{
|
|
var tempSumTr = _trBuffer.Sum - (_trBuffer.Count == Length ? _trBuffer[0] : 0) + tr;
|
|
var tempSumVm = _vmBuffer.Sum - (_vmBuffer.Count == Length ? _vmBuffer[0] : 0) + vm;
|
|
result = tempSumTr != 0 ? tempSumVm / tempSumTr : 0;
|
|
}
|
|
|
|
return IsFormed ? result : 0;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override bool CalcIsFormed() => _trBuffer.Count >= Length;
|
|
}
|
|
|
|
/// <summary>
|
|
/// <see cref="VortexIndicator"/> indicator value.
|
|
/// </summary>
|
|
public interface IVortexIndicatorValue : IComplexIndicatorValue
|
|
{
|
|
/// <summary>
|
|
/// Gets the <see cref="VortexIndicator.PlusVi"/> value.
|
|
/// </summary>
|
|
IIndicatorValue PlusViValue { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the <see cref="VortexIndicator.PlusVi"/> value.
|
|
/// </summary>
|
|
[Browsable(false)]
|
|
decimal? PlusVi { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the <see cref="VortexIndicator.MinusVi"/> value.
|
|
/// </summary>
|
|
IIndicatorValue MinusViValue { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the <see cref="VortexIndicator.MinusVi"/> value.
|
|
/// </summary>
|
|
[Browsable(false)]
|
|
decimal? MinusVi { get; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Vortex Indicator value implementation.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Initializes a new instance of the <see cref="VortexIndicatorValue"/> class.
|
|
/// </remarks>
|
|
/// <param name="indicator">The parent Vortex Indicator.</param>
|
|
/// <param name="time">Time associated with this indicator value.</param>
|
|
public class VortexIndicatorValue(VortexIndicator indicator, DateTime time) : ComplexIndicatorValue<VortexIndicator>(indicator, time), IVortexIndicatorValue
|
|
{
|
|
/// <inheritdoc />
|
|
public IIndicatorValue PlusViValue => this[TypedIndicator.PlusVi];
|
|
/// <inheritdoc />
|
|
public decimal? PlusVi => PlusViValue.ToNullableDecimal(TypedIndicator.Source);
|
|
|
|
/// <inheritdoc />
|
|
public IIndicatorValue MinusViValue => this[TypedIndicator.MinusVi];
|
|
/// <inheritdoc />
|
|
public decimal? MinusVi => MinusViValue.ToNullableDecimal(TypedIndicator.Source);
|
|
|
|
/// <inheritdoc />
|
|
public override string ToString() => $"PlusVi={PlusVi}, MinusVi={MinusVi}";
|
|
}
|