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 04-21-2012, 12:40 PM   #1
user_456
Junior Member
 
Join Date: Jun 2011
Posts: 5
Thanks: 0
Thanked 0 times in 0 posts
Default 2 Longs don't make a right!!

I wrote a strategy script that is generating a second long order in back-testing that I don't understand and can't seem to figure out how to NOT generate? I have attached a screenshot of the orders. One of these longs should not be generated. The reason I say that is because of my crossover condition. I'm using 8 tick range bars in ES. Please see below for my script info. I'm fully prepared to be completely wrong in thinking my assumptions match my code Thanks in advance for your advice/assistance!



protected override void Initialize()
{
Add(RSI45LOW55HIGH(7, 3));
Add(MACD(12, 26, 9));
Add(RSI45LOW55HIGH(7, 3));
Add(MACD(12, 26, 9));
SetProfitTarget("Long Market Entry", CalculationMode.Ticks, 4);
SetStopLoss("Long Market Entry", CalculationMode.Ticks, 8, false);
SetProfitTarget("Short Market Entry", CalculationMode.Ticks, 4);
SetStopLoss("Short Market Entry", CalculationMode.Ticks, 8, false);
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
if (CrossAbove(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 2)
&& RSI45LOW55HIGH(7, 3).Avg[0] > 45
&& MACD(12, 26, 9).Diff[0] >= 0.1
&& Position.Quantity == 0
&& RSI45LOW55HIGH(7, 3)[0] > RSI45LOW55HIGH(7, 3)[1])
{
EnterLong(DefaultQuantity, "Long Market Entry");
ExitLong("Long Exit", "Long Market Entry");
}
// Condition set 2
if (CrossAbove(MACD(12, 26, 9).Avg, MACD(12, 26, 9), 2)
&& RSI45LOW55HIGH(7, 3).Avg[0] < 55
&& MACD(12, 26, 9).Diff[0] <= -0.1
&& Position.Quantity == 0)
{
EnterShort(DefaultQuantity, "Short Market Entry");
ExitShort("Short Exit", "Short Market Entry");
}
}
Attached Images
File Type: gif Untitled-2.png (10.6 KB, 5 views)
user_456 is offline  
Reply With Quote
Old 04-22-2012, 11:41 AM   #2
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 376 times in 366 posts
Default

user,

CrossAbove(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 2)

The "2" here will check over the last 2 bars for crossover conditions. As such the condition could be true for one bar and the next. You can partially remedy this by setting appropriate EntriesPerDirection settings.

http://www.ninjatrader.com/support/f...37&postcount=2
NinjaTrader_AdamP is offline  
Reply With Quote
Old 04-22-2012, 11:52 AM   #3
user_456
Junior Member
 
Join Date: Jun 2011
Posts: 5
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks so much for the information. I will certainly take and use your advice. However, just so I understand can you please describe in more detail your comment regarding "will check over the last 2 bars for crossover conditions"? My now-understood misunderstanding of the "Look back period" was that it looked back 2 bars ago, not OVER the span of 2 bars.

Could I not accomplish my goal by changing the Look back period to 1, then change my "Bars ago" setting to 2?

Thanks again for your assistance.
user_456 is offline  
Reply With Quote
Old 04-22-2012, 11:56 AM   #4
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 376 times in 366 posts
Default

User,

If the lookback period is set to 2, it will check the current bar and previous bar for a cross over condition, then it will also check the previous bar and the bar before that for a cross over condition.

CrossAbove/CrossBelow already automatically check the current bar and previous bar for crossing conditions, so setting lookback to 1 is sufficient to check for the most recent crossover.

Please let me know if I may assist further.
NinjaTrader_AdamP is offline  
Reply With Quote
Old 04-22-2012, 12:00 PM   #5
user_456
Junior Member
 
Join Date: Jun 2011
Posts: 5
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks very much for the info
user_456 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
Trailing Stop for Longs and shorts? crimsonite Automated Trading 1 03-02-2012 04:45 PM
Condition...If market position=2 longs raycam Strategy Development 2 05-28-2010 08:09 AM
No new longs 'til I get a short signal? 5iver Strategy Development 2 01-12-2009 09:07 AM
Exiting Longs (or shorts) one at a time rather than all at once? ag987654321 Strategy Analyzer 3 12-19-2008 11:09 AM
Hedging Longs and Shorts three faced devil Strategy Development 8 11-05-2008 09:24 AM


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