![]() |
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Member
Join Date: Jan 2009
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
|
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. |
|
|
|
|
|
#2 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
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
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#3 | |
|
Member
Join Date: Jan 2009
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
|
Quote:
Also, how do I define the start and end times? |
|
|
|
|
|
|
#4 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
if (ToTime(Time[0]) >= 120000 && ToTime(Time[0]) < 130000)
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Member
Join Date: Jan 2009
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
|
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])); } |
|
|
|
|
|
#6 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
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
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#7 |
|
Member
Join Date: Jan 2009
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
|
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? |
|
|
|
|
|
#8 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
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.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#9 |
|
Member
Join Date: Jan 2009
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
|
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]; |
|
|
|
|
|
#10 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
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];
}
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#11 |
|
Member
Join Date: Jan 2009
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
|
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]; } |
|
|
|
|
|
#12 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
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;
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#13 |
|
Member
Join Date: Jan 2009
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
|
understood. That declaration is made in teh initialize section or onbarupdate section?
|
|
|
|
|
|
#14 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
In the Variables region of your code above Initialize().
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#15 |
|
Member
Join Date: Jan 2009
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
|
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() |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |