EURUSD Index V2

by lec0456 in category Trend at 25/04/2015
Description

Determines the daily increase or decrease in the value of the Euro and Dollar.The actual and projected value of the pair is also plotted. This version of the EURUSD index allows for both daily and weekly yield indexes.  It gives you the option to weight the index based on volume and the option to use 4 or 8 pairs for calculation. Finally, it corrects a problem with pairs that don't start trading at the same time.

Download
2200 downloads
How to install
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.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false, AutoRescale = true, ScalePrecision = 2, TimeZone = TimeZones.UTC)]
    [Levels( -2, -1.5, -1, -.5, 0, .5, 1, 1.5, 2)]
    public class myIndexEURUSD : Indicator
    {
        [Parameter(DefaultValue = true)]
        public bool HideYeilds { get; set; }
        [Parameter(DefaultValue = false)]
        public bool Weighted { get; set; }
        [Parameter(DefaultValue = true)]
        public bool AllPairs { get; set; }
        [Parameter(DefaultValue = true)]
        public bool DailyMeasure { get; set; }

        [Output("USD Index", Color = Colors.Green)]
        public IndicatorDataSeries USDIDX { get; set; }
        [Output("USD Points", Color = Colors.LightGreen, Thickness = 2, PlotType = PlotType.Points)]
        public IndicatorDataSeries USDIDXPts { get; set; }
        [Output("EUR Index", Color = Colors.Blue)]
        public IndicatorDataSeries EURIDX { get; set; }
        [Output("EUR Points", Color = Colors.LightBlue, Thickness = 2, PlotType = PlotType.Points)]
        public IndicatorDataSeries EURIDXPts { get; set; }
        [Output("ProjYld", Color = Colors.Red)]
        public IndicatorDataSeries ProjYld { get; set; }
        [Output("ProjYld Points", Color = Colors.Pink, Thickness = 2, PlotType = PlotType.Points)]
        public IndicatorDataSeries ProjYldPts { get; set; }
        [Output("EURUSD", Color = Colors.Yellow)]
        public IndicatorDataSeries YldEURUSD { get; set; }
        [Output("EURUSD Points", Color = Colors.LightYellow, Thickness = 2, PlotType = PlotType.Points)]
        public IndicatorDataSeries YldEURUSDPts { get; set; }

        [Output("Delta", PlotType = PlotType.Histogram, Color = Colors.Purple)]
        public IndicatorDataSeries Delta { get; set; }
       
        [Output("USDJPY", Color = Colors.LightGreen)]
        public IndicatorDataSeries YldUSDJPY { get; set; }
        [Output("GBPUSD", Color = Colors.LightGreen)]
        public IndicatorDataSeries YldGBPUSD { get; set; }
        [Output("AUDUSD", Color = Colors.LightGreen)]
        public IndicatorDataSeries YldAUDUSD { get; set; }
        [Output("USDCHF", Color = Colors.LightGreen)]
        public IndicatorDataSeries YldUSDCHF { get; set; }
        [Output("NZDUSD", Color = Colors.LightGreen)]
        public IndicatorDataSeries YldNZDUSD { get; set; }
        [Output("USDCAD", Color = Colors.LightGreen)]
        public IndicatorDataSeries YldUSDCAD { get; set; }
        [Output("USDNOK", Color = Colors.LightGreen)]
        public IndicatorDataSeries YldUSDNOK { get; set; }
        [Output("USDSEK", Color = Colors.LightGreen)]
        public IndicatorDataSeries YldUSDSEK { get; set; }

        [Output("EURJPY", Color = Colors.LightBlue)]
        public IndicatorDataSeries YldEURJPY { get; set; }
        [Output("EURGBP", Color = Colors.LightBlue)]
        public IndicatorDataSeries YldEURGBP { get; set; }
        [Output("EURAUD", Color = Colors.LightBlue)]
        public IndicatorDataSeries YldEURAUD { get; set; }
        [Output("EURCHF", Color = Colors.LightBlue)]
        public IndicatorDataSeries YldEURCHF { get; set; }
        [Output("EURNZD", Color = Colors.LightBlue)]
        public IndicatorDataSeries YldEURNZD { get; set; }
        [Output("EURCAD", Color = Colors.LightBlue)]
        public IndicatorDataSeries YldEURCAD { get; set; }
        [Output("EURNOK", Color = Colors.LightBlue)]
        public IndicatorDataSeries YldEURNOK { get; set; }
        [Output("EURSEK", Color = Colors.LightBlue)]
        public IndicatorDataSeries YldEURSEK { get; set; }
        
        [Output("Center", LineStyle = LineStyle.DotsRare, Color = Colors.White)]
        public IndicatorDataSeries CenterLine { get; set; }
        [Output("UpperLimit", PlotType = PlotType.Line, LineStyle = LineStyle.DotsRare, Thickness = 1, Color = Colors.Red)]
        public IndicatorDataSeries UpperLimit { get; set; }
        [Output("LowerLimit", PlotType = PlotType.Line, LineStyle = LineStyle.DotsRare, Thickness = 1, Color = Colors.Red)]
        public IndicatorDataSeries LowerLimit { get; set; }
        
        private MarketSeries[] prdMS =new MarketSeries[17];
        private MarketSeries[] dlyMS =new MarketSeries[17];
        private MarketSeries[] wlyMS =new MarketSeries[17];
           
        protected override void Initialize()
        {
            string IndicatorName = this.ToString();//  returns ClassName
            Print("{0,20}{1,18}{2,15}{3,15}","Indicator","TimeZone","Offset","DST");
            Print("{0,0}{1,10}{2,20}{3,15}",IndicatorName,TimeZone,TimeZone.BaseUtcOffset,TimeZone.SupportsDaylightSavingTime);
       
            string[] Pairs=
            {"EURUSD",
             "USDJPY","GBPUSD","AUDUSD","USDCHF", 
             "NZDUSD","USDCAD","USDNOK","USDSEK",
             "EURJPY","EURGBP","EURAUD","EURCHF",
             "EURNZD","EURCAD","EURNOK","EURSEK"};
             
//Get period, daily and weekly series' for each currency pair
            for (int i=0;i<Pairs.Length; i++)
            {
                prdMS[i]=MarketData.GetSeries(Pairs[i],TimeFrame);
                dlyMS[i]=MarketData.GetSeries(Pairs[i],TimeFrame.Daily);
                wlyMS[i]=MarketData.GetSeries(Pairs[i],TimeFrame.Weekly);
            }            
        }

        public override void Calculate(int index)
        {
            if (index<1)return;

            int[] prdIDX = new int[17];
            int[] dlyIDX = new int[17];
            int[] wlyIDX = new int[17];
            int[] PairSigns={1,1,-1,-1,1,-1,1,1,1,1,1,1,1,1,1,1,1};
//Get the period, daily and weekly indexes for each seriies account for timeZone where daily candles are calculated. This may vary depending on broker.                       
            for (int i=0;i<prdMS.Length; i++)
            {
                prdIDX[i]=prdMS[i].OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]);
                dlyIDX[i]=dlyMS[i].OpenTime.GetIndexByTime(MarketSeries.OpenTime[index].AddHours(3).Date);
                wlyIDX[i]=wlyMS[i].OpenTime.GetIndexByTime(MarketSeries.OpenTime[index].AddHours(3).Date);
            }
