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 12-03-2007, 05:12 PM   #1
Ninja Learner
Junior Member
 
Join Date: Dec 2007
Posts: 1
Thanks: 0
Thanked 0 times in 0 posts
Default EMA cross Question

I am trying to code a basic entry of when the 9 EMA crosses the 34 EMA to enter into a position. With no look back period. Therefore I changed in the code

if (CrossAbove(EMA(9), EMA(34), 1))
to
if (CrossAbove(EMA(9), EMA(34), 0))

and included the conditions ToTime coded as such:

if (ToTime(Time[0]) >= 61500 && ToTime(Time[0]) <= 111500)
if (ToTime(Time[0]) >= 131500 && ToTime(Time[0]) <= 161500)
if (ToTime(Time[0]) >= 191500 && ToTime(Time[0]) <= 231500)

Now the the trading strategy is always exiting the trade once entered, paying the spread.

I am trying to eneter into One position on the 9 Crossing the 34 EMA with no look back period and only trading from 6:15 AM to 11:15AM and 13:15PM to 16:15PM and from 19:15PM to 23:15PM

I've posted my coding below if anyone is so kind enough to please point me out my mistakes.


///<summary>
/// Called on each bar update event (incoming tick)
///</summary>
protectedoverridevoid OnBarUpdate()
{
// Condition set 1
if (CrossAbove(EMA(9), EMA(34), 0))
{
}
// Condition set 2
if (ToTime(Time[0]) >= 61500 && ToTime(Time[0]) <= 111500)
if (ToTime(Time[0]) >= 131500 && ToTime(Time[0]) <= 161500)
if (ToTime(Time[0]) >= 191500 && ToTime(Time[0]) <= 231500)
{
}
EnterLong(DefaultQuantity, "");
// Condition set 4
if (CrossBelow(EMA(9), EMA(34), 0))
{
}
// Condition set 5
if (ToTime(Time[0]) >= 61500 && ToTime(Time[0]) <= 111500)
if (ToTime(Time[0]) >= 131500 && ToTime(Time[0]) <= 161500)
if (ToTime(Time[0]) >= 191500 && ToTime(Time[0]) <= 231500)
{
}
EnterShort(DefaultQuantity, "");
}
Last edited by Ninja Learner; 12-03-2007 at 05:29 PM.
Ninja Learner is offline  
Reply With Quote
Old 12-03-2007, 11:39 PM   #2
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

Unfortunately we can not review complete trading strategies for bandwidth reasons. I suggest starting simple as possible, adding logic line by line and using debugging techniques as per here: http://www.ninjatrader-support.com/v...ead.php?t=3418
NinjaTrader_Dierk is offline  
Reply With Quote
Old 12-04-2007, 12:42 AM   #3
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

To lead you in the right direction, you will need watch where your brackets are. You create a condition if( blah blah blah) then you have brackets { }. The if condition only applies to things inside the {}.

So for instance you have
Code:
if (CrossAbove(EMA(9), EMA(34), 0))
{
}

This means you've created a cross above condition that needs to be evaluated true, BUT you are not applying this "filter" to anything.
Code:
if (CrossAbove(EMA(9), EMA(34), 0))
 {
     // Do something when this is true
  }
NinjaTrader_Josh is offline  
Reply With Quote
Old 04-16-2009, 12:53 PM   #4
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default CrossAbove/Below Look Back Period

Hey Josh,

I have

If (CrossAbove(SMA(Fast), SMA(Slow), 0)
{
EnterLong()
}

My question is, by setting my look back period to 0, does that mean the second my last bar closes and hence a cross over occurs, that it will execute the code inside my bracket?

For some reason, (and this is all real time trading), I can't seem to fire the trade on immediate after the bar closes, hence putting me in the trade based on the crossabove/below occurance. It seems to wait a whole other bar (when it was set to 1, it worked, but a whole bar went by, that's a lot of lag on say a 10/15m bar, no?)


Thanks,
r2kTrader is offline  
Reply With Quote
Old 04-16-2009, 01:07 PM   #5
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

r2kTrader,

You should use a lookback period of at least 1. To determine a cross, NinjaTrader requires two data points. One to identify that the SMA(Fast) was below the SMA(Slow). Another to identify that the SMA(Fast) is above the SMA(Slow). When you set a lookback period to 0 you are looking at the same data point and no crosses can occur when comparing on one data point.

If you want to trade immediately intrabar use a lookback period of 1 and CalculateOnBarClose = false. The moment it crosses, you will trade. Remember crosses can flip flop many times throughout the building of a bar.
NinjaTrader_Josh is offline  
Reply With Quote
Old 04-16-2009, 01:10 PM   #6
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default Bug?

Josh,

