indicator not working as expected
indicator not working as expected
28 Dec 2024, 09:21
I have a multi-timeframe pinescript momentum oscillator indicator in tradingview which has the following code , it is supposed to draw a histogram which has a red color if <0 and a green color if >0 , it works well in tradingview but not with ctrader as the shape and indicator values are not the same :
//@version=5
indicator(title="Momentum oscillator copy 2", shorttitle="Mom oscillator copy 2", timeframe="", timeframe_gaps=true)
lengthInput = input.int(9, title="Length")
tf=input.timeframe(title="Timeframe 5 mins",defval="5")
hull9open = ta.hma(open , lengthInput)
hull9open5mins=request.security(syminfo.tickerid,tf,hull9open,gaps=barmerge.gaps_on)
momhull9open = hull9open-hull9open[1]
momhull9open5mins = hull9open5mins-hull9open5mins[1]
osc = momhull9open+momhull9open5mins
plot(osc , color = osc>0? color.green : color.red , title = "osc" , style = plot.style_columns)
I tried writing the same indicator for ctrader using the following code :
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo
{
[Indicator(AccessRights = AccessRights.None)]
public class MomentumOscillator : Indicator
{
// Declare the necessary parameters and outputs
[Output("Oscillator", LineColor = "Red")]
public IndicatorDataSeries Oscillator { get; set; }
private HullMovingAverage hma9_1Hour;
private HullMovingAverage hma9_5Min;
protected override void Initialize()
{
// Initialize Hull Moving Averages for 1-hour and 5-minute timeframes using the open prices
hma9_1Hour = Indicators.HullMovingAverage(Bars.OpenPrices, 9); // Access the open prices directly (no method call needed)
hma9_5Min = Indicators.HullMovingAverage(Bars.OpenPrices, 9); // Access the open prices directly (no method call needed)
}
public override void Calculate(int index)
{
// Ensure we don't go out of bounds (check index)
if (index <= 0)
return;
// Calculate the momentum for the 1-hour timeframe (subtract current value from previous)
double currentHMA1Hour = hma9_1Hour.Result[index];
double prevHMA1Hour = hma9_1Hour.Result[index - 1];
double momentum1Hour = currentHMA1Hour - prevHMA1Hour;
// Calculate the momentum for the 5-minute timeframe (subtract current value from previous)
double currentHMA5Min = hma9_5Min.Result[index];
double prevHMA5Min = hma9_5Min.Result[index - 1];
double momentum5Min = currentHMA5Min - prevHMA5Min;
// Calculate the sum of both momenta
double oscillatorValue = momentum1Hour + momentum5Min;
// Store the oscillator value in the output series
Oscillator[index] = oscillatorValue;
}
}
}
How can i plot the same indicator as tradingview with the same shape & values ?
Replies
karimemad94
31 Dec 2024, 16:51
( Updated at: 02 Jan 2025, 07:06 )
RE: indicator not working as expected
PanagiotisCharalampous said:
Hi there,
Can you explain what are the differences by sharing a screenshot of the cTrader indicator and explain what you would expect to see instead?
Best regards,
Panagiotis
these are the same images for gold (XAUUSD) on both platforms on the 1 hour timeframe , i want the one in ctrader to look exactly like that of tradingview , more importantly i want it to respond as fast as tradingview. because tradingview calculates indicators whenever their values change even when their values are obtained from a timeframe different than that of the displayed chart. For example, this indicator in tradingview sums up the momentum oscillator value of the 5 mins chart and the momentum oscillator value of the 1 hour chart while the displayed chart here is the 1 hour timeframe, so it gives an early entry signal (crossover or crossunder zero line) on the 1 hour chart when compared to using the 1 hour oscillator by itself. However, when i tried doing the same idea in ctrader, i found that even when i added the value of the 5 mins oscillator to the 1 hour oscillator , the chart did not respond until after the completion of a full chart period/candlestick (1 hour in this case). So how can i rewrite the code to make it as responsive as tradingview ?
@karimemad94
PanagiotisCharalampous
30 Dec 2024, 07:38
Hi there,
Can you explain what are the differences by sharing a screenshot of the cTrader indicator and explain what you would expect to see instead?
Best regards,
Panagiotis
@PanagiotisCharalampous