NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > Application Technical Support > Automated Trading

Automated Trading Support for automated trading systems using NinjaScript. Support for our ATI (Automated Trading Interface) used to link an external application such as TradeStation and eSignal to NinjaTrader.

Reply
 
Thread Tools Display Modes
Old 03-26-2012, 01:53 PM   #1
kenb2004
Senior Member
 
Join Date: Jul 2010
Posts: 415
Thanks: 1
Thanked 4 times in 4 posts
Default Unique Entries?

I have a strategy with 2 unique entries: Buy_1 and Buy_2. Each uses a different set of conditions to enter. How can I specify which Entry is Long when using a condition like?

if (Position.MarketPosition == MarketPosition.Long)

Do something;

thanks
kenb2004 is offline  
Reply With Quote
Old 03-26-2012, 02:10 PM   #2
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello kenb2004,
Thanks for writing in and I am happy to assist you.

Unfortunately MarketPosition will not return the position of any particular order.
To know the state of the individual order you can compare the IOrders.

For example:

in variable
Code:
IOrder Buy_1 = null;
IOrder Buy_2 = null;
in OnBarUpdate
Code:
if (condition)
{
  Buy_1 = EnterLong("Buy_1");
  Buy_2 = EnterLong("Buy_2");
}

if (Position.MarketPosition == MarketPosition.Long && Buy_1 != null &&  Buy_1.OrderState == OrderState.Filled)
{
   //do something
}
http://www.ninjatrader.com/support/h...tml?iorder.htm

Please let me know if I can assist you any further.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 03-26-2012, 02:23 PM   #3
kenb2004
Senior Member
 
Join Date: Jul 2010
Posts: 415
Thanks: 1
Thanked 4 times in 4 posts
Default

if (Position.MarketPosition == MarketPosition.Long && Buy_1 != null && Buy_1.OrderState == OrderState.Filled)

This appears to be watching a moment in time when the entry for Buy_1 was filled but not the continued state of the Long Position of Buy_1. I am presently using "buy1entry" IOrder as the entry order for the Long Position Buy_1, but I need a condition that monitors the Long Position Buy_1 for it's duration, not just it's fill.
thanks
kenb2004 is offline  
Reply With Quote
Old 03-26-2012, 02:46 PM   #4
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello kenb2004,
Yes, there will be a time lag till the order gets filled. However you can also create a market position field for it

in variable
Code:
MarketPosition Buy_1MarketPosition = MarketPosition.Flat;
in OnBarUpdate
Code:
IOrder Buy_1 = EnterLong("Buy_1");
Buy_1MarketPosition = MarketPosition.Long;

if (Buy_1MarketPosition == MarketPosition.Long)
{
  //do something
}
This way you wont have any lag, but do remember to take care if the order gets cancelled/rejected etc or when the position gets flats.

Please let me know if I can assist you any further.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 03-26-2012, 02:55 PM   #5
kenb2004
Senior Member
 
Join Date: Jul 2010
Posts: 415
Thanks: 1
Thanked 4 times in 4 posts
Default

Am I not understanding your code correctly? If an entry order is filled does that mean there is an open position until an exit order is filled to close the position? Or does that mean that the entry order was filled, period?
Last edited by kenb2004; 03-26-2012 at 02:59 PM.
kenb2004 is offline  
Reply With Quote
Old 03-26-2012, 03:03 PM   #6
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello kenb2004,
If an entry order (IOrder object) is in filled state it means that the order was filled, period

Please let me know if I can assist you any further.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 03-26-2012, 03:07 PM   #7
kenb2004
Senior Member
 
Join Date: Jul 2010
Posts: 415
Thanks: 1
Thanked 4 times in 4 posts
Default

OK, thanks. It looks like the:

MarketPosition Buy_1MarketPosition = MarketPosition.Flat

is the answer to my initial question.

Thank you...
kenb2004 is offline  
Reply With Quote
Old 03-26-2012, 03:09 PM   #8
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello kenb2004,
Glad to know you got a solution.

Please let me know if I can assist you any further.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 03-27-2012, 02:38 PM   #9
kenb2004
Senior Member
 
Join Date: Jul 2010
Posts: 415
Thanks: 1
Thanked 4 times in 4 posts
Default

Joydeep

As to your last statement "remember to take care if the order gets cancelled/rejected etc", since in managed approach NinjaTrader cancels working orders INTERNALLY "from order" Buy_1, how would I make
Buy_1MarketPosition = MarketPosition.Flat;
after the trade's round trip is complete?
thanks
Last edited by kenb2004; 03-27-2012 at 02:44 PM.
kenb2004 is offline  
Reply With Quote
Old 03-27-2012, 02:46 PM   #10
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello kenb2004,
After the round trip, you can reset the Buy_1MarketPosition in the OnExecution event

Code:
protected override void OnExecution(IExecution execution)
{
	
	if (execution.Order != null && execution.Order.OrderState == OrderState.Filled && Buy_1 != null && execution.Order == Buy_1)
	{
		Buy_1MarketPosition == MarketPosition.Flat;
	}
}
Please let me know if I can assist you any further.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 03-27-2012, 02:59 PM   #11
kenb2004
Senior Member
 
Join Date: Jul 2010
Posts: 415
Thanks: 1
Thanked 4 times in 4 posts
Default

