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

Set Stop Loss as my Entry[1] bar!

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

    Set Stop Loss as my Entry[1] bar!

    Hi there,
    how can i set the stop loss as my entry lowest price? I use the EXitlongStop function, but the exit price always set as a closing price, its not stop loss immediately.
    Thanks for the reply,
    Attached Files

    #2
    Hi, thanks for posting. The strategy builder will only run scripts OnBarClose, so only the OHLC price of each bar are available. You must add intrabar granularity to submit orders at intrabar prices. See here for a guide:



    Kind regards,
    -ChrisL
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChrisL View Post
      Hi, thanks for posting. The strategy builder will only run scripts OnBarClose, so only the OHLC price of each bar are available. You must add intrabar granularity to submit orders at intrabar prices. See here for a guide:

      https://ninjatrader.com/support/foru...-backtest-live

      Kind regards,
      -ChrisL
      Thanks for your reply, but i still can plot it and the backtest result is 0. Can you see my code and What's wrong here, Thanks!!

      //
      // Copyright (C) 2015, NinjaTrader LLC <www.ninjatrader.com>.
      // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
      //
      region Using declarations
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Linq;
      using System.Text;
      using System.Windows.Media;
      using System.Xml.Serialization;
      using NinjaTrader.NinjaScript.Indicators;
      using NinjaTrader.Cbi;
      #endregion

      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class SampleIntrabarBacktest : Strategy
      {
      private int fast;
      private int slow;

      protected override void OnStateChange()
      {
      if(State == State.SetDefaults)
      {
      Fast = 10;
      Slow = 25;
      Calculate = Calculate.OnBarClose;
      Name = "SampleIntrabarBacktest";
      }

      else if(State == State.Configure)
      {
      /* Add a secondary bar series.
      Very Important: This secondary bar series needs to be smaller than the primary bar series.

      Note: The primary bar series is whatever you choose for the strategy at startup. In this example I will
      reference the primary as a 5min bars series. */
      AddDataSeries(Data.BarsPeriodType.Second, 3);

      // Add two EMA indicators to be plotted on the primary bar series
      AddChartIndicator(EMA(Fast));
      AddChartIndicator(EMA(Slow));

      /* Adjust the color of the EMA plots.
      For more information on this please see this tip: http://www.ninjatrader-support.com/v...ead.php?t=3228 */
      EMA(Fast).Plots[0].Brush = Brushes.Blue;
      EMA(Slow).Plots[0].Brush = Brushes.Green;
      }
      }

      protected override void OnBarUpdate()
      {
      /* When working with multiple bar series objects it is important to understand the sequential order in which the
      OnBarUpdate() method is triggered. The bars will always run with the primary first followed by the secondary and
      so on.

      Important: Primary bars will always execute before the secondary bar series.
      If a bar is timestamped as 12:00PM on the 5min bar series, the call order between the equally timestamped 12:00PM
      bar on the 1min bar series is like this:
      12:00PM 5min
      12:00PM 1min
      12:01PM 1min
      12:02PM 1min
      12:03PM 1min
      12:04PM 1min
      12:05PM 5min
      12:05PM 1min

      When the OnBarUpdate() is called from the primary bar series (5min series in this example), do the following */
      if (BarsInProgress == 0)
      {
      // When the fast EMA crosses above the slow EMA, enter long on the secondary (1min) bar series
      if (CrossAbove(EMA(Fast), EMA(Slow), 1))
      {
      /* The entry condition is triggered on the primary bar series, but the order is sent and filled on the
      secondary bar series. The way the bar series is determined is by the first parameter: 0 = primary bars,
      1 = secondary bars, 2 = tertiary bars, etc. */
      EnterLong(1, 1, "Long: 1min");
      }
      // When the fast EMA crosses below the slow EMA, enter short on the secondary (1min) bar series
      else if (CrossBelow(EMA(Fast), EMA(Slow), 1))
      {
      /* The entry condition is triggered on the primary bar series, but the order is sent and filled on the
      secondary bar series. The way the bar series is determined is by the first parameter: 0 = primary bars,
      1 = secondary bars, 2 = tertiary bars, etc. */
      EnterShort(1, 1, "Short: 1min");
      }
      }
      // When the OnBarUpdate() is called from the secondary bar series, do nothing.
      else
      {
      return;
      }
      }

      region Properties

      [Range(1, int.MaxValue), NinjaScriptProperty]
      [Display(ResourceType = typeof(Custom.Resource), Name = "Fast", GroupName = "NinjaScriptParameters", Order = 0)]
      public int Fast
      { get; set; }

      [Range(1, int.MaxValue), NinjaScriptProperty]
      [Display(ResourceType = typeof(Custom.Resource), Name = "Slow", GroupName = "NinjaScriptParameters", Order = 0)]
      public int Slow
      { get; set; }

      #endregion
      }
      }

      Comment


        #4
        Hi strongelephant, If your strategy is not making any trades, you will need to use Print to visualize the signal being set up. You can add a print inside of your entry condition block and if you get a text print out in the output windows, you know that section of code is being hit. Here is a brief guide on the subject:



        Kind regards,
        -ChrisL
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by junkone, 04-28-2024, 02:19 PM
        8 responses
        92 views
        1 like
        Last Post brucerobinson  
        Started by mkouitra, 10-23-2021, 04:40 PM
        17 responses
        1,964 views
        0 likes
        Last Post NinjaTrader_Jason  
        Started by Vietanhnguyen2hotmailcom, 05-03-2024, 10:29 AM
        4 responses
        29 views
        0 likes
        Last Post Vietanhnguyen2hotmailcom  
        Started by PhillT, 04-19-2024, 02:16 PM
        4 responses
        37 views
        0 likes
        Last Post PhillT
        by PhillT
         
        Started by ageeholdings, 05-01-2024, 05:22 AM
        5 responses
        40 views
        0 likes
        Last Post ageeholdings  
        Working...
        X