Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

TrailStop with Strategy Builder

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

    TrailStop with Strategy Builder

    Hello - I am having trouble with the TrailStop in Strategy Builder. When I run the strategy through the Strategy Analyzer it exits on every bar. Any suggestions? I am attaching a Chart Image and the Code. Thank you for your help.


    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class E1RgaussZ : Strategy
    {
    private ZLEMA ZLEMA1;
    private RGaussianFilter RGaussianFilter1;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "E1RgaussZ";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    Zlema_period = 8;
    RGauss_period = 34;
    TrailStop_value = 16;
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Tick, 1);
    AddDataSeries(Data.BarsPeriodType.Day, 1);
    }
    else if (State == State.DataLoaded)
    {
    ZLEMA1 = ZLEMA(Close, Convert.ToInt32(Zlema_period));
    RGaussianFilter1 = RGaussianFilter(Close, 3, Convert.ToInt32(RGauss_period));
    ZLEMA1.Plots[0].Brush = Brushes.Red;
    RGaussianFilter1.Plots[0].Brush = Brushes.Blue;
    AddChartIndicator(ZLEMA1);
    AddChartIndicator(RGaussianFilter1);
    SetTrailStop("", CalculationMode.Ticks, TrailStop_value, false);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if (ZLEMA1[0] > RGaussianFilter1[0])
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"Long671");
    }

    // Set 2
    if (ZLEMA1[0] < RGaussianFilter1[0])
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), @"Short671");
    }

    }

    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="Zlema_period", Order=1, GroupName="Parameters")]
    public int Zlema_period
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="RGauss_period", Order=2, GroupName="Parameters")]
    public int RGauss_period
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="TrailStop_value", Order=3, GroupName="Parameters")]
    public int TrailStop_value
    { get; set; }
    #endregion

    }
    }


    #2
    Hello JohnS52,

    As a tip, to export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
    1. Click Tools -> Export -> NinjaScript...
    2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
    3. Click the 'Export' button
    4. Enter a unique name for the file in the value for 'File name:'
    5. Choose a save location -> click Save
    6. Click OK to clear the export location message
    By default your exported file will be in the following location:
    • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
    Below is a link to the help guide on Exporting NinjaScripts.



    With the running instance of the strategy, what is the TrailStop_value set to?

    If this is increased to 50 are you still seeing the trail stop fill on every bar?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Attached is the zip file. The default value for TrailStop is 16: it often fires at the same point on the bar as the entry price. Changing it to 50 does make it more of a multi-bar tool. Optimizer chooses a value of 5 as being best. I wonder what I am doing wrong. Thanks for any guidance.
      Attached Files

      Comment


        #4
        Hello JohnS52,

        The default value may not be the value being used in the script if it is changed in the parameters. The defaults just set the initial values.

        What is the value being used by the script that is either set in the parameters (or shown in prints)?

        To confirm, if you increase the distance the order does not fill as quickly? If so, just increase the distance.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        43 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        124 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        65 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        42 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X