As per my last post, it seems that if I use 0 for the look back period, that the strategy does not work. No error, it just doesn't fire any trades.

Can the look back period be set to zero in real time trading? How can I get rid of that one bar lag.


thanks.

Quote:
Originally Posted by r2kTrader View Post
Hey Josh,

I have

If (CrossAbove(SMA(Fast), SMA(Slow), 0)
{
EnterLong()
}

My question is, by setting my look back period to 0, does that mean the second my last bar closes and hence a cross over occurs, that it will execute the code inside my bracket?

For some reason, (and this is all real time trading), I can't seem to fire the trade on immediate after the bar closes, hence putting me in the trade based on the crossabove/below occurance. It seems to wait a whole other bar (when it was set to 1, it worked, but a whole bar went by, that's a lot of lag on say a 10/15m bar, no?)


Thanks,
r2kTrader is offline  
Reply With Quote
Old 04-16-2009, 01:15 PM   #7
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

As stated in my prior post. You need to use a lookback period >= 1 with CalculateOnBarClose = false. There is no lag already.
NinjaTrader_Josh is offline  
Reply With Quote
Old 04-16-2009, 01:30 PM   #8
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default

CalculateOnBarClose = True in my code.

Are you saying I have to have it set to false?

If I am understanding you correctly, all I need to do is change look back to 1 and calc to False?

Isn't that more processor intensive though? Is that the only solution?



Quote:
Originally Posted by NinjaTrader_Josh View Post
As stated in my prior post. You need to use a lookback period >= 1 with CalculateOnBarClose = false. There is no lag already.
r2kTrader is offline  
Reply With Quote
Old 04-16-2009, 01:47 PM   #9
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

Yes. If you want to do anything intrabar you have to use CalculateOnBarClose = false. This will only make a difference in real-time strategies too.

It is the tradeoff you have to decide on. You either calculate intrabar and have it be more computationally intensive or you calculate at end of the bar and have "delays".
NinjaTrader_Josh is offline  
Reply With Quote
Old 04-16-2009, 06:18 PM   #10
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default

Josh,

There is one thing I don't quite understand. Even if I set CalculateOnBarClose to true, why doesn't it fire signal on the next bar? This doesn't seem logic.

The bar closed, it crossed above/below, next bar should fire.

I did test what you said and it did fire correctly, but I don't get the logic.


THanks,
r2kTrader is offline  
Reply With Quote
Old 04-17-2009, 06:08 AM   #11
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,569
Thanks: 262
Thanked 1,018 times in 999 posts
Default

r2kTrader, the bar is 'closed' when you receive the open tick of the next bar, so essentially the order is send / placed then.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 04-17-2009, 06:18 AM   #12
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default

Bertrand,

Thank you for the reply. Can we walk through this like a 5 year old

Let's say the system is set to run at 9:30 am on 5m bars.

we look for crossover above/below an SMA

15m has rolled by with the last 5m bar closing 1 tick ago. I can see the crossover drawn on the screen, but the trade won't go off until the next bar.

Can you explain why this is happening?

These are the only additions I have added to the sample SMA example. I don't want historical orders to submit, so I put in the If Historical test.

CalculateOnBarClose = true
If(Historical)
{return};
r2kTrader is offline  
Reply With Quote
Old 04-17-2009, 07:16 AM   #13
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

r2kTrader,

If you want to trade intrabar you cannot use CalculateOnBarClose = true. You need to set it to false.
NinjaTrader_Josh is offline  
Reply With Quote
Old 04-17-2009, 07:40 AM   #14
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default

Quote:
Originally Posted by NinjaTrader_Josh View Post
r2kTrader,

If you want to trade intrabar you cannot use CalculateOnBarClose = true. You need to set it to false.
Josh,

I understand that you cannot trade IntraBar with CalcOnBarClose set to true. I really do. My question (see above posts), is regarding why this is considered Intrabar? I am waiting for the bar to close.

Thanks,
r2kTrader is offline  
Reply With Quote
Old 04-17-2009, 07:50 AM   #15
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

r2kTrader,

It is absolutely intrabar. You have a signal bar. You see the cross on the signal bar. The signal bar evaluates your condition to true. Next tradeable location is the open of the next bar because you are using CalculateOnBarClose = true and as such the order goes in on the next bar.
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
MTF Ema Cross buderim Market Analyzer 1 10-17-2007 04:17 AM
How to write second cross over of MACD? Jenny Strategy Development 2 07-18-2007 06:23 AM
Alert on cross over/under scjohn Market Analyzer 5 05-25-2007 07:21 AM
Global Cross Hair Cursor guym Suggestions And Feedback 2 12-07-2006 06:40 AM
Global Cross Hair Cursor guym Charting 1 12-31-1969 06:00 PM


All times are GMT -6. The time now is 01:21 PM.