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 not set to an instance of an object

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

    Object reference not set to an instance of an object

    Hello,
    I created a strategy that goes against the trend. Works well until the market is trending. When trending I will be fill numerous times for loses until the trend is over. I'm attempting to fix that problem by not allowing another trade after my stoploss is hit and the market 'resets' and I'm attempting to use the IOrder() to accomplish this. Here is the logic:

    private bool m_stopLos****Long = false;
    private IOrder m_longOrder = null;
    private bool m_longOrderFilled = false;

    protected override void Initialize()
    SetStopLoss("Long", CalculationMode.Ticks, 25, false);
    SetStopLoss("Short", CalculationMode.Ticks, 25, false);

    protected override void OnBarUpdate()
    if (Position.MarketPosition == MarketPosition.Flat && m_longOrderFilled)
    {
    m_stopLos****Long = true;
    m_longOrderFilled = false;
    }
    if (m_stopLos****Long && !CrossBelow(DM(10).DiMinus, DM(10).DiPlus, 1))//Stoploss hit but normal exit condition has not been called yet
    {
    return;
    }
    else if (m_stopLos****Long && CrossBelow(DM(10).DiMinus, DM(10).DiPlus, 1))//Stoploss hit and exit condition signaled
    m_stopLos****Long = false;

    // VCR Long Entry
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    if (RValueCharts(Color.Blue, 3, 3).VClose[0] <= -4.4
    && Position.MarketPosition == MarketPosition.Flat
    && TrendStrengthA(VC.NinjaScript.Utility.MovingAverag eType.VWMA, 200, 20, 10).TrendStrength[0] < 0)
    {
    m_longOrder = EnterLong(DefaultQuantity, "Long");
    m_longOrderFilled = false;
    }
    }
    else
    {
    // VCR Long Exit
    if (CrossBelow(DM(10).DiMinus, DM(10).DiPlus, 1))

    {
    ExitLong("Exit VCR Long", "Long");
    m_longOrderFilled = false;
    }
    }

    protected override void OnOrderUpdate(IOrder order)
    {
    if (m_longOrder.OrderId == order.OrderId)
    {
    if (order.OrderState== OrderState.PartFilled || order.OrderState == OrderState.Filled)
    {
    m_longOrderFilled = true;
    }
    }

    This works on the long side.
    The problem is when I try the same template on the short side. It compiles, but no trades fire and I get an error in the log stating:
    "Object reference not set to an instance of an object".


    When I take one side alone it works. When I do both sides I get that error. Any suggestions?

    #2
    Hello CaptainAmericaXX,

    There is needed structure anytime you work with IOrders, for both checking null reference before submitting, as well as resetting back to null when the order is filled or cancelled. Your code snippet is currently missing this structure. For help you can refer to the IOrders page here:


    There are also a couple samples that use IOrders:
    The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()

    When using NinjaTrader's Enter() and Exit() methods, the default behavior is to automatically expire them at the end of a bar unless they are resubmitted to keep them alive. Sometimes you may want more flexibility in this behavior and wish to submit orders as live-until-cancelled. When orders are submitted as live-until
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      You have not added your indicators to the strategy, before you are calling their property values.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Segwin, 05-07-2018, 02:15 PM
      14 responses
      1,788 views
      0 likes
      Last Post aligator  
      Started by Jimmyk, 01-26-2018, 05:19 AM
      6 responses
      837 views
      0 likes
      Last Post emuns
      by emuns
       
      Started by jxs_xrj, 01-12-2020, 09:49 AM
      6 responses
      3,293 views
      1 like
      Last Post jgualdronc  
      Started by Touch-Ups, Today, 10:36 AM
      0 responses
      12 views
      0 likes
      Last Post Touch-Ups  
      Started by geddyisodin, 04-25-2024, 05:20 AM
      11 responses
      62 views
      0 likes
      Last Post halgo_boulder  
      Working...
      X