How to Add Choice for Source and TimeFrame on an indicator

Created at 07 Jun 2023, 21:38
JA

jacopotrono

Joined 17.06.2022

How to Add Choice for Source and TimeFrame on an indicator
07 Jun 2023, 21:38


Hi everyone,

what is the best way to add choice of Symbol and TimeFrame on a script?

Example: RSI on EURUSD standard + RSI on EURUSD about EURCHF for same TimeFrame

 

Thanks in advance


@jacopotrono
Replies

firemyst
08 Jun 2023, 03:16

RE:

jacopotrono said:

Hi everyone,

what is the best way to add choice of Symbol and TimeFrame on a script?

Example: RSI on EURUSD standard + RSI on EURUSD about EURCHF for same TimeFrame

 

Thanks in advance

For different symbols, I do a simple comma-separated text input. In the code, I just split the inputs at a comma, remove any white space, and then get the bars for each symbol I want.

To allow a user to select a different time frame than what's on the chart, just create a TimeFrame parameter:

[Parameter("Source Time Frame")]
public TimeFrame SourceTimeFrame { get; set; }

and use that parameter when getting the bars.

//Example
private Bars _marketSeries;

if (SourceTimeFrame != null)
    _marketSeries = MarketData.GetBars(SourceTimeFrame, Symbol.Name);
else
    _marketSeries = MarketData.GetBars(Bars.TimeFrame, Symbol.Name);

 


@firemyst

jacopotrono
08 Jun 2023, 22:33

RE: RE:

Thank you firemist for your time and answer.

Could you show me an example applied to this code? 

 

using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = false)]
    public class RSIIndicator : Indicator
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("RSI Periods", DefaultValue = 14, MinValue = 1)]
        public int Periods { get; set; }

        [Output("RSI", Color = Colors.Green)]
        public IndicatorDataSeries RSI { get; set; }

        public override void Calculate(int index)
        {
            double avgUp = 0;
            double avgDown = 0;

            for (int i = index - Periods + 1; i <= index; i++)
            {
                double diff = Source[i] - Source[i - 1];

                if (diff > 0)
                    avgUp += diff;
                else
                    avgDown += diff;
            }

            avgUp /= Periods;
            avgDown /= Periods;

            double rsi = 100 - (100 / (1 + (avgUp / -avgDown)));

            RSI[index] = rsi;
        }
    }
}

@jacopotrono