//Calculate the daily or weekly yields for each pair            
            int DateDifference;
            double[] yld = new double[17];
            for (int i=0;i < prdMS.Length; i++)
            {
                DateDifference = (int)(dlyMS[i].OpenTime[dlyIDX[i]].AddHours(3).Date - prdMS[i].OpenTime[prdIDX[i]].AddHours(3).Date).TotalDays;
                if (DailyMeasure)
                {
                    if(DateDifference>0)yld[i]=0;//correct yeilds when no period candle is produced dafter a change in day; prevents using yesterday's candle to calculate the yeild for the current week.
                    else yld[i]=((prdMS[i].Close[prdIDX[i]]-dlyMS[i].Open[dlyIDX[i]])/dlyMS[i].Open[dlyIDX[i]])*100*PairSigns[i];
                }
                else
                {                
                    if(DateDifference>1)yld[i]=0;//correct yeilds when no period candle is produced dafter a change in week; prevents using last week's candle to calculate the yeild for the current week.
                    else yld[i]=((prdMS[i].Close[prdIDX[i]]-wlyMS[i].Open[wlyIDX[i]])/wlyMS[i].Open[wlyIDX[i]])*100*PairSigns[i];
                }
            }

                YldEURUSD[index] = yld[0];
            
            if (!HideYeilds)
            {
                YldUSDJPY[index] = yld[1];
                YldGBPUSD[index] = yld[2];
                YldAUDUSD[index] = yld[3];
                YldUSDCHF[index] = yld[4];
                YldNZDUSD[index] = yld[5];
                YldUSDCAD[index] = yld[6];
                YldUSDNOK[index] = yld[7];
                YldUSDSEK[index] = yld[8];
                
                YldEURJPY[index] = yld[9];
                YldEURGBP[index] = yld[10];
                YldEURAUD[index] = yld[11];
                YldEURCHF[index] = yld[12];
                YldEURNZD[index] = yld[13];
                YldEURCAD[index] = yld[14];
                YldEURNOK[index] = yld[15];
                YldEURSEK[index] = yld[16];
            }
//calculate the Eur and USd indexes based on equal wieghting or volume weighting
            double totUSDvol=0;
            double totEURvol=0;
            double usdidx0=0;
            double euridx0=0;
            double usdpairsum=0;
            double eurpairsum=0;

            if (AllPairs)
            {
                if (Weighted)
                {
                    for(int i=1;i<9;i++)totUSDvol += prdMS[i].TickVolume[prdIDX[i]];
                    for(int i=9;i<17;i++)totEURvol += prdMS[i].TickVolume[prdIDX[i]];
                    for(int i=1;i<9;i++)usdidx0 += (yld[i]*(prdMS[i].TickVolume[prdIDX[i]]/totUSDvol));
                    for(int i=9;i<17;i++)euridx0 += (yld[i]*(prdMS[i].TickVolume[prdIDX[i]]/totEURvol));
                } 
                else
                {
                    for(int i=1;i<9;i++)usdpairsum += yld[i];
                    usdidx0=usdpairsum/8;
                    for(int i=9;i<17;i++)eurpairsum += yld[i];
                    euridx0=eurpairsum/8;
                }
            }
            else
            {
                if (Weighted)
                { 
                    for(int i=1;i<5;i++)totUSDvol += prdMS[i].TickVolume[prdIDX[i]];
                    for(int i=9;i<13;i++)totEURvol += prdMS[i].TickVolume[prdIDX[i]];
                    for(int i=1;i<5;i++)usdidx0 += (yld[i]*(prdMS[i].TickVolume[prdIDX[i]]/totUSDvol));
                    for(int i=9;i<13;i++)euridx0 += (yld[i]*(prdMS[i].TickVolume[prdIDX[i]]/totEURvol));
                } 
                else
                { 
                    for(int i=1;i<5;i++)usdpairsum += yld[i];
                    usdidx0=usdpairsum/4;
                    for(int i=9;i<13;i++)eurpairsum += yld[i];
                    euridx0=eurpairsum/4;
                }
            }
            
            USDIDX[index] = usdidx0;
            EURIDX[index] = euridx0;
//Calculate the projected yeid based on the indexes
            ProjYld[index] = euridx0 - usdidx0;
//Calculate the change in yield            
            Delta[index]= ProjYld[index]-ProjYld[index-1];
            
            USDIDXPts[index] = USDIDX[index];
            EURIDXPts[index] = EURIDX[index];            
            YldEURUSDPts[index] = YldEURUSD[index];
            ProjYldPts[index]= ProjYld[index];
            
            CenterLine[index] = 0;
            LowerLimit[index]=-.5;
            UpperLimit[index]=.5;
            
            string USDTxt = string.Format("USD {0}", Math.Round(USDIDX[index], 2));
            string EURTxt = string.Format("EUR {0}", Math.Round(EURIDX[index], 2));
            string ACTTxt = string.Format("Act {0}", Math.Round(YldEURUSD[index], 2));
            string PROJTxt = string.Format("Pro {0}", Math.Round(ProjYld[index], 2));
            ChartObjects.DrawText("USDlabel", USDTxt, index, USDIDX[index], VerticalAlignment.Center, HorizontalAlignment.Right, Colors.Green);
            ChartObjects.DrawText("EURlabel", EURTxt, index, EURIDX[index], VerticalAlignment.Center, HorizontalAlignment.Right, Colors.Blue);
            ChartObjects.DrawText("ACTlabel", ACTTxt, index, YldEURUSD[index], VerticalAlignment.Top, HorizontalAlignment.Right, Colors.Yellow);
            ChartObjects.DrawText("PROJlabel", PROJTxt, index, ProjYld[index], VerticalAlignment.Bottom, HorizontalAlignment.Right, Colors.Red);
        }
    }
}
Comments
5