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

On first tick development - EMA

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

    On first tick development - EMA

    Hello community,

    I have an issue again with the 1st tick indicator development

    I am trying to replicate the EMA indicator so it can only calculate on the 1st tick but I am have significant difference between the result. It seems a little much that I have to recode all the indicators so I can be used with a strategy the works on eachticks

    any idea why we have this behavior ?

    Thank you

    Here the code of the modified indicator

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
     public class EMAFroceonbarclose : Indicator
     {   private double constant1;
        private double constant2;
    
      protected override void OnStateChange()
      {
    
       if (State == State.SetDefaults)
       {
        Description         = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionEMA;
        Name          = "EMAFroceonbarclose";
        IsOverlay     = true;
        IsSuspendedWhileInactive = true;
        Period      = 14;
        AddPlot(Brushes.Goldenrod, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameEMA);
       }
       else if (State == State.Configure)
       {
        constant1 = 2.0 / (1 + Period);
        constant2 = 1 - (2.0 / (1 + Period));
       }
      }
    
      protected override void OnBarUpdate()
      {
    
       if (CurrentBar == 0)
        Value[0] = Input[0];
    
       if (CurrentBar < 2)
        return;
    
       if (IsFirstTickOfBar)
       {
        Value[0]  = (CurrentBar == 0 ? Input[1] : Input[1] * constant1 + constant2 * Value[2]);
    
       }
       else
        Value[0] = Value[1];
      }
    
      #region Properties
      [Range(1, int.MaxValue), NinjaScriptProperty]
      [Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)]
      public int Period
      { get; set; }
      #endregion
    
     }
    }


    Click image for larger version

Name:	Capture.JPG
Views:	386
Size:	107.1 KB
ID:	1092787





    #2
    Hello madams212121,

    When using a strategy that has Calculate set to OnEachTick, any indicators called from that strategy will also be using OnEachTick.

    You could choose to only call the SMA indicator from the strategy on the first tick of a new bar.

    Are you trying to change the calculation or just improve performance?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      well the problem was, trailing stop only update the value at candle close, so I have been developing a strategy utilized strategy based on each tick. did you see any issue with my coding for the EMA that calculate on the first tick ?

      Comment


        #4
        Hello madams212121,

        I'm not sure that I understand. You can have custom logic for exit orders and have them only update when the bar closes while the strategy is Calculating on each tick if you would like.
        This would not require re-coding indicators.

        Below are links to examples of custom exit order movement logic.




        That said, the logic in your script is different that the SMA indicator included with NinjaTrader.

        I would expect that different calculations would produce different results.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          This is the same calculation of the EMA indicator not SMA currently included in the ninja trader platform

          if (IsFirstTickOfBar) // Added to calculate on the only on the 1st tick of the candle
          {
          Value[0] = (CurrentBar == 0 ? Input[1] : Input[1] * constant1 + constant2 * Value[2]);

          }
          else // if different than 1st tick of the candle
          Value[0] = Value[1];
          }

          if the indicator calculate at candle close , checking the value of the first tick should not change the calculation of the EMA indicator .

          This was recommended by another customer service NinjaTrader_Jim to do it this way .

          Hello, I have a challenge, I am trying to use the trailingstop that is updating onpricechange and not onbarclose. Basically ( see picture below) 1- if the price goes below the green line entry long with trailing stop 2- if the price goes above the red line entry short with trailing stop the problem if I call that

          Comment


            #6
            I found the solution


            Value[1] = Input[1] * constant1 + constant2 * Value[2];


            and it working fine now

            thank you

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by love2code2trade, 04-17-2024, 01:45 PM
            4 responses
            31 views
            0 likes
            Last Post love2code2trade  
            Started by cls71, Today, 04:45 AM
            2 responses
            10 views
            0 likes
            Last Post eDanny
            by eDanny
             
            Started by proptrade13, Today, 11:06 AM
            0 responses
            5 views
            0 likes
            Last Post proptrade13  
            Started by kulwinder73, Today, 10:31 AM
            1 response
            10 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by RookieTrader, Today, 09:37 AM
            3 responses
            15 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X