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 > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 02-28-2011, 05:48 PM   #1
Stephan123
Senior Member
 
Join Date: Feb 2011
Posts: 117
Thanks: 2
Thanked 0 times in 0 posts
Default Buy on Close (Today)

Hi folks,

I want to implement the following logic:

If the Close today is higher than the Close yesterday, buy on today's close and sell at tomorrows close.
Short analog.


Seems like forcing NT to buy or short on the Close of the actual bar is only possible with Index -1
with:

if (Close[-1] >Close[0])
{
EnterLongStopLimit(1, Close[-1], Close[-1], "Long-Entry");
}

short analog,

and to exit on the actual bar with:

if (BarsSinceEntry("Long-Entry") == 0 )
{

ExitLongStopLimit(Close[-1], Close[-1], "Exit Long", "Long-Entry");
}

Unfurtunately that's not supported by the support.

The system is always in market, evaluating Close-prices only and positions are sometimes subject to reversion.
I just realized that NT is generating exit orders by itself in that case and so an open position is getting two exit-orders. So I figured out a work around for that case.
But it's still not always working-there are bars where no new entries are generated, although I set max. entries per direction to 2.

I have uploaded the strategy - so if anyone knows a fix - awesome!
Maybe there is a much simplier way to code a system that evaluates Closes only - so in that case - please tell me :-)

I don't want to belief it's unpossible with NT...

Thanks in advance!
Stephan
Attached Files
File Type: cs PHNHPattern.cs (3.1 KB, 2 views)
Last edited by Stephan123; 02-28-2011 at 05:51 PM.
Stephan123 is offline  
Reply With Quote
Old 03-01-2011, 05:48 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,382
Thanks: 252
Thanked 967 times in 950 posts
Default

Stefan, with the approach posted you're unfortunately running into the Internal Order Handling Rules of the managed approach in NT - http://www.ninjatrader.com/support/h...d_approach.htm

To enter at the close of today or shortly before it, please submit the order to a finer minute series - or work directly from the intraday series by reformulating your conditions to work with the Current and PriorDayOHLC indicator methods.
NinjaTrader_Bertrand is online now  
Reply With Quote
Old 03-01-2011, 11:57 AM   #3
Stephan123
Senior Member
 
Join Date: Feb 2011
Posts: 117
Thanks: 2
Thanked 0 times in 0 posts
Default Buy on Close

-Is there a way to implement the desired logic
via unmanaged approach?

It has to run on daily OHLC data, therefore I can't increase granularity.
Stephan123 is offline  
Reply With Quote
Old 03-01-2011, 02:29 PM   #4
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

Stephan123,

You can run a multi-time frame strategy to add the granularity you need to allow for the trade while still taking signals off your daily OHLC data. Please see here for more information: http://www.ninjatrader.com/support/h...nstruments.htm
NinjaTrader_Josh is offline  
Reply With Quote
Old 03-01-2011, 03:01 PM   #5
Stephan123
Senior Member
 
Join Date: Feb 2011
Posts: 117
Thanks: 2
Thanked 0 times in 0 posts
Default

-Unfurtunately I only have daily data available, which is why I can't provide any higher
granularity. I have to figure out a way to do it with that daily resolution.
Do unmangedorders work in this case?
Stephan123 is offline  
Reply With Quote
Old 03-01-2011, 03:24 PM   #6
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

Unfortunately not. The issue is not about managed or unmanaged, but rather that you need more data points to be able to trade at the level you are trying to trade at.
NinjaTrader_Josh is offline  
Reply With Quote
Old 03-01-2011, 04:20 PM   #7
Stephan123
Senior Member
 
Join Date: Feb 2011
Posts: 117
Thanks: 2
Thanked 0 times in 0 posts
Default

Can't I look one bar into the future to submit a limit order with the limit beeing the Close
of the desired bar. This order would be filled on the close of the next bar.
I tried it like that with manged orders, which worked, but only in 50-60%
of all entries.
Stephan123 is offline  
Reply With Quote
Old 03-01-2011, 04:31 PM   #8
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

Stephan123,

Unfortunately there is absolutely no way to peak into the future. Likewise, there is absolutely no way to trade at points in the past once you've seen data further forward.
NinjaTrader_Josh is offline  
Reply With Quote
Old 03-01-2011, 05:00 PM   #9
Stephan123
Senior Member
 
