• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

TS indicator that trades on Ninjatrader Sim

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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;

    #2
    Dave,

    Thank you very much for sharing this with the community.
    It's highly appreciated!!
    Vince B.NinjaTrader Customer Service

    Comment


      #3
      My pleasure.
      All I have to do now is develop some signals that yeild profits.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Sulfred, Yesterday, 08:34 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_ChristopherD  
      Started by MercuryRed, Yesterday, 03:24 PM
      1 response
      16 views
      0 likes
      Last Post NinjaTrader_PaulH  
      Started by Anonytrade, Today, 05:18 AM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_Marco  
      Started by dave01star, Today, 02:20 AM
      2 responses
      6 views
      0 likes
      Last Post dave01star  
      Started by kahei626, Yesterday, 09:30 PM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_ChrisSch  
      Working...
      X