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-05-2009, 09:09 AM   #1
MAX
Senior Member
 
Join Date: Sep 2006
Location: Rome, , Italy
Posts: 170
Thanks: 2
Thanked 1 time in 1 post
Default Exit Position n bars since entry

Hello,

I'm testing a strategy and I've got a couple of questions:

1) Once in position, I'm looking to exit at the close of n bars since my entry bar.
2) I need to choose a different order, depending on the position of the next opening bar.
Let's say, for example, that if the next bars opens in a gap up, I want to enter short at
today's high and exit at the close of the bar.

Could someone help me with the code?

Thanks.
MAX is offline  
Reply With Quote
Old 09-05-2009, 01:05 PM   #2
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 92 times in 82 posts
Default

Hi MAX, for #1, you could use BarsSinceEntry(). For #2, you can specify which entry signal to look for with BarsSinceEntry():
Code:
// if the signal name you're looking for is "longEntry"
int barsSinceEntry = BarsSinceEntry("longEntry");
NinjaTrader_Austin is offline  
Reply With Quote
Old 09-05-2009, 01:29 PM   #3
MAX
Senior Member
 
Join Date: Sep 2006
Location: Rome, , Italy
Posts: 170
Thanks: 2
Thanked 1 time in 1 post
Default

For #2, maybe I wasn't so clear.
What I need to translate is:

If open of next bars is higher than actual high, then sell next bar (stop) at actual high and close the position at the end of the bar.

Hope this clarify.

Thanks for your kind help.
MAX is offline  
Reply With Quote
Old 09-06-2009, 01:27 PM   #4
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 92 times in 82 posts
Default

Max, I'm not sure I follow. It would almost seem like the strategy you have envisioned "looks into the future". How would you sell at the high of the next bar? Anyways, unless you add intrabar granularity to your backtest, the backtester can't be told to buy and sell at the same bar.

If next bar means current bar and if actual means previous bar, then I think this code would do what you want:
Code:
// this code is untested and incomplete
// currentbarentry is a bool
protected override void OnBarUpdate()
{
     if (Open[0] > High[1])
     {
         EnterShortStop(High[1]);
         currentbarentry = true;
     }

if (currentbarentry == true)
{
         currentbarentry = false;
         ExitShort();
}
NinjaTrader_Austin is offline  
Reply With Quote
Old 09-06-2009, 04:49 PM   #5
MAX
Senior Member
 
Join Date: Sep 2006
Location: Rome, , Italy
Posts: 170
Thanks: 2
Thanked 1 time in 1 post
Default

Hi,

I still didn't test your code, although I don't want to look into the future, but at the present bar (doesn't matter the time frame).

If, on the next bar, the open is in gap, I want to sell the market at this bar high in stop.
So I already know which my entry level is (this bar high). I just need to tell the code : If open of next bar is higher than actual bar's high then sell at actual bar's high stop.

I'm just giving an order based on my actual setup, depending on next opening.

Hope this clear.

Thanks.
MAX is offline  
Reply With Quote
Old 09-08-2009, 04:43 AM   #6
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 24,524
Thanks: 386
Thanked 1,395 times in 1,364 posts
Default

Max, thanks for clarifying - then I would suggest testing out the sample code Austin did for you.
NinjaTrader_Bertrand is online now  
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
entry and exit wes7763 Strategy Development 3 07-29-2009 04:13 PM
Entry and EXIT an SMA Sailor General Programming 10 07-06-2009 10:13 AM
How to get the entry signal or bars array index for an open position cunparis Strategy Development 4 12-24-2008 08:04 AM
Enter on bars, exit on ticks and min bars requirment coolraz Strategy Development 3 12-15-2008 10:59 AM
It seems to take 2 bars to Exit a position and then enter in opposite direction scjohn Strategy Development 1 11-06-2008 04:03 PM


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