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 03-25-2011, 11:06 PM   #1
cassb
Senior Member
 
Join Date: Nov 2007
Location: Victor, NY
Posts: 578
Thanks: 5
Thanked 4 times in 4 posts
Send a message via Yahoo to cassb Send a message via Skype™ to cassb
Default Entry Order number vs. Execution Order number

In my strategy, I have this code:

Code:
        protected override void OnExecution(IExecution execution)
        {
             if (EntryOrder != null && EntryOrder == execution.Order)
            {
                if (execution.Order.OrderState == OrderState.Filled)
                { 
[...]
In this strategy, I have a SetStopLoss and SetProfitTarget methods setting a stop and target price for the position. So whenever the target is hit or the stop is hit, I want this code to execute. The problem is that the EntryOrder token and the execution.Order token do not match if the stoploss is hit.

I think I got this code that checks the EntryOrder with the execution.Order from one of your samples. So how do I know that a stoploss execution is part of the original EntryOrder that was filled? Apparently I can't compare the order tokens... is there another way?

Thanks!
Bryan
Price-Dynamics.com
A NinjaTrader Official Partner

Visit http://www.price-dynamics.com to learn more about the Rhythm Relay trading system. Yes, you can discretionary or auto-trade with 75%+ accuracy!

Use our Contact Us page to request free access to our daily Trader's Lounge where you can see the system in action in a live account!
cassb is offline  
Reply With Quote
Old 03-26-2011, 10:49 AM   #2
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 90 times in 82 posts
Default

Bryan, how are you setting the profit targets and stop losses? There is a reference sample that shows how to do this with all IOrders.
NinjaTrader_Austin is offline  
Reply With Quote
Old 03-26-2011, 12:56 PM   #3
cassb
Senior Member
 
Join Date: Nov 2007
Location: Victor, NY
Posts: 578
Thanks: 5
Thanked 4 times in 4 posts
Send a message via Yahoo to cassb Send a message via Skype™ to cassb
Default

Quote:
Originally Posted by NinjaTrader_Austin View Post
Bryan, how are you setting the profit targets and stop losses? There is a reference sample that shows how to do this with all IOrders.
Ah, yes you found it thank you. That's the indicator I copied my logic from. In the sample is this code:

Code:
        protected override void OnExecution(IExecution execution)
        {
            /* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
            which ensures your strategy has received the execution which is used for internal signal tracking. */
            if (entryOrder != null && entryOrder.Token == execution.Order.Token)
            {
                if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                {
I set my stoploss and targets with the SetStopLoss() and SetProfitTarget() methods. These methods produce a new order with a new token. That means that the statement

if (entryOrder != null && entryOrder.Token == execution.Order.Token)


will be false if the stoploss is hit before the profit target. Is there a parameter or something in these methods where I can get it to share the same token as the entry order has?

Bryan
Price-Dynamics.com
A NinjaTrader Official Partner

Visit http://www.price-dynamics.com to learn more about the Rhythm Relay trading system. Yes, you can discretionary or auto-trade with 75%+ accuracy!

Use our Contact Us page to request free access to our daily Trader's Lounge where you can see the system in action in a live account!
cassb is offline  
Reply With Quote
Old 03-26-2011, 01:13 PM   #4
cassb
Senior Member
 
Join Date: Nov 2007
Location: Victor, NY
Posts: 578
Thanks: 5
Thanked 4 times in 4 posts
Send a message via Yahoo to cassb Send a message via Skype™ to cassb
Default

Ural01, if you have some trouble with NT strategies, maybe you can get help here from me or others. I know it can be complicated and do things you don't expect, but there is usually a reason for the issues you're having. And the reason is seldom "NT is piece of junk." ;-) I'm sure there are many people who have used NT and been very successful with good money management, as you say.
Price-Dynamics.com
A NinjaTrader Official Partner

Visit http://www.price-dynamics.com to learn more about the Rhythm Relay trading system. Yes, you can discretionary or auto-trade with 75%+ accuracy!

Use our Contact Us page to request free access to our daily Trader's Lounge where you can see the system in action in a live account!
cassb is offline  
Reply With Quote
Old 03-26-2011, 01:33 PM   #5
ural01
Junior Member
 
Join Date: Mar 2010
Posts: 1
Thanks: 0
Thanked 0 times in 0 posts
Default Ninja junk

I,m not looking for help fom you
i let nfa and cftc help you to count better ballance in chart and dom window and compare to the broket window ballance
i do 80 lots every day so $20 dollars difference on every lot betwen broker price in you cost 80 x $20 = $1800 every day ? Not too match ? A
do you split prifit with mbt 50x50 or just for you nt ?
And how about orders that open just like boom ? I never place any orders , atm of , no pending orders how ?
I,m trading every day 5 days week 18 hours a day from 80 to 150 lots a day , i,m member of nfa and cbot and never see thinks like you do with ballance and extra possitions open



Quote:
Originally Posted by cassb View Post
ural01, if you have some trouble with nt strategies, maybe you can get help here from me or others. I know it can be complicated and do things you don't expect, but there is usually a reason for the issues you're having. And the reason is seldom "nt is piece of junk." ;-) i'm sure there are many people who have used nt and been very successful with good money management, as you say.
ural01 is offline  
Reply With Quote
Old 03-26-2011, 02:01 PM   #6
cassb
Senior Member
 
Join Date: Nov 2007
Location: Victor, NY
Posts: 578
Thanks: 5
Thanked 4 times in 4 posts
Send a message via Yahoo to cassb Send a message via Skype™ to cassb
Default

Quote:
Originally Posted by ural01 View Post
I,m not looking for help fom you
i let nfa and cftc help you to count better ballance in chart and dom window and compare to the broket window ballance
i do 80 lots every day so $20 dollars difference on every lot betwen broker price in you cost 80 x $20 = $1800 every day ? Not too match ? A
do you split prifit with mbt 50x50 or just for you nt ?
And how about orders that open just like boom ? I never place any orders , atm of , no pending orders how ?
I,m trading every day 5 days week 18 hours a day from 80 to 150 lots a day , i,m member of nfa and cbot and never see thinks like you do with ballance and extra possitions open

Good money management is key, and if you are saving $20 per lot, then that's good money management. It sounds like you're doing well with the system you have already, so I would say to keep using what works for you. Good luck!

Bryan
Price-Dynamics.com
A NinjaTrader Official Partner

Visit http://www.price-dynamics.com to learn more about the Rhythm Relay trading system. Yes, you can discretionary or auto-trade with 75%+ accuracy!

Use our Contact Us page to request free access to our daily Trader's Lounge where you can see the system in action in a live account!
cassb is offline  
Reply With Quote
Old 03-26-2011, 10:40 PM   #7
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 90 times in 82 posts
Default

Quote:
Originally Posted by cassb View Post
Ah, yes you found it thank you. That's the indicator I copied my logic from. In the sample is this code:

Code:
        protected override void OnExecution(IExecution execution)
        {
            /* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
            which ensures your strategy has received the execution which is used for internal signal tracking. */
            if (entryOrder != null && entryOrder.Token == execution.Order.Token)
            {
                if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                {
I set my stoploss and targets with the SetStopLoss() and SetProfitTarget() methods. These methods produce a new order with a new token. That means that the statement

if (entryOrder != null && entryOrder.Token == execution.Order.Token)


will be false if the stoploss is hit before the profit target. Is there a parameter or something in these methods where I can get it to share the same token as the entry order has?

Bryan
To get this thread back on track, order tokens can and do change over time. Please set your stop losses and profit targets with specific exit methods, like ExitLongStop() and ExitLongLimit() and such. You can then reference the IOrder method to see which order was filled and take appropriate action.
NinjaTrader_Austin is offline  
Reply With Quote
Old 03-27-2011, 07:09 PM   #8
cassb
Senior Member
 
Join Date: Nov 2007
Location: Victor, NY
Posts: 578
Thanks: 5
Thanked 4 times in 4 posts
Send a message via Yahoo to cassb Send a message via Skype™ to cassb
Default

Thanks Austin, but what if I have two positions open in the same strategy? How do I know which entry a stoploss execution.order is for? If there was some variable or property I could use to tie a stoploss order to its associated entry order, that would be great. Is it maybe the fromEntrySignal parameter in the SetStopLoss method?

If that would tie the stoploss order to the entry order, then when the execution comes in for the stoploss, how do I check to see if the fromEntrySignal matches the proper entry order?

Bryan
Price-Dynamics.com
A NinjaTrader Official Partner

Visit http://www.price-dynamics.com to learn more about the Rhythm Relay trading system. Yes, you can discretionary or auto-trade with 75%+ accuracy!

Use our Contact Us page to request free access to our daily Trader's Lounge where you can see the system in action in a live account!
cassb is offline  
Reply With Quote
Old 03-28-2011, 06:35 AM   #9
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,555
Thanks: 261
Thanked 1,014 times in 995 posts
Default

Bryan, you should be able to check the Execution.Name property to see from which entry signal the execution comes in and then you can make changes to your stops / targets accordingly.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 03-28-2011, 07:01 AM   #10
cassb
Senior Member
 
Join Date: Nov 2007
Location: Victor, NY
Posts: 578
Thanks: 5
Thanked 4 times in 4 posts
Send a message via Yahoo to cassb Send a message via Skype™ to cassb
Default

Quote:
Originally Posted by NinjaTrader_Bertrand View Post
Bryan, you should be able to check the Execution.Name property to see from which entry signal the execution comes in and then you can make changes to your stops / targets accordingly.
I already do that, Bertrand, but the execution.Name property does not tell me which of several entry orders the execution came from:

Code:
if (execution.Name == "Profit target" || execution.Name == "Stop loss" || execution.Name == "Trail stop" || execution.Name == "Close" || execution.Name.Contains("cover") || execution.Name == "Sell")
There is an execution.Order.FromEntrySignal that I will see if it will work. The problem is creating a unique entry signal name that can later be recreated somehow. I can of course make a fixed text signal name like "My Entry", but I'd have to append some unique value to it like the bar number or date/time or something. But then later, when the stoploss is hit, given the IOrder data - how do I determine what the entry's bar number or date/time was to match up the stoploss with the proper entry?

Maybe I'm missing the obvious here -- it seems simple. Maybe I'm making it too complicated?
Price-Dynamics.com
A NinjaTrader Official Partner

Visit http://www.price-dynamics.com to learn more about the Rhythm Relay trading system. Yes, you can discretionary or auto-trade with 75%+ accuracy!

Use our Contact Us page to request free access to our daily Trader's Lounge where you can see the system in action in a live account!
cassb is offline  
Reply With Quote
Old 03-28-2011, 07:53 AM   #11
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,555
Thanks: 261
Thanked 1,014 times in 995 posts
Default

Thanks for clarifying, I misunderstood you - you will need to work with the Exit() methods then offering native IOrder returns, those would include the FromEntrySignal property for an IOrder.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 03-28-2011, 08:07 AM   #12
cassb
Senior Member
 
Join Date: Nov 2007
Location: Victor, NY
Posts: 578
Thanks: 5
Thanked 4 times in 4 posts
Send a message via Yahoo to cassb Send a message via Skype™ to cassb
Default

Quote:
Originally Posted by NinjaTrader_Bertrand View Post
Thanks for clarifying, I misunderstood you - you will need to work with the Exit() methods then offering native IOrder returns, those would include the FromEntrySignal property for an IOrder.
Is there a sample strategy you can point me to that would handle multiple open orders and executions, linking the stop and target executions to their original entry order? I would like to see how this would be done.

Thanks!
Bryan
Price-Dynamics.com
A NinjaTrader Official Partner

Visit http://www.price-dynamics.com to learn more about the Rhythm Relay trading system. Yes, you can discretionary or auto-trade with 75%+ accuracy!

Use our Contact Us page to request free access to our daily Trader's Lounge where you can see the system in action in a live account!
cassb is offline  
Reply With Quote
Old 03-28-2011, 08:18 AM   #13
bukkan
Senior Member
 
Join Date: Feb 2009
Posts: 285
Thanks: 2
Thanked 54 times in 41 posts
Default

in case of multiple orders just create a list (of orders). you can directly compare the orders from the list. from the help files
Quote:
To check for equality you can compare IOrder objects directly
like
Code:
List<IOrder> list = new List<IOrder>();
OnExecution
Code:
if (list.Contains(execution.Order))
{
  //do something
}

this is really a very powerful feature.

as Bertrand said its always a good idea to give the name property a unique value, like

"Buy" + count.ToString(); //count is an integer
bukkan is offline  
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
Option to submit ATM stop loss order at the same time as entry order Softturbo Version 7 Beta General Questions & Bug Reports 1 06-15-2010 04:02 AM
Get ID Order Number MAX Automated Trading 1 02-09-2010 05:23 PM
execution.Order.Filled Vs. execution.Order.Quantity binwang2 General Programming 1 08-04-2009 05:52 AM
Cancelling Order/Entry Order filled cancels all other entry orders ScottB ATM Strategies (Discretionary Trading) 2 06-03-2009 12:19 PM
Why my order.StopPrice alway a Zero number? asalada General Programming 1 03-23-2008 01:25 PM


All times are GMT -6. The time now is 12:56 AM.