Go Back   NinjaTrader Support Forum > NinjaTrader 7 > 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: 180
Thanks: 2
Thanked 2 times in 2 posts
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 94 times in 83 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: 180
Thanks: 2
Thanked 2 times in 2 posts
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 94 times in 83 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: 180
Thanks: 2
Thanked 2 times in 2 posts
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: 26,202
Thanks: 487
Thanked 1,840 times in 1,781 posts
Default

Max, thanks for clarifying - then I would suggest testing out the sample code Austin did for you.
NinjaTrader_Bertrand 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
entry and exit wes7763 Strategy Development 3 07-29-2009 04:13 PM
Entry and EXIT an SMA Sailor General Development 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 04:05 PM.

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates. 

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content. Visit the CFTC resources for education regarding the industry and signs of fraud.