We're using CrossAbove() with stochastics in our strategy in backtesting and observed that sometimes it doesn't return true when it should have (based on visuals) and in one instance it seems to have gotten triggered twice.
Here is the settings:
These two I thought it should've returned true. I use the values in Crossed Above 1.
--------------------------------------------------------------
--------------------------------------------------------------
In this one Crossed Above 1 appears to return true twice in succession but visually it shouldn't have.
------------------------------------------------------------------
------------------------------------------------------------------
Here is the test strategy that prints out log of CrossAbove() and CrossBelow() results along with stochastics D and K values for bars ago 0, 1, and 2.
namespace NinjaTrader.NinjaScript.Strategies
{
public class TestCrossAbove : Strategy
{
private Stochastics stochastics;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"TestCrossAbove";
Name = "TestCrossAbove";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
IsUnmanaged = true;
}
else if (State == State.Configure)
{
AddDataSeries(Data.BarsPeriodType.Minute, 1);
AddDataSeries(Data.BarsPeriodType.Tick, 1);
}
else if (State == State.DataLoaded)
{
stochastics = Stochastics(BarsArray[0], 10, 10, 6);
stochastics.Lines[0].Value = 30;
stochastics.Lines[1].Value = 70;
stochastics.Plots[0].Brush = Brushes.DodgerBlue;
stochastics.Plots[1].Brush = Brushes.Goldenrod;
AddChartIndicator(stochastics);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress == 0 && CurrentBar < 10) return;
if (BarsInProgress == 0 && IsFirstTickOfBar)
{
bool crossedAbove0 = CrossAbove(stochastics.K, stochastics.D, 0);
bool crossedAbove1 = CrossAbove(stochastics.K, stochastics.D, 1);
bool crossedBelow0 = CrossBelow(stochastics.K, stochastics.D, 0);
bool crossedBelow1 = CrossBelow(stochastics.K, stochastics.D, 1);
Print(String.Format("{0:dd/MM/yyyy HH:mm:ss.ffffff}", Bars.GetTime(CurrentBar)) + " >>> " +
", Crossed Above 0: " + crossedAbove0 +
", Crossed Above 1: " + crossedAbove1 +
", Crossed Below 0: " + crossedBelow0 +
", Crossed Below 1: " + crossedBelow1 +
", Stochs.D[0]=" + stochastics.D[0] + ", Stochs.D[1]=" + stochastics.D[1] + ", Stochs.D[2]=" + stochastics.D[2] +
", Stochs.K[0]=" + stochastics.K[0] + ", Stochs.K[1]=" + stochastics.K[1] + ", Stochs.K[2]=" + stochastics.K[2]);
}
}
}
}
Comment