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

Help to code a simple strategy

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

    Help to code a simple strategy

    Can somebody help me and write a simple code to backtest the TSSupertrend indicator?



    Long = If close above TSSupertrend (5,1,10), buy at next day open.
    Exit Long = If low below TSSupertrend (5,1,10).

    (The opposite for short)

    I know this "strategy" is not profitable. I need just a Entry/Exit Tool if my fundamental condition match.

    Is there anything to note by backtesting Futures? How to set the merge policy?

    Thank you very much!

    #2
    Hello 7Maxx,

    This indicator has multiple plots.
    You will need to specify the plot you want to compare.

    If the name is TSSuperTrend using a set of brackets and a barsAgo value an index would access the most recent bar of that plot.
    TSSuperTrend(/*parameters*/)./*plot name here*/[0]

    Close is a variable that holds a data series of closes for each bar. This is also accessed with a set of brackets and a barsAgo index.
    Close[0]

    In c# you will need a if statement and in the parenthesis compare the indicator's plot value with the close value.
    Taking a peak at this indicator I see there is an UpTrend plot.
    if (Close[0] > TSSuperTrend(/*parameters here*/).UpTrend[0])

    if statements - http://ninjatrader.com/support/helpG...g_commands.htm
    Close - http://ninjatrader.com/support/helpGuides/nt7/close.htm
    Low - http://ninjatrader.com/support/helpGuides/nt7/low.htm

    If the primary data series is less than 1 day in interval, you can use Bars.FirstBarOfSession to know that a bar is the first bar of a session.


    To place an entry order such as a buy market order use the EnterLong() method call.


    For an exit at a specific price, you can use a Stop or Stop Limit behind the order and a Limit ahead of the order.
    ExitLongStop - http://ninjatrader.com/support/helpG...itlongstop.htm
    ExitShortStop - http://ninjatrader.com/support/helpG...tshortstop.htm
    ExitLongStopLimit - http://ninjatrader.com/support/helpG...gstoplimit.htm
    ExitShortStopLimit - http://ninjatrader.com/support/helpG...tstoplimit.htm
    ExitLongLimit - http://ninjatrader.com/support/helpG...tlonglimit.htm
    ExitShortLimit - http://ninjatrader.com/support/helpG...shortlimit.htm


    Please also see the post below about getting started learning NinjaScript.
    Support for the development of custom indicators using NinjaScript.


    This thread will remain open for any community members that would like to build your script as a convenience to you.

    You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi ChelseaB

      Thank you for the answer!

      I've tried to write the script with the strategy wizard, but I got a lot of error massages.

      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      /// <summary>
      /// Backtest TSSupertrend
      /// </summary>
      [Description("Backtest TSSupertrend ")]
      public class TestSuperTrend : Strategy
      {
      #region Variables
      // Wizard generated variables
      private bool superTrend = true; // Default setting for SuperTrend
      // User defined variables (add any user defined variables below)
      #endregion

      /// <summary>
      /// This method is used to configure the strategy and is called once before any strategy method is called.
      /// </summary>
      protected override void Initialize()
      {
      Add(TSSuperTrend(5, TSSuperTrend.Utility.MovingAverageType.HMA, 1, 10, TSSuperTrend.Utility.SuperTrendMode.ATR));

      CalculateOnBarClose = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Condition set 1
      if (CrossAbove(Close, TSSuperTrend(5, TSSuperTrend.Utility.MovingAverageType.HMA, 1, 10, TSSuperTrend.Utility.SuperTrendMode.ATR).UpTrend, 1))
      {
      EnterLong(DefaultQuantity, "");
      }

      // Condition set 2
      if (CrossBelow(Close, TSSuperTrend(5, TSSuperTrend.Utility.MovingAverageType.HMA, 1, 10, TSSuperTrend.Utility.SuperTrendMode.ATR).DownTrend , 1))
      {
      EnterShort(DefaultQuantity, "");
      }
      }

      #region Properties
      [Description("")]
      [GridCategory("Parameters")]
      public bool SuperTrend
      {
      get { return superTrend; }
      set { superTrend = value; }
      }
      #endregion
      }
      }


      I don't need a stop loss or profit target, just buy and sell on the Supertrend indicator.

      Comment


        #4
        Hello 7Maxx,

        What is the error message you are receiving?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          erorr code : CS 0119

          Screenshot is in the attachment
          Attached Files

          Comment


            #6
            Hello 7Maxx,

            What is the full error message you are reading in the error column?
            (You will need to expand the error column)

            The error message is going to let you know what the issue is.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by TheWhiteDragon, 01-21-2019, 12:44 PM
            4 responses
            541 views
            0 likes
            Last Post PaulMohn  
            Started by GLFX005, Today, 03:23 AM
            0 responses
            2 views
            0 likes
            Last Post GLFX005
            by GLFX005
             
            Started by XXtrader, Yesterday, 11:30 PM
            2 responses
            11 views
            0 likes
            Last Post XXtrader  
            Started by Waxavi, Today, 02:10 AM
            0 responses
            7 views
            0 likes
            Last Post Waxavi
            by Waxavi
             
            Started by TradeForge, Today, 02:09 AM
            0 responses
            14 views
            0 likes
            Last Post TradeForge  
            Working...
            X