Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adding Trailing Stop to MA Crossover Strategy

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

    Adding Trailing Stop to MA Crossover Strategy

    Hi,

    I am trying to add a trailing stop to the MA Crossover Strategy script. I am extremely new to coding so have tried a few things but am not succeeding. The SampleMACrossOver code is below. Any help is appreciated.

    //
    // Copyright (C) 2019, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class SampleMACrossOver : Strategy
    {
    private SMA smaFast;
    private SMA smaSlow;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = NinjaTrader.Custom.Resource.NinjaScriptStrategyDes criptionSampleMACrossOver;
    Name = NinjaTrader.Custom.Resource.NinjaScriptStrategyNam eSampleMACrossOver;
    Fast = 10;
    Slow = 25;
    // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = false;
    }
    else if (State == State.DataLoaded)
    {
    smaFast = SMA(Fast);
    smaSlow = SMA(Slow);

    smaFast.Plots[0].Brush = Brushes.Goldenrod;
    smaSlow.Plots[0].Brush = Brushes.SeaGreen;

    AddChartIndicator(smaFast);
    AddChartIndicator(smaSlow);
    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < BarsRequiredToTrade)
    return;

    if (CrossAbove(smaFast, smaSlow, 1))
    EnterLong();
    else if (CrossBelow(smaFast, smaSlow, 1))
    EnterShort();
    }

    #region Properties
    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "Fast", GroupName = "NinjaScriptStrategyParameters", Order = 0)]
    public int Fast
    { get; set; }

    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "Slow", GroupName = "NinjaScriptStrategyParameters", Order = 1)]
    public int Slow
    { get; set; }
    #endregion
    }
    }

    #2
    hello check this link https://ninjatraderecosystem.com/use...lingstop_101b/

    also this link https://www.screencast.com/t/IXoV3FnFr and https://ninjatrader.com/support/foru...rategy-builder
    Last edited by Emma1; 09-13-2019, 10:32 AM.

    Comment


      #3
      Thanks for the code. I'm trying to figure out how to put my code into the one you sent. Can you help me there?

      Comment


        #4
        public class SampleMACrossOver : Strategy
        {

        private SMA smaFast;
        private SMA smaSlow;
        private x;
        where 'private x' is added as part of the modification.

        Comment


          #5
          Hi,

          I'm looking for a trailing stop strategy that stops out below the low of previous bar (for a long). I assume this exists somewhere. If so, could you please help point me in the right direction?

          Thanks!

          Comment


            #6
            check this link:https://ninjatrader.com/support/foru...ategy?t=112106

            Comment


              #7
              Thank you Emma1!!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by RookieTrader, Today, 09:37 AM
              3 responses
              15 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by kulwinder73, Today, 10:31 AM
              0 responses
              5 views
              0 likes
              Last Post kulwinder73  
              Started by terofs, Yesterday, 04:18 PM
              1 response
              23 views
              0 likes
              Last Post terofs
              by terofs
               
              Started by CommonWhale, Today, 09:55 AM
              1 response
              4 views
              0 likes
              Last Post NinjaTrader_Erick  
              Started by Gerik, Today, 09:40 AM
              2 responses
              7 views
              0 likes
              Last Post Gerik
              by Gerik
               
              Working...
              X