Indicator not drawing Arrows for both conditions
Indicator not drawing Arrows for both conditions
08 Jun 2023, 01:00
Hi there!
Thanks in advance to anyone who takes the time to help me clear things up. I've only got a basic understanding of programming and I'm stuck at the moment so any help will be greatly appreciated.
The indicator's logic:
The following indicator looks at a daily chart of the current symbol to determine a main trend direction using the crossover between a couple of EMAs. Once we know that, it should also evaluate the direction of a StochasticOscillator and make sure that it's not in overbought or oversold territory to see if we'll draw a green upwards arrow or red downwards arrow.
The current issue:
After testing the current code, I see that I'm able to determine the current main trend's direction, but depending on that, the indicator is only drawing upwards arrows when the current main trend direction is going upwards or downward arrows when the current main trend direction is headed down but it's never drawing both types of arrows.
Here are a couple of examples:
1.
Here on the GBPCHF chart, since the current trend direction of the daily time frame at the time is headed upwards, the indicator is only drawing green arrows even at moments when the main trend's direction was headed upwards.
2.
Here on the EURUSD chart, since at the moment, the main trend is going downwards, the indicator only draws red arrows even at the end of March and beginning of April when the main trend was headed upwards.
using cAlgo.API;
using cAlgo.API.Indicators;
using Color = System.Drawing.Color;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
public class CustomArrowsIndicator : Indicator
{
//Arrows
private readonly string upArrow = "\u25B2";
private readonly string downArrow = "\u25BC";
private double arrowOffset;
private void SetSymbolAlignment(ChartText symbol)
{
symbol.HorizontalAlignment = HorizontalAlignment.Center;
}
//indicators
private Bars higherTimeFrameIndex;
private ExponentialMovingAverage higherTimeFrameFastEMA;
private ExponentialMovingAverage higherTimeFrameSlowEMA;
private double rsi;
protected override void Initialize()
{
arrowOffset = Symbol.PipSize * 10;
higherTimeFrameIndex = MarketData.GetBars(TimeFrame.Daily);
rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 9).Result.LastValue;
higherTimeFrameFastEMA = Indicators.ExponentialMovingAverage(higherTimeFrameIndex.ClosePrices, 5);
higherTimeFrameSlowEMA = Indicators.ExponentialMovingAverage(higherTimeFrameIndex.ClosePrices, 10);
}
public override void Calculate(int index)
{
int x = index;
double y;
string arrowName;
StochasticOscillator stochastic = Indicators.StochasticOscillator(10, 3, 3,MovingAverageType.Simple);
bool mainDirectionIsUpwards = higherTimeFrameFastEMA.Result.LastValue > higherTimeFrameSlowEMA.Result.LastValue;
bool mainDirectionIsDownwards = higherTimeFrameFastEMA.Result.LastValue < higherTimeFrameSlowEMA.Result.LastValue;
bool stochasticIsOverbought = stochastic.PercentK.LastValue >= 80;
bool stochasticIsOversold = stochastic.PercentK.LastValue <= 10;
bool isStochasticOscillatorUpward = stochastic.PercentK.LastValue > stochastic.PercentD.LastValue;
bool isStochasticOscillatorDownward = stochastic.PercentK.LastValue < stochastic.PercentD.LastValue;
var high = Bars.HighPrices[index];
var low = Bars.LowPrices[index];
arrowName = string.Format("Arrow {0}", index);
if (mainDirectionIsUpwards && rsi > 50 && isStochasticOscillatorUpward && !stochasticIsOverbought )
{
y = low - arrowOffset;
var drawUpArrow = Chart.DrawText(arrowName, upArrow, x, y, API.Color.Green);
SetSymbolAlignment(drawUpArrow);
}
else if (mainDirectionIsDownwards && rsi < 50 && isStochasticOscillatorDownward && !stochasticIsOversold )
{
y = high + arrowOffset;
var drawDownArrow = Chart.DrawText(arrowName, downArrow, x, y, API.Color.Red);
SetSymbolAlignment(drawDownArrow);
}
}
}
}
firemyst
09 Jun 2023, 03:09
The code you have gets the EMA values for the Higher Time Frame, which is the daily chart.
The charts you're showing in the screen captures are the 4 hour charts.
So while on the GBPCHF 4-hour chart the fast email is below the slow ema, on the daily chart, the fast ema can still be above the slower ema, hence only the one arrow being displayed on the 4 hour.
@firemyst