![]() |
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Member
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
|
Need some help here. Getting the error "Object reference not set to an instance of an object" in the log. This is all I get in the output window...all of which printed between 83000 and 83030:
WITHIN ONBARUPDATE WITHIN ONBARUPDATE WITHIN BARSINPROGRESS = 1 SECTION The OnBarUpdate section then stops running and I get the error message. I'm applying this on a 1 min chart and adding a secondary bars set to tick by tick. [code] protectedoverridevoid Initialize() { Add(PeriodType.Tick, 1); Add(SMA(Period)); Add(BarTimer()); TraceOrders = true; CalculateOnBarClose = true; } protectedoverridevoid OnBarUpdate() { Print("WITHIN ONBARUPDATE"); if (BarsInProgress == 0) return; if (BarsInProgress == 1) { Print("WITHIN BARSINPROGRESS = 1 SECTION"); if (ToTime(Time[0]) >= MarketOpen && ToTime(Time[0]) < MarketClose) { Print("WITHIN MARKET HOURS SECTION"); if (myEntryOrder == null && Position.MarketPosition == MarketPosition.Flat) { //Buy if current tick is above SMA of primary bars previous bar Print("WITHIN ENTRY CONDITIONS SECTION"); if (Close[0] > SMA(BarsArray[0], Period)[1]) { myEntryOrder = EnterLongLimit(1, true, 100, Closes[0][1], "Buy"); SetStopLoss("Buy",CalculationMode.Ticks, 10, false); barNumberOfOrder = CurrentBar; Print("WITHIN ENTRY CONDITIONS SATISFIED SECTION"); } } elseif (myEntryOrder != null && Position.MarketPosition == MarketPosition.Flat) { Print("WITHIN ENTRY CONDITIONS SECTION 2"); if (Close[0] > SMA(BarsArray[0], Period)[1]) { myEntryOrder = EnterLongLimit(1, true, 100, Closes[0][1], "Buy"); SetStopLoss("Buy",CalculationMode.Ticks, 10, false); barNumberOfOrder = CurrentBar; Print("WITHIN ENTRY CONDITIONS SATISFIED SECTION 2"); } } } } if (CurrentBar > barNumberOfOrder + 2) CancelOrder(myEntryOrder); } protectedoverridevoid OnOrderUpdate(IOrder order) { if (myEntryOrder != null && myEntryOrder.Token == order.Token) { Print(order.ToString()); if (order.OrderState == OrderState.Filled) myEntryOrder = null; } } |
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
|
Hello,
Try commenting out lines of code and conditions until it progresses, then you will know what is the cause. You might want to start by commenting out this line since the Print() right above it was the last thing to work: if (ToTime(Time[0]) >= MarketOpen && ToTime(Time[0]) < MarketClose)
Ben
NinjaTrader Customer Service |
|
|
|
|
|
#3 | |
|
Member
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
|
Quote:
I've attached the strategy. If someone could try it and see if they get the same results that would be helpful. First time trying to do a Multi-time frame strategy, so I'm sure I'm just missing something. Although, most of the code in the attached strategy was copied right out of NT reference samples. Thanks, kc |
|
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
|
Hello,
Print() your MarketOpen and MarketClose and your ToTime() values just prior to the condition and see what values are being used.
Ben
NinjaTrader Customer Service |
|
|
|
|
|
#5 | |
|
Member
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
|
Quote:
Code:
if (myEntryOrder == null && Position.MarketPosition == MarketPosition.Flat) Does it have something to do with == null? Or perhaps something in the code under that line where I'm asking for information about a certain bar and I don't have that many bars yet or something? Thanks, kc |
|
|
|
|
|
|
#6 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
Here is likely your problem.
if (CurrentBar > barNumberOfOrder + 2) CancelOrder(myEntryOrder); You can't call cancel order on a null IOrder object. You need to check for null before.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#7 | |
|
Member
Join Date: Dec 2008
Location: Kansas City
Posts: 90
Thanks: 0
Thanked 2 times in 2 posts
|
Quote:
That was it. I have my real strategy working now. As usual, thanks for the great support. |
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| ...instance of an object error? | Burga1 | General Programming | 12 | 05-05-2008 04:03 PM |
| Failed to call method "Initialize" for indicator 'Test': Object reference not set to | clearpicks | Indicator Development | 3 | 04-23-2008 12:53 PM |
| Object reference... | ATI user | Strategy Development | 3 | 04-08-2008 06:46 AM |
| Object not set to an instance of an object | ATI user | Historical NinjaTrader 6.5 Beta Threads | 1 | 12-14-2007 03:35 AM |
| Error: Object reference not set to an instance of an object. | Januson | Market Analyzer | 1 | 05-18-2007 12:12 AM |