Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Preventing Strategy from Entering Virtual Positions Based off Historical Data

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Preventing Strategy from Entering Virtual Positions Based off Historical Data

    Hi Folks, I am trying to update my code to NOT enter virtual positions based off historical data. My problem is when I input the following snippet into my code:

    protected override void OnBarUpdate()
    {
    // Only run on real-time data
    if (Historical)
    return;
    }

    per this official guidance: https://ninjatrader.com/support/help...cal.htm​

    I get an error code of CS0103 "The name 'Historical' does not exist in the current context" when trying to compile the code.



    My entire code is this:
    #region Using declarations

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.ComponentModel.DataAnnotations;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using System.Windows;

    using System.Windows.Input;

    using System.Windows.Media;

    using System.Xml.Serialization;

    using NinjaTrader.Cbi;

    using NinjaTrader.Gui;

    using NinjaTrader.Gui.Chart;

    using NinjaTrader.Gui.SuperDom;

    using NinjaTrader.Gui.Tools;

    using NinjaTrader.Data;

    using NinjaTrader.NinjaScript;

    using NinjaTrader.Core.FloatingPoint;

    using NinjaTrader.NinjaScript.Indicators;

    using NinjaTrader.NinjaScript.DrawingTools;

    #endregion


    namespace NinjaTrader.NinjaScript.Strategies

    {

    public class SampleMACrossoverWithATRstopsV3 : Strategy

    {

    private SMA smaFast;

    private SMA smaSlow;

    private ATR atr;


    private double atrStopLong;

    private double atrStopShort;


    protected override void OnStateChange()

    {

    if (State == State.SetDefaults)

    {

    Description = "SampleMACrossoverWithATRstopsV3";

    Name = "SampleMACrossoverWithATRstopsV3";

    Calculate = Calculate.OnPriceChange;

    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;

    IsInstantiatedOnEachOptimizationIteration = true;


    Fast = 9;

    Slow = 21;

    AtrPeriod = 14;

    AtrMultiplier = 2;


    DrawIndicators = false;

    }

    else if (State == State.DataLoaded)

    {

    smaFast = SMA(Fast);

    smaSlow = SMA(Slow);

    atr = ATR(Close, Convert.ToInt32(AtrPeriod));


    if (DrawIndicators)

    {

    smaFast.Plots[0].Brush = Brushes.Snow;

    smaSlow.Plots[0].Brush = Brushes.Yellow;

    atr.Plots[0].Brush = Brushes.DarkCyan;


    AddChartIndicator(smaFast);

    AddChartIndicator(smaSlow);

    AddChartIndicator(atr);

    }

    }

    }


    protected override void OnBarUpdate()

    {

    // Only run on real-time data

    if (Historical)

    return;



    if (BarsInProgress != 0 || CurrentBars[0] < Slow) return;


    double atrValue = atr[0] * AtrMultiplier;


    if (Position.MarketPosition == MarketPosition.Flat)

    {

    if (CrossAbove(smaFast, smaSlow, 1))

    {

    EnterLong(Convert.ToInt32(DefaultQuantity), "Long");

    atrStopLong = Close[0] - atrValue;

    }

    else if (CrossBelow(smaFast, smaSlow, 1))

    {

    EnterShort(Convert.ToInt32(DefaultQuantity), "Short");

    atrStopShort = Close[0] + atrValue;

    }

    }


    if (Position.MarketPosition == MarketPosition.Long)

    {

    double newStop = Close[0] - atrValue;

    if (newStop > atrStopLong)

    {

    atrStopLong = newStop;

    ExitLongStopMarket(0, true, Position.Quantity, atrStopLong, "ExitLong", "Long");

    }

    }


    if (Position.MarketPosition == MarketPosition.Short)

    {

    double newStop = Close[0] + atrValue;

    if (newStop < atrStopShort)

    {

    atrStopShort = newStop;

    ExitShortStopMarket(0, true, Position.Quantity, atrStopShort, "ExitShort", "Short");

    }

    }

    }


    region Properties

    [Range(1, int.MaxValue)]

    [NinjaScriptProperty]

    public int Fast { get; set; }


    [Range(1, int.MaxValue)]

    [NinjaScriptProperty]

    public int Slow { get; set; }


    [Range(1, int.MaxValue)]

    [NinjaScriptProperty]

    public int AtrPeriod { get; set; }


    [Range(0.1, double.MaxValue)]

    [NinjaScriptProperty]

    public double AtrMultiplier { get; set; }


    [NinjaScriptProperty]

    public bool DrawIndicators { get; set; }

    #endregion

    }

    }




    #2
    Hello joedtrades1982,

    Thanks for your post.

    To skip historical processing so the strategy always starts from a flat position you would need to add the code below to the top of your OnBarUpdate() logic.

    if (State == State.Historical)
    return;


    State: https://ninjatrader.com/support/help.../nt8/state.htm
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NJA_MC, 01-03-2021, 10:34 PM
    12 responses
    158 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by ageeholdings, Today, 05:22 AM
    1 response
    12 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by PaulMohn, Yesterday, 02:06 AM
    4 responses
    16 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by kujista, 04-22-2024, 07:46 AM
    4 responses
    17 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by SentientDavid, Today, 01:34 AM
    1 response
    24 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Working...
    X