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 11-19-2007, 07:03 AM   #1
z32000
Member
 
Join Date: Nov 2007
Posts: 37
Thanks: 0
Thanked 0 times in 0 posts
Default strategy comparing two instruments

how would I go about creating a strategy where... say for example


if ES is up from 9:30am to 10:00am, enterlong YM at 11am and
if ES is down from 9:30am to 10:00am, entershort YM at 11am

I'm assuming I would run a backtest on YM, but how do I call upon the instrument ES.

Thanks in advance
z32000 is offline  
Reply With Quote
Old 11-19-2007, 07:09 AM   #2
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
Default

Here is a great start for Multi-Instrument strategies.

http://www.ninjatrader-support.com/H...struments.html
NinjaTrader_Ray is offline  
Reply With Quote
Old 11-19-2007, 08:13 AM   #3
z32000
Member
 
Join Date: Nov 2007
Posts: 37
Thanks: 0
Thanked 0 times in 0 posts
Default

I looked at the below code and was not sure...
if I want to know if ES is up between 9:30am and 10:00am...
if so, Enterlong in YM at 11am

do I have to first place the below code in Initialize()

Add("ES 12-07", PeriodType.Minute, 1);


and then how do I call upon it? Sorry if I mangled it up. Still new at programming

if ("ES 12-07".ToTime(93000)<=ToTime(100000))
Enterlong(1).ToTime(110000);





Quote:

/// This method is used to configure the strategy and is called once before any strategy method is called.
///</summary>
protectedoverridevoid Initialize()
{
// Add an ER2 09-07 1 minute Bars object to the strategy
Add("ER2 09-07", PeriodType.Minute, 1);

// Note: Bars are added to the BarsArray and can be accessed via an index value
// E.G. BarsArray[1] ---> Accesses the 1 minute Bars added above

// Add RSI and ADX indicators to the chart for display
// This only displays the indicators for the pimary Bars object (main instrument) on the chart
Add(RSI(14, 0));
Add(ADX(
14));

// Sets a 20 tick trailing stop for an open position
SetTrailStop(CalculationMode.Ticks, 20);

CalculateOnBarClose =
true;
}
///<summary>
/// Called on each bar update event (incoming tick)
///</summary>
protectedoverridevoid OnBarUpdate()
{
// OnBarUpdate() will be called on incoming tick events on all Bars objects added to the strategy
// We only want to process events on our primary Bars object (main instrument) (index = 0) which
// is set when adding the strategy to a chart
if (BarsInProgress != 0)
return;

// Checks if the 14 period ADX on both instruments are trending (above a value of 30)
if (ADX(14)[0] > 30 && ADX(BarsArray[1], 14)[0] > 30)
{
// If RSI crosses above a value of 30 then enter a long position via a limit order
if (CrossAbove(RSI(14, 0), 30, 1))
{
// Draws a square 1 tick above the high of the bar identifying when a limit order is issued
DrawSquare("My square" + CurrentBar, 0, High[0] + 1 * TickSize, Color.DodgerBlue);

// Enter a long position via a limit order at the current ask price
EnterLongLimit(GetCurrentAsk(), "RSI");
}
}

// Any open long position will exit if RSI crosses below a value of 75
// This is in addition to the trail stop set in the Initialize() method
if (CrossBelow(RSI(14, 0), 75, 1))
ExitLong();
}
z32000 is offline  
Reply With Quote
Old 11-19-2007, 09:48 PM   #4
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

Right z32000. First place Add("ES 12-07", PeriodType.Minute, 1); into the Initialize() method. Then in the OnBarUpdate() you want to run your checks on the BarsInProgress = 1 and place your trades in the context of BarsInProgress = 0 (when you load your strategy run it on the YM not the ES).

Code:
if (BarsInProgress == 1)
{
     if (ToTime(Time[0]) == 93000)
          esOpen = Open[0];
     else if (ToTime(Time[0]) == 100000)
          esClose = Close[0];
     if (esClose > esOpen)
          tradeYM = true;
}

if (BarsInProgress == 0)
{
     if (tradeYM == true)
          EnterLong();
}
NinjaTrader_Josh is offline  
Reply With Quote
Old 11-19-2007, 10:02 PM   #5
z32000
Member
 
