Help for a loop

Created at 26 Mar 2016, 19:49
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!
BA

badger.cook

Joined 25.02.2016

Help for a loop
26 Mar 2016, 19:49


Hi everyone,

Can somebody help me to loop the last line of the code? I would like to chose, with a parameter, how many bars the indicator use to loop that line, and not having to do it by "hand". 

Thanks a lot for your help,

B. 

 

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
    public class CorrelationIndex : Indicator
    {
        [Output("Index curr 1", Color = Colors.Red)]
        public IndicatorDataSeries Curr1 { get; set; }

        [Output("Index curr 2", Color = Colors.Blue)]
        public IndicatorDataSeries Curr2 { get; set; }

        [Parameter(DefaultValue = 1)]
        public int Period { get; set; }

        [Parameter(DefaultValue = 60)]
        public int Delay { get; set; }

        [Parameter("Source EURUSD")]
        public DataSeries Source1 { get; set; }

        private PriceROC priceROCEURUSD;
        private IndicatorDataSeries value1;

        protected override void Initialize()
        {
            priceROCEURUSD = Indicators.PriceROC(Source1, Period);
            value1 = CreateDataSeries();
        }

        public override void Calculate(int index)
        {

            value1[index] = (priceROCEURUSD.Result[index]);

            Curr1[index] = (100 * (1 + value1[index]) * (1 + value1[index - 1]) * (1 + value1[index - 2]) * (1 + value1[index - 3]) * (1 + value1[index - 4]) * (1 + value1[index - 5]) * (1 + value1[index - 6]) * (1 + value1[index - 7]) * (1 + value1[index - 8]) * (1 + value1[index - 9]) * (1 + value1[index - 10]) * (1 + value1[index - 11]) * (1 + value1[index - 12]));

        }
    }
}

 


@badger.cook
Replies

Jiri
26 Mar 2016, 22:28

Hey, I haven't tested but it should work.

double result = 0;
for (int i = 0; i < Period; i++)
{
    if (i == 0)
    {
        result = (1 + value1[index]);
    }
    else
    }
        result *= (1 + value1[index - i])
    }
}

Curr1[index] = 100 * result;

 


@Jiri

Jiri
26 Mar 2016, 22:33

Short version.

double result = 100;
for (int i = 0; i < Period; i++)
{
    result *= (1 + value1[index - i])
}

Curr1[index] = result;

 


@Jiri

benjamintermonia
27 Mar 2016, 23:04

Thanks ! Didnt test it yet but I'll let you know. What if I want to set the index at 100 at a specific time (say 9am each day) and then use the loop on the bars between current time and 9am ? Thanks for you help, again !
@benjamintermonia