NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 08-17-2009, 06:02 AM   #1
Shansen
Senior Member
 
Join Date: Jan 2009
Location: Melbourne
Posts: 179
Thanks: 3
Thanked 0 times in 0 posts
Default 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
Shansen is offline  
Reply With Quote
Old 08-17-2009, 06:22 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,559
Thanks: 261
Thanked 1,015 times in 996 posts
Default

Shannon, thanks for the post - could you please clarify what you mean by 'breaking apart'? Do you get any errors in the log tab?
NinjaTrader_Bertrand is online now  
Reply With Quote
Old 08-18-2009, 03:59 AM   #3
Shansen
Senior Member
 
Join Date: Jan 2009
Location: Melbourne
Posts: 179
Thanks: 3
Thanked 0 times in 0 posts
Default

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
Shansen is offline  
Reply With Quote
Old 08-18-2009, 07:14 AM   #4
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,559
Thanks: 261
Thanked 1,015 times in 996 posts
Default

Shansen, with this error message in mind, please doublecheck your code to ensure you don't attempt to access an empty object.
NinjaTrader_Bertrand is online now  
Reply With Quote
Old 08-19-2009, 05:29 AM   #5
Shansen
Senior Member
 
Join Date: Jan 2009
Location: Melbourne
Posts: 179
Thanks: 3
Thanked 0 times in 0 posts
Default

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
Shansen is offline  
Reply With Quote
Old 08-19-2009, 06:02 AM   #6
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,559
Thanks: 261
Thanked 1,015 times in 996 posts
Default

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....
NinjaTrader_Bertrand is online now  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
BarsSinceEntry in IOrder strategy structure Shansen Strategy Development 1 06-18-2009 06:43 AM
Coding structure questions dirtybrown Indicator Development 2 05-26-2009 06:35 AM
Data structure xewoox Strategy Development 1 03-02-2009 02:00 PM
Using IOrder in a Strategy clfield General Programming 7 12-01-2008 03:46 PM
Fee structure for V6 Antraman Installation and Licensing 1 12-18-2006 01:53 PM


All times are GMT -6. The time now is 02:46 AM.