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 02-19-2009, 07:14 AM   #1
Insearch
Member
 
Join Date: Jan 2009
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
Default Help with Range Based Strategy

Hi,

I'm new to programming and was wondering whether someone can point me in the right direction with the following strategy:

Say I define lunch hour as 12pm-1pm. I want the strategy to go long if right after 1pm the price breaks the high of that "lunch hour" and short if the price breaks the low, by 2 ticks. I want to get out at a 1pt profit or out at a 3pt stop loss. What if I want to trade 2 contracts and set the stop loss to B/E on the second contract after the target on the 1st contract has been reached and trail that second contract stop to half the move from my entry?

I want the strategy to last until the end of day, but only execute 1 side each. That is, if it goes long, then no more longs, and if it goes short, no more shorts. The strategy can execute 1 long and 1 short if the above conditions are met.
Insearch is offline  
Reply With Quote
Old 02-19-2009, 07:23 AM   #2
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

Insearch,

This is all possible through programming. Please see this reference sample for a breakout strategy: http://www.ninjatrader-support2.com/...ead.php?t=3223
NinjaTrader_Josh is offline  
Reply With Quote
Old 02-19-2009, 08:46 AM   #3
Insearch
Member
 
Join Date: Jan 2009
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_Josh View Post
Insearch,

This is all possible through programming. Please see this reference sample for a breakout strategy: http://www.ninjatrader-support2.com/...ead.php?t=3223
Thx. This seems to roll the range . . . What should I do if I want to "lock" the lunch hour range . . . .

Also, how do I define the start and end times?
Insearch is offline  
Reply With Quote
Old 02-19-2009, 08:49 AM   #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

if (ToTime(Time[0]) >= 120000 && ToTime(Time[0]) < 130000)
NinjaTrader_Josh is offline  
Reply With Quote
Old 02-19-2009, 10:04 AM   #5
Insearch
Member
 
Join Date: Jan 2009
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
Default

Would this logic return the high and low price for the time range specified?

protectedoverridevoid OnBarUpdate()
{

// Defining the high and low range
if (ToTime(Time[0]) >= 120000 && ToTime(Time[0]) < 130000)
{
double highestHigh = MAX(High, ToTime (Time [120000]) – ToTime (Time [130000]));

double lowestLow = MIN(Low, ToTime (Time [120000]) – ToTime (Time [130000]));

}
Insearch is offline  
Reply With Quote
Old 02-19-2009, 10:08 AM   #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

Insearch,

The second parameter for MAX() is the Period as in how many bars you want to check. Period is not related to time. It is bar driven.

Time[120000] is the time of bars ago # 120000. It is not the time at 12:00:00.

If you want the highest high and lowest low of a certain time please see this reference sample: http://www.ninjatrader-support2.com/...ead.php?t=8600
NinjaTrader_Josh is offline  
Reply With Quote
Old 02-19-2009, 02:40 PM   #7
Insearch
Member
 
Join Date: Jan 2009
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks. That is for an indicator, so wanted to to see if that can be somehow used in a strategy. In that code, there's this piece:

// Calculate the number of bars ago for the start and end bars of the specified time range
int startBarsAgo = GetBar(startDateTime);
int endBarsAgo = GetBar(endDateTime);

Is there any way to put a time stamp in the "startDateTime" and "endDateTime". That is, can I use ToTime in there to specify the time?
Insearch is offline  
Reply With Quote
Old 02-19-2009, 02:44 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

Insearch,

The concepts from the indicator can be carried over to a strategy.

ToTime() does not work in letting you specify a time. All it does is takes a DateTime object and converts it to an integer that represents time in HHMMSS format.

GetBar() gets the bar that a specific time happened on. In the example startDateTime and endDateTime are DateTime objects. Running ToTime() on them will just make them into integers representing the time.
NinjaTrader_Josh is offline  
Reply With Quote
Old 02-19-2009, 04:56 PM   #9
Insearch
Member
 
Join Date: Jan 2009
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
Default

ok!

What is the syntax for getting the high of the first bar in the time range?

Would the code below do this do it . . . I doubt, as definining highestHigh as High[0] would make high of every current bar the "active high" . . . . .

// Defining the high time range
if (ToTime(Time[0]) >= 120000 && ToTime(Time[0]) <= 130000)
highestHigh = High[
0];
Insearch is offline  
Reply With Quote
Old 02-20-2009, 07:16 AM   #10
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

You need to have an additional check inside there to confirm that the recent high being processed is actually greater than the stored high.

Code:
if (ToTime(Time[0])..........)
{
     if (High[0] > highestHigh)
          highestHigh = High[0];
}
NinjaTrader_Josh is offline  
Reply With Quote
Old 02-20-2009, 07:48 AM   #11
Insearch
Member
 
Join Date: Jan 2009
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
Default

If that is my first line, then there really is no stored highestHigh, no? I.e. the strategy code would actually never store a "highestHigh"?

here's the code as you suggested, but don't we need to store highestHigh first?

protectedoverridevoid OnBarUpdate()
{

{
// Defining the high in the specified time range
if (ToTime(Time[0]) >= 120000 && ToTime(Time[0]) <= 130000)
{
if (High[0] > highestHigh)
highestHigh = High[
0];
}
Insearch is offline  
Reply With Quote
Old 02-20-2009, 07:51 AM   #12
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

When you declare highestHigh as a double you can set it to zero. The first time it runs through and checks this High[0] will be > 0 and then it will take on the high value.

Code:
private double highestHigh = 0;
NinjaTrader_Josh is offline  
Reply With Quote
Old 02-20-2009, 07:59 AM   #13
Insearch
Member
 
Join Date: Jan 2009
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
Default

understood. That declaration is made in teh initialize section or onbarupdate section?
Insearch is offline  
Reply With Quote
Old 02-20-2009, 08:02 AM   #14
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

In the Variables region of your code above Initialize().
NinjaTrader_Josh is offline  
Reply With Quote
Old 02-20-2009, 08:14 AM   #15
Insearch
Member
 
Join Date: Jan 2009
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
Default

Inputing it like below, gives me an error message:

///<summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
///</summary>
privatedouble highestHigh = 0
protectedoverridevoid Initialize()
Insearch 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
custom Strategy based on price ninjacao SuperDOM and other Order Entry Windows 1 12-10-2008 08:16 AM
Min and Max for indicator based range stefy Strategy Development 10 09-29-2008 01:47 PM
Alternative to tick based/bar based strategy calculation? (i.e every minute/X ticks) Elliott Wave Strategy Development 9 07-11-2008 03:11 AM
Change volume bar color based on price and volume range TiP2012 Indicator Development 5 05-26-2008 08:09 AM
Need help with strategy based on Simulated stop vking ATM Strategies (Discretionary Trading) 9 10-18-2007 06:27 AM


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