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 09-12-2009, 05:18 AM   #1
Argo1
Member
 
Join Date: Sep 2009
Posts: 51
Thanks: 0
Thanked 0 times in 0 posts
Default Reverse position & auto closing previous

When I reverse position then NT's Internal Order Handling mechanism automaticaly closes my current position @ new position's entry price.

How can I close my current position @ another price, for example @ bar's open price?

These are limit orders.
Last edited by Argo1; 09-12-2009 at 05:31 AM.
Argo1 is offline  
Reply With Quote
Old 09-12-2009, 12:32 PM   #2
NinjaTrader_Ben
NinjaTrader Customer Service
 
NinjaTrader_Ben's Avatar
 
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
Default

Hello,

Are you talking about backtesting or in a live environment?

Post your code so I see what you are doing currently.
I'm not 100% sure what you are want and what is happening.
NinjaTrader_Ben is offline  
Reply With Quote
Old 09-13-2009, 10:31 AM   #3
Argo1
Member
 
Join Date: Sep 2009
Posts: 51
Thanks: 0
Thanked 0 times in 0 posts
Default

Backtesting. 1 day bars. Forex market.
I'm no longer using limit orders but stop orders.

In brief:
How to push NT to close long stop position at market price, not at entry price for reversal position, when reversing?


Example:

Day 1:
I went long with EnterLongStop() method. Stop price=156.98. NT State=Filled. - That's OK.

Day 2:
There is a signal fo reversing position (from today's close price).
1) I try to open short position with EnterShortStop() method. Stop price=155.87. Output window:
2000-11-28 00:00:00 Entered internal PlaceOrder() method at 2000-11-28 00:00:00: Action=SellShort OrderType=Stop Quantity=0,1M LimitPrice=0 StopPrice=155,87 SignalName='MyEntryShort' FromEntrySignal=''

2) NT automatically tries to close my long position:
2000-11-28 00:00:00 OnOrderUpdate(): Order='NT-00011/Back101' Name='Close position' State=PendingSubmit Instrument='$GBPJPY' Action=Sell Limit price=0 Stop price=155,87 Quantity=0,1M Strategy='X' Type=Stop Tif=Gtc Oco='' Filled=0 Fill price=0 Token=

3) Close position's order is not going to "Filled" state because today is no market price for this:
Prices for this day bar: O:156.73 H:157.14 L:155.96 C:156.07
Stop price for close position is: 155.87.

Day 3:
Today close long order and open short stop order are cancelled because of expiration.
Prices for this day bar: O:157.28 H:160.74 L:157.23 C:160.43


What I want to have is that NT could close my long position at market price, not at stop price from new short order when I reverse market position.
Long position should be closed at open price for day 3.
Last edited by Argo1; 09-13-2009 at 11:19 AM.
Argo1 is offline  
Reply With Quote
Old 09-13-2009, 10:45 AM   #4
NinjaTrader_Ben
NinjaTrader Customer Service
 
NinjaTrader_Ben's Avatar
 
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
Default

Hello,

Why don't you use ExitLong(), which uses a market sell order? Then enter in the other direction. The internal reversal logic that comes into play when you try to go both ways in the market is not something you can change. It is hard coded in the orders.
NinjaTrader_Ben is offline  
Reply With Quote
Old 09-13-2009, 10:57 AM   #5
Argo1
Member
 
Join Date: Sep 2009
Posts: 51
Thanks: 0
Thanked 0 times in 0 posts
Default

I tried ExitLong() before, but it doesn't work well:
It closes long at day 3. open price - this is good, but it does not opens short position after this.
I put ExitLong() in OnBarUpdate(). Maybe I should put it elsewhere?
Argo1 is offline  
Reply With Quote
Old 09-13-2009, 11:17 AM   #6
Argo1
Member
 
Join Date: Sep 2009
Posts: 51
Thanks: 0
Thanked 0 times in 0 posts
Default

This is log for this "reversal" day:

Code:
Cancelled custom managed order at 2000-11-28 00:00:00: Order='NT-00009/Back101' Name='MyTargetLong' State=Working Instrument='$GBPJPY' Action=Sell Limit price=159,98 Stop price=0 Quantity=0,1M Strategy='X' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='02cf63bf921841819c803a6e43e64e15' Gtd='2099-12-01 00:00:00'
2000-11-28 00:00:00 Entered internal PlaceOrder() method at 2000-11-28 00:00:00: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
2000-11-28 00:00:00 Entered internal PlaceOrder() method at 2000-11-28 00:00:00: Action=SellShort OrderType=Stop Quantity=0,1M LimitPrice=0 StopPrice=155,87 SignalName='MyEntryShort' FromEntrySignal=''
2000-11-28 00:00:00 OnOrderUpdate(): Order='NT-00009/Back101' Name='MyTargetLong' State=PendingCancel Instrument='$GBPJPY' Action=Sell Limit price=159,98 Stop price=0 Quantity=0,1M Strategy='X' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='02cf63bf921841819c803a6e43e64e15' Gtd='2099-12-01 00:00:00'
2000-11-28 00:00:00 OnOrderUpdate(): Order='NT-00009/Back101' Name='MyTargetLong' State=Cancelled Instrument='$GBPJPY' Action=Sell Limit price=159,98 Stop price=0 Quantity=0,1M Strategy='X' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='02cf63bf921841819c803a6e43e64e15' Gtd='2099-12-01 00:00:00'
2000-11-28 00:00:00 OnOrderUpdate(): Order='NT-00010/Back101' Name='Sell' State=PendingSubmit Instrument='$GBPJPY' Action=Sell Limit price=0 Stop price=0 Quantity=0 Strategy='X' Type=Market Tif=Gtc Oco='' Filled=0 Fill price=0 Token='4d7ce5d907e64efaa81bb4455143af21' Gtd='2099-12-01 00:00:00'
2000-11-28 00:00:00 OnOrderUpdate(): Order='NT-00010/Back101' Name='Sell' State=Accepted Instrument='$GBPJPY' Action=Sell Limit price=0 Stop price=0 Quantity=0 Strategy='X' Type=Market Tif=Gtc Oco='' Filled=0 Fill price=0 Token='4d7ce5d907e64efaa81bb4455143af21' Gtd='2099-12-01 00:00:00'
2000-11-28 00:00:00 OnOrderUpdate(): Order='NT-00010/Back101' Name='Sell' State=Working Instrument='$GBPJPY' Action=Sell Limit price=0 Stop price=0 Quantity=0 Strategy='X' Type=Market Tif=Gtc Oco='' Filled=0 Fill price=0 Token='4d7ce5d907e64efaa81bb4455143af21' Gtd='2099-12-01 00:00:00'
2000-11-28 00:00:00 OnOrderUpdate(): Order='NT-00010/Back101' Name='Sell' State=Filled Instrument='$GBPJPY' Action=Sell Limit price=0 Stop price=0 Quantity=0,1M Strategy='X' Type=Market Tif=Gtc Oco='' Filled=100000 Fill price=156,14 Token='4d7ce5d907e64efaa81bb4455143af21' Gtd='2099-12-01 00:00:00'
2000-11-28 00:00:00 OnExecution(): Execution='NT-00007' Instrument='$GBPJPY' Account='Back101' Name='Sell' Exchange=Default Price=156,14 Quantity=0,1M Market position=Short Commission=0 Order='NT-00010' Time='2000-11-29 00:00:00' Multiplier=1E-05
2000-11-28 00:00:00 OnPositionUpdate(): Instrument='$GBPJPY' Account='Back101' Avg price=0 Quantity=0 Market position=Flat


There is "Entered internal PlaceOrder() method at 2000-11-28 00:00:00: Action=SellShort" but nothing more this day nor next day.
Last edited by Argo1; 09-13-2009 at 12:35 PM.
Argo1 is offline  
Reply With Quote
Old 09-13-2009, 11:47 AM   #7
Argo1
Member
 
Join Date: Sep 2009
Posts: 51
Thanks: 0
Thanked 0 times in 0 posts
Default

Is it possible that NT can't process these two orders: ExitLong() and EnterShortStop() on the same bar?
Last edited by Argo1; 09-13-2009 at 11:57 AM.
Argo1 is offline  
Reply With Quote
Old 09-13-2009, 11:22 PM   #8
NinjaTrader_Ben
NinjaTrader Customer Service
 
NinjaTrader_Ben's Avatar
 
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
Default

Hello,

Are you backtesting? If so, everything is done on bar close. Also the code iterates in OnBarUpdate() only when an new bar comes in/closes. This may be what is causing the issue for you.
NinjaTrader_Ben is offline  
Reply With Quote
Old 09-14-2009, 04:51 AM   #9
Argo1
Member
 
Join Date: Sep 2009
Posts: 51
Thanks: 0
Thanked 0 times in 0 posts
Default

It is backtesting on daily bars. There are bars after day 3.
Normally, when I reverse position using only EnterShortStop() method (without ExitLong()) then NT automatically closes long and opens short on the same bar.
NT disallows
manual execution both ExitLong() and EnterShortStop() orders on the same bar?
Argo1 is offline  
Reply With Quote
Old 09-14-2009, 07:25 AM   #10
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

Argo1,

That is correct. In backtesting this happens because it does not realize these were actually placed in the same bar to go together. You will need to use the Enter() directly to get the reversal on the same bar.
NinjaTrader_Josh is offline  
Reply With Quote
Old 09-14-2009, 08:45 AM   #11
Argo1
Member
 
Join Date: Sep 2009
Posts: 51
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks, Josh.

Thus, answer for my post #1 question is: it's impossible ?
Argo1 is offline  
Reply With Quote
Old 09-14-2009, 09:08 AM   #12
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

Correct. From a backtesting scenario that would be the case.
NinjaTrader_Josh is offline  
Reply With Quote
Old 09-14-2009, 09:11 AM   #13
Argo1
Member
 
Join Date: Sep 2009
Posts: 51
Thanks: 0
Thanked 0 times in 0 posts
Default

Ok, thank you.
Argo1 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
Sounds & position close w/strategy on nicknamed Suggestions And Feedback 2 05-20-2009 06:03 AM
Line From Previous Day Close cre8it8 Indicator Development 4 04-17-2009 04:32 PM
Auto Reverse Option on Chart Trader aslane Automated Trading 1 04-03-2009 09:59 AM
Best way to reverse a position? Idruna Strategy Development 10 09-24-2008 10:17 PM
Previous Day's Close, High, Low Billy Valentine General Programming 2 04-13-2008 04:36 PM


All times are GMT -6. The time now is 11:38 PM.