I have managed to create my first custom indicator and stratgeies. Everything seams to work fine except that my custom strategies enter positions one bar to late. I have spent a lot of time trying to solve this on my own, but I'm stuck and would appreciate some help. I suspect that this is just a simple misstake on my side.
To illustrate the problem I have created a simple as possible strategy that enters and exits a position when EMA(18) crossabove/ crossbelow EMA(55). You can see in the attached pic that entries and exits lag 1 bar comapred to the "cross". I'v also created a custom indicator that writes a gray triangle above/ below the bar where the entries and exits should be. This indicator displays the correct bars for exits and entries and contains the same logic as the strategy, but as mentioned the entries and exits of the strategy lags 1 bar behind the indicator. I can't figure out what the reason for this may be. I have set the option OnBarsClose = false in the strategy.
I have attcahed my strategy settings below.
The code of the stratgey is:
protected override void Initialize() { CalculateOnBarClose = false; } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { if (CrossAbove(EMA(FAST), EMA(SLOW), 1)) { EnterLong(); } if (CrossBelow(EMA(FAST), EMA(SLOW), 1)) { EnterShort(); } }
protected override void Initialize() { Long = new BoolSeries(this); Short = new BoolSeries(this); Overlay = true; PriceTypeSupported = false; } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { // Use this method for calculating your indicator values. Assign a value to each if (CurrentBar < sLOW) return; bool EnterLong = false; bool EnterShort = false; if (CrossAbove(EMA(fAST), EMA(sLOW), 1)) { EnterLong = true; } if (CrossBelow(EMA(fAST), EMA(sLOW), 1)) { EnterShort = true; } Long.Set(EnterLong); if (Long[0] == true) { DrawTriangleUp( "Long" + CurrentBar.ToString(), false, 0, Low[0] - 100*TickSize, Color.LightSlateGray); } Short.Set(EnterShort); if (Short[0] == true) { DrawTriangleDown( "Short" + CurrentBar.ToString(), false, 0, High[0] + 100*TickSize, Color.LightSlateGray); } }
Comment