Category Other  Published on 18/12/2020

JPY 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 = 37.97)]
        public double usdWeight { get; set; }

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

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

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

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

        [Parameter("CHF Weight", DefaultValue = 14.68)]
        public double chfWeight { get; set; }

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


        [Output("JPY ", LineColor = "Pink")]

        public IndicatorDataSeries Result { get; set; }

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


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

        public IndicatorDataSeries Result2 { get; set; }


        // gbp, aud, usd, cad, eur, chf 

        public Bars Gbpjpy;
        public Bars Audjpy;
        public Bars Usdjpy;
        public Bars Cadjpy;
        public Bars Eurjpy;
        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_Audjpy, f_Usdjpy, f_Gbpjpy, f_Eurjpy, f_Cadjpy, f_Chfjpy;

        public double r_Audjpy, r_Usdjpy, r_Gbpjpy, r_Eurjpy, r_Cadjpy, r_Chfjpy;
        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);


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

            // gbp, aud, usd, cad, eur, chf 

            f_Gbpjpy = 1 / 142.581;
            f_Audjpy = 1 / 75.879;
            f_Usdjpy = 1 / 108.566;
            f_Cadjpy = 1 / 83.614;
            f_Eurjpy = 1 / 121.3;
            f_Chfjpy = 1 / 111.726;


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

            f_pairs[0] = f_Gbpjpy;
            f_pairs[1] = f_Audjpy;
            f_pairs[2] = f_Usdjpy;
            f_pairs[3] = f_Cadjpy;
            f_pairs[4] = f_Eurjpy;
            f_pairs[5] = f_Chfjpy;

            // gbp, aud, usd, cad, eur, chf 

            r_pairs[0] = r_Gbpjpy;
            r_pairs[1] = r_Audjpy;
            r_pairs[2] = r_Usdjpy;
            r_pairs[3] = r_Cadjpy;
            r_pairs[4] = r_Eurjpy;
            r_pairs[5] = r_Chfjpy;

            eur_Pairs[0] = Gbpjpy;
            eur_Pairs[1] = Audjpy;
            eur_Pairs[2] = Usdjpy;
            eur_Pairs[3] = Cadjpy;
            eur_Pairs[4] = Eurjpy;
            eur_Pairs[5] = Chfjpy;

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

            eur_Symbols[0] = "GBPJPY";
            eur_Symbols[1] = "AUDJPY";
            eur_Symbols[2] = "USDJPY";
            eur_Symbols[3] = "CADJPY";
            eur_Symbols[4] = "EURJPY";
            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 < eur_Pairs.Length)
                {
                    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: JPYX Weighed Currency Index .algo
  • Rating: 0
  • Installs: 1355
  • Modified: 13/10/2021 09:55
Comments
Log in to add a comment.
No comments found.