using System;
using System.Collections.Generic;
using TSLab.Script;
using TSLab.Script.Handlers;
using TSLab.Script.Helpers;
namespace TSLab.NRMA
{
public class NRMA : BasePeriodIndicatorHandler, IDouble2DoubleHandler
{
// функция вычисления индикатора NRMA (последовательность значений)
// kShift - коэффициент смещения
// kSharp - степень для усиления выраженности индикатора (2-3)
public IList<double> GenNRMA(ISecurity source, double kShift, double kSharp)
{
#region Variables
int Dir; // нарпавление индикатора NRTR (+1 вверх, -1 вниз)
int MinPeriod;
double MaxPrice, MinPrice, UpPrice, DownPrice;
double vNRTR, vRatio, vNRMA;
double vOSC, vOSC1, vOSC2;
IList<double> nNRMA = new List<double>(source.Bars.Count);
#endregion
//--------------------------------------------------------------------------------
#region Init vars
Dir = 0;
MinPeriod = 2;
vOSC = 0;
vOSC1 = 0;
vOSC2 = 0;
vNRMA = 0;
#endregion
//--------------------------------------------------------------------------------
#region
MaxPrice = source.HighPrices[0];
MinPrice = source.LowPrices[0];
UpPrice = MinPrice * (1 + kShift / 100);
DownPrice = MaxPrice * (1 - kShift / 100);
#endregion
for (int bar = 0; bar < source.Bars.Count-1; bar++)
{
//--------------------------------------------------------------------------------
#region calculate values
int NewDir = Dir;
double NewUpPrice = source.LowPrices[bar] * (1 + kShift / 100);
double NewDownPrice = source.HighPrices[bar] * (1 - kShift / 100);
if (Dir > -1)
{
if (source.LowPrices[bar] < DownPrice)
{
NewDir = -1;
UpPrice = NewUpPrice;
}
}
if (Dir < 1)
{
if (source.HighPrices[bar] > UpPrice)
{
NewDir = 1;
DownPrice = NewDownPrice;
}
}
Dir = NewDir;
if ((Dir > -1) && (NewDownPrice > DownPrice)) DownPrice = NewDownPrice;
if ((Dir < 1) && (NewUpPrice < UpPrice)) UpPrice = NewUpPrice;
vNRTR = DownPrice;
if (Dir < 1) vNRTR = UpPrice;
vOSC2 = vOSC1;
vOSC1 = vOSC;
vOSC = (100 * Math.Abs(source.ClosePrices[bar] - vNRTR) / source.ClosePrices[bar]) / kShift;
if (bar == 0)
{
vOSC1 = vOSC;
vOSC2 = vOSC;
vNRMA = source.ClosePrices[bar];
}
vRatio = Math.Pow((vOSC + vOSC1 + vOSC2) / 3, kSharp);
double Factor = 2.0 / (1 + MinPeriod);
vNRMA = vNRMA + vRatio * Factor * (source.ClosePrices[bar] - vNRMA);
#endregion
//--------------------------------------------------------------------------------
nNRMA.Add(vNRMA);
}
return nNRMA;