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

EMA crossover signal (bar late)

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

    EMA crossover signal (bar late)

    I have an issue on my strategy that when the slow EMA crosses the Fast i want it to buy on that bar. however its waiting one bar after - how do i get it to enter on current bar with the cross
    Attached Files
    Last edited by SevanKambel; 04-12-2023, 09:26 PM.

    #2
    Hello SevanKambel,

    Calculate.OnPriceChange (with TickReplay and 1-tick intra-bar granularity for historical).

    Below is a link to a forum post that discusses.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello SevanKambel,

      Calculate.OnPriceChange (with TickReplay and 1-tick intra-bar granularity for historical).

      Below is a link to a forum post that discusses.
      https://forum.ninjatrader.com/forum/...992#post782992
      Ok that doesnt help .. In tradingview it puts the entry at the close of the bar.. the entry for Ninja Trader it shows the entry on the next bar... not sure why but it makes a difference .. is there a way to create a entry at the very last second of the close of the bar so it tags that bar?

      Comment


        #4
        Hello SevanKambel,

        With Calculate OnBarClose the logic is evaluated, and any orders submitted, after the bar has fully closed.

        If the order is sent after the bar is closed, then it was sent after the new bar opened.

        If the order is submitted before the bar is closed with Calculate.OnPriceChange, then the order appears on that bar.

        Is this historical or real-time?

        To confirm, you have implemented 1-tick intra-bar granularity, enabled TickReplay, and set Calculate to OnPriceChange, but the order is still appearing on the next bar?

        This would imply that something was not implemented correctly.

        Are you certain that you are submitting the orders to BarsInProgress 1 of the 1-tick series?
        If you Print("Calculate: " + Calculate); from OnBarUpdate() to the output window, this is showing that Calculate is OnPriceChange?
        If you Print("State: " + State); from OnBarUpdate() when the order is submitted, is this showing real-time?
        If you Print("Bars.IsTickReplay: " + Bars.IsTickReplay); from OnBarUpdate(), this is showing true?
        If you Print("BarsArray.Count: " + BarsArray.Count); this is outputting a value of 2? (For the primary bars and the added 1 tick series)


        is there a way to create a entry at the very last second of the close of the bar so it tags that bar?
        You would need to be able to predict the bar is going to close. On a time based bar, like a 1 minute bar, this would be fairly easy. You can add a 1 second series, and trigger the action in BarsInProgress 2, when Times[2][0] is equal to Times[0][0].AddSeconds(-1).
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I have tried this all - attached is my code also a screen shot from Tradingview and from NINJA and you will see the difference in entry and exit... NINJA takes a 95%winrate strategy to a massive loser by having the wrong bar entry

          public class FuturesGold : Strategy
          {
          private double ATRStop;
          private double ATRPrice;

          private EMA EMA1;
          private EMA EMA2;
          private SMA SMA1;
          private RSI RSI1;
          private ATR ATR1;
          private EMA EMA3;
          private EMA EMA4;

          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"Enter the description for your new custom Strategy here.";
          Name = "FuturesGold";
          Calculate = Calculate.OnPriceChange;
          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;
          ATRPeriod = 14;
          ATRMultiply = 0.3;
          ATRStop = 1;
          ATRPrice = 1;
          }
          else if (State == State.Configure)
          {
          }
          else if (State == State.DataLoaded)
          {
          EMA1 = EMA(Close, 7);
          EMA2 = EMA(Close, 26);
          SMA1 = SMA(Close, 50);
          RSI1 = RSI(Close, 17, 1);
          ATR1 = ATR(Close, Convert.ToInt32(ATRPeriod));
          EMA3 = EMA(Close, 7);
          EMA4 = EMA(Close, 26);
          EMA1.Plots[0].Brush = Brushes.Blue;
          EMA2.Plots[0].Brush = Brushes.Red;
          AddChartIndicator(EMA1);
          AddChartIndicator(EMA2);
          }
          }

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

          if (CurrentBars[0] < 1)
          return;

          // Set 1
          if ((CrossAbove(EMA1, EMA2, 1))
          && (Close[0] > SMA1[0])
          && (RSI1.Avg[0] <= 60)
          && (RSI1.Avg[0] >= 40))
          {
          EnterLong(Convert.ToInt32(DefaultQuantity), "");
          ATRPrice = (ATR1[0] * ATRMultiply) ;
          ATRStop = (Position.AveragePrice - (ATRPrice)) ;
          }

          // Set 2
          if ((Position.MarketPosition == MarketPosition.Long)
          || (CrossBelow(EMA3, EMA4, 1)))
          {
          ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), ATRStop, "", "");
          ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
          }

          }​
          Attached Files

          Comment


            #6
            Hello SevanKambel,

            Below is a link to a forum post that demonstrates how to use Print() and TraceOrders to understand behavior.


            The issue you are having is that the order is appearing at the open of the bar after the signal bar, is this correct?

            Please provide print output that shows the strategy is submitting the order before the bar closes and not after the bar closes.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hello SevanKambel,

              Below is a link to a forum post that demonstrates how to use Print() and TraceOrders to understand behavior.


              The issue you are having is that the order is appearing at the open of the bar after the signal bar, is this correct?

              Please provide print output that shows the strategy is submitting the order before the bar closes and not after the bar closes.
              I provided the full strategy code... what code should i add to make sure it is submitting the order before the bar closes?? Say the last tick of the 15min bar or 5min bar it submits the order..

              Comment


                #8
                Hello SevanKambel,

                The strategy code provided does not show 1-tick intra-bar granularity has been implemented, does not have prints added to show what Calculate setting has been chosen in the strategy parameters window (iIf the value is set in State.SetDefaults, the defaults are only pulled for new instances, and the user can still change the setting), and does not print to show that TickReplay is enabled.

                To make sure the order is being submitted before the bar closes in historical, implement 1-tick intra-bar granularity, enable TickReplay, and set Calculate to OnPriceChange. (Ensure that the condition is not checking for IsFirstTickOfBar to be true).

                Please provide the requested print output and we can verify these things.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hello SevanKambel,

                  The strategy code provided does not show 1-tick intra-bar granularity has been implemented, does not have prints added to show what Calculate setting has been chosen in the strategy parameters window (iIf the value is set in State.SetDefaults, the defaults are only pulled for new instances, and the user can still change the setting), and does not print to show that TickReplay is enabled.

                  To make sure the order is being submitted before the bar closes in historical, implement 1-tick intra-bar granularity, enable TickReplay, and set Calculate to OnPriceChange. (Ensure that the condition is not checking for IsFirstTickOfBar to be true).

                  Please provide the requested print output and we can verify these things.
                  Is this not just for backtesting? I have done backtest with those settings and it wasnt any different ... its obvious i need something to tell it to use the actual bar that crosses the ema... your own strategy EMA crossover DEMO does the same exact thing.. starts on the bar after the crossover instead of that bar

                  Comment


                    #10
                    Hello SevanKambel,

                    You would not be able to tell the order what bar it will fill on. Instead, you can only choose to submit the order while the bar is open, instead of waiting for it to close before sending the order.

                    If the order is submitted while the bar is open, the order will appear on that bar. If the order is submitted after the bar closes, it will appear at the open of the next bar.

                    The SampleMACrossover included with NinjaTrader was not designed to place orders before the bar closes in historical data.

                    In the forum post on why orders appear on the next bar I have linked in post # 2, there is a link to a forum post on intra-bar granularity, with a link to the help guide reference sample 'SampleIntrabarBacktest_NT8' that is designed to place orders before the bar closes.
                    Be sure to run the sample with TickReplay enabled and Calculate set to OnPriceChange in the strategy parameters window.

                    Below is a direct link in case you are choosing not to read the information I have provided you.
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by ETFVoyageur, Today, 02:08 AM
                    0 responses
                    7 views
                    0 likes
                    Last Post ETFVoyageur  
                    Started by kujista, 04-22-2024, 07:46 AM
                    3 responses
                    12 views
                    0 likes
                    Last Post kujista
                    by kujista
                     
                    Started by kujista, 04-23-2024, 06:23 AM
                    7 responses
                    57 views
                    0 likes
                    Last Post kujista
                    by kujista
                     
                    Started by SentientDavid, Today, 01:34 AM
                    0 responses
                    8 views
                    0 likes
                    Last Post SentientDavid  
                    Started by MrForgetful, Today, 01:28 AM
                    0 responses
                    6 views
                    0 likes
                    Last Post MrForgetful  
                    Working...
                    X