Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

trailing example not working right

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

    trailing example not working right

    Hi, I have the Long script working right (the trailer updates correctly).
    But on this BEAR script, it tries to update every tick.
    IDK what I'm doing wrong?
    I put BOLD on the important relevant parts so it would be easier for you. thanks

    Code:
    #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.Gui.Tools;
    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 BEARcrossWTrailer : Strategy
    {
    private int EntryBar;
    private double CurrentTriggerPrice;
    private double CurrentStopPrice;
    
    private EMA EMA1;
    private EMA EMA2;
    private Indicators.RegressionChannel RegressionChannel1;
    private EMA EMA3;
    private EMA EMA4;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"";
    Name = "BEARcrossWTrailer";
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 5;
    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;
    
    [B]TrailFrequency = 100;
    TrailStopDistance = 100;[/B]
    EntryBar = 1;
    CurrentTriggerPrice = 0;
    CurrentStopPrice = 0;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    EMA1 = EMA(Close, 8);
    EMA2 = EMA(Close, 34);
    RegressionChannel1 = RegressionChannel(Close, 100, 2);
    EMA3 = EMA(Close, 8);
    EMA4 = EMA(Close, 34);
    EMA1.Plots[0].Brush = Brushes.LimeGreen;
    EMA2.Plots[0].Brush = Brushes.Red;
    AddChartIndicator(EMA1);
    AddChartIndicator(EMA2);
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    // Set 1
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    CurrentStopPrice = 0;
    }
    
    if (CurrentBars[0] < 30)
    return;
    
    // Set 2
    if ((CurrentBars[0] != EntryBar)
    && (State == State.Realtime)
    && (Position.MarketPosition == MarketPosition.Flat)
    // Bar delay
    && ((BarsSinceExitExecution(0, "", 0) > 0)
    || (BarsSinceExitExecution(0, "", 0) == -1))
    && ([B]CrossBelow[/B](EMA1, EMA2, 1))
    && (Slope(RegressionChannel1.Middle, 30, 0) > -0.4))
    {
    [B]EnterShortLimit[/B](Convert.ToInt32(DefaultQuantity), GetCurrentAsk(0), @"Short");
    BarBrush = Brushes.Blue;
    EntryBar = Convert.ToInt32(CurrentBars[0]);
    [B]CurrentTriggerPrice = (Close[0] + (TrailFrequency * TickSize)) ;
    CurrentStopPrice = (Close[0] + (TrailStopDistance * TickSize)) ;[/B]
    Print(Convert.ToString(Slope(RegressionChannel1.Mi ddle, 30, 0)));
    }
    
    // Set 3
    if ((Position.MarketPosition == MarketPosition.Short)
    [B]&& (Close[0] < CurrentTriggerPrice))[/B]
    {
    [B]CurrentTriggerPrice = (Close[0] + (TrailFrequency * TickSize)) ;
    CurrentStopPrice = (Close[0] + (TrailStopDistance * TickSize)) ;[/B]
    }
    
    // Set 4
    if ((CurrentStopPrice != 0)
    && (Position.MarketPosition == MarketPosition.Short))
    {
    ExitShortStopMarket(Convert.ToInt32(DefaultQuantit y), CurrentStopPrice, "", "");
    Print(@"stop price");
    }
    
    // Set 5
    if ((Position.GetUnrealizedProfitLoss(PerformanceUnit .Currency, Close[0]) < -30)
    && (Position.MarketPosition == MarketPosition.Short))
    {
    ExitShort(Convert.ToInt32(DefaultQuantity), @"-$30", "");
    Print(@"-$30");
    }
    
    // Set 6
    if ((CrossAbove(EMA3, EMA4, 1))
    && (Position.MarketPosition == MarketPosition.Short))
    {
    ExitShort(Convert.ToInt32(DefaultQuantity), @"34 > 8", "");
    }
    
    }
    
    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="TrailFrequency", Order=1, GroupName="Parameters")]
    public int TrailFrequency
    { get; set; }
    
    [NinjaScriptProperty]
    [Range(-9999, int.MaxValue)]
    [Display(Name="TrailStopDistance", Order=2, GroupName="Parameters")]
    public int TrailStopDistance
    { get; set; }
    #endregion
    
    }
    }

    #2
    Hello ezrollin,

    Thanks for your post.

    I see that you have Calculate set to OnEachTick in State.SetDefaults. This would result in OnBarUpdate being called with each new tick when you are processing realtime data.

    You may change this to Calculate.OnBarClose to have logic updated with every bar closure.

    Let us know if you have any additional questions.

    Comment


      #3
      My good man. I tried changing and no improvement. The long side is OnEachTick and it works fine so I dont think that is the issue. Or I just think the long side is working fine and even though I'm happy with the way it is, it was a fluke? I dont know what I'm not seeing. thanks

      Comment


        #4
        Hello ezrollin,

        What additional issues are you experiencing?

        A) Do you expect an action to take place that is not occurring?

        B) Do you see an action take place that you do not expect?

        If A, I suggest identifying the specific action that you expect to take place, and the condition that controls it. Then use debugging prints to check the values used to evaluate that condition to see why it is not becoming true.

        If B,I would suggest using prints and reproducing the issue to see where that action is triggered. Next, use prints outside of the condition controlling that action to print out exactly how the condition is being evaluated. This will tell you why the condition becomes true, and if some part of that condition is not how you have expected it to evaluate, additional prints can be used to track how that variable gets set/modified.

        Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        38 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
        64 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        41 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