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

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;
    
    TrailFrequency = 100;
    TrailStopDistance = 100;
    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))
    && (CrossBelow(EMA1, EMA2, 1))
    && (Slope(RegressionChannel1.Middle, 30, 0) > -0.4))
    {
    EnterShortLimit(Convert.ToInt32(DefaultQuantity), GetCurrentAsk(0), @"Short");
    BarBrush = Brushes.Blue;
    EntryBar = Convert.ToInt32(CurrentBars[0]);
    CurrentTriggerPrice = (Close[0] + (TrailFrequency * TickSize)) ;
    CurrentStopPrice = (Close[0] + (TrailStopDistance * TickSize)) ;
    Print(Convert.ToString(Slope(RegressionChannel1.Mi ddle, 30, 0)));
    }
    
    // Set 3
    if ((Position.MarketPosition == MarketPosition.Short)
    && (Close[0] < CurrentTriggerPrice))
    {
    CurrentTriggerPrice = (Close[0] + (TrailFrequency * TickSize)) ;
    CurrentStopPrice = (Close[0] + (TrailStopDistance * TickSize)) ;
    }
    
    // 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.
    JimNinjaTrader Customer Service

    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
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Leeroy_Jenkins, Today, 06:35 AM
        0 responses
        5 views
        0 likes
        Last Post Leeroy_Jenkins  
        Started by moneyexe, Today, 01:38 AM
        3 responses
        21 views
        0 likes
        Last Post bltdavid  
        Started by EmmaaPerkin, Today, 12:29 AM
        0 responses
        9 views
        0 likes
        Last Post EmmaaPerkin  
        Started by appleacv, Today, 12:17 AM
        0 responses
        8 views
        0 likes
        Last Post appleacv  
        Started by ReingerJamir, Yesterday, 10:51 PM
        0 responses
        14 views
        0 likes
        Last Post ReingerJamir  
        Working...
        X