my MTF MACD doesn't work
my MTF MACD doesn't work
03 Apr 2021, 20:30
Hi,
I'm basically just want to make the MACD indicator from different timeframe to show up in current timeframe. For example, I am currently viewing 15 minute chart and I want to see 1-hour MACD in my current view, instead of changing m15 and h1 chart back and forth or opening up multiple chart windows. I've wrote the codes for it but it only works when the selected timeframe is the same as current view, else, it didn't work. I don't know what is the problem since I am new to coding.
Just a side note, although there's a lot of resource in the internet, I just can't digest it by reading only. I'm learning this by messing around with the examples.
Below is my indicator code. Your help is very much appreciated. Thank you in advance!
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class MTFMACD : Indicator
{
[Output("MACD Histogram,", LineColor = "#FF40E0D0", PlotType = PlotType.Histogram, LineStyle = LineStyle.Solid, Thickness = 5)]
public IndicatorDataSeries MACD_Histogram { get; set; }
[Output("MACD Line,", LineColor = "Blue", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
public IndicatorDataSeries MACD_Line { get; set; }
[Output("Signal Line,", LineColor = "Red", PlotType = PlotType.Line, LineStyle = LineStyle.Lines, Thickness = 1)]
public IndicatorDataSeries Signal_Line { get; set; }
[Parameter("Timeframe", DefaultValue = "Hour")]
public TimeFrame tf { get; set; }
[Parameter("Applied price", DefaultValue = Price.Close)]
public Price Source { get; set; }
public enum Price
{
Open,
Close,
High,
Low,
Median,
Typical,
Weighted
}
[Parameter("MA Type", DefaultValue = 1)]
public MovingAverageType MAType { get; set; }
[Parameter("Long Period", DefaultValue = 26)]
public int LongPeriod { get; set; }
[Parameter("Short Period", DefaultValue = 12)]
public int ShortPeriod { get; set; }
[Parameter("Signal Period", DefaultValue = 9)]
public int SignalPeriod { get; set; }
private MovingAverage MA1, MA2, MA3;
private DataSeries Series1;
private Price refresh;
private TimeFrame checktimeframe;
protected override void Initialize()
{
restart();
}
public override void Calculate(int index)
{
if (refresh != Source || checktimeframe != tf)
restart();
var prevMACD_Line = MACD_Line[index - 1];
var prevSignal_Line = Signal_Line[index - 1];
if (double.IsNaN(prevMACD_Line))
MACD_Line[index] = Series1[index];
else
MACD_Line[index] = MA2.Result.LastValue - MA1.Result.LastValue;
if (double.IsNaN(prevSignal_Line))
Signal_Line[index] = MACD_Line[index];
else
Signal_Line[index] = MA3.Result.LastValue;
MACD_Histogram[index] = MACD_Line[index] - Signal_Line[index];
}
private void restart()
{
Series1 = GetSeries(MarketData.GetBars(tf), Source);
refresh = Source;
checktimeframe = tf;
MA1 = Indicators.MovingAverage(Series1, LongPeriod, MAType);
MA2 = Indicators.MovingAverage(Series1, ShortPeriod, MAType);
MA3 = Indicators.MovingAverage(MACD_Line, SignalPeriod, MAType);
}
private DataSeries GetSeries(Bars bars, Price type)
{
switch (type)
{
case Price.Open:
return bars.OpenPrices;
case Price.Close:
return bars.ClosePrices;
case Price.High:
return bars.HighPrices;
case Price.Low:
return bars.LowPrices;
case Price.Median:
return bars.MedianPrices;
case Price.Typical:
return bars.TypicalPrices;
case Price.Weighted:
return bars.WeightedPrices;
default:
return bars.ClosePrices;
}
}
}
}
Replies
K3Charts
19 Oct 2024, 17:26
( Updated at: 20 Oct 2024, 05:18 )
atualized in 10/2024. naw wich histogram change color above and below level =0
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class MTFMACD : Indicator
{
private MacdCrossOver _macd;
private Bars _baseTimeFrameBars;
[Output("Histogram Up", LineColor = "Green", PlotType = PlotType.Histogram, Thickness = 5)]
public IndicatorDataSeries MACD_Histogram_Up { get; set; }
[Output("Histogram Down", LineColor = "Red", PlotType = PlotType.Histogram, Thickness = 5)]
public IndicatorDataSeries MACD_Histogram_Down { get; set; }
[Output("MACD Line", LineColor = "Gold", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
public IndicatorDataSeries MACD_Line { get; set; }
[Output("Signal Line", LineColor = "Red", PlotType = PlotType.Line, LineStyle = LineStyle.Lines, Thickness = 1)]
public IndicatorDataSeries Signal_Line { get; set; }
[Parameter("Base Timeframe", DefaultValue = "Daily")]
public TimeFrame BaseTimeFrame { get; set; }
[Parameter("Applied price", DefaultValue = Price.Close)]
public Price Source { get; set; }
[Parameter("Long Period", DefaultValue = 10)]
public int LongPeriod { get; set; }
[Parameter("Short Period", DefaultValue = 3)]
public int ShortPeriod { get; set; }
[Parameter("Signal Period", DefaultValue = 16)]
public int SignalPeriod { get; set; }
protected override void Initialize()
{
_baseTimeFrameBars = MarketData.GetBars(BaseTimeFrame);
var baseSeries = GetBaseSeries();
_macd = Indicators.MacdCrossOver(baseSeries, LongPeriod, ShortPeriod, SignalPeriod);
}
public override void Calculate(int index)
{
var baseSeriesIndex = _baseTimeFrameBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
// MACD histogram logic with color change based on value
double macdHistogramValue = _macd.Histogram[baseSeriesIndex];
if (macdHistogramValue >= 0)
{
MACD_Histogram_Up[index] = macdHistogramValue; // Histogram Up (green)
MACD_Histogram_Down[index] = double.NaN; // NaN to hide the down histogram
}
else
{
MACD_Histogram_Up[index] = double.NaN; // NaN to hide the up histogram
MACD_Histogram_Down[index] = macdHistogramValue; // Histogram Down (red)
}
// MACD line and signal line
MACD_Line[index] = _macd.MACD[baseSeriesIndex];
Signal_Line[index] = _macd.Signal[baseSeriesIndex];
}
private DataSeries GetBaseSeries()
{
switch (Source)
{
case Price.Open:
return _baseTimeFrameBars.OpenPrices;
case Price.Close:
return _baseTimeFrameBars.ClosePrices;
case Price.High:
return _baseTimeFrameBars.HighPrices;
case Price.Low:
return _baseTimeFrameBars.LowPrices;
case Price.Median:
return _baseTimeFrameBars.MedianPrices;
case Price.Typical:
return _baseTimeFrameBars.TypicalPrices;
case Price.Weighted:
return _baseTimeFrameBars.WeightedPrices;
default:
return _baseTimeFrameBars.ClosePrices;
}
}
}
public enum Price
{
Open,
Close,
High,
Low,
Median,
Typical,
Weighted
}
}
@K3Charts
amusleh
04 Apr 2021, 13:27
Hi,
Try this:
Please read the API references and take a look to sample indicators/cBots code before building an indicator or cBot.
@amusleh