Need Help on Indicator Code
Need Help on Indicator Code
29 Jul 2024, 10:01
Dear forum friends,
I am a newbie in ctrader and indicator coding. I want to create an indicator to plot arrow based on criteria below:
1. ema10>ema20 and ema20>ema50
2. candle[index] (this is a green candle) close above the open candle[index-1] (this is a red candle).
3. Plot an arrow when the above criteria are met
Problem I face:
When price is moving up and down and whenever the above criteria are met, the arrow will be plotted directly without waiting for the candle to closed. I would like the candle to closed only plot the arrow.
Any idea how to make my code below to plot arrow only after candle closed?
Thanks for the help.
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;namespace cAlgo.Indicators
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class CustomEMAIndicator : Indicator
{
[Parameter("EMA10 Period", Group = "EMA Settings", DefaultValue = 10)]
public int EMA10Period { get; set; }[Parameter("EMA20 Period", Group = "EMA Settings", DefaultValue = 20)]
public int EMA20Period { get; set; }[Parameter("EMA50 Period", Group = "EMA Settings", DefaultValue = 50)]
public int EMA50Period { get; set; }private ExponentialMovingAverage _ema10;
private ExponentialMovingAverage _ema20;
private ExponentialMovingAverage _ema50;protected override void Initialize()
{
_ema10 = Indicators.ExponentialMovingAverage(Bars.ClosePrices, EMA10Period);
_ema20 = Indicators.ExponentialMovingAverage(Bars.ClosePrices, EMA20Period);
_ema50 = Indicators.ExponentialMovingAverage(Bars.ClosePrices, EMA50Period);
}public override void Calculate(int index)
{
if (index < 3)
return;//For Long
if (_ema10.Result[index] > _ema20.Result[index] && _ema20.Result[index] > _ema50.Result[index])
{
bool candle1Red = Bars[index - 1].Close < Bars[index - 1].Open; //red candle
bool candle2Green = Bars[index].Close > Bars[index].Open; //green candle
bool candle2CloseAboveCandle1Open = Bars[index].Close > Bars[index - 1].Open;
if (candle1Red && candle2Green && candle2CloseAboveCandle1Open)
{
Chart.DrawIcon("upArrow" + index, ChartIconType.UpArrow, index, Bars[index].Low, Color.Green);}
//
}}
}
}
Replies
leeliangwee
29 Jul 2024, 14:20
( Updated at: 30 Jul 2024, 05:37 )
RE: Need Help on Indicator Code
PanagiotisCharalampous said:
Hi there,
This happens because you check the values of the current candle that has not closed yet. Try shifting your checks to index - 1 and index - 2.
Best regards,
Panagiotis
Hi,
I get what you mean, thanks a lot for the help. :)
@leeliangwee
PanagiotisCharalampous
29 Jul 2024, 10:22
Hi there,
This happens because you check the values of the current candle that has not closed yet. Try shifting your checks to index - 1 and index - 2.
Best regards,
Panagiotis
@PanagiotisCharalampous