Subtracting the value of a series to another one

Created at 08 Jan 2022, 20:11
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
TR

triccomane

Joined 07.04.2021

Subtracting the value of a series to another one
08 Jan 2022, 20:11


Hi, I'm trying to subtract a Series from another series. In my case I'm trying to get RSI(dowjones - nasdaq, period).

I know I cannot just subtract a series from another series, can anyone suggest how to do that.

I think that to do that, I need to subtract each element of a series from each element of the other series. But I cannot find anything here that explains how to do that.

Thanks to whoever can help me with that.

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class nasdaqdowjonesspread : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

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

        private RelativeStrengthIndex nasdaq;
        private RelativeStrengthIndex dowjones;

        protected override void OnStart()
        {
            var nasdaqSeries = MarketData.GetSeries("US TECH 100", TimeFrame);
            var dowjonesSeries = MarketData.GetSeries("US 30", TimeFrame);
            var nasdow = dowjonesSeries - nasdaqSeries;

            diffRSI = Indicators.RelativeStrengthIndex(nasdow.Close, Periods);


            dow = Symbol.GetSymbol("US 30");
            nas = Symbol.GetSymbol("US TECH 100");
            downas = dow - nas;
        }

 


@triccomane
Replies

triccomane
09 Jan 2022, 13:19

RE:

I made some progress. I succeeded in getting the two symbols on two different arrays, but I still cannot find a way to subtract each element of the first array to the second one

            for (int i = 0; i < Periods; i++)
            {
                double close_nasdaq = (MarketData.GetSeries(MarketData.GetSymbol("US TECH 100"), TimeFrame.Daily).Close.Last(i));
                Print("nasdaq close price: ", i, " ", close_nasdaq);
                double close_dowjones = (MarketData.GetSeries(MarketData.GetSymbol("US 30"), TimeFrame.Daily).Close.Last(i));
                Print("dowjones close price: ", i, " ", close_dowjones);

                double downas;
                downas[i] = close_dowjones[i] - close_nasdaq[i]; //I get an erorr on this line 

 


@triccomane

firemyst
09 Jan 2022, 15:38

RE: RE:

triccomane said:

I made some progress. I succeeded in getting the two symbols on two different arrays, but I still cannot find a way to subtract each element of the first array to the second one

            for (int i = 0; i < Periods; i++)
            {
                double close_nasdaq = (MarketData.GetSeries(MarketData.GetSymbol("US TECH 100"), TimeFrame.Daily).Close.Last(i));
                Print("nasdaq close price: ", i, " ", close_nasdaq);
                double close_dowjones = (MarketData.GetSeries(MarketData.GetSymbol("US 30"), TimeFrame.Daily).Close.Last(i));
                Print("dowjones close price: ", i, " ", close_dowjones);

                double downas;
                downas[i] = close_dowjones[i] - close_nasdaq[i]; //I get an erorr on this line 

 

You're receiving an error on that line because you're treating a variable declared as a "double" as an array.

This is what you need to change that line to:

downas = close_dowjones - close_nasdaq;

which will save the single value.

If you want to keep an array of all the values, then your code needs some work. Here's a rough draft as I'm not in front of an editor:

double[] close_nasdaq = new double[Periods];
double[] close_dowjones = new double[Periods];
double[] downas = new double[Periods];

 for (int i = 0; i < Periods; i++)
            {
close_nasdaq[i] = (MarketData.GetSeries(MarketData.GetSymbol("US TECH 100"), TimeFrame.Daily).Close.Last(i));
                Print("nasdaq close price: ", i, " ", close_nasdaq[i]);
                close_dowjones[i] = (MarketData.GetSeries(MarketData.GetSymbol("US 30"), TimeFrame.Daily).Close.Last(i));
                Print("dowjones close price: ", i, " ", close_dowjones[i]);

                downas[i] = close_dowjones[i] - close_nasdaq[i];

 


@firemyst

amusleh
10 Jan 2022, 08:35

Hi,

Try this:

using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

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

        private RelativeStrengthIndex diffRSI;

        private IndicatorDataSeries nasdow;

        private Bars _nasdaqSeries, _dowjonesSeries;

        protected override void OnStart()
        {
            _nasdaqSeries = MarketData.GetBars(TimeFrame, "US TECH 100");
            _dowjonesSeries = MarketData.GetBars(TimeFrame, "US 30");

            nasdow = CreateDataSeries();

            diffRSI = Indicators.RelativeStrengthIndex(nasdow, Periods);
        }

        protected override void OnTick()
        {
            var index = Bars.Count - 1;

            var dowjonesSeriesIndex = _dowjonesSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
            var nasdaqSeriesIndex = _nasdaqSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);

            nasdow[index] = _dowjonesSeries.ClosePrices[dowjonesSeriesIndex] - _nasdaqSeries.ClosePrices[nasdaqSeriesIndex];
        }
    }
}

The "nasdow" series contains the subtraction result of two other series, and you can use it on any indicators.


@amusleh

triccomane
11 Jan 2022, 11:35

RE:

amusleh said:

Hi,

Try this:

using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

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

        private RelativeStrengthIndex diffRSI;

        private IndicatorDataSeries nasdow;

        private Bars _nasdaqSeries, _dowjonesSeries;

        protected override void OnStart()
        {
            _nasdaqSeries = MarketData.GetBars(TimeFrame, "US TECH 100");
            _dowjonesSeries = MarketData.GetBars(TimeFrame, "US 30");

            nasdow = CreateDataSeries();

            diffRSI = Indicators.RelativeStrengthIndex(nasdow, Periods);
        }

        protected override void OnTick()
        {
            var index = Bars.Count - 1;

            var dowjonesSeriesIndex = _dowjonesSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
            var nasdaqSeriesIndex = _nasdaqSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);

            nasdow[index] = _dowjonesSeries.ClosePrices[dowjonesSeriesIndex] - _nasdaqSeries.ClosePrices[nasdaqSeriesIndex];
        }
    }
}

The "nasdow" series contains the subtraction result of two other series, and you can use it on any indicators.

Thanks a lot to the both of you. You were very helpful.


@triccomane