Description
At FXTS, we've been working on a selection of adaptive moving averages over the last week to be released in succession.
These can be used for crossovers, or for trailing stoplosses in cBots (or preferred use).
We've also set up a new Telegram group for support of our indicators and robots, and where we aim to discuss quantative strategies for a large range of assets: t.me/quantFXAlgos
Shown here are:
- ROC-12 Adaptive MA in Lime
- 2-Period EMA in Cyan
- 30-Period EMA in Purple
Idea:
Alpha (the adaptive parameter) is calculated from the Absolute value of Price ROC, as this is a measure of current momentum.
The ROC MA is then a weighted average of the fast MA, the slowMA and it's previous result (depending on the parameters options below).
Parameters:
- Fast MA Period: The period of the Fast MA that the ROC MA will follow
- Slow MA Period: The period of the Slow MA that the ROC MA will follow
- MA Type: The type of fast and slow MA to follow.
- MA To Follow: When alpha is large (in this case, when absolute momentum is large) this will be the MA that the ROC MA will follow closest.
- ROC Period: The period to calculate the Price ROC over.
- ROC Scale Type: Max, or Odds. Max scales alpha into the [0,1] range by using the input from ROC Max (below). Odds scales alpha using alpha = alpha / (alpha + 1).
- ROC Max: The Maximum ROC you expect in the period, any values large than this are scaled so that alpha = 1.
- ROC-MA Balance Type: Alpha, InvAlpha, Manual. This controls the smoothing between new values and previous values. For manual, see below. If Alpha is selected, then when alpha is large new results will be favoured more heavily. If InvAlpha is selected, then when Alpha is large, the previous value is weighted more heavily (a smoother MA).
- ROC-MA Balance: If Manual is selected for the balance type, then this is the smoothing between the new result and old result. Values close to 1 favour the new result and give a less-smooth MA.
Any questions, then you can comment below, or join the new Telegram group: t.me/quantFXAlgos
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class ROCMA : Indicator
{
[Parameter("Fast MA Period", Group = "MA Settings", DefaultValue = 2)]
public int FastPeriod { get; set; }
[Parameter("Slow MA Period", Group = "MA Settings", DefaultValue = 30)]
public int SlowPeriod { get; set; }
[Parameter("MAType", Group = "MA Settings", DefaultValue = MovingAverageType.Exponential)]
public MovingAverageType MAType { get; set; }
[Parameter("MA To Follow", Group = "MA Settings", DefaultValue = Adherence.Fast)]
public Adherence MAtoFollow { get; set; }
public enum Adherence
{
Fast,
Slow
}
public enum ScaleType
{
Max,
Odds
}
public enum SmoothType
{
Alpha,
InvAlpha,
Manual
}
[Parameter("ROC Period", Group = "Price ROC Settings", DefaultValue = 14)]
public int ROCPeriod { get; set; }
[Parameter("ROC Scale Type", Group = "Adaptive Settings", DefaultValue = ScaleType.Max)]
public ScaleType ScaleSettings { get; set; }
[Parameter("ROC Max", Group = "Adaptive Settings", DefaultValue = 0.5)]
public double ROCMax { get; set; }
[Parameter("ROC-MA Balance Type", Group = "Adaptive Settings", DefaultValue = SmoothType.InvAlpha)]
public SmoothType BalanceType { get; set; }
[Parameter("ROC-MA Balance", Group = "Adaptive Settings", DefaultValue = 0.5, MinValue = 0, MaxValue = 1)]
public double Balance { get; set; }
[Output("Main", LineColor = "DodgerBlue")]
public IndicatorDataSeries Result { get; set; }
private MovingAverage fastMA, slowMA;
private PriceROC priceROC;
protected override void Initialize()
{
fastMA = Indicators.MovingAverage(Bars.ClosePrices, FastPeriod, MAType);
slowMA = Indicators.MovingAverage(Bars.ClosePrices, SlowPeriod, MAType);
priceROC = Indicators.PriceROC(Bars.ClosePrices, ROCPeriod);
}
public override void Calculate(int index)
{
// Take care of Nan Values:
if (double.IsNaN(Result[index - 1]))
{
Result[index] = Bars.ClosePrices[index];
return;
}
// Get ROC Result:
double trPips = Math.Abs(priceROC.Result[index]);
// Calculate alpha:
double alpha;
if (ScaleSettings == ScaleType.Odds)
alpha = trPips / (trPips + 1);
else
alpha = trPips / ROCMax;
if (alpha > 1)
alpha = 1;
// Calculate the adjustment:
double newAdjustment;
if (MAtoFollow == Adherence.Fast)
newAdjustment = alpha * fastMA.Result[index] + (1 - alpha) * slowMA.Result[index];
else
newAdjustment = (1 - alpha) * fastMA.Result[index] + alpha * slowMA.Result[index];
// Calculate the result:
if (BalanceType == SmoothType.Alpha)
Result[index] = (1 - alpha) * Result[index - 1] + alpha * newAdjustment;
else if (BalanceType == SmoothType.InvAlpha)
Result[index] = alpha * Result[index - 1] + (1 - alpha) * newAdjustment;
else
Result[index] = (1 - Balance) * Result[index - 1] + Balance * newAdjustment;
}
}
}
fxtradersystems
Joined on 10.09.2020
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: ROC MA.algo
- Rating: 0
- Installs: 1330
- Modified: 13/10/2021 09:54