use enum in IF?
05 Sep 2019, 09:08
Greetings all -- I am trying to learn how things work here, and could use some help, please.
I have done the following: Created an enum. Created a parameter that allows the selction of the enum.
Defined basic OHLC, and created an IF to plot a dot if (the IF) is satisfied...
What I want is to have the argument in the IF stand in for the actual enum - so that selecting the enum in the parameter will casue the IF to evaluate the defined variables, and if true, to plot a dot. Everything works but the if/enum business.
I am sure that ther is some basic aspect of C# I am violating or don't understand, so any help would be greatly appreciated!
Here is the code:
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator("Test00", IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class CT_BarColor : Indicator
{
public enum FormationType
{
UpClose,
DownClose
}
[Parameter("BarColor")]
public FormationType BarColor { get; set; }
[Parameter("V Spacing Text", DefaultValue = 6, MinValue = 0, Step = 0.1)]
public double V_space { get; set; }
[Parameter("H Spacing Text", DefaultValue = 1.5, MinValue = 0, Step = 0.1)]
public double H_space { get; set; }
[Parameter("Dot Spacing", DefaultValue = 8, MinValue = 0, Step = 0.1)]
public int DotSpacing { get; set; }
[Output("Dot Above", LineColor = "#FD6341", PlotType = PlotType.Points, Thickness = 7)]
public IndicatorDataSeries DotAbove { get; set; }
[Output("Dot Below", LineColor = "#008000", PlotType = PlotType.Points, Thickness = 7)]
public IndicatorDataSeries DotBelow { get; set; }
protected override void Initialize()
{
// Initialize and create nested indicators
}
public override void Calculate(int index)
{
double Open = MarketSeries.Open[index];
double High = MarketSeries.High[index];
double Low = MarketSeries.Low[index];
double Close = MarketSeries.Close[index];
bool DownClose = Close < Open;
bool UpClose = Close > Open;
///This below works -- But this is where I want the argument for the IF (currently DownClose), to grab the selected enum ///from the above parameter.
///i.e. I want it to be: if (BarColor) {}, so that (BarColor) returns whatever has been selected in the parameter
if (DownClose)
{
DotAbove[index] = High + (DotSpacing * Symbol.PipSize);
}
}
}
}
Replies
Aiki1000
05 Sep 2019, 17:21
RE:
Panagiotis Charalampous said:
Hi Aiki1000,
See below how to do this
if (BarColor == FormationType.DownClose) { DotAbove[index] = High + (DotSpacing * Symbol.PipSize); }Best Regards,
Panagiotis
Thank you, sir!
And all other enum would follow as further If(s) or else(s), correct?
That's great, thank you - I really appreciate the help...!
@Aiki1000
Aiki1000
05 Sep 2019, 18:29
RE:
Panagiotis Charalampous said:
Hi Aiki1000,
See below how to do this
if (BarColor == FormationType.DownClose) { DotAbove[index] = High + (DotSpacing * Symbol.PipSize); }Best Regards,
Panagiotis
Hello again - Just tried your solution. If I use the code snippet pasted above, I get plots over every bar.
What, please, am I doing wrong?
Also, is there a way to enumerate (each of the enums) within the IF argyument? Or do I need to build an If/else chain for each enum individually? I have no idea, obviously, but I would have thought the code would have been something like:
if (BarColor == FormationType.XXX)
{
DotAbove[index] = High + (DotSpacing * Symbol.PipSize);
}
with the XXX somehow calling the enum currently selected in the parameter
Thx again for the help -
@Aiki1000
PanagiotisCharalampous
09 Sep 2019, 09:55
Hi Aiki1000,
My example demonstrated how to use an enum in the if statement. If you want to add more conditions in the statement just add them with a && operator.
Regarding
Also, is there a way to enumerate (each of the enums) within the IF argyument?
I am not sure what do you mean. If you want to check all the cases of an enum you can consider a switch statement
Best Regards,
Panagiotis
@PanagiotisCharalampous

PanagiotisCharalampous
05 Sep 2019, 09:31
Hi Aiki1000,
See below how to do this
if (BarColor == FormationType.DownClose) { DotAbove[index] = High + (DotSpacing * Symbol.PipSize); }Best Regards,
Panagiotis
@PanagiotisCharalampous