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

Object reference error

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

    Object reference error

    Hello, I'm getting the Error on calling 'OnBarUpdate' method on bar 2453: Object reference not set to an instance of an object, when I run this very simple code, any idea? thanks!

    Random rnd = new Random();

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    //if(CurrentBar < 50) return;

    if (CurrentBars[0] < 10)
    return;

    // only process real-time OnBarUpdate events
    if (State == State.Historical)
    return;

    if (rnd == null)
    return;


    R = rnd.Next(1, 10);

    // Set 1
    if ((R == 1) && (Position.MarketPosition == MarketPosition.Flat))
    {
    Print(@"COMPRA, R = " + Convert.ToString(R));
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }

    // Set 2
    if ((R == 2) && (Position.MarketPosition == MarketPosition.Flat))
    {
    Print(@"VENTA, R = " + Convert.ToString(R));
    EnterShort(Convert.ToInt32(DefaultQuantity), "");
    }

    // Set 3
    if ((R != 1)
    && (R != 2))
    {
    Print(@"R = " + Convert.ToString(R));
    }

    }

    #2
    Hello federicoo,

    The error you are seeing means an object was null.

    I tried the code but don't see what the error may be.

    The best way to track down this type of error in a simple code like you provided would be to just comment out the code and then uncomment sections of the code to find which specific part is throwing the error. Once you find the line in question we could review that together if its not apparent what the problem is.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Irukandji, Today, 04:58 AM
    0 responses
    2 views
    0 likes
    Last Post Irukandji  
    Started by fitspressoburnfat, Today, 04:25 AM
    0 responses
    2 views
    0 likes
    Last Post fitspressoburnfat  
    Started by Skifree, Today, 03:41 AM
    1 response
    4 views
    0 likes
    Last Post Skifree
    by Skifree
     
    Started by usazencort, Today, 01:16 AM
    0 responses
    1 view
    0 likes
    Last Post usazencort  
    Started by kaywai, 09-01-2023, 08:44 PM
    5 responses
    604 views
    0 likes
    Last Post NinjaTrader_Jason  
    Working...
    X