Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Building my first Custom NinjaScript.cs (Need some help/direction)

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

    Building my first Custom NinjaScript.cs (Need some help/direction)

    Hello, I am trying to create a simple HeikinAshi Potential Buy/Sell signal strategy.
    I've attached my full NinjaScript that I can't get to compile. Any suggestions for changes?

    Overall very new to C# and NinjaScript in general. Wanting to continue to learn and build more complex indicators and strategies eventually. Figured this would be a good place to start. I've watched Strategy Builder 301 & NinjaScript Editor 401 and reviewed these: https://ninjatrader.com/support/helpGuides/nt8/

    This is essentially what I am going for, it works on top of any type of candlestick pattern/timeframe in TD Ameritrade:
    Code:
    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class HeikenAshiCustom : Strategy
    {
    private Series<double> HaClose;
    private Series<double> HaOpen;
    private Series<bool> HaTrendUp;
    private Series<bool> HaTrendDn;
    private Series<int> HaSignal;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"HeikenAshi Trend Potential Buy/Sell Signals";
    Name = "HeikenAshiCustom";
    Calculate = Calculate.OnPriceChange;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.UniqueEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 1200;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlatSynchronizeAccount;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    HaClose = new Series<double>((Open + High + Low + Close) / 4);
    HaOpen = new Series<double>((HaOpen[1] + HaClose[1]) / 2);
    HaTrendUp = new Series<bool>(HaClose >= HaOpen);
    HaTrendDn = new Series<bool>(HaClose < HaOpen);
    HaSignal = new Series<int>(HaTrendUp == true && HaTrendUp[1] == true ? 1 : HaTrendDn == true && HaTrendDn[1] ? -1 : 0);
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    // LongEntrySingal
    if (HaSignal[1] == 0 && HaSignal == 1)
    {
    Draw.ArrowUp(this, @"HeikenAshi Arrow up_1", false, 0, 0, Brushes.Lime);
    }
    // CloseLongSignal
    if (HaSignal[1] == 1 && HaSignal == 0)
    {
    Draw.Dot(this, @"HeikenAshi Dot_1", false, 0, 0, Brushes.Yellow);
    }
    //ShortEntrySignal
    if (HaSignal[1] == 0 && HaSignal == -1)
    {
    Draw.ArrowDown(this, @"HeikenAshi Arrow down_1", false, 0, 0, Brushes.Red);
    }
    //CloseShortSignal
    if (HaSignal[1] == -1 && HaSignal == 0)
    {
    Draw.Dot(this, @"HeikenAshi Dot_1", false, 0, 0, Brushes.Yellow);
    }
    }
    }
    }
    Attached Files

    #2
    Hello j0sephk3nt,

    If you are getting an error message you would like assistance with, please provide the full error message as this will contain the details of what is wrong and what to correct.

    You can click on an error message and Ctrl + c to copy and Ctrl + v to paste in your response.

    Also, if this is a compile error, please provide the line of code specified in the error message.


    I see some mistakes in your code.

    HaClose = new Series<double>((Open + High + Low + Close) / 4);

    The Open, High, Low, and Close, are series. These cannot be used in OnStateChange(). Use these in OnBarUpdate().

    In OnStateChange():
    HaClose = new Series<double>(this);

    In OnBarUpdate():
    HaClose[0] = (Open[0] + High[0] + Low[0] + Close[0]) / 4;

    https://ninjatrader.com/support/help...t8/seriest.htm
    https://ninjatrader.com/support/help.../nt8/close.htm


    Last edited by NinjaTrader_ChelseaB; 07-06-2022, 09:55 AM.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    50 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    126 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    69 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X