Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SMA Crossover on Hrly Plotted on lower time Frame Tick Chart

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

    SMA Crossover on Hrly Plotted on lower time Frame Tick Chart

    I'm getting very inconsistent results with my strategy via print statements. In addition, I cannot plot a Vertical line on a lower timeframe chart at an Hourly SMA crossover.

    Can someone please provide some suggestions for a solution?

    Here's what I have

    PHP Code:
       namespace NinjaTrader.NinjaScript.Strategies
    {
    public class xFastSMAoverUnder : Strategy
    {
    private bool BullishHourly;
    private bool BearishHourly;
    
    private SMA SMA3;
    private SMA SMA4;
    
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Buy when price crosses about the fast SMA (7 or 8). Sell visvers";
    Name = "xFastSMAoverUnder";
    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;
    PositionQty = 1;
    FastSMA = 8;
    SlowSMA = 20;
    BullishHourly = false;
    BearishHourly = false;
    }
    else if (State == State.Configure)
    {
    AddDataSeries("NQ DEC24", Data.BarsPeriodType.Minute, 5, Data.MarketDataType.Last);
    AddDataSeries("NQ DEC24", Data.BarsPeriodType.Minute, 60, Data.MarketDataType.Last);
    }
    else if (State == State.DataLoaded)
    {
    SMA1 = SMA(Close, Convert.ToInt32(FastSMA));
    SMA2 = SMA(Close, Convert.ToInt32(FastSMA));
    EMA1 = EMA(Close, 200);
    SMA3 = SMA(Closes[2], Convert.ToInt32(FastSMA));
    SMA4 = SMA(Close, Convert.ToInt32(SlowSMA));
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < 10
    || CurrentBars[1] < 0)
    return;
    
    if (IsFirstTickOfBar == true)
    {
    }
    
    if (CrossAbove(SMA3, SMA4, 1))
    { // Bullish
    Draw.VerticalLine(this, "Hourly Cross" + CurrentBar, 0, Brushes.Lime, DashStyleHelper.Dash, 2);
    Print("Bar Close: " + Close[0] + " At Time: " + Time[0]);
    BullishHourly = true;
    BearishHourly = false;
    }
    }
    }
    }&#8203;  ​ 
    

    #2
    Hello nelslynn,

    Thank you for your post.

    I'm getting very inconsistent results with my strategy via print statements.
    Can you please provide more details? How exactly are the results inconsistent? What are the results you are getting vs the expected results?

    In addition, I cannot plot a Vertical line on a lower timeframe chart at an Hourly SMA crossover.
    Drawing objects can only be drawn on the primary price panel or their own separate panel. They cannot be drawn on an added secondary timeframe.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Inconsistent = Providing the wrong values...Vertical lines are in the wrong place.

      Vertical lines are drawn on the hourly chart and the main tick chart. They are correct on the hourly chart but not correct on the main chart. I will provide a screenshot later. In the meantime does the code look correct?

      Comment


        #4
        Here is a screenshot. It seems one plot is placed around an hour after midnight as well as other plots that are wrong.

        The screenshot shows a red arrow where the lines should be placed. I realize we can't get exact results but the plots/lines are several hours off
        Attached Files

        Comment


          #5
          Hello nelslynn,

          To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

          In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order.

          The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

          The debugging print output should clearly show what the condition is, what time the conditions are being compared, all values being compared, and how they are being compared.

          Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

          Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

          After enabling TraceOrders remove the instance of the strategy from the Configured list in the Strategies window and add a new instance of the strategy from the Available list.

          I am happy to assist you with analyzing the output from the output window.

          Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

          Below is a link to a support article that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.

          https://support.ninjatrader.com/s/ar...nd-TraceOrders
          Gaby V.NinjaTrader Customer Service

          Comment


            #6
            Here is the output. There should only be 2-3 lines and you can see from the screenshot there are several and they are in the wrong location. I also attached an hourly chart to show where the line should be drawn.
            Thanks!
            Attached Files

            Comment


              #7
              Hello nelslynn,

              Unfortunately these prints aren't descriptive enough provide any insight as to why the line is being drawn when not expected.

              The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

              The debugging print output should clearly show what the condition is, what time the conditions are being compared, all values being compared, and how they are being compared.

              The print should also be 1 line above the condition.

              For example, based on the code you have posted here:​

              Print(Times[0][0] + " SMA3[1]: " + SMA3[1] + " < SMA4[1]: " + SMA4[1] + " & SMA3[0]: " + SMA3[0] + " > SMA4[0]: " + SMA4[0]);

              Gaby V.NinjaTrader Customer Service

              Comment


                #8
                See the attached. Note the variable change:

                PHP Code:
                SMA5 = SMA(Closes[2], Convert.ToInt32(FastSMA)); // Fast SMA on Hour Chart
                SMA6 = SMA(Close, Convert.ToInt32(SlowSMA)); // Slow SMA on Hour Chart​​
                
                  if  (CrossAbove(SMA5, SMA6, 1))
                            { 
                                Draw.VerticalLine(this, "Bull Hourly Cross" + CurrentBar, 0, Brushes.Lime, DashStyleHelper.Dash, 2);
                                Print("SMA Cross ABOVE " + Times[0][0] + " SMA5[1]: " + SMA5[1] + " < SMA6[1]: " + SMA6[1] + " & SMA5[0]: " + SMA5[0] + " > SMA6[0]: " + SMA6[0]);
                            }
                            
                            if  (CrossBelow(SMA5, SMA6, 1))
                            { 
                                Draw.VerticalLine(this, "Bear Hourly Cross" + CurrentBar, 0, Brushes.Pink, DashStyleHelper.Dash, 2);
                                Print("SMA Cross BELOW " + Times[0][0] + " SMA5[1]: " + SMA5[1] + " < SMA6[1]: " + SMA6[1] + " & SMA5[0]: " + SMA5[0] + " > SMA6[0]: " + SMA6[0]);
                            }
                &#8203; 
                
                Thanks
                Attached Files

                Comment


                  #9
                  Hello nelslynn,

                  The prints need to be 1 line above the condition, currently you have them inside the conditions.

                  Additionally, you have the same print for CrossAbove as you have for CrossBelow. You would need to switch the logic for CrossBelow.

                  Lastly, please note which date/timestamp we are looking at where you are seeing the line being drawn that you're not expecting.
                  Gaby V.NinjaTrader Customer Service

                  Comment


                    #10
                    All the lines drawn are unexpected and off by 2-4 hours (no line is correct). I have used a print statement where the lines are being drawn.
                    Attached Files

                    Comment


                      #11
                      Hello nelslynn,

                      I understand the lines are off, however, what date/timestamp in particular in the code are we examining (a timestamp where the condition to draw the line was true, and the line was drawn incorrectly)?

                      Please include a screenshot the demonstrates that that particular drawing object (same tag name and timestamp) on the chart does not match up with the output.
                      Gaby V.NinjaTrader Customer Service

                      Comment


                        #12
                        The print statements indicate where the lines are being drawn. Look for " ---------------- Cross BELOW Line, Close of Bar: 21703.25 At Time: 12/12/2024 10:35:34 AM"

                        See attached for other requests.
                        Attached Files

                        Comment


                          #13
                          Hello nelslynn,

                          I understand that - I am asking for you to provide a screenshot of the specific drawing object the output you are referencing below is referring to.

                          Your screenshot is just a screenshot of the chart, with no indication about which line this is output is corresponding to.

                          SMA Cross BELOW 12/12/2024 10:35:34 AM SMA5[1]: 21717.9375 &gt; SMA6[1]: 21710.5625 &amp; SMA5[0]: 21708.1875 &lt; SMA6[0]: 21710.9
                          ---------------- Cross BELOW Line, Close of Bar: 21703.25 At Time: 12/12/2024 10:35:34 AM

                          You're supplying 0 as the barsAgo value, so the drawing object should be drawn at the current bar processing at the time the condition becomes true. For the above output, I would expect this object to be drawn around the 12/12/2024 10:35:34 mark.

                          You could also print out of the CurrentBar value at the time the line is being drawn, then look at the bar index of the bar the line is drawn on. Do these values coincide?

                          If you're not sure, print out the tag name you are giving the drawing object, then find that drawing object on the chart. What bar is it drawn on - is it the same index and CurrentBar value?

                          Please provide a screenshot that points out that specific drawing object.


                          You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services.

                          Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.
                          Last edited by NinjaTrader_Gaby; 12-13-2024, 09:04 AM.
                          Gaby V.NinjaTrader Customer Service

                          Comment


                            #14
                            Never mind, I figured it out.

                            Had this:

                            PHP Code:
                            SMA5                = SMA(Closes[2], Convert.ToInt32(FastSMA));      // Fast SMA on Hour Chart
                            SMA6                = SMA(Close, Convert.ToInt32(SlowSMA));&#8203; 
                            
                            Instead of this:

                            PHP Code:
                            SMA5                = SMA(Closes[2], Convert.ToInt32(FastSMA));
                            SMA6                = SMA(Closes[2], Convert.ToInt32(SlowSMA));&#8203; 
                            
                            Suggestion next time, instead of going through debugging via print statements, look at the code first. It would have saved us a lot of time. That's all I was really asking since I have just started learning this code 2 weeks ago.

                            Are there not any videos about the basics of coding with NinjaTrader? The Script Editor is useless for elaborate Strategies.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by tmk-c, 07-19-2017, 11:29 PM
                            6 responses
                            1,685 views
                            0 likes
                            Last Post Brightredmegaphone  
                            Started by Trader4Life, Today, 09:20 PM
                            0 responses
                            2 views
                            0 likes
                            Last Post Trader4Life  
                            Started by WileCoyote, Today, 08:26 PM
                            0 responses
                            8 views
                            0 likes
                            Last Post WileCoyote  
                            Started by madankumars, 11-18-2019, 11:12 AM
                            10 responses
                            175 views
                            0 likes
                            Last Post ulisesguerrero  
                            Started by rrsch, Today, 10:26 AM
                            2 responses
                            12 views
                            0 likes
                            Last Post rrsch
                            by rrsch
                             
                            Working...
                            X