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

Nothing in Strategy Analyzer

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

    Nothing in Strategy Analyzer

    Hi, I am trying to learn ninjascript. I have made this strategy and it compiles but when I try it in the analyzer, nothing happens. I figure there is probably something wrong with the script because I can run the sample strategies fine. Its supposed to just take the opening price and buy or sell at the first 10s place. For instance open 4995, sell at 5000 or buy at 4990.

    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 TakeThree : Strategy
    {
    private double openingPrice; // Declare openingPrice at the class level

    region Strategy Parameters

    [Range(1, double.MaxValue), NinjaScriptProperty]
    public double Target { get; set; } = 80; // Default target in ticks

    [Range(1, double.MaxValue), NinjaScriptProperty]
    public double StopLoss { get; set; } = 40; // Default stop loss in ticks
    #endregion

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"The";
    Name = "TakeThree";
    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;
    }
    else if (State == State.Configure)
    {
    AddDataSeries("MES 03-24", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] == 0)
    {
    // Get the opening price of the current bar
    openingPrice = Open[0];
    }
    }

    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
    // Ensure we are processing market data for the primary instrument
    if (marketDataUpdate.MarketDataType != MarketDataType.Last || marketDataUpdate.Instrument != Instrument)
    return;

    // Get the current price
    double currentPrice = marketDataUpdate.Price;

    // Calculate the first 10s place above and below the opening price
    double first10sPlaceAbove = Math.Ceiling(openingPrice / 10) * 10;
    double first10sPlaceBelow = Math.Floor(openingPrice / 10) * 10;

    // Check if we have already placed orders
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    // Place a buy order (long position) if the price reaches the first 10s place below
    if (currentPrice <= first10sPlaceBelow)
    {
    EnterLong(Convert.ToInt32(DefaultQuantity));
    SetProfitTarget(CalculationMode.Ticks, Target);
    SetStopLoss(CalculationMode.Ticks, StopLoss);
    }
    // Place a sell order (short position) if the price reaches the first 10s place above
    else if (currentPrice >= first10sPlaceAbove)
    {
    EnterShort(Convert.ToInt32(DefaultQuantity));
    SetProfitTarget(CalculationMode.Ticks, Target);
    SetStopLoss(CalculationMode.Ticks, StopLoss);
    }
    }
    }
    }
    }

    Thank you

    #2
    Hello JGriff5646,

    Thanks for your post.

    I see you are adding a secondary data series to the script to use for logic in the script.

    To backtest the strategy in the Strategy Analyzer you must have Historical Data for all instruments the strategy uses. This means you will need to have Historical Data for the primary instrument you are testing the script on and have Historical Data downloaded for the added series (MNQ 03-24).

    I suggest downloading Historical Data in the Tools > Historical Data window for both the primary instrument you are running the script on and the MNQ 03-24 secondary series that is added to the script.

    Downloading Historical Data: https://ninjatrader.com/support/help...8/download.htm

    OnMarketData() is not called on historical data (backtest), however, it can be called historically by using TickReplay

    If used with TickReplay, please keep in mind Tick Replay ONLY replays the Last market data event, and only stores the best inside bid/ask price at the time of the last trade event. You can think of this as the equivalent of the bid/ask price at the time a trade was reported. As such, historical bid/ask market data events (i..e, bid/ask volume) DO NOT work with Tick Replay. To obtain those values, you need to use a historical bid/ask series separately from TickReplay through OnBarUpdate(). More information can be found under Developing for Tick Replay.

    OnMarketData(): https://ninjatrader.com/support/help...marketdata.htm

    If you can successfully test the sample strategies, such as SampleMACrossover, but you are not seeing backtesting results when testing your strategy after downloading the historical data then the conditions to place orders are likely not becoming true.

    Debugging prints should be added to the strategy (outside the conditions) that prints out all the values being used in your conditions to place an order along with the time of the bar to see how the logic is evaluating.

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.


    I also see you are calling Set methods after the Entry method in your script. Set methods prep NinjaTrader to submit protective orders when the Entry order is filled. Due to this, Set methods should be called before the Entry method.

    For example:

    SetProfitTarget();
    SetStopLoss();
    EnterLong();

    From the SetProfitTarget()/SetStopLoss() help guide: "Since they are submitted upon receiving an execution, the Set method should be called prior to submitting the associated entry order to ensure an initial level is set."

    SetProfitTarget(): https://ninjatrader.com/support/help...ofittarget.htm
    SetStopLoss(): https://ninjatrader.com/support/help...oss.htm​
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by geddyisodin, 04-25-2024, 05:20 AM
    8 responses
    60 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by jxs_xrj, 01-12-2020, 09:49 AM
    4 responses
    3,287 views
    1 like
    Last Post jgualdronc  
    Started by Option Whisperer, Today, 09:55 AM
    0 responses
    5 views
    0 likes
    Last Post Option Whisperer  
    Started by halgo_boulder, 04-20-2024, 08:44 AM
    2 responses
    22 views
    0 likes
    Last Post halgo_boulder  
    Started by mishhh, 05-25-2010, 08:54 AM
    19 responses
    6,189 views
    0 likes
    Last Post rene69851  
    Working...
    X