Join Date: Nov 2007
Posts: 37
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks Josh! You're a genius
I'm not sure if Ninjatrader takes this into account but some instruments...
such as hong kong futures where the market opens the night before...

So say if I wanted to know if the hong kong market was up between 10pm and 2am and if so, go long on ES the next day...

does this make things much more tricker? I would think NT only compares two instruments within the same time period.


Quote:
Originally Posted by Josh View Post
Right z32000. First place Add("ES 12-07", PeriodType.Minute, 1); into the Initialize() method. Then in the OnBarUpdate() you want to run your checks on the BarsInProgress = 1 and place your trades in the context of BarsInProgress = 0 (when you load your strategy run it on the YM not the ES).

Code:
if (BarsInProgress == 1)
{
     if (ToTime(Time[0]) == 93000)
          esOpen = Open[0];
     else if (ToTime(Time[0]) == 100000)
          esClose = Close[0];
     if (esClose > esOpen)
          tradeYM = true;
}
 
if (BarsInProgress == 0)
{
     if (tradeYM == true)
          EnterLong();
}
z32000 is offline  
Reply With Quote
Old 11-19-2007, 10:10 PM   #6
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

Add the HK instrument like you did the ES instrument.

Code:
if (BarsInProgress == 1)
{
     if (ToTime(Time[0]) == 93000 && ToDay(Time[0]) == (ToDay(DateTime.Now) - 1))
          hkOpen = Open[0];
     else if (ToTime(Time[0]) == 100000 && ToDay(Time[0]) == (ToDay(DateTime.Now) - 1))
          hkClose = Close[0];
     if (hkClose > hkOpen)
          tradeHK = true;
}

if (BarsInProgress == 0)
{
     if (tradeHK == true && ToDay(Time[0]) == ToDay(DateTime.Now))
          EnterLong();
}
DateTime.Now will only work in real-time situations because it is checking your system clock. If you want this to work in backtest you will need to go and save some time variables in your code for the comparison.
NinjaTrader_Josh is offline  
Reply With Quote
Old 11-19-2007, 11:03 PM   #7
z32000
Member
 
Join Date: Nov 2007
Posts: 37
Thanks: 0
Thanked 0 times in 0 posts
Default

I'm not quite sure what do you mean by save some time variables...
I am doing this for backtesting first and foremost, so it's rather tough not being able to use the DateTime.Now...



Quote:
Originally Posted by Josh View Post
Add the HK instrument like you did the ES instrument.

Code:
if (BarsInProgress == 1)
{
     if (ToTime(Time[0]) == 93000 && ToDay(Time[0]) == (ToDay(DateTime.Now) - 1))
          hkOpen = Open[0];
     else if (ToTime(Time[0]) == 100000 && ToDay(Time[0]) == (ToDay(DateTime.Now) - 1))
          hkClose = Close[0];
     if (hkClose > hkOpen)
          tradeHK = true;
}
 
if (BarsInProgress == 0)
{
     if (tradeHK == true && ToDay(Time[0]) == ToDay(DateTime.Now))
          EnterLong();
}
DateTime.Now will only work in real-time situations because it is checking your system clock. If you want this to work in backtest you will need to go and save some time variables in your code for the comparison.
z32000 is offline  
Reply With Quote
Old 11-19-2007, 11:15 PM   #8
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

Code:
if (BarsInProgress == 1)
{
     if (ToTime(Time[0]) == 93000)
          hkOpen = Open[0];
     else if (ToTime(Time[0]) == 100000)
     {
          hkClose = Close[0];
          if (hkClose > hkOpen)
         {
               tradeHK = true;
               hkDay = ToDay(Time[0]);
         }
     }
}

if (BarsInProgress == 0)
{
     if (tradeHK == true && ToDay(Time[0]) == (hkDay + 1))
     {
          EnterLong();
          tradeHK = false;
          hkDay = 0;
          hkOpen = 0;
          hkClose = 0;
     }
}
Something like this. I haven't tested this code so you'll need to play with it yourself.
Last edited by NinjaTrader_Josh; 11-19-2007 at 11:22 PM.
NinjaTrader_Josh is offline  
Reply With Quote
Old 11-19-2007, 11:33 PM   #9
z32000
Member
 
