Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to implement a new custom class into a strategy

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

    How to implement a new custom class into a strategy

    Hi,
    I did a new custom class called "PatternFilter" into "NinjaTrader.Custom.Classes", but when I try to call "PatternFilter" class from my strategy class, called "Test2", then it doesen't work.

    In the code below, I explane how I did it.
    After had created my Test2 strategy, I create a custom PatternFilter, then use PatternFilter from Test2.
    The problem is that when I try to launch the strategy analyzer, it doesn't work.

    1) Is maybe that I must applicate my custom class in other method in place of "State.DataLoaded" and "onBarsUpdate"?
    2) Should I use the constructor differently?


    Exemple:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class Test2 : Strategy (that's my strategy)
    {
    //constructor
    public Test2()
    {
    customPatternFilter = new CustomPatternFilter(this);
    }


    if (State == State.SetDefaults)​{
    ...
    }
    if (State == State.DataLoaded)
    {
    ...
    }

    customPatternFilter = new CustomPatternFilter(this);
    }


    protected override void OnBarUpdate()
    {

    //exemple, here I call my custom method.
    customPatternFilter.DrawPatternCandles(this);
    customPatternFilter.CheckPatternFilter();
    }

    ecc..
    }



    My custom method:

    using NinjaTrader.NinjaScript.DrawingTools;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.Strategies;
    using System.Windows.Media;

    namespace NinjaTrader.Custom.Classes (that's my custom method)
    {
    public class PatternFilter
    {

    private CandlestickPattern candlestickPattern;
    private Test2 strategy;

    //constructor
    public PatternFilter(Test2 strategy)
    {
    this.candlestickPattern = new CandlestickPattern();
    this.strategy = strategy;
    }

    public bool CheckPatternFilter()
    {
    if (strategy == null)
    {
    return false;
    }

    if (candlestickPattern.CandlestickPattern(ChartPatter n.BearishEngulfing, 0)[0] == 1)
    {
    return true;
    }
    ecc...

    else
    return false;
    }

    //other methods
    }

    #2
    Hello TeoSigno,

    I would suggest looking at the candlestick pattern indicator for an example of using a custom class, that would be the same general concept to use in a strategy.

    If you have made the class in an external file from the strategy then you will need to use fully qualified namespaces for the strategies class type NinjaTrader.NinjaScript.Strategies.Test2.

    Comment


      #3
      ​Dear Jesse,
      nice to meet you.
      I tried using full name, but even it, the custom class is as if it blocks the strategy from working.
      If I call the PatternFilter class then the analyzer remains at 0, as if it exits the code flow and does not process anything.​
      I added two screenshots.





      Comment


        #4
        Hello TeoSigno,

        I would not be sure, you would have to debug the code in your class. If you want to so something similar to the candlestick pattern I would suggest to just use that as a direct starting point as that code already works, you would need to rename the classes so they are unique in your script but it should otherwise look identical to that script.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        60 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        39 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        21 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        23 views
        0 likes
        Last Post TheRealMorford  
        Started by Mindset, 02-28-2026, 06:16 AM
        0 responses
        51 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Working...
        X