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

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 WHICKED, Today, 12:56 PM
        2 responses
        15 views
        0 likes
        Last Post WHICKED
        by WHICKED
         
        Started by Felix Reichert, Today, 02:12 PM
        0 responses
        1 view
        0 likes
        Last Post Felix Reichert  
        Started by Tim-c, Today, 02:10 PM
        0 responses
        1 view
        0 likes
        Last Post Tim-c
        by Tim-c
         
        Started by cre8able, Today, 01:16 PM
        2 responses
        9 views
        0 likes
        Last Post cre8able  
        Started by chbruno, 04-24-2024, 04:10 PM
        3 responses
        49 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Working...
        X