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

No Trades on Simple SMA Condition

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

    No Trades on Simple SMA Condition

    When I set a simple long entry based on price condition such as Close[0]>Close[1] I get expected results, many trades entered.

    But when I add this condition: Close[0]>SMA(50)[1] I then have zero trades triggered. No error messages on compile or run, and no clues found in the help files on setting this up.

    I suspect the SMA does not return a value I can compare directly to price, but no idea how to work around that. Any help on this will be greatly appreciated, thanks!

    #2
    Hello scrat,

    I would recommend adding a print to print the value of the SMA(50) value of the previous bar and the close of the current bar along with the time.

    Print(string.Format("{0} | Close[0]: {1} > SMA(50)[1]: {2}", Time[0], Close[0], SMA(50)[1]));

    To understand why the script is behaving as it is, either placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints that print the values of every variable used in every condition that places an order along with the time of that bar.

    This will print to the output window. Backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output from both computers to your reply.

    Below I am providing a link to videos that demonstrate adding prints to a script to get further information about the behavior of the script.

    NT8 — https://www.youtube.com/watch?v=BA0W...tu.be&t=51m51s
    NT7 — https://www.youtube.com/watch?v=K8v_...tu.be&t=48m35s

    Please add prints to your script and send the output from the script and I will assist in analyzing this output to understand the behavior.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks Chelsea. The script is very simple, I'm new to NT8 (some experience with NT7) so I'm just trying some basic conditions to understand the behaviors. I have not used any variables in the script, just the two conditions I mentioned in the op. Here is the output:

      6/20/2017 6:59:43 AM CancelAllOrders: BarsInProgress=0
      6/20/2017 6:59:43 AM CancelAllOrders: BarsInProgress=0
      6/20/2017 6:59:43 AM CancelAllOrders: BarsInProgress=0
      6/20/2017 6:59:43 AM CancelAllOrders: BarsInProgress=0
      6/20/2017 6:59:43 AM CancelAllOrders: BarsInProgress=0
      6/20/2017 6:59:44 AM CancelAllOrders: BarsInProgress=0
      6/20/2017 6:59:44 AM CancelAllOrders: BarsInProgress=0
      6/20/2017 6:59:44 AM CancelAllOrders: BarsInProgress=0
      6/20/2017 6:59:44 AM CancelAllOrders: BarsInProgress=0
      Strategy 'Doublecross': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
      6/20/2017 6:59:55 AM CancelAllOrders: BarsInProgress=0
      6/20/2017 6:59:55 AM CancelAllOrders: BarsInProgress=0
      6/20/2017 6:59:55 AM CancelAllOrders: BarsInProgress=0
      6/20/2017 6:59:55 AM CancelAllOrders: BarsInProgress=0

      Comment


        #4
        Hello scrat,

        An example of a variable used in a condition could the Close variable. This variable is created by NinjaTrader and holds a dataseries. When accessing a specific barsAgo index of that series from that variable, will produce the close price of that bar.
        For example:
        Print(Close[0]);

        This would print the close of the most recent bar.

        Basically what I am getting at is that anything in the condition (even hard coded values) should be printed.

        This would be how in the video I provided this is done by printing the values of the SMA on the current and previous bars.

        This information you have provided is letting you know that your strategy is unable to place trades because you have indexing error that is disabling the strategy.

        "Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart"

        This message indicates an invalid index was used.

        For example, calling Close[1] on the very first bar will cause this error.
        The reason for this is because there will not be a bar ago, essentially there is not enough data at bar 0 to calculate to look a bar ago.

        The script will start from the far left of the chart at the very beginning where the CurrentBar will equal 0 and then start working to the right, calling each bar for OnBarUpdate();

        What you would nee to do instead is wait for after the first bar to be built from both data series and then run your if check for closes, to ensure that you have enough data.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by scrat View Post
          Thanks Chelsea. The script is very simple, I'm new to NT8 (some experience with NT7) so I'm just trying some basic conditions to understand the behaviors. I have not used any variables in the script, just the two conditions I mentioned in the op. Here is the output:

          6/20/2017 6:59:43 AM CancelAllOrders: BarsInProgress=0
          6/20/2017 6:59:43 AM CancelAllOrders: BarsInProgress=0
          6/20/2017 6:59:43 AM CancelAllOrders: BarsInProgress=0
          6/20/2017 6:59:43 AM CancelAllOrders: BarsInProgress=0
          6/20/2017 6:59:43 AM CancelAllOrders: BarsInProgress=0
          6/20/2017 6:59:44 AM CancelAllOrders: BarsInProgress=0
          6/20/2017 6:59:44 AM CancelAllOrders: BarsInProgress=0
          6/20/2017 6:59:44 AM CancelAllOrders: BarsInProgress=0
          6/20/2017 6:59:44 AM CancelAllOrders: BarsInProgress=0
          Strategy 'Doublecross': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
          6/20/2017 6:59:55 AM CancelAllOrders: BarsInProgress=0
          6/20/2017 6:59:55 AM CancelAllOrders: BarsInProgress=0
          6/20/2017 6:59:55 AM CancelAllOrders: BarsInProgress=0
          6/20/2017 6:59:55 AM CancelAllOrders: BarsInProgress=0
          Code:
          if (CurrentBar < 1) return;
          at the top of OnBarUpdate().

          Not being nasty, but if you had searched the forum on the error message, you would have got many posts showing this.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by maybeimnotrader, Today, 05:46 PM
          0 responses
          6 views
          0 likes
          Last Post maybeimnotrader  
          Started by quantismo, Today, 05:13 PM
          0 responses
          6 views
          0 likes
          Last Post quantismo  
          Started by AttiM, 02-14-2024, 05:20 PM
          8 responses
          166 views
          0 likes
          Last Post jeronymite  
          Started by cre8able, Today, 04:22 PM
          0 responses
          8 views
          0 likes
          Last Post cre8able  
          Started by RichStudent, Today, 04:21 PM
          0 responses
          5 views
          0 likes
          Last Post RichStudent  
          Working...
          X