Join Date: Feb 2011
Posts: 117
Thanks: 2
Thanked 0 times in 0 posts
Default

The command DrawText("My text" + CurrentBar, Close[-1].ToString(), 0, High[0], Color.Red); plots absolutely correct the Close of the bar x+1 on bar x.
Therefor I can generate all entry/exit signals one bar in advance. Correct?
At the next bar, the one bar in advance generated (limit)order is executed at a given price, in that case, the Close.price
The only question to me is 1) Do limit orders in backtests always fill if the limit price is the close of the bar and 2) how to handle an execution of an exit and an entry at the same bar at the same price, in this case the close price.
Stephan123 is offline  
Reply With Quote
Old 03-02-2011, 04:42 AM   #10
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,382
Thanks: 252
Thanked 967 times in 950 posts
Default

Stephan, unfortunately working with the negative bar indices is not supported - filling of limit orders in backtesting will depend on the type of fill algorithm chosen, with the default one price has to trade through the limit price, a touch is ok in liberal mode.

http://www.ninjatrader.com/support/h...a_strategy.htm
NinjaTrader_Bertrand is online now  
Reply With Quote
Old 03-02-2011, 10:46 AM   #11
Stephan123
Senior Member
 
Join Date: Feb 2011
Posts: 117
Thanks: 2
Thanked 0 times in 0 posts
Default

Does that mean, there is absolutely no way to
implement a logic in NT as it is done with any easy language package like this:

If CloseD(0) > OpenD(0) then begin
Buy this bar at Close;
End;

If marketposition = 1 then begin
Exitlong ("Exit_Long") this bar at close;
End;
Stephan123 is offline  
Reply With Quote
Old 03-02-2011, 11:36 AM   #12
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,382
Thanks: 252
Thanked 967 times in 950 posts
Default

Stephan, there is for sure a way to test a strategy like this in NinjaTrader as well: you would need to more granular intraday data though to allow for more conservative backtesting - as we do not allow historical backtest orders to execute on the close of the bar, as if you recognize this bar you would be on the next bar already when attempting to trade such a setup live.
NinjaTrader_Bertrand is online now  
Reply With Quote
Old 03-24-2011, 06:31 PM   #13
capstonetrader
Junior Member
 
Join Date: Feb 2011
Posts: 9
Thanks: 0
Thanked 0 times in 0 posts
Default

I have a similar question when working on intra-day bars. For example,

If Time=1200 Then ExitShort This Bar on Close;

Is this possible? I know how to set up the NT exits but they are market (which is next bar at open) or limit or stops, but no exit on closes based on a specific time that I have found.
capstonetrader is offline  
Reply With Quote
Old 03-24-2011, 06:56 PM   #14
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,200
Thanks: 24
Thanked 1,226 times in 997 posts
Send a message via Skype™ to koganam
Default

Quote:
Originally Posted by capstonetrader View Post
I have a similar question when working on intra-day bars. For example,

If Time=1200 Then ExitShort This Bar on Close;

Is this possible? I know how to set up the NT exits but they are market (which is next bar at open) or limit or stops, but no exit on closes based on a specific time that I have found.
If Time=1200 && Close[0] >= 0 Then ExitShort This Bar;
koganam is online now  
Reply With Quote
Old 03-25-2011, 12:16 AM   #15
capstonetrader
Junior Member
 
Join Date: Feb 2011
Posts: 9
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks, the best I could do in the Ninja Script was:
If (ToTime(Time[0]) == 1200 && Close[0]>=0)
{
ExitShort("exit signal name", "entry signal name");
}

It would not let me compile if I just state:

Then ExitShort This Bar on Close;

after the Time and Close >= 0 condition.

So with the Ninja script above, it still exits at the open of the next bar instead of the close of the current bar.
capstonetrader 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
Buy/Sell this bar at close? dowhk General Programming 9 02-22-2012 07:16 AM
Buy open and sell close JayNice Strategy Development 5 08-03-2009 02:36 PM
How to buy the open and sell the close... JayNice Strategy Development 2 07-07-2009 07:51 AM
How to get the lowest close of today, and yesterday? namo10 General Programming 5 06-29-2009 05:05 AM
buy/sell at close of bar? coffee898 Charting 4 03-20-2007 03:55 AM


All times are GMT -6. The time now is 11:03 AM.