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

Method Works INSIDE Strategy, does NOT when moved to & called from separate file

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

    Method Works INSIDE Strategy, does NOT when moved to & called from separate file

    Hi there,
    I am running a strategy and it is getting quite lengthy. In an effort to clean it up, I started moving some of my methods out of the strategy file and into the appropriately named file "ninjaweapons.cs." I have a number of methods that I offloaded there, and they function without issue. I have one method, however, that is no longer functioning after the move. I could go back to the way things were when they worked, but I feel there is a fundamental lesson I need to learn at this point. As a programmer, I need to understand completely how to call methods from another file!

    When the method was IN the strategy file, it served as the entry signal (kind of important). So my strategy would buy and sell as intended. After moving it from my strategy file to ninjaweapons.cs, it no longer triggers those entries. Typically I'll insert Print()'s along the way to debug, but what I'm finding in my ninjaweapons.cs file, is that Print() generates a compile error CS0103 The name 'Print' does not exist in the current context. This is potentially a separate issue all together, or perhaps it's related, not sure, but figured it was worth mentioning.

    I have been working with Ninjascripts for a while now (going on 3 years), so I think I know some things, but there are certainly some holes in my game as I am entirely self-taught, and most of my learning comes from necessity (not best practices). That being said, I have a feeling I'm making some newbie error in calling methods from other files. Maybe passing variables incorrectly, or failing to call a namespace or something. I don't know!
    Below is some code. If you need more context, please let me know. Thank you for your time and help in advance!

    Note that the method giving me trouble is CompletePricePivot(). From my strategy I'm writing something like this:
    private bool WthenCppOccurs(int MinRedBars, int MaxRedBars, int Midnight, int SecondLowBar, int TrendBreakBar, int PullbackStartBar, ISeries<double> Open, ISeries<double> High, ISeries<double> Low, ISeries<double> Close, ISeries<double> Volume, int CurBar)
    {
    if (SecondLowBar != 0 && PullbackStartBar == 0 && PullbackOccurs(TrendBreakBar, MinRedBars, MaxRedBars, Midnight, Open, High, Low, Close, Volume, CurBar))
    return true;

    else return false;
    }
    private bool PullbackOccurs (int TrendBreakBar, int MinRedBars, int MaxRedBars, int Midnight, ISeries<double> Open, ISeries<double> High, ISeries<double> Low, ISeries<double> Close, ISeries<double> Volume, int CurBar)
    {
    if (ninjaweapons.CompletePricePivot(MinRedBars, MaxRedBars, Midnight, CurBar, TrendBreakBar, Open, High, Low, Close, Volume, CurBar).bar > TrendBreakBar)
    .
    .
    .
    {

    And in ninjaweapons.cs it looks like this:
    public static Point CompletePricePivot(int MinRedBars, int MaxRedBars, int Midnight, int StartBar, int SeekToBar, ISeries<double> Open, ISeries<double> High, ISeries<double> Low, ISeries<double> Close, ISeries<double> Volume, int CurBar)
    {
    .
    .
    .
    {

    It is my thought that I have to pass all of my ISeries<double> Open, High, Low, Close, Volume, Time and CurrentBar into any method I call lfrom OnBarUpdate().

    One last thing, the beginning of ninjaweapons.cs looks like this:
    using NinjaTrader.Custom.Indicators;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.NinjaScript.Indicators;
    using System;
    using System.Linq;

    namespace NinjaTrader.Custom.NinjaWeapons
    {

    #2
    Originally posted by NateG0310 View Post
    if (NinjaTrader.Custom.NinjaWeapons.CompletePricePivot(MinRedBars, MaxRedBars, Midnight, CurBar, TrendBreakBar, Open, High, Low, Close, Volume, CurBar).bar > TrendBreakBar)

    One last thing, the beginning of ninjaweapons.cs looks like this:

    namespace NinjaTrader.Custom.NinjaWeapons
    Answer #1:
    NinjaWeapons != ninjaweapons
    Why? Because two namespaces with the same name but different
    casing are actually different namespaces

    Answer #2:
    Since you don't say anything about your Strategy using your
    custom namespace, note how I fully-qualified the method call.
    Last edited by bltdavid; 08-26-2021, 05:02 AM.

    Comment


      #3
      Originally posted by NateG0310 View Post
      Typically I'll insert Print()'s along the way to debug, but what I'm finding in my ninjaweapons.cs file, is that Print() generates a compile error CS0103 The name 'Print' does not exist in the current context. This is potentially a separate issue all together, or perhaps it's related, not sure, but figured it was worth mentioning.
      Correct, that is a separate issue.

      The easiest and quickest way to solve that is to organize your
      custom 'auxiliary' code library in a different way,

      1. Use the normal NinjaTrader.NinjaScript.Strategies namespace.
      2. Use the partial class method, see @Strategy.cs for an example.

      One huge benefit of this route is that you can omit the passing of
      class instance variables as arguments to your custom methods,
      because anything defined inside the partial becomes a part of
      the class
      , and therefore it already has access to all the normal
      Strategy methods and variables.

      All of your custom methods in this partial class that are defined
      as 'static' will be unable to access the normal Strategy instance
      variables, so it's quite common to omit that specifier.

      My point is: remove 'static' from CompletePricePivot, it's just
      making your life harder. Why? Because the convenience of
      being able to directly use class instance variables (like Open,
      Close, and CurrentBar) is only possible if you remove it.

      Only use 'static' if you know C# well enough. I mean, you can
      go pretty far in C# without using 'static' so skip it until you know
      enough to know why you're using it.

      Comment


        #4
        Hi Nate, thanks for posting.

        The best way to include custom code is including your methods as part of the Strategy class e.g.

        Code:
        #region Using declarations
        //...
        using NinjaTrader.NinjaScript;
        //...
        #endregion
        
        namespace NinjaTrader.NinjaScript.Strategies
        {
        public partial class Strategy
        {
            public void PrintPriceData(StrategyBase sb)
            {
                Print("Here is some price data: " + sb.Close[0]);
            }
        }
        }
        Within the strategy:

        Code:
        PrintPriceData(this);
        It can also be a static class and independent of other classes e.g.

        Code:
        namespace MyNameSpace
        {
        public static class MyHelperClass
        {
            public static void PrintPriceData(double price) //could also pass in StrategyBase
            {
                NinjaTrader.Code.Output.Process("Price Data: " + price, PrintTo.OutputTab1);
            }
        }
        }
        Within the strategy:

        Code:
        using MyNameSpace;
        
        MyNameSpace.MyHelperClass.PrintPriceData(Close[0]);
        Kind regards,
        -ChrisL
        Last edited by NinjaTrader_ChrisL; 08-26-2021, 08:54 AM.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          bltdavid, awesome information! I feel like you just have a ton of experience with this.
          ChrisL, thank you so much, your information is awesome as well!

          I'm going to work with this information and see what I come up with. I have a feeling it may generate a few more questions, but I will be certain to explore these thoroughly before I pop those of. Thank you both!

          Nathan.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Shansen, 08-30-2019, 10:18 PM
          24 responses
          942 views
          0 likes
          Last Post spwizard  
          Started by Max238, Today, 01:28 AM
          0 responses
          9 views
          0 likes
          Last Post Max238
          by Max238
           
          Started by rocketman7, Today, 01:00 AM
          0 responses
          4 views
          0 likes
          Last Post rocketman7  
          Started by wzgy0920, 04-20-2024, 06:09 PM
          2 responses
          28 views
          0 likes
          Last Post wzgy0920  
          Started by wzgy0920, 02-22-2024, 01:11 AM
          5 responses
          33 views
          0 likes
          Last Post wzgy0920  
          Working...
          X