Thanks for your assistance but I'm confused with the duplication of Buy_1. To clarify in my strategy "buy1entry" is the IOrder and Buy_1 is the Open Long Position. In your example where would my "buy1entry" IOrder go and where would the Buy_1 Open Position which is now closed go?
thanks
kenb2004 is offline  
Reply With Quote
Old 03-27-2012, 03:31 PM   #12
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello kenb2004,
In my sample code Buy_1 is the IOrder object and Buy_1MarketPosition is the MarketPosition. I only demonstrated how to code it.

Since I am not aware of the exact naming of the objects in your strategy, please modify the code accordingly to suit your needs.

Please let me know if I can assist you any further.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 03-29-2012, 09:24 AM   #13
kenb2004
Senior Member
 
Join Date: Jul 2010
Posts: 415
Thanks: 1
Thanked 4 times in 4 posts
Default

below is how I am attempting to monitor Buy_1MarketPosition.

protectedoverridevoid OnExecution(IExecution execution)
{
#region buy1entry // *****************************************
// If Long set buy1entry to null
if (buy1entry != null && buy1entry == execution.Order) {
if (execution.Order.OrderState == OrderState.Filled
|| execution.Order.OrderState == OrderState.PartFilled
|| (execution.Order.OrderState == OrderState.Cancelled
&& execution.Order.Filled >
0)){
if (execution.Order.OrderState != OrderState.PartFilled){
buy1entry =
null;
buy1target = ExitLongLimit(
0, true, 1, Position.AvgPrice + PT * TickSize, "Buy_1_Profit", "Buy_1");
buy1stop = ExitLongStop (
0, true, 1, Position.AvgPrice - ST * TickSize, "Buy_1_Stop", "Buy_1");
Buy_1MarketPosition = MarketPosition.Long;
// Unique entry is long
} } }
#endregion

But I do not see how your code can possibly make Buy_1MarketPosition Flat.

protected override void OnExecution(IExecution execution)
{

if (execution.Order != null && execution.Order.OrderState == OrderState.Filled && Buy_1 != null && execution.Order == Buy_1)
{
Buy_1MarketPosition == MarketPosition.Flat;
}
}

Isn't your code saying, "if the execution order is not a working order, because the order was filled, and the buy1entry IOrder is not a working order, and buy1entry IOrder is the execution order"......

How does that make Buy_1MarketPosition Flat??? It seems to me that all your code is saying is the same as mine, that "buy1entry=null". Not that Buy_1MarketPosition is Flat.

Thanks
Last edited by kenb2004; 03-29-2012 at 09:27 AM.
kenb2004 is offline  
Reply With Quote
Old 03-29-2012, 09:47 AM   #14
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello kenb2004
The below code sets the buy1entry to null and resets the Buy_1MarketPosition to flat.

Code:
//if buy1entry is long and fully filled then nullify it.
if (execution.Order != null && execution.Order == buy1entry && Buy_1MarketPosition == MarketPosition.Long && execution.Order.OrderState == OrderState.Filled)
{
  buy1entry = null;
  Buy_1MarketPosition == MarketPosition.Flat;  //reset Buy_1MarketPosition to flat
  
}
The above is only a demonstrates how to get the information from the Buy_1MaketPosition. Please modify it accordingly.

Please let me know if I can assist you any further.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 03-29-2012, 11:29 AM   #15
kenb2004
Senior Member
 
Join Date: Jul 2010
Posts: 415
Thanks: 1
Thanked 4 times in 4 posts
Default

Ok, since all I'm looking for is how to monitor the Buy_1MarketPosition, would the following code accomplish this?

#region OnExecution //**********************************************
protectedoverridevoid OnExecution(IExecution execution)
{
#region buy1entry // *****************************************
// If Long set buy1entry to null
if (buy1entry != null && buy1entry == execution.Order) {
if (execution.Order.OrderState == OrderState.Filled
|| execution.Order.OrderState == OrderState.PartFilled
|| (execution.Order.OrderState == OrderState.Cancelled
&& execution.Order.Filled >
0)){
if (execution.Order.OrderState != OrderState.PartFilled){
buy1entry =
null;
buy1target = ExitLongLimit(
0, true, 1, Position.AvgPrice + PT * TickSize, "Buy_1_Profit", "Buy_1");
buy1stop = ExitLongStop (
0, true, 1, Position.AvgPrice - ST * TickSize, "Buy_1_Stop", "Buy_1");
Buy_1MarketPosition = MarketPosition.Long;
// Unique entry is long
} }
//if Buy_1MarketPosition WAS long and NOW filled, then Buy_1MarketPosition IS Flat
if (Buy_1MarketPosition == MarketPosition.Long && execution.Order.OrderState == OrderState.Filled)
{
Buy_1MarketPosition = MarketPosition.Flat;
//reset Buy_1MarketPosition to flat
}
}
#endregion

thanks I appreciate all your help!
Last edited by kenb2004; 03-29-2012 at 11:36 AM.
kenb2004 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
Entries per direction: difference between all and unique ch9090 Strategy Analyzer 10 03-15-2012 05:13 PM
unique entries kenb2004 Automated Trading 3 03-05-2012 11:41 AM
Cumulative Profits for unique entries. tjendra Strategy Analyzer 5 06-12-2009 07:15 AM
Unigue Entries with unique responses edgeliner Strategy Development 2 02-20-2009 07:38 AM
Chart Trader: Condition based entries, time-axis entries, multiple OCO pairs etc. Elliott Wave Suggestions And Feedback 1 11-25-2008 03:23 AM


All times are GMT -6. The time now is 08:35 PM.