NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 07-21-2012, 03:58 PM   #1
jperales
Junior Member
 
Join Date: Nov 2011
Location: Durham, NC
Posts: 5
Thanks: 8
Thanked 0 times in 0 posts
Default backtesting, entering a position in a secondary bar sereies

Hi,

I'm having trouble with executing code that enters a position in a secondary bar series while backtesting. The command I am trying to execute is: if the price high of the current bar of the secondary bar series is greater than the variable ORhigh + (the variable Buyticks * the tick size) then enter a long position in the secondary bar series for the the number of contracts represented by the variable Shares.

The code I have is:

if (Highs [1] [0] >= ORhigh + Buyticks * TickSize)
{
count = count + 1;
EnterLong(1,Shares, "BLong");
}

The code has no compilation errors. However, when I back test the strategy no trades are executed.

I am fairly certain that he problem lies with the lines of code mentioned above, because when I substitute with the code below, that does not have any references to a secondary bar series, I see the expected trades take place when back tested.


if (High [0] >= ORhigh + Buyticks * TickSize)
{
count = count + 1;
EnterLong(Shares, "BLong");
}

It seems that my problem is getting the trade to enter a position in a secondary bar series. Please help.

Thanks in advance.

Joe
jperales is offline  
Reply With Quote
Old 07-21-2012, 04:17 PM   #2
sledge
Senior Member
 
Join Date: Aug 2010
Location: Washington, D.C.
Posts: 1,269
Thanks: 193
Thanked 332 times in 287 posts
Default

edit: I re-read advanced order handling, working with multi instrument strategy, and even if you had that, it should work.

What is ORHigh?

I would use Output Window and put Print statements in the code to see what the actual values are (and if it is ever reaching this code).

-------------------------------------
I don't know what the rest of your code says, (I think the problem is there)..

do you by chance have?
Code:
if (BarsInProgress!=0) return;
Quote:
Originally Posted by jperales View Post
Hi,

I'm having trouble with executing code that enters a position in a secondary bar series while backtesting. The command I am trying to execute is: if the price high of the current bar of the secondary bar series is greater than the variable ORhigh + (the variable Buyticks * the tick size) then enter a long position in the secondary bar series for the the number of contracts represented by the variable Shares.

The code I have is:

if (Highs [1] [0] >= ORhigh + Buyticks * TickSize)
{
count = count + 1;
EnterLong(1,Shares, "BLong");
}

The code has no compilation errors. However, when I back test the strategy no trades are executed.

I am fairly certain that he problem lies with the lines of code mentioned above, because when I substitute with the code below, that does not have any references to a secondary bar series, I see the expected trades take place when back tested.


if (High [0] >= ORhigh + Buyticks * TickSize)
{
count = count + 1;
EnterLong(Shares, "BLong");
}

It seems that my problem is getting the trade to enter a position in a secondary bar series. Please help.

Thanks in advance.

Joe
Last edited by sledge; 07-21-2012 at 04:25 PM. Reason: don't like my answer
sledge is offline  
Reply With Quote
The following user says thank you to sledge for this post:
Old 07-21-2012, 05:09 PM   #3
marcow
Senior Member
 
Join Date: Dec 2009
Location: Netherlands
Posts: 180
Thanks: 15
Thanked 72 times in 51 posts
Default

Can you post the complete code, or at least the initialize section of it ?
What timeframes and datatypes are your primary/secondary dataseries ? (like 5min or 4Renko)
marcow is offline  
Reply With Quote
Old 07-22-2012, 01:02 PM   #4
jperales
Junior Member
 
Join Date: Nov 2011
Location: Durham, NC
Posts: 5
Thanks: 8
Thanked 0 times in 0 posts
Default BarsInProgress help

Thank you for your reply.

I do not have the suggested line:
if (BarsInProgress!=0) return;
in the code.

I tried to insert the line into the code however I still end up with the same result of no trades being executed when back testing. I'm still a novice when it comes to coding in NinjaTrader so forgive me for my ignorance on the subject. Where would you suggest I insert the line of code suggested above?

