Category Other  Published on 18/12/2020

CHF Weighted Currency Index


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


using System.Collections.Generic;



namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class JPYXWeighedCurrencyIndex : Indicator
    {

        [Parameter("USD Weight", DefaultValue = 20.82)]
        public double usdWeight { get; set; }

        [Parameter("EUR Weight", DefaultValue = 62.33)]
        public double eurWeight { get; set; }

        [Parameter("GBP Weight", DefaultValue = 9.84)]
        public double gbpWeight { get; set; }

        [Parameter("AUD Weight", DefaultValue = 1.28)]
        public double audWeight { get; set; }

        [Parameter("JPY Weight", DefaultValue = 3.99)]
        public double jpyWeight { get; set; }

        [Parameter("CAD Weight", DefaultValue = 1.71)]
        public double cadWeight { get; set; }

        [Parameter("Line TimeFrame", DefaultValue = 0)]
        public TimeFrame TF { get; set; }


        [Parameter("MA Period", DefaultValue = 20)]
        public int Period { get; set; }


        [Output("Average", LineColor = "White")]

        public IndicatorDataSeries Result2 { get; set; }


        [Output("CHF Index", LineColor = "Pink")]

        public IndicatorDataSeries Result { get; set; }


        // eur, gbp, cad, aud, usd, jpy 


        public Bars Eurchf;
        public Bars Gbpchf;
        public Bars Cadchf;
        public Bars Audchf;
        public Bars Usdchf;
        public Bars Chfjpy;



        public Bars[] eur_Pairs = new Bars[6];

        public string[] eur_Symbols = new string[6];

        public double[] weights = new double[6];


        public double[] f_pairs = new double[6];

        public double[] r_pairs = new double[6];

        public double[] l_weight = new double[6];


        public double f_Audchf, f_Usdchf, f_Cadchf, f_Chfjpy, f_Gbpchf, f_Eurchf;

        public double r_Audchf, r_Usdchf, r_Cadchf, r_Chfjpy, r_Gbpchf, r_Eurchf;


        private IndicatorDataSeries averagebff;

        private IndicatorDataSeries temp;

        private MovingAverage average;

        protected override void Initialize()
        {

            // Reference Exhchange Rates from close prices of 02/01/2020

            //-------------------------------------------------------------------------------------------------------------------------------------------------------------
            temp = CreateDataSeries();

            averagebff = CreateDataSeries();

            average = Indicators.MovingAverage(averagebff, Period, MovingAverageType.Exponential);


            // eur, gbp, cad, aud, usd, jpy

            f_Eurchf = 1.08506;
            f_Gbpchf = 1.27555;
            f_Cadchf = 0.74779;
            f_Audchf = 0.6788;
            f_Usdchf = 0.97115;
            f_Chfjpy = 1 / 111.77;


            //-------------------------------------------------------------------------------------------------------------------------------------------------------------


            f_pairs[0] = f_Eurchf;
            f_pairs[1] = f_Gbpchf;
            f_pairs[2] = f_Cadchf;
            f_pairs[3] = f_Audchf;
            f_pairs[4] = f_Usdchf;
            f_pairs[5] = f_Chfjpy;

            // eur, gbp, cad, aud, usd, jpy

            r_pairs[0] = r_Eurchf;
            r_pairs[1] = r_Gbpchf;
            r_pairs[2] = r_Cadchf;
            r_pairs[3] = r_Audchf;
            r_pairs[4] = r_Usdchf;
            r_pairs[5] = r_Chfjpy;

            eur_Pairs[0] = Eurchf;
            eur_Pairs[1] = Gbpchf;
            eur_Pairs[2] = Cadchf;
            eur_Pairs[3] = Audchf;
            eur_Pairs[4] = Usdchf;
            eur_Pairs[5] = Chfjpy;

            l_weight[0] = eurWeight;
            l_weight[1] = gbpWeight;
            l_weight[2] = cadWeight;
            l_weight[3] = audWeight;
            l_weight[4] = usdWeight;
            l_weight[5] = cadWeight;

            eur_Symbols[0] = "EURCHF";
            eur_Symbols[1] = "GBPCHF";
            eur_Symbols[2] = "CADCHF";
            eur_Symbols[3] = "AUDCHF";
            eur_Symbols[4] = "USDCHF";
            eur_Symbols[5] = "CHFJPY";



            for (int i = 0; i < eur_Pairs.Length; i++)
            {
                eur_Pairs[i] = MarketData.GetBars(TF, eur_Symbols[i]);
            }

        }

        public override void Calculate(int index)
        {
            // Setting the reference quote

            //var index2 = GetIndexByTF(BarSeries[i], this.Bars, index);

            double sum = 0;


            for (int i = 0; i < eur_Pairs.Length; i++)
            {
                var index2 = GetIndexByTF(eur_Pairs[i], this.Bars, index);

                if (i < 4)
                {
                    r_pairs[i] = ((1 / eur_Pairs[i].ClosePrices[index2]) / f_pairs[i]) * l_weight[i];

                    sum += r_pairs[i];
                }

                else
                {
                    r_pairs[i] = (eur_Pairs[i].ClosePrices[index2] / f_pairs[i]) * l_weight[i];

                    sum += r_pairs[i];
                }
            }
            averagebff[index] = sum;

            Result[index] = sum;

            if (double.IsNaN(average.Result[index]) == false)
            {
                Result2[index] = average.Result[index];
            }


        }

        int GetIndexByTF(Bars B1, Bars B2, int index)
        {
            var index2 = B1.OpenTimes.GetIndexByTime(B2.OpenTimes[index]);
            return index2;
        }

    }
}


AN
andurei

Joined on 30.09.2020

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: CHF Currency Index.algo
  • Rating: 0
  • Installs: 1222
  • Modified: 13/10/2021 09:55
Comments
Log in to add a comment.
No comments found.