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

Strategy Analyzer - Strange Entries in Swing Breakout/Breakdown Strategy

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

    Strategy Analyzer - Strange Entries in Swing Breakout/Breakdown Strategy

    Making a copy of Bill Williams Fractals strategy and while I've tried a few different ways to get the orders to enter consistently none seem to be working. Given that it's a breakout strategy that uses previous swings it should only purchase when the price crosses the swing however it's not doing that at the minute. It's pretty simple and I've tried it with 3 different order entry methods. I am curious if someone can review this and give a better method for order entry? In the picture I tried to mark the points to show where entries were entered when they shouldn't have been. There's also a few missed entries.

    The debugging produces the right swing values so I'm at a loss.

    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 BillWilliams : Strategy
    {
    private Swing Swing1;
    private WisemanAlligator Alligator1;
    private double lips0;
    private double jaws0;
    private double teeth0;
    private double swinglow0;
    private double swinghigh0;
    private double close0;
    
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Fractal and Alligator";
    Name = "BillWilliams";
    Calculate = Calculate.OnPriceChange;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = false;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 1;
    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;
    SwingPeriod = 15;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    Swing1 = Swing(Close, SwingPeriod);
    Alligator1 =WisemanAlligator(13,8,5,8,5,3);
    Swing1.Plots[0].Brush = Brushes.DarkCyan;
    Swing1.Plots[1].Brush = Brushes.Goldenrod;
    AddChartIndicator(Swing1);
    AddChartIndicator(Alligator1);
    }
    }
    
    protected override void OnBarUpdate()
    {
    jaws0 = Alligator1.Jaw[0];
    teeth0 = Alligator1.Teeth[0];
    lips0 = Alligator1.Lips[0];
    
    close0 = Close[0];
    
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBar < 100)
    return;
    swinghigh0 = High[Math.Max(0, Swing1.SwingHighBar(0,1,100))];
    swinglow0 = Low[Math.Max(0, Swing1.SwingLowBar(0,1,100))];
    
    // Up trend Entry
    
    if(lips0 > teeth0 && teeth0> jaws0 && close0 > swinghigh0 && Position.MarketPosition == MarketPosition.Flat)
    //if(lips0 > teeth0 && close0 == swinghigh0 && Position.MarketPosition == MarketPosition.Flat)
    //if(CrossAbove(Close,swinghigh0,0))
    {
    EnterLong();
    Print("Long entered at: " + close0);
    Print("Last Swing High: " + swinghigh0);
    Print(" ");
    }
    
    //Down Trend
    if(lips0 < teeth0 && teeth0 < jaws0 && close0 < swinglow0 && Position.MarketPosition == MarketPosition.Flat)
    //if(lips0 < teeth0 && close0 == swinglow0 && Position.MarketPosition == MarketPosition.Flat)
    //if(CrossBelow(Close,swinglow0,0))
    {
    
    EnterShort();
    Print("Short entered at: " + close0);
    Print("Last Swing Low: " + swinglow0);
    Print(" ");
    }
    
    if(Alligator1.Lips[0] < Alligator1.Teeth[0] && Position.MarketPosition == MarketPosition.Long)
    {
    if(CrossBelow(Alligator1.Lips, Alligator1.Teeth,1))
    {
    BarBrushes[0] = Brushes.Blue;
    ExitLong();
    
    }
    
    }
    
    if(Alligator1.Lips[0] > Alligator1.Teeth[0] && Position.MarketPosition == MarketPosition.Short)
    {
    if(CrossAbove(Alligator1.Lips,Alligator1.Teeth,1))
    {
    BarBrushes[0] = Brushes.Purple;
    ExitShort();
    }
    }
    
    
    
    
    // Set 1
    // if (GetCurrentBid(0) == Swing1.SwingHigh[0])
    // {
    // EnterLong(Convert.ToInt32(DefaultQuantity), "");
    // }
    
    }
    
    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="SwingPeriod", Order=1, GroupName="Parameters")]
    public int SwingPeriod
    { get; set; }
    #endregion
    
    }
    }
    Attached Files
    Last edited by itsthefriz; 11-04-2021, 04:11 PM.

    #2
    Hello itsthefriz,

    From the given details I couldn't really say what may be wrong, but I can say that printing the swing would be only part of the debugging process. Because your conditions use other variables it would be best to output those other variables as well. That would let you know on each bar why the condition was or was not true.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Thank you for the reply but unfortunately it does not really help me in any way, I've attached another picture of the strategy in action where it's printing the close0 value at the time of entry along with the swing value. I agree that in a lot of the cases printing variables is the first step of debugging but with the simplicity here it looks like there's a different issue. Note the order entry conditions where if close0 = the swing value then enter in the direction. Note again the picture shows that the entry is always coming late and always quite a few points after the entry is called for, usually 5~15 points after the condition has actually been met.

      I guess my concern is the way my entry conditions are defined may not be coded effectively for the strategy analyzer?
      Attached Files

      Comment


        #4
        Hello itsthefriz,


        I guess my concern is the way my entry conditions are defined may not be coded effectively for the strategy analyzer?
        I couldn't really say, you will need to output the variables for the condition to see how it equates in the analyzer. You may need to make modifications for historical processing if your condition evaluates differently than realtime.

        Another note would be printing the Close at the time of the entry is not relevant except that you used it in the condition, if you are looking for the Fill price you need to add the OnExecutionUpdate override so you can get the actual execution and its average fill price.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Mestor, 03-10-2023, 01:50 AM
        16 responses
        388 views
        0 likes
        Last Post z.franck  
        Started by rtwave, 04-12-2024, 09:30 AM
        4 responses
        31 views
        0 likes
        Last Post rtwave
        by rtwave
         
        Started by yertle, Yesterday, 08:38 AM
        7 responses
        29 views
        0 likes
        Last Post yertle
        by yertle
         
        Started by bmartz, 03-12-2024, 06:12 AM
        2 responses
        22 views
        0 likes
        Last Post bmartz
        by bmartz
         
        Started by funk10101, Today, 12:02 AM
        0 responses
        7 views
        0 likes
        Last Post funk10101  
        Working...
        X