LuisLizarazoFX

Info
Username: | LuisLizarazoFX |
Name: | LuisLizarazoFX |
Member since: | 13 Sep 2022 |
About
Signature
Last Forum Posts
@The indicator does not work in Renko Charts, can someone modify and improve the code?... thanks: 13 Sep 2022, 14:09
using System;
using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AutoRescale = true, AccessRights = AccessRights.None)]
public class USDXDollarIndex : Indicator
{
[Parameter("Time Frame", DefaultValue = "m1")]
public TimeFrame Frame { get; set; }
[Parameter("Show USDX", DefaultValue = true)]
public bool ShowUSDX { get; set; }
[Output("USDX", LineColor = "Blue", Thickness = 1, PlotType = PlotType.Line, LineStyle = LineStyle.Solid)]
public IndicatorDataSeries USDX { get; set; }
[Parameter(DefaultValue = 10)]
public int PeriodsUSDX { get; set; }
[Output("MA", LineColor = "Yellow", PlotType = PlotType.Line, Thickness = 2)]
public IndicatorDataSeries MA { get; set; }
private Index _usdxIndex;
private MovingAverage USDXMA;
protected override void Initialize()
{
USDXMA = Indicators.MovingAverage(USDX, PeriodsUSDX, MovingAverageType.Simple);
_usdxIndex = new Index
{
Name = "USDX",
Multiplier = 50.14348112,
Constituents = new List<Constituent>
{
//wieght is negative when USD is not the base currency (EURUSD and GBPUSD)
new Constituent("EURUSD", -0.576),
new Constituent("USDJPY", 0.136),
new Constituent("GBPUSD", -0.119),
new Constituent("USDCAD", 0.091),
new Constituent("USDSEK", 0.042),
new Constituent("USDCHF", 0.036)
}
};
}
public override void Calculate(int index)
{
var date = MarketSeries.OpenTime[index];
if (ShowUSDX)
{
USDX[index] = CalculateIndex(_usdxIndex, date);
}
MA[index] = USDXMA.Result[index];
}
private double CalculateIndex(Index index, DateTime date)
{
//index is calculated as a weighted geometric mean of its constituents' close prices
double result = index.Multiplier;
foreach (var weight in index.Constituents)
{
var series = MarketData.GetSeries(weight.Symbol, TimeFrame);
if (series == null)
{
return double.NaN;
}
double close = GetCloseByDate(date, series);
result *= Math.Pow(close, weight.Weight);
}
return result;
}
private double GetCloseByDate(DateTime date, MarketSeries series)
{
var idx = series.OpenTime.GetIndexByExactTime(date);
if (idx == -1)
{
return double.NaN;
}
return series.Close[idx];
}
}
public class Index
{
public string Name { get; set; }
/// <summary>
/// Constant multiplier as defined in ICE contract spec
/// </summary>
public double Multiplier { get; set; }
/// <summary>
/// List of index constituents
/// </summary>
public List<Constituent> Constituents { get; set; }
}
public class Constituent
{
public Constituent(string symbol, double cx)
{
Symbol = symbol;
Weight = cx;
}
public string Symbol { get; private set; }
/// <summary>
/// Constituent Weight
/// </summary>
public double Weight { get; private set; }
}
}