Join Date: Nov 2007
Posts: 37
Thanks: 0
Thanked 0 times in 0 posts
Default

thanks Josh again I see you used the time from 9:30am to 10:00am..
but instead if it was 9:30am to 1:00am (the next day)...
is this possible?


Quote:
Originally Posted by Josh View Post
Code:
if (BarsInProgress == 1)
{
     if (ToTime(Time[0]) == 93000)
          hkOpen = Open[0];
     else if (ToTime(Time[0]) == 100000)
     {
          hkClose = Close[0];
          if (hkClose > hkOpen)
         {
               tradeHK = true;
               hkDay = ToDay(Time[0]);
         }
     }
}
 
if (BarsInProgress == 0)
{
     if (tradeHK == true && ToDay(Time[0]) == (hkDay + 1))
     {
          EnterLong();
          tradeHK = false;
          hkDay = 0;
          hkOpen = 0;
          hkClose = 0;
     }
}
Something like this. I haven't tested this code so you'll need to play with it yourself.
z32000 is offline  
Reply With Quote
Old 11-19-2007, 11:47 PM   #10
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

All time comparisons are possible. You will just need to work out various techniques through the use of Time[0], ToTime(), and ToDay().
NinjaTrader_Josh is offline  
Reply With Quote
Old 11-20-2007, 06:29 AM   #11
z32000
Member
 
Join Date: Nov 2007
Posts: 37
Thanks: 0
Thanked 0 times in 0 posts
Default

would I still need to define the variables hkDay, tradeHK, hkOpen and hkClose in the Variable and Properties section?

thanks

Quote:
Originally Posted by Josh View Post
All time comparisons are possible. You will just need to work out various techniques through the use of Time[0], ToTime(), and ToDay().
z32000 is offline  
Reply With Quote
Old 11-20-2007, 11:53 PM   #12
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

Right. I provided you with different techniques you can use. You don't necessarily need to use the exact technique I've outlined under all circumstances.
NinjaTrader_Josh is offline  
Reply With Quote
Old 11-22-2007, 06:26 PM   #13
z32000
Member
 
Join Date: Nov 2007
Posts: 37
Thanks: 0
Thanked 0 times in 0 posts
Default

hi Josh, I just gave the code a try, but I don't think it works...

{
if (tradeHK == true && ToDay(Time[0]) == (hkDay + 1))
{
EnterLong();
tradeHK = false;
hkDay = 0;
hkOpen = 0;
hkClose = 0;
}
}


You wrote the above... but I don't see how this long position can ever be executed since your condition states... Enter long "if current date is equal to current date plus 1"...

I gave it a try and unfortunately it did not work...any ideas?
Thanks


Quote:
Originally Posted by Josh View Post
Right. I provided you with different techniques you can use. You don't necessarily need to use the exact technique I've outlined under all circumstances.
z32000 is offline  
Reply With Quote
Old 11-22-2007, 06:42 PM   #14
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

The technique shown there is suppose to say "if the current date is the previous date + 1 (aka if current date is current date)". The hkDay variable is set in the BarsInProgress = 1. When your strategy goes through the historical data of the previous date and it evalutes to true it will set the variable to that date. I see what you mean it will never evalute to true. You will need to add a condition to prevent it from updating on the latest date.

The code I posted are just pseudocodes and have never been tested. They are to demonstrate theoretical techniques, but you will need to develop it to your exact needs on your own.

The idea behind what I posted is this. On timeframe 1 store the previous day's up/down trend in the opening hour. On timeframe 0 check to see if timeframe1 was up or down on the previous day and do something accordingly.
NinjaTrader_Josh 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
Exporting Instruments Rollins Suggestions And Feedback 1 10-31-2007 05:47 AM
Linking different instruments Rollins Charting 2 10-08-2007 02:03 PM
Not able to chart certain instruments pipperjack Charting 1 09-20-2007 12:39 PM
Same Strategy Only Runs on Part of Instruments rabidturtle Strategy Development 4 09-14-2007 02:36 PM
What Instruments do You Trade??? chipper338 ATM Strategies (Discretionary Trading) 5 11-30-2004 04:30 AM


All times are GMT -6. The time now is 02:04 PM.