![]() |
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Junior Member
Join Date: Dec 2007
Posts: 1
Thanks: 0
Thanked 0 times in 0 posts
|
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.
|
|
|
|
|
|
#2 |
|
Administrator
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
|
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
Dierk
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
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
}
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
|
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, |
|
|
|
|
|
#5 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
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.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#6 | |
|
Senior Member
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
|
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:
|
|
|
|
|
|
|
#7 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
As stated in my prior post. You need to use a lookback period >= 1 with CalculateOnBarClose = false. There is no lag already.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#8 |
|
Senior Member
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
|
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? |
|
|
|
|
|
#9 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
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".
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#10 |
|
Senior Member
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
|
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, |
|
|
|
|
|
#11 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,569
Thanks: 262
Thanked 1,018 times in 999 posts
|
r2kTrader, the bar is 'closed' when you receive the open tick of the next bar, so essentially the order is send / placed then.
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#12 |
|
Senior Member
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
|
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}; |
|
|
|
|
|
#13 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
r2kTrader,
If you want to trade intrabar you cannot use CalculateOnBarClose = true. You need to set it to false.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#14 | |
|
Senior Member
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
|
Quote:
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, |
|
|
|
|
|
|
#15 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
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.
Josh
NinjaTrader Customer Service |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |