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

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 DanielTynera, Today, 01:14 AM
    0 responses
    2 views
    0 likes
    Last Post DanielTynera  
    Started by yertle, 04-18-2024, 08:38 AM
    9 responses
    40 views
    0 likes
    Last Post yertle
    by yertle
     
    Started by techgetgame, Yesterday, 11:42 PM
    0 responses
    12 views
    0 likes
    Last Post techgetgame  
    Started by sephichapdson, Yesterday, 11:36 PM
    0 responses
    2 views
    0 likes
    Last Post sephichapdson  
    Started by bortz, 11-06-2023, 08:04 AM
    47 responses
    1,615 views
    0 likes
    Last Post aligator  
    Working...
    X