NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


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.

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old 03-07-2008, 10:54 AM   #1
DaveS
Senior Member
 
Join Date: Feb 2006
Location: London U.K.
Posts: 152
Thanks: 3
Thanked 2 times in 2 posts
Default TS indicator that trades on Ninjatrader Sim

Looking through this forum I noticed that several people have had problems using the DLL interface to trade Ninja from TS. Initialy I had the same problems and even started to think about using the email route. I then recalled one of the more arcane aspects of TS, how it likes to do things at the end of a bar only. It turned out that this was the behaviour that was causing my problems, values of TS variables do not persist from tick to tick by default, they revert to the last end of bar value. This will certainly upset any attempt to trade through the DLL on a tick by tick basis, setting 'Update value Intrabar' to true is not sufficient. The answer is to declare all variables that can possibly change value, to be 'IntrabarPersist'.
It is probably also a good idea to 'flag' orders sent to the DLL so that no further actions are taken till the last order has been processed.
Here is an indicator that includes these aspects at trades MA crossovers, it works fine,
****BUT DO NOT EXPECT TO MAKE PROFITS WITH IT****
*****IT IS FOR DEMONSTRATION PURPOSES ONLY******
You would need to substitute your own signals.
It has inputs to restrict trading to within set times, probably best to alter the defaults to suite your location.
Another input sets a time when an exit will be forced.
You can turn trading on and off by setting the input 'Trade' to true or false

Here is the code, I hope it helps.
Code:
inputs: 
		Price( 0.5*(High + Low )), FastLength( 7 ), SlowLength( 12 ), Trade( False ),
 		AllowEntriesFrom( 1430 ), AllowEntriesTill( 2045 ), ForceExitAt( 2059);

variables:
		IntrabarPersist FastMA( 0 ), IntrabarPersist SlowMA( 0 ), AllowEntries(False), 
		IntrabarPersist MktPosn( 0 ), IntrabarPersist OrderID( " " ), IntrabarPersist Ordered( "No" ) ;

If Time >= AllowEntriesFrom and Time <= AllowEntriesTill then
	AllowEntries = True
Else
	AllowEntries = False;

FastMA = AverageFC(Price, FastLength) ;
SlowMA = AverageFC(Price, SlowLength) ;

Plot1( FastMA, "FastMA" ) ;
Plot2( SlowMA, "SlowMA" ) ;

If NTConnected(1) and LastBarOnChart and Trade = True and AllowEntries = True then
Begin
	
	MktPosn = NTMarketPosition( "" ) ;

	// If the initial long or short is in place, set Ordered = "No" to allow further actions
	If Ordered = "Long" or Ordered = "Short" then
		Begin
		If MktPosn <> 0 then
			Ordered = "No";
		End;

	// If a long has toggled to a short.
	If Ordered = "LongToShort" then
		Begin
		If MktPosn = -1 then
			Ordered = "No";
		End;

	// If a short has toggled to a long.
	If Ordered = "ShortToLong" then
		Begin
		If MktPosn = 1 then
			Ordered = "No";
		End;

	// If first trade is a long.
	If MktPosn = 0 and Ordered = "No" and FastMA Crosses Above SlowMA then	
	Begin
		OrderID = NTNewOrderID;
		If NTBuyMarket(OrderID, 1) = 0 then
			Ordered = "Long";	// No further actions can take place till Ordered = "No"
	End;

	// If first trade is a short.
	If MktPosn = 0 and Ordered = "No" and FastMA Crosses Below SlowMA then	
	Begin
		OrderID = NTNewOrderID;
		If NTSellMarket(OrderID, 1) = 0 then
			Ordered = "Short";	// No further actions can take place till Ordered = "No"
	End;

	// Toggle second and subsequent trades long to short.
	If MktPosn = 1 and Ordered = "No" and FastMA Crosses Below SlowMA then	
	Begin
		OrderID = NTNewOrderID;
		If NTSellMarket(OrderID, 2) = 0 then
			Ordered = "LongToShort";	// No further actions can take place till Ordered = "No"
	End;

	// Toggle second and subsequent trades short to long.
	If MktPosn = -1 and Ordered = "No" and FastMA Crosses Above SlowMA then
	Begin
		OrderID = NTNewOrderID;
		If NTBuyMarket(OrderID, 2) = 0 then
			Ordered = "ShortToLong";	// No further actions can take place till Ordered = "No"
	End;
End;

If NTConnected(1) and LastBarOnChart and Trade = True and Time >= ForceExitAt then
Begin
	
	MktPosn = NTMarketPosition( "" ) ;

	// If the final exit is complete.
	If Ordered = "ExitLong" or Ordered = "ExitShort" then
		Begin
		If MktPosn = 0 then
			Ordered = "No";
		End;

	// Get flat from a long at end of trading.
	If MktPosn = 1 and Ordered = "No"  then
	Begin
		OrderID = NTNewOrderID;
		If NTSellMarket(OrderID, 1) = 0 then
			Ordered = "ExitLong";	// No further actions can take place till Ordered = "No"
	End;

	// Get flat from a short at end of trading.
	If MktPosn = -1 and Ordered = "No" then
	Begin
		OrderID = NTNewOrderID;
		If NTBuyMarket(OrderID, 1) = 0 then
			Ordered = "ExitShort";	// No further actions can take place till Ordered = "No"
	End;
End;
DaveS is offline  
Reply With Quote
 

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
Bid/Ask spread indicator updating even if no trades take place RedDuke General Programming 10 05-12-2008 03:29 PM
PowerZone Trading Launches Indicator Package for the NinjaTrader Platform NinjaTrader_Ray News and Announcements 0 10-26-2007 09:48 AM
Indicator aware of trades and orders bboyle1234 General Programming 1 09-25-2007 10:26 AM
"The namespace Ninjatrader.Indicator already contains a definition for '<indicator>'" eeisen Strategy Development 3 09-02-2007 09:47 PM
Unable to create instance of 'NinjaTrader.Indicator.BuiltInIndicator' SuzyG Indicator Development 3 01-08-2007 05:14 PM


All times are GMT -6. The time now is 05:08 AM.