Choose how many labels you want from a drop-down menu

Thinker20

New member
Good evening, hope you are doing great.
I have been writing a code that involves labels with the "AddLabel()" function but I want to know if it's possible to use the function switch and select from a drop-down menu if I want for example 2 labels to appear, is this possible? I haven't been able to do it since the switch statement needs to define or plot something before and that can't be done with this function.
I attached the code for a better understanding of what I want, any help in order to do this from a single menu (not selecting which one I want and which one doesn't individually), will be greatly appreciated, thank you very much in advance.
Code:
input MovingAverages = {default "1", "2", "3", "4", "5"};
input Price = close;
input ma1Length = 10;
input AverageTypeMa1 = AverageType.EXPONENTIAL;
input ma2Length = 20;
input AverageTypeMa2 = AverageType.EXPONENTIAL;
input ma3Length = 30;
input AverageTypeMa3 = AverageType.EXPONENTIAL;
input ma4Length = 40;
input AverageTypeMa4 = AverageType.EXPONENTIAL;
input ma5Length = 50;
input AverageTypeMa5 = AverageType.EXPONENTIAL;
input Value = 100;

def MA1 = MovingAverage(AverageTypeMa1, Price, ma1Length);
def MA2 = MovingAverage(AverageTypeMa2, Price, ma2Length);
def MA3 = MovingAverage(AverageTypeMa3, Price, ma3Length);
def MA4 = MovingAverage(AverageTypeMa4, Price, ma4Length);
def MA5 = MovingAverage(AverageTypeMa5, Price, ma5Length);

AddLabel(MovingAverages, MA1);

plot Label;
switch (MovingAverages){
case "1":
    Label = AddLabel(yes,MA1);
}
 

SleepyZ

Active member
Good evening, hope you are doing great.
I have been writing a code that involves labels with the "AddLabel()" function but I want to know if it's possible to use the function switch and select from a drop-down menu if I want for example 2 labels to appear, is this possible? I haven't been able to do it since the switch statement needs to define or plot something before and that can't be done with this function.
I attached the code for a better understanding of what I want, any help in order to do this from a single menu (not selecting which one I want and which one doesn't individually), will be greatly appreciated, thank you very much in advance.
Code:
input MovingAverages = {default "1", "2", "3", "4", "5"};
input Price = close;
input ma1Length = 10;
input AverageTypeMa1 = AverageType.EXPONENTIAL;
input ma2Length = 20;
input AverageTypeMa2 = AverageType.EXPONENTIAL;
input ma3Length = 30;
input AverageTypeMa3 = AverageType.EXPONENTIAL;
input ma4Length = 40;
input AverageTypeMa4 = AverageType.EXPONENTIAL;
input ma5Length = 50;
input AverageTypeMa5 = AverageType.EXPONENTIAL;
input Value = 100;

def MA1 = MovingAverage(AverageTypeMa1, Price, ma1Length);
def MA2 = MovingAverage(AverageTypeMa2, Price, ma2Length);
def MA3 = MovingAverage(AverageTypeMa3, Price, ma3Length);
def MA4 = MovingAverage(AverageTypeMa4, Price, ma4Length);
def MA5 = MovingAverage(AverageTypeMa5, Price, ma5Length);

AddLabel(MovingAverages, MA1);

plot Label;
switch (MovingAverages){
case "1":
    Label = AddLabel(yes,MA1);
}
See if this helps you make what you want

Code:
input MovingAverages = 2;#Hint Movingaverages: Must be between 1 through 5
AddLabel(!Between(MovingAverages, 1, 5), "Movingaverages must be between 1 and 5", Color.YELLOW);

input Price = close;
input ma1Length = 10;
input AverageTypeMa1 = AverageType.EXPONENTIAL;
input ma2Length = 20;
input AverageTypeMa2 = AverageType.EXPONENTIAL;
input ma3Length = 30;
input AverageTypeMa3 = AverageType.EXPONENTIAL;
input ma4Length = 40;
input AverageTypeMa4 = AverageType.EXPONENTIAL;
input ma5Length = 50;
input AverageTypeMa5 = AverageType.EXPONENTIAL;
input Value = 100;

def MA1 = MovingAverage(AverageTypeMa1, Price, ma1Length);
def MA2 = MovingAverage(AverageTypeMa2, Price, ma2Length);
def MA3 = MovingAverage(AverageTypeMa3, Price, ma3Length);
def MA4 = MovingAverage(AverageTypeMa4, Price, ma4Length);
def MA5 = MovingAverage(AverageTypeMa5, Price, ma5Length);

AddLabel(Between(MovingAverages, 1, 5), ma1Length + ": " + AsText(MA1), Color.WHITE);
AddLabel(Between(MovingAverages, 2, 5), ma2Length + ": " + AsText(MA2), Color.WHITE);
AddLabel(Between(MovingAverages, 3, 5), ma3Length + ": " + AsText(MA3), Color.WHITE);
AddLabel(Between(MovingAverages, 4, 5), ma4Length + ": " + AsText(MA4), Color.WHITE);
AddLabel(Between(MovingAverages, 5, 5), ma5Length + ": " + AsText(MA5), Color.WHITE);
 

Similar threads

Top