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

Strategy Analyzer and On Each Tick is not working

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

    Strategy Analyzer and On Each Tick is not working


    I am using Strategy Builder with On Each Tick

    And I want to close my Long position after +/- $2 dollars

    I started testing at 13402.25

    When I use the strategy in real-time it seems to be working.

    For example from the attachment picture
    Long Entry on 13402.25 and exit on TP 13403.25 ( not $2 but anyways )
    Long Entry on 13404.25 and exit on TP 13406.00 ( closer to $2 )
    Long Entry on 13406.25 and exit on TP 13408.00 ( consistent at least )

    BUT, as you can see, it doesn't work for past data.
    The strategy waits for the bar to close

    For example from the attachment picture
    Long Entry on 13412.00 and exit on TP 13416.75 ( it waited for the bar to close, it should've closed on 13414.00 )
    Long Entry on 13416.75 and exit on SL 13413.50 ( it waited for the bar to close but at least it opened a new position at the same price )
    Long Entry on 13406.00 and exit on SL 13401.25 ( it waited for the bar to close, it should've closed on 13404.00 )

    When I use the Strategy Analyzer, the same thing happens, the strategy waits for the bar to close, then updates the position

    this is a problem because that means I cannot really get real-time results from the Strategy Analyzer.
    If I want to know how my strategy does in real-time, I have to run it in real-time. That sounds like it makes sense but
    I want to get results from past data

    I hope to be wrong. what am I doing wrong? how can I use Strategy Analyzer to get real-time results?



    Code:
    #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
    
    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class eeeee : Strategy
    {
    private double TP;
    private double SL;
    
    private DEMA DEMA1;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "eeeee";
    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;
    TP = 1;
    SL = 1;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    DEMA1 = DEMA(Close, 4);
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < 1)
    return;
    
    // Set 1
    if ((Position.MarketPosition == MarketPosition.Flat)
    && (Close[0] > DEMA1[0]))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"LONG");
    TP = (Close[0] + 2) ;
    SL = (Close[0] - 2) ;
    }
    
    // Set 2
    if ((Position.MarketPosition == MarketPosition.Long)
    && (Close[0] >= TP))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), @"TP", @"LONG");
    }
    
    // Set 3
    if ((Position.MarketPosition == MarketPosition.Long)
    && (Close[0] <= SL))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), @"SL", @"LONG");
    }
    
    }
    }
    }
    Attached Files

    #2
    Hello AaronKoRn,

    From the help guide:
    "Notes:
    1.On a historical data set, only the OHLCVT of the bar is known and not each tick that made up the bar. As a result, State.Historical data processes OnBarUpdate() only on the close of each historical bar even if this property is set to OnEachTick or OnPriceChange. You can use TickReplay or a Multi-time frame script to obtain intrabar data."
    https://ninjatrader.com/support/help.../calculate.htm

    Below is a link to a forum post on 1-tick intra-bar granularity for order fills, and TickReplay for OnPriceChange/OnEachTick updates in historical data.
    https://ninjatrader.com/support/foru...377#post773377

    As a tip, to export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient (instead of posting the text of a script) do the following:
    1. Click Tools -> Export -> NinjaScript...
    2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
    3. Click the 'Export' button
    4. Enter a unique name for the file in the value for 'File name:'
    5. Choose a save location -> click Save
    6. Click OK to clear the export location message
    By default your exported file will be in the following location:
    • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
    Below is a link to the help guide on Exporting NinjaScripts.
    http://ninjatrader.com/support/helpG...-us/export.htm
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by bortz, 11-06-2023, 08:04 AM
    47 responses
    1,605 views
    0 likes
    Last Post aligator  
    Started by jaybedreamin, Today, 05:56 PM
    0 responses
    8 views
    0 likes
    Last Post jaybedreamin  
    Started by DJ888, 04-16-2024, 06:09 PM
    6 responses
    18 views
    0 likes
    Last Post DJ888
    by DJ888
     
    Started by Jon17, Today, 04:33 PM
    0 responses
    4 views
    0 likes
    Last Post Jon17
    by Jon17
     
    Started by Javierw.ok, Today, 04:12 PM
    0 responses
    13 views
    0 likes
    Last Post Javierw.ok  
    Working...
    X