GBP Custom Weighted Index free

Description

A Picture speaks more than a thousand words

Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
´╗┐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 GBPWeighedCurrencyIndex : Indicator
    {

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

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

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

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

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

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

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


        [Output("GBP Index", LineColor = "Red")]

        public IndicatorDataSeries Result { get; set; }



        public Bars Eurgbp;
        public Bars Gbpusd;
        public Bars Gbpaud;
        public Bars Gbpcad;
        public Bars Gbpjpy;
        public Bars Gbpchf;


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

        public string[] usd_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_Eurgbp, f_gbpusd, f_Gbpaud, f_Gbpjpy, f_Gbpcad, f_Gbpchf;

        public double r_Eurgbp, r_gbpusd, r_Gbpaud, r_Gbpjpy, r_Gbpcad, r_Gbpchf;

        protected override void Initialize()
        {

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

            f_Eurgbp = 1 / 0.85049;
            f_gbpusd = 1.31353;
            f_Gbpaud = 1.87927;
            f_Gbpcad = 1.7059;
            f_Gbpjpy = 142.605;
            f_Gbpchf = 1.27555;
            //-------------------------------------------------------------------------------------------------------------------------------------------------------------

            f_pairs[0] = f_Eurgbp;
            f_pairs[1] = f_gbpusd;
            f_pairs[2] = f_Gbpaud;
            f_pairs[3] = f_Gbpcad;
            f_pairs[4] = f_Gbpjpy;
            f_pairs[5] = f_Gbpchf;

            r_pairs[0] = r_Eurgbp;
            r_pairs[1] = r_gbpusd;
            r_pairs[2] = r_Gbpaud;
            r_pairs[3] = r_Gbpcad;
            r_pairs[4] = r_Gbpjpy;
            r_pairs[5] = r_Gbpchf;

            usd_Pairs[0] = Eurgbp;
            usd_Pairs[1] = Gbpusd;
            usd_Pairs[2] = Gbpaud;
            usd_Pairs[3] = Gbpcad;
            usd_Pairs[4] = Gbpjpy;
            usd_Pairs[5] = Gbpchf;

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

            usd_symbols[0] = "EURGBP";
            usd_symbols[1] = "GBPUSD";
            usd_symbols[2] = "GBPAUD";
            usd_symbols[3] = "GBPCAD";
            usd_symbols[4] = "GBPJPY";
            usd_symbols[5] = "GBPCHF";



            for (int i = 0; i < usd_Pairs.Length; i++)
            {
                usd_Pairs[i] = MarketData.GetBars(TF, usd_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 < usd_Pairs.Length; i++)
            {
                var index2 = GetIndexByTF(usd_Pairs[i], this.Bars, index);

                if (i == 0)
                {

                    r_pairs[i] = ((1 / usd_Pairs[i].ClosePrices[index2]) / f_pairs[i]) * l_weight[i];

                    sum += r_pairs[i];
                }

                else
                {

                    r_pairs[i] = (usd_Pairs[i].ClosePrices[index2] / f_pairs[i]) * l_weight[i];

                    sum += r_pairs[i];
                }


            }

            Result[index] = sum;

        }


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

    }
}
Comments
0