NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > Application Technical Support > Automated Trading

Automated Trading Support for automated trading systems using NinjaScript. Support for our ATI (Automated Trading Interface) used to link an external application such as TradeStation and eSignal to NinjaTrader.

Reply
 
Thread Tools Display Modes
Old 04-26-2012, 11:01 AM   #1
kemosabe
Junior Member
 
Join Date: Jul 2011
Posts: 8
Thanks: 0
Thanked 0 times in 0 posts
Default Help with basic SMA Crossover strategy

I am trying to modify the Simple Moving Crossover strategy. I need to do the following to the basic strategy.
1. Add a limit entry (long or short) that triggers at crossover that has the ability to enter several ticks above or below the crossover entry point.
2. Add a automatic market exit (long or short) that triggers at next crossover.
3. If the limit entry was not taken (not reached) then the entry/exit cycle would be cancled and renewed at the next crossover.
I would like to use this in backtesting.
Thanks, I have tried but failed to do this.
kemosabe is offline  
Reply With Quote
Old 04-26-2012, 11:27 AM   #2
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,770
Thanks: 158
Thanked 562 times in 553 posts
Default

Hello,

Thank you for your post and welcome to our forums!

Would you be able to post a snippet of the code you have tried that did not work? This should help us identify what was not working and help you get the results you are expecting.

I look forward to assisting you further.
NinjaTrader_Matthew is offline  
Reply With Quote
Old 04-26-2012, 11:40 AM   #3
kemosabe
Junior Member
 
Join Date: Jul 2011
Posts: 8
Thanks: 0
Thanked 0 times in 0 posts
Default Kemosabe

I just used the SMA crossover stratgey supplied in Ninjatrader and unsucessfully tried to modify it. Then I gave up and returned it to normal and asked for help and started studying the help guides for strategies.
kemosabe is offline  
Reply With Quote
Old 04-26-2012, 11:55 AM   #4
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,770
Thanks: 158
Thanked 562 times in 553 posts
Default

Here is a basic example of the SampleMA modified to help get you started.

For your first requirement, we specify a limit order by using EnterLongLimit() or EnterShortLimit().

I have specified a price of 5 ticks from the current value of the slow SMA. You can change this to meet your needs.
Code:
if (CrossAbove(SMA(Fast), SMA(Slow), 1))
                EnterLongLimit(SMA(Slow)[0] - 5 * TickSize);
            else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
                EnterShortLimit(SMA(Slow)[0] + 5 * TickSize);
Next, we are using Position.MarketPosition to check if the strategy is in a long position AND checking for the opposite cross over to exit the long or short position.

Code:
            if (Position.MarketPosition == MarketPosition.Long && CrossBelow(SMA(Fast), SMA(Slow), 1))
            {
                ExitLong();
            }
            
            else if (Position.MarketPosition == MarketPosition.Short && CrossAbove(SMA(Fast), SMA(Slow), 1))
            {
                ExitShort();
            }
The default behavior for NinjaTrader is to cancel limit orders if the trigger conditions are no longer true. However this can be changed if you need. Please take a look at the following reference sample:

http://www.ninjatrader.com/support/f...ad.php?t=19169
NinjaTrader_Matthew is offline  
Reply With Quote
Old 04-26-2012, 12:06 PM   #5
kemosabe
Junior Member
 
Join Date: Jul 2011
Posts: 8
Thanks: 0
Thanked 0 times in 0 posts
Default Kemosabe

Thanks, let me give it a spin.
Do I have to insert this into the existing SMA strategy (if so, where) or does this serve as a stand alone?
Thanks so much.
kemosabe is offline  
Reply With Quote
Old 04-26-2012, 12:09 PM   #6
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,770
Thanks: 158
Thanked 562 times in 553 posts
Default

You can make a copy of the existing SampleMACrossovers by going to Tools--> Edit NinjaScript--> Indicators--> open the SampleMACrossovers

From the code, please right click on this window and select "Save As" and give it a new name.

Then you can freely edit and modify this script.
NinjaTrader_Matthew is offline  
Reply With Quote
Old 04-26-2012, 12:19 PM   #7
kemosabe
Junior Member
 
Join Date: Jul 2011
Posts: 8
Thanks: 0
Thanked 0 times in 0 posts
Default Kemosabe

Thanks....
kemosabe is offline  
Reply With Quote
Old 04-26-2012, 12:23 PM   #8
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,770
Thanks: 158
Thanked 562 times in 553 posts
Default

If this gets too complicated, I would recommend starting with the Strategy Wizard.
Please see our Help Guide articles on How to use the Strategy Wizard as well as NinjaScript Strategies. More information on these subjects can be found below:

There is also a 2 hour long Strategy Development Webinar conducted by our Product Manager Josh Peng hosted on the popular online trading community Big Mike Trading.com:

http://www.bigmiketrading.com/webina...y_development/
NinjaTrader_Matthew is offline  
Reply With Quote
Old 04-26-2012, 12:35 PM   #9
kemosabe
Junior Member
 
Join Date: Jul 2011
Posts: 8
Thanks: 0
Thanked 0 times in 0 posts
Default kemosabe

I figured where to replace the first part of the code but I am having some trouble inserting the second section in the right place.
We are allmost there.
kemosabe is offline  
Reply With Quote
Old 04-26-2012, 12:48 PM   #10
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,770
Thanks: 158
Thanked 562 times in 553 posts
Default

This would just go anywhere in the OnBarUpdate() method between the opening { and closing } (see screen shot)
Attached Images
File Type: png Capture.PNG (73.4 KB, 17 views)
NinjaTrader_Matthew 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
SMA Crossover ktinn Strategy Development 3 03-26-2012 05:07 AM
SMA CrossOver tacticaltrader General Programming 2 09-21-2011 05:25 PM
SMA/EMA crossover help? Trankuility Indicator Development 2 11-05-2010 09:24 AM
SMA crossover kaywai Strategy Development 13 12-21-2009 09:37 AM
making a sma crossover strategy quicker to enter and exit GONZO Strategy Analyzer 3 12-10-2009 03:41 PM


All times are GMT -6. The time now is 03:52 PM.