• 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

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:
    #region Variables
    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 indy73, Yesterday, 10:52 PM
    0 responses
    3 views
    0 likes
    Last Post indy73
    by indy73
     
    Started by martyn73, Yesterday, 07:04 PM
    0 responses
    5 views
    0 likes
    Last Post martyn73  
    Started by sergey_z, Yesterday, 02:56 PM
    1 response
    10 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by samir, Yesterday, 02:46 PM
    1 response
    20 views
    0 likes
    Last Post NinjaTrader_Kate  
    Started by neoikon, Yesterday, 02:24 PM
    1 response
    8 views
    0 likes
    Last Post NinjaTrader_ChristopherJ  
    Working...
    X