So what I want to accomplish is not very complex I think.
I would like it to find the Highest price in a certain range of bars. Then if the price closes above the Highest price it should open a long position.
I prefer to have it triggered as soons as the current price crosses above the Highest price.
This is my code:
private int maxLookbackPeriod = 300; // Define a maximum lookback period to avoid excessively long periods private double highestHigh; private int forwardBars = 10; Calculate = Calculate.OnEachTick; AddDataSeries(Data.BarsPeriodType.Minute, 1); protected override void OnBarUpdate() { int lookbackPeriod = Math.Min(CurrentBar, maxLookbackPeriod); if (CurrentBar < lookbackPeriod || CurrentBars[0] < lookbackPeriod) { return; } int highestBarIndex = HighestBar(Highs[0], lookbackPeriod); double newHighestHigh = Highs[0][highestBarIndex]; bool isNewHigh = newHighestHigh != highestHigh; if (isNewHigh) { highestHigh = newHighestHigh; DateTime startTime = Times[0][highestBarIndex]; DateTime endTime = Times[0][Math.Min(highestBarIndex + forwardBars, Count - 1)]; Draw.Line(this, "HighLine" + CurrentBar, false, startTime, highestHigh, endTime, highestHigh, Brushes.LimeGreen, DashStyleHelper.Dot, 2); Print("Highest High: " + highestHigh); } if (Close[0] > highestHigh) { EnterLong(); } }
Comment