Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

converting strategy from nt7 ti nt8

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

    converting strategy from nt7 ti nt8

    good day to everyone,


    some time ago i found this sample strategy for nt7 in this post at morbidly obese mike's forum:


    https://futures.io/ninjatrader/2512-...-strategy.html


    the strategy compiles and works without problem in nt7. this sample strategy helped me to begin putting some ideas into ninjascript and i have used it as a platform to create some simple strategies of my own.

    Code:
    [INDENT]#region Variables
    [/INDENT]private int smalength = 120;
            private int emalength = 120;
            private int hmalength = 120;
            
            private int target1 = 12;
            private int target2 = 12;
            private int target3 = 12;
            
            private int stop = 12;
            
            private bool be2 = false;
            private bool be3 = false;
            
            
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                CalculateOnBarClose = true;
                EntryHandling = EntryHandling.UniqueEntries;
            }
    
            private void GoLong()
            {    
                SetStopLoss("target1", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
                SetStopLoss("target2", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
                SetStopLoss("target3", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
                
                
                SetProfitTarget("target1", CalculationMode.Price, Close[0] + (Target1*TickSize));
                SetProfitTarget("target2", CalculationMode.Price, Close[0] + ((Target1+Target2)*TickSize));
                SetProfitTarget("target3", CalculationMode.Price, Close[0] + ((Target1+Target2+Target3)*TickSize));
                
                EnterLong("target1");
                EnterLong("target2");
                EnterLong("target3");
            }
            
            
            private void GoShort()
            {    
                SetStopLoss("target1", CalculationMode.Price, Close[0] + (Stop*TickSize), false);
                SetStopLoss("target2", CalculationMode.Price, Close[0] + (Stop*TickSize), false);
                SetStopLoss("target3", CalculationMode.Price, Close[0] + (Stop*TickSize), false);
                
                
                SetProfitTarget("target1", CalculationMode.Price, Close[0] - (Target1*TickSize));
                SetProfitTarget("target2", CalculationMode.Price, Close[0] - ((Target1+Target2)*TickSize));
                SetProfitTarget("target3", CalculationMode.Price, Close[0] - ((Target1+Target2+Target3)*TickSize));
                
                EnterShort("target1");
                EnterShort("target2");
                EnterShort("target3");
            }
            
            private void ManageOrders()
            {    
                if (Position.MarketPosition == MarketPosition.Long)
                    
                    {
                        
                        if (BE2 && High[0] > Position.AvgPrice + (Target1*TickSize))
                        SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice, false);
                
            
                        if (BE3 && High[0] > Position.AvgPrice + ((Target1+Target2)*TickSize))
                        SetStopLoss("target3", CalculationMode.Price, Position.AvgPrice, false);
                        
                    }
                    
                    
                if (Position.MarketPosition == MarketPosition.Short)
            
    
            
                    {
                        
                        if (BE2 && Low[0] < Position.AvgPrice - (Target1*TickSize))
                        SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice, false);
                
            
                        if (BE3 && Low[0] < Position.AvgPrice - ((Target1+Target2)*TickSize))
                        SetStopLoss("target3", CalculationMode.Price, Position.AvgPrice, false);
                        
                    }
            
            }
            protected override void OnBarUpdate()
            {
                EntryHandling = EntryHandling.UniqueEntries;
            
                SMA        smav = SMA(smalength);
                EMA        emav = EMA(emalength);
                HMA        hmav = HMA(hmalength);
                
                
                ManageOrders();
                
                
                if (Position.MarketPosition != MarketPosition.Flat) return;
                
                
                if (Rising(smav) && Rising(emav) && Rising(hmav))
                    GoLong();
                
                
                else if (Falling(smav) && Falling(emav) && Falling(hmav))
                    GoShort();
                
                
            }

    - as an exercise, i am now trying to convert morbidly obsese mike's strategy from nt7 to nt8. this is as far as i have been able to go on my own:


    Code:
    {
            
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    
                    Description                                    = "morbidly obese mike's ema hma sma.";
                    Name                                        = "sampleemahmasma";
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 3;
                    EntryHandling                                = EntryHandling.UniqueEntries;
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 50;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 3;
                    StartBehavior                                = StartBehavior.AdoptAccountPosition;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = false;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelCloseIgnoreRejects;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 200;
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
                    IsInstantiatedOnEachOptimizationIteration    = true;
                    Smalength                    = 120;
                    Emalength                    = 120;
                    Hmalength                    = 120;
                    Target1                    = 12;
                    Target2                    = 12;
                    Target3                    = 12;
                    Stop                    = 12;
                    Be2                    = false;
                    Be3                    = false;
                    
                }
                else if (State == State.DataLoaded)
                {
                    
                    Smav[0] = SMA(Smalength)[0];
                    Emav[0] = EMA(Emalength)[0];
                    Hmav[0] = HMA(Hmalength)[0];
                    
                }
                
                                
            
            }
    
            protected override void OnBarUpdate()
            {
                
                ManageOrders();
                
                
                if (Position.MarketPosition != MarketPosition.Flat) return;
                
                
                if ( IsRising(Smav) && IsRising(Emav) && IsRising(Hmav))
                    GoLong();
                
                
                else if ( IsFalling(Smav) && IsFalling(Emav) && IsFalling(Hmav))
                    GoShort();
                
            }

    i haven't been able to include the manage orders, go long and go short private voids as i have no idea how to define and where to include them in nt8. ¿in which of the nt8 sections should i place them? ¿and how would the initialize and onbarupdate sections in nt7 correspond to the onstatechange, setdefaults, dataloaded and onbarupdate sections on nt8?


    very well, thanks, regards.
    Last edited by rtwave; 01-19-2018, 12:43 PM.

    #2
    Hello rtwave,

    Thanks for opening the thread.

    Private class member methods should be defined in the class you would like them to exist in, it would be the same as NT7.

    OnBarUpdate() code would still go in OnBarUpdate().

    Initialize() will mostly translate to State.SetDefaults in OnStateChange(). Adding additional data series should be done in State.Configure and custom resource creation should be done in State.DataLoaded. State.DataLoaded is a good place to put OnStratUp() code as well if your NT7 NinjaScript uses it.

    I will include links to publicly available resources where you can reference the Code Breaking Changes between NinjaTrader 7 and NinjaTrader 8 as well as further detail on OnStateChange() and the NinjaScript Lifecycle. The Best Practices page also explains further how the State System can be used.


    Good luck with your conversion!

    Please let us know if you have any questions.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by algospoke, Yesterday, 06:40 PM
    2 responses
    20 views
    0 likes
    Last Post algospoke  
    Started by ghoul, Today, 06:02 PM
    3 responses
    14 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by jeronymite, 04-12-2024, 04:26 PM
    3 responses
    45 views
    0 likes
    Last Post jeronymite  
    Started by Barry Milan, Yesterday, 10:35 PM
    7 responses
    21 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by AttiM, 02-14-2024, 05:20 PM
    10 responses
    181 views
    0 likes
    Last Post jeronymite  
    Working...
    X