Description
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class MeanReversion : Indicator
{
[Output("Result1", LineColor = "lightGreen")]
public IndicatorDataSeries Result1 { get; set; }
[Output("Result2", LineColor = "Red")]
public IndicatorDataSeries Result2 { get; set; }
[Output("Result3", LineColor = "Aqua")]
public IndicatorDataSeries Result3 { get; set; }
private Bars tf;
private int idx;
private int previousIdx;
private int buyPeriod;
private int sellPeriod;
private double buySum;
private double sellSum;
private double buyAverage;
private double sellAverage;
private int savedbp;
private int savedsp;
private double maxbp;
private double minbp;
private double maxsp;
private double minsp;
private IndicatorDataSeries main;
protected override void Initialize()
{
tf = MarketData.GetBars(Bars.TimeFrame);
main = CreateDataSeries();
}
public override void Calculate(int index)
{
idx = tf.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
if (idx > previousIdx)
{
buyPeriod++;
savedbp = buyPeriod;
sellPeriod++;
savedsp = sellPeriod;
}
if (buyPeriod == 0)
buyPeriod = savedbp;
buySum = 0;
for (int i = index - buyPeriod + 1; i <= index; i++)
buySum += Bars.OpenPrices[i];
if (sellPeriod == 0)
sellPeriod = savedsp;
sellSum = 0;
for (int i = index - sellPeriod + 1; i <= index; i++)
sellSum += Bars.OpenPrices[i];
buyAverage = buySum / buyPeriod;
sellAverage = sellSum / sellPeriod;
if (Bars.ClosePrices[index] > buyAverage)
{
buyAverage = Bars.ClosePrices[index];
buyPeriod = 0;
}
if (Bars.ClosePrices[index] < sellAverage)
{
sellAverage = Bars.ClosePrices[index];
sellPeriod = 0;
}
maxbp = Bars.OpenPrices.Maximum(buyPeriod + 1);
minbp = Bars.OpenPrices.Minimum(buyPeriod + 1);
maxsp = Bars.OpenPrices.Maximum(sellPeriod + 1);
minsp = Bars.OpenPrices.Minimum(sellPeriod + 1);
main[index] = (2 * buyAverage - maxbp - minsp) + (2 * sellAverage - maxbp - minsp);
Result1[index] = main.Minimum(sellPeriod + 1);
Result2[index] = main.Maximum(buyPeriod + 1);
Result3[index] = main[index];
previousIdx = idx;
}
}
}
srm_bcn
Joined on 01.09.2019
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: MeanReversion.algo
- Rating: 0
- Installs: 1389
- Modified: 13/10/2021 09:54
Note that publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section, please use the Copyright Infringement Notification form to submit a claim.
Comments
Log in to add a comment.
No comments found.