![]() |
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Mar 2009
Posts: 109
Thanks: 0
Thanked 0 times in 0 posts
|
Hi guys,
I have a strategy that works as follows (without all the necessary parenthesis stuff): public class Strategy123 { int StoppedOut = 0; protected override void OnBarUpdate() { if StoppedOut == 1 {Say "You are stopped out" or do nothing} else if StoppedOut == 0 --> set up a stop loss STPLMT order "SL123" } protected override void OnExecution(Iexecution execution) { if SL123.Token == execution.Order.Token {StoppedOut = 1; DrawText(I am stopped out);} } } What happens is: The Stop Loss order SL123 gets set up correctly within the OnBarUpdate() method and eventually triggers and executes correctly. The system then correctly recognizes via OnExecution that the SL123 order has been executed and amends the value of the StoppedOut variable to 1. However, once the next bar updates and OnBarUpdate() should now see the StoppedOut variable as 1, it simply does not do so. Instead, for example if I tell it to DrawText using the now-should-be-1-StoppedOut variable, it simply does not draw any text; it seems to have "lost" the variable. Any info about how I can tell the system to "memorize" the change in the StoppedOut variable, so that the next OnBarUpdate() can make use of it? Thank you all for taking the time to read. Best regards, noincomenojobnoassets |
|
|
|
|
|
#2 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
Print the value of your variable on each step of the way. Remember these things can be running on separate threads so the next OnBarUpdate() simply triggered too early.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Mar 2009
Posts: 109
Thanks: 0
Thanked 0 times in 0 posts
|
Hi Josh,
Thanks for answering. When I code a DrawText saying "Stoppedout" + StoppedOut into OnExecution, it correcty draws the text "Stoppedout1" into the chart. Therefore, within the OnExecution method it seems to know the "new" value of 1 once the position gets stopped out. Or does printing the current value of the variable help anything in terms of letting the OnBarUpdate method know the current value? Coming to your idea of the OnBarUpdate triggering too early: Even when I get stopped out "early", with still a few minutes to go until the next OnBarUpdate triggers, the strategy seems to "lose" the variable. I don't get any sign that OnBarUpdate has an idea about the variable StoppedOut being set to 1 now, not even that StoppedOut exists anymore. Similar DrawText attempts as set out above don't work... Any more ideas? Help is, as always, greatly appreciated. Thanks, n.i.n.j.n.a. |
|
|
|
|
|
#4 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
Not sure I follow what you mean exactly, but once you store a value into a variable, it is accessible throughout any method regardless of if that is OnExecution or OnBarUpdate.
You should be creating these as private variables in the variable region instead of declaring them inside the method. If you declared them inside the method then they won't be accessible in other methods. If you declare them in the variables region of the code then they will be accessible.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Senior Member
Join Date: Mar 2009
Posts: 109
Thanks: 0
Thanked 0 times in 0 posts
|
Hi Josh,
I'm creating the variable outside the methods - this is not the problem. Printing the variable's value actually STOPS after it has been set to 1 by the OnExecution method - after correctly printing "The variable is now 1" from the OnExecution method, all the following Print statements within the next OnBarUpdate simply do not trigger anymore. The strategy seems to dislike the variable being given a different value in the OnExecution method - when I have it changed within the OnBarUpdate method, it correctly refers to the new value afterwards. When I have it changed in the OnExecution method, the system somehow loses it. It appears the strategy stops - not even a Print("Hello") command is executed in the OnBarUpdate method once the variable's value has been set to 1 in the OnExecution method. Any suggestions? =/ |
|
|
|
|
|
#6 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
It doesn't matter where you set variables, it is all the same. You will need to debug why your strategy stops working. Please check your Control Center logs for errors.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#7 |
|
Senior Member
Join Date: Mar 2009
Posts: 109
Thanks: 0
Thanked 0 times in 0 posts
|
It says
Error on calling the 'OnExecution' method for strategy '<strategyname>': Object reference not set to an instance of an object. |
|
|
|
|
|
#8 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
Have you checked for null before trying to access IOrders and such? http://www.ninjatrader-support2.com/...ead.php?t=4226
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#9 |
|
Senior Member
Join Date: Mar 2009
Posts: 109
Thanks: 0
Thanked 0 times in 0 posts
|
For the Stop Loss order, I have a CancelOrder command in the OnBarUpdate method that checks for null before cancelling the order. (The order is then set up and transmitted again further on in the OnBarUpdate code).
Hm, as to the remaining code, I can't exactly tell, since I can't access it from here now - will check and let you know tomorrow. Thanks so far! |
|
|
|
|
|
#10 |
|
Senior Member
Join Date: Mar 2009
Posts: 109
Thanks: 0
Thanked 0 times in 0 posts
|
That did it!
I wasn't checking for null inside the OnExecution method - it started with if SL123.Token == execution.Order.Token... After I included an if SL123 != null check before the Token check, it all started running properly and smoothly all of a sudden! ![]() Thank you very mich for your help, Josh - you damn sure know your stuff! Thumbs up! Will let you know in case not everything works out as expected. Thanks, n.i.n.j.n.a. *** SOLVED *** |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How can access a variable defined in other section | jrbolsa | General Programming | 6 | 04-06-2009 12:42 PM |
| access last bar data in OnBarUpdate() method | randomcz | Indicator Development | 1 | 02-22-2009 11:55 PM |
| Access seconds from minute bar data in OnBarUpdate | shawnj | General Programming | 1 | 12-09-2008 05:07 AM |
| access variable outside of if block? | auspiv | General Programming | 6 | 07-13-2008 07:52 PM |
| Best variable to access to find last order entry price? | scriabinop23 | General Programming | 3 | 02-14-2008 03:04 PM |