Description
Stochastic MTF (Multi Time Frame)
This indicator is a complete version of this topic
cTDN Forum - stochastic multitime (ctrader.com)
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
public class AWStochasticMTF : Indicator
{
// you could change default Value to Minute,Minute15,Hour2 and ...
[Parameter("Base Timeframe", DefaultValue = "Minute15")]
public TimeFrame BaseTimeFrame { get; set; }
[Parameter("K%", DefaultValue = 5)]
public int kPeriod { get; set; }
[Parameter("K slowing", DefaultValue = 3)]
public int kslowing { get; set; }
[Parameter("D%", DefaultValue = 3)]
public int dPeriod { get; set; }
[Output("K%,", LineColor = "Red", LineStyle = LineStyle.Solid, Thickness = 2)]
public IndicatorDataSeries k_Line { get; set; }
[Output("D%,", LineColor = "Blue", LineStyle = LineStyle.Solid, Thickness = 2)]
public IndicatorDataSeries d_Line { get; set; }
private StochasticOscillator _stoch;
private Bars _baseTimeFrameBars;
double slopeK, slopeD;
int n, nBars, lastBar = 0, One = 0, modeTimeframe = 0;
protected override void Initialize()
{
_baseTimeFrameBars = MarketData.GetBars(BaseTimeFrame);
_stoch = Indicators.StochasticOscillator(_baseTimeFrameBars, kPeriod, kslowing, dPeriod, MovingAverageType.Simple);
nBars = Bars.Count;
if (Chart.TimeFrame < BaseTimeFrame)
modeTimeframe = 1;
else if (Chart.TimeFrame == BaseTimeFrame)
modeTimeframe = 2;
}
private void Draw()
{
for (int j = 1; j <= nBars; j++)
{
var baseSeriesIndex = _baseTimeFrameBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[j]);
double k1 = _stoch.PercentK[baseSeriesIndex];
double d1 = _stoch.PercentD[baseSeriesIndex];
var baseSeriesIndexB = _baseTimeFrameBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[j - 1]);
double k2 = _stoch.PercentK[baseSeriesIndexB];
if (k1 != k2)
{
for (int i = j; i <= nBars; i++)
{
k2 = _stoch.PercentK[_baseTimeFrameBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[i])];
if (k1 != k2)
{
slopeK = (k2 - k1) / (i - j);
double d2 = _stoch.PercentD[_baseTimeFrameBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[i])];
slopeD = (d2 - d1) / (i - j);
n = 0;
lastBar = j;
break;
}
}
}
k_Line[j] = k1 + (slopeK * n);
d_Line[j] = d1 + (slopeD * n);
n++;
}
}
public override void Calculate(int index)
{
if (modeTimeframe == 1)
{
if (nBars < Bars.Count)
nBars = Bars.Count;
if (One != nBars)
{
Draw();
One = nBars;
}
if (IsLastBar)
{
var baseSeriesIndex = _baseTimeFrameBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
double k3 = _stoch.PercentK[_baseTimeFrameBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[lastBar - 1])];
double k4;
for (int i = lastBar - 1; i > 0; i--)
{
k4 = _stoch.PercentK[_baseTimeFrameBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[i])];
if (k3 != k4)
{
k4 = _stoch.PercentK[_baseTimeFrameBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[i + 1])];
k3 = _stoch.PercentK[_baseTimeFrameBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[lastBar])];
slopeK = (k3 - k4) / (lastBar - i);
double d4 = _stoch.PercentD[_baseTimeFrameBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[i + 1])];
double d3 = _stoch.PercentD[_baseTimeFrameBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[lastBar])];
slopeD = (d3 - d4) / (lastBar - i);
n = 0;
for (int j = i + 1; j <= lastBar; j++)
{
k_Line[j] = k4 + (slopeK * n);
d_Line[j] = d4 + (slopeD * n);
n++;
}
break;
}
}
}
}
else if (modeTimeframe == 2)
{
k_Line[index] = _stoch.PercentK[index];
d_Line[index] = _stoch.PercentD[index];
}
}
}
}
AM
Amin.fx
Joined on 02.08.2021
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: AW Stochastic MTF.algo
- Rating: 0
- Installs: 2005
- Modified: 13/10/2021 09:54
Note that publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section, please use the Copyright Infringement Notification form to submit a claim.
Comments
Log in to add a comment.
hi…
do u know why this ind not draw for HA candle ??
/rgs