I'm confused as to how and when to use BarsInProgress in code. I understand BarsInProgress allows for the separation of trading logic for different bars events, I'm just not sure how and when to use it.

For instance in my code below, in Condition 1, within the OnBarUpdate() method, I want to use the primary series (3 min bars) to define the variables ORhigh and ORlow. Would I need to use a BarsInProgress line of code to specify that the primary series needs to be used, or is that the default if BarsInProgress is not used.

In Condition 2, I want to switch to the secondary (tick) bar series to enter a trade after 9:36:00 and after price breaks through n # of ticks above the variable ORhigh, defined in condition 1 that used the primary bar series. Exactly how would I do that?

Conditions 3, 4, 5 and 6 represent the Stop loss commands and exit commands. Would I need to reference BarsInProgress again since BarsInProgress was used in condition 2 and possibly condition1, or is the primary series the default and there is no need for using BarsInProgress again?

Clarification on these questions would greatly be appreciated, instructional and very useful for putting together this strategy as well as other strategies that I plan to write in the future. Sorry if it's a simple question, as I'm still a novice at writing code for strategies and feel like I'm in the early part of the learning curve.

Hope this answers the questions posted back to me, and thanks again in advance for helping me out with my questions/problem and education.

Joe

public class ORangeBreakout1 : Strategy
{
#region Variables
// Wizard generated variables
private int buyticks = 2; // Default setting for Buyticks
private int sellticks = 2; // Default setting for Sellticks
private int stopticks4long = 2; // Default setting for Stopticks4long
private int stopticks4short = 2; // Default setting for Stopticks4short
private int exitbars = 1; // Default setting for Exitbars
private int shares = 100; // Default setting for Shares
// User defined variables (add any user defined variables below)
#endregion

// variable to keep count of how many trades are taken per day and for keeping track of each days opening range high/low
int count = 1;
double ORhigh;
double ORlow;
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
// Adding a tick Bars object to strategy
Add(PeriodType.Tick, 1);
CalculateOnBarClose = false;
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Conditon 1 finds opening range high and low, in the primary bar series, and resets variable count to 0
if (ToTime(Time[0]) == 93600)
{
count = 0;
ORhigh = MAX(High, 2) [0];
ORlow = MIN(Low,2) [0];
}
// Condition 2 enters trades in the secondary bar series only after 9:36 and only allows 1 trade per day
if (ToTime(Time[0]) >= 93600 && count == 0)
{
if (Highs [1] [0] >= ORhigh + Buyticks * TickSize)
{
count = count + 1;
EnterLong(1,Shares, "BLong");
}
else
{
if (Low [0] <= ORlow - Sellticks * TickSize)
{
count = count + 1;
EnterShort(Shares, "SShort");
}
}
}
// Condition 3 stoploss for long position

SetStopLoss("BLong", CalculationMode.Price, ORlow - stopticks4long * TickSize, false);

// Condition 4 stoploss for short position

SetStopLoss("SShort", CalculationMode.Price, ORhigh + stopticks4short * TickSize, false);

// Condition 5 exit n bars from entry of long position
if (BarsSinceEntry() == Exitbars - 1)
{
ExitLong("BLongExit", "BLong");
}
// Condition 6 exit n bars from entry of short position
if (BarsSinceEntry() == Exitbars - 1)
{
ExitShort("SShortExit", "SShort");
}
}
jperales is offline  
Reply With Quote
Old 07-22-2012, 05:00 PM   #5
marcow
Senior Member
 
Join Date: Dec 2009
Location: Netherlands
Posts: 180
Thanks: 15
Thanked 72 times in 51 posts
Default

I made some minor modifications to your strategy please see attachment.
You have to copy this attachment into your strategy directory (C:\Users\......\Documents\NinjaTrader 7\bin\Custom\Strategy ) and compile.

In the code (line 58,88) are 2 print statements which we can use for further debugging.You have to open the output screen to view the output.

Probably it will still not work, but at least we now have the same code


You have tick-based historical data do you ? Because the secondary timeframe is 1 Tick

edit: for updated attachment see post#10

Marco
Last edited by marcow; 07-22-2012 at 06:47 PM.
marcow is offline  
Reply With Quote
The following user says thank you to marcow for this post:
Old 07-22-2012, 05:49 PM   #6
sledge
Senior Member
 
Join Date: Aug 2010
Location: Washington, D.C.
Posts: 1,269
Thanks: 193
Thanked 332 times in 287 posts
Default

**NT** Error on calling 'OnBarUpdate' method for strategy 'ccc1/04ec68f05e5649318243d9bc7e6fdbb8': You must use the overload that has a 'BarsInProgress' parameter when calling the BarsSinceEntry() method in the context of a multi-time frame and instrument strategy.
sledge is offline  
Reply With Quote
Old 07-22-2012, 05:51 PM   #7
sledge
Senior Member
 
Join Date: Aug 2010
Location: Washington, D.C.
Posts: 1,269
Thanks: 193
Thanked 332 times in 287 posts
Default

BarsSinceEntry()



Definition
Returns the number of bars that have elapsed since the last specified entry.

Method Return Value
An int value that represents a number of bars.

Syntax
BarsSinceEntry()
BarsSinceEntry(string signalName)

The following method signature should be used when working with multi-time frame and instrument strategies:

BarsSinceEntry(int barsInProgressIndex, string signalName, int entriesAgo)
sledge is offline  
Reply With Quote
The following user says thank you to sledge for this post:
Old 07-22-2012, 06:02 PM   #8
sledge
Senior Member
 
Join Date: Aug 2010
Location: Washington, D.C.
Posts: 1,269
Thanks: 193
Thanked 332 times in 287 posts
Default

I use this in market replay:
Code:
      protected override void OnBarUpdate()
        {
            if (Historical) return;
...

Code:
                    // Condition 5 exit n bars from entry of long position 
                    if (BarsSinceEntry(1,"BLong",0) == Exitbars - 1)
                        ExitLong("BLongExit", "BLong");
                            
                    // Condition 6 exit n bars from entry of short position
                    if (BarsSinceEntry(1,"SShort",0) == Exitbars - 1)
                        ExitShort("SShortExit", "SShort");
sledge is offline  
Reply With Quote
The following user says thank you to sledge for this post:
Old 07-22-2012, 06:08 PM   #9
sledge
Senior Member
 
Join Date: Aug 2010
Location: Washington, D.C.
Posts: 1,269
Thanks: 193
Thanked 332 times in 287 posts
Default

The short failed with some error about setting stop limit above entry or something......
sledge is offline  
Reply With Quote
Old 07-22-2012, 06:08 PM   #10
marcow
Senior Member
 
Join Date: Dec 2009
Location: Netherlands
Posts: 180
Thanks: 15
Thanked 72 times in 51 posts
Default

thank you sledge for the heads-up on the BarsSinceEntry !
please find attached the updated strategy.

Do you still get the error for the stoploss ?

Marco
Attached Files
File Type: cs ccc.cs (3.6 KB, 10 views)
Last edited by marcow; 07-22-2012 at 06:36 PM.
marcow is offline  
Reply With Quote
The following user says thank you to marcow for this post:
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
Entering a position at a certain price or better only kenb2004 Automated Trading 4 05-04-2012 06:26 AM
Position.Quantity for secondary instrument stefy Strategy Development 1 08-02-2010 09:57 AM
Entering a position based on pips sparhawk Strategy Development 1 02-04-2010 11:17 AM
Only entering long position if entry bar closes at high of range newtrader1 Strategy Development 2 03-04-2009 07:34 AM
When backtesting does entering on a shorter timeframe make a difference? cunparis Strategy Development 5 01-29-2009 07:17 AM


All times are GMT -6. The time now is 12:37 PM.