Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Alerts and printing in an IOrder strategy structure

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

    Alerts and printing in an IOrder strategy structure

    NT Team,

    I'm trying to better understand how a strategy I've built is operating. The strategy is constructed using the IOrder structure including "OnOrderUpdate(IOrder order)", "OnExecution (IExecution execution)", and OnPositionUpdate(IPosition position) elements.

    When Print commands or Alerts are inserted into these elements the strategy seems to fall apart. It should be noted, I've created two near identical strategies, one for backtesting and one for live trading. The only difference is the backtesting strategy calculates on bar close, while the live strategy calculates within the last three seconds before a bar ends.

    There is clearly a gap in my understanding. Any insight on why these commands are causing such grief when placed in these elements of the code?

    As always, thanks
    Shannon

    #2
    Shannon, thanks for the post - could you please clarify what you mean by 'breaking apart'? Do you get any errors in the log tab?

    Comment


      #3
      Bertrand,

      Apologies for the lack of clarity.

      The strategy in question contains the below segment of code (excluding the Italicized Print lines). The code operates (i.e. runs successfully) but does not yield the expected results. The Italicized Print lines were added to help me understand what's happening and make amendments were necessary.
      // Long position TIME PERIOD, raise the stop-loss order to half of entry bar range and the profit-target order to breakeven.
      if (Position.MarketPosition == MarketPosition.Long && BarsSinceEntry(0, "Long", 0) >= 6)
      {
      Print ("Long BarsSinceEntry >= 6");
      Print ("stopOrder.ToString()" + stopOrder.ToString());

      // Checks to see if our Stop Order has been submitted already
      if (stopOrder != null && stopOrder.StopPrice < Position.AvgPrice)
      {
      stopOrder = ExitLongStop(0, true, stopOrder.Quantity, ((LongEntryLow + LongEntryHigh) / 2), "ModifiedLongStop", "Long");
      //targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice, "ModifedLongTarget", "Long");
      }
      }

      With the Italicized Print lines added, the strategy analyser runs until the first instance where the above code would be called, where a long position is six or more bars since entry. At this point the strategy stops (the last position remains open) and the following error appears in the log "Error on calling 'OnBarUpdate' method for strategy 'LivePPT2': Object reference not set to an instance of an object.".

      I'm sure I'm missing something, I've tried different syntax to return information on the stopOrder, in place of stopOrder.ToString(), but no luck.

      Any guidance would be appreciated.
      Regards
      Shannon

      Comment


        #4
        Shansen, with this error message in mind, please doublecheck your code to ensure you don't attempt to access an empty object.

        Comment


          #5
          Bertrand,

          Thanks for the advice. It look quite some looking, but I uncovered the problem. Thanks

          Now I've corrected that problem and have a better understanding of the below code... I think what I was trying to do will not work. To frame my question, in the strategy when a long trade is entered a stoploss is placed at the low of the entry bar. In the event a trade has been in effect for 6 or more bars, I wished to move the stoploss up to the entry bar midpoint (i.e. (LongEntryLow + LongEntryHigh) / 2).

          I am unsure whether I can do this or need to check to ensure the CurrentBid is greater than this level first?

          Or, whether it would be better to state if a trade has been in effect for 6 or more bars and the CurrentBid is below entry bar midpoint ExitLong. If this is possible will existing Stop Orders be cancelled in the IOrder strategy structure.

          I'm sure this problem is on the ropes.
          Any guidance would be appreciated.
          Regards
          Shannon

          Comment


            #6
            Shansen, yes monitoring the CurrentBid / CurrentAsk is good to ensure those stops do not get rejected as you place them, you can of course also exit those with ExitLong / ExitShort as your Exit condition triggers, though you might need to test which gives you better fills....

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            650 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            370 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            109 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            574 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            577 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X