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

Modified AutoTrendH with filters

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

    Modified AutoTrendH with filters

    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    namespace NinjaTrader.Strategy
    {

    [Description("Demonstration of AutoTrendH Indicator")]
    //
    public class AutoTrendHDemo : Strategy
    {
    #region Variables
    // User Variables
    //User variables for AutoTrendH indicator settings
    private bool alert =true;
    private bool showHistory =true;
    private int strength =25; // Good number for 15 minute time frame (my preferred trading period)
    // User variables used for Stop strategys (not implimented in this strategy)
    private int stopEven = 4; // Move exit to breakeven after profitable stopEven pips/ticks
    private int stopProfit = 16; // Move stop up after profitable move of stopProfit pips/ticks
    private int stopShock = 4; // Exit trade is sudden negative excursion of stopShock pips/ticks. NOTE: Use tick or 1 minute secondary time frame to monitor
    private int stopLoss = 16; // Initial stopLoss setting upon trade entry.
    private int stopReverse = 0; // Reverse trade after stopReverse pips/ticks
    private int quantity = 1; // DefaultQuantity to trade (one full lot in Forex)
    private int eMAslowslow = 200; // Default setting for EMASlowSlow
    private int eMAslowfast = 180; // Default setting for EMASlowFast
    private int starttime = 100000;
    private int stoptime = 120000;
    private int startdate = 20180101;
    private int stopdate = 20181231;
    // Constants
    int cBreakUp=1; int cBreakDown =-1;
    int cRising =1; int cFalling =-1; int cFlat=0;
    int cLong =1; int cShort =-1;
    bool debug = false;
    #endregion
    //
    protected override void Initialize()
    { Add(EMA(Low, EMAslowfast));
    Add(EMA(Low, EMAslowslow));
    DefaultQuantity = Quantity;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    CalculateOnBarClose = true;
    Add(AutoTrendH(alert,showHistory,strength));
    if (stopLoss>0) SetStopLoss(CalculationMode.Ticks,stopLoss);
    if (stopProfit>0) SetProfitTarget(CalculationMode.Ticks,stopProfit);

    //if (stopShock>0) Add(PeriodType.Minute, 1); // If we are monitoriing for price shocks, add lower time bar/tick for higher resolution detection
    //if (stopLoss>0) SetStopLoss(CalculationMode.Ticks,stopLoss);
    }//ENDInitialize()
    //
    protected override void OnBarUpdate()

    {
    if (ToDay(Time[0])>= startdate && ToDay(Time[0]) <= stopdate)
    {
    if ((ToTime(Time[0]) >= starttime && ToTime(Time[0]) <= stoptime))
    {
    //STOP Price Shock
    if (stopShock>0)
    {// If negative Intrabar excursion more then stopShock pips, exit trade
    }
    if (BarsInProgress != 0) return; //We only want the primary bars processing from this point on.
    //
    //PRELOAD
    //I use this as my NT is not set to account for the tick price difference in the JPY's
    if ( (Historical) && (Instrument.FullName == "$USDJPY") || (Instrument.FullName=="$EURJPY") )
    { DefaultQuantity=(int)(quantity*.01); }
    //Preload the AutoTrendH values this bar for later use in the strategy
    int trendDirection = AutoTrendH(alert,showHistory,strength).Direction; //1=TrendUp, -1=TrendDown, 0=New trend not yet determined
    double trendPrice = AutoTrendH(alert,showHistory,strength).TrendPrice; //Tick value at rightmost bar of current trend line
    int trendSignal = AutoTrendH(alert,showHistory,strength).Signal; //1=resistance break, -1=support break

    //ENTRYS


    // If price breaks through trend, reverse position.
    if ( (trendDirection==cFalling) && (Close[0]>trendPrice)
    && EMA(Low, EMAslowfast)[0] > EMA(Low, EMAslowslow)[0])
    EnterLong("long");
    if ( (trendDirection==cRising) && (Close[0]<trendPrice)
    && EMA(Low, EMAslowfast)[0] < EMA(Low, EMAslowslow)[0])
    EnterShort( "short");


    }
    }
    }

    //USER DEFINED METHODS
    //
    private int myPosition(int posNbr)
    {// A cleaner way of coding position direction. Use posNbr if writing strategy with multiple position entries on different insturments/timeframes
    if (Positions[posNbr].MarketPosition==MarketPosition.Long) return cLong;
    if (Positions[posNbr].MarketPosition==MarketPosition.Short) return cShort;
    if (Positions[posNbr].MarketPosition==MarketPosition.Flat) return cFlat;
    return cFlat;
    }//endMyPosition()
    //




    #region Properties
    //
    [Description("Sets audible and logs alert if set to true")]
    [GridCategory("Parameters")]
    public bool Alert
    { get { return alert; }
    set { alert = value; }
    }
    //
    [Description("Saves trendlines of auto-generated Trends")]
    [GridCategory("Parameters")]
    public bool ShowHistory
    { get { return showHistory; }
    set { showHistory = value; }
    }
    //
    [Description("Sets the granularity of trend detection (smaller # = finer trend detection")]
    [GridCategory("Parameters")]
    public int Strength
    { get { return strength; }
    set { strength = Math.Max(1, value); }
    }
    //
    [Description("Lock in profits after StopProfit pips/ticks")]
    [GridCategory("Parameters")]
    public int StopProfit
    { get { return stopProfit; }
    set { stopProfit = value; }
    }
    //
    [Description("1 Minute timeframe catastrophic loss stop")]
    [GridCategory("Parameters")]
    public int StopShock
    { get { return stopShock; }
    set { stopShock = value; }
    }
    //
    [Description("Move stop to breakeven after StopEven pips/ticks")]
    [GridCategory("Parameters")]
    public int StopEven
    { get { return stopEven; }
    set { stopEven = value; }
    }
    //
    [Description("Initial StopLoss on entry")]
    [GridCategory("Parameters")]
    public int StopLoss
    { get { return stopLoss; }
    set { stopLoss = value; }
    }
    //
    [Description("Reverse position after StopReverse pips/ticks")]
    [GridCategory("Parameters")]
    public int StopReverse
    { get { return stopReverse; }
    set { stopReverse = value; }
    }
    //
    [Description("Number of shares/contracts/Lots to buy")]
    [GridCategory("Parameters")]
    public int Quantity
    { get { return quantity; }
    set { quantity = Math.Max(0, value); }
    }
    public int Startdate
    {
    get { return startdate; }
    set { startdate = Math.Max(1, value); }
    }
    [Description("trend filter")]
    [GridCategory("Parameters")]
    public int EMAslowslow
    {
    get { return eMAslowslow; }
    set { eMAslowslow = Math.Max(1, value); }
    }
    [Description("trend filter")]
    [GridCategory("Parameters")]
    public int EMAslowfast
    {
    get { return eMAslowfast; }
    set { eMAslowfast = Math.Max(1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]

    public int Stoptdate
    {
    get { return stopdate; }
    set { stopdate = Math.Max(1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int Starttime
    {
    get { return starttime;}
    set { starttime = Math.Max(1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int Stoptime
    {
    get { return stoptime; }
    set { stoptime = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #2
    Its used for ES, tested it on Sep Contract.

    Need to download AutoTrendH from Ninjatrader 7 Strategy Sharing and delete the strategy code and copy and paste the above code.

    Comment


      #3
      screen image
      Attached Files

      Comment


        #4
        Is this supposed to run with market replay? It won't stay enabled unless connected live,

        Also, I think this is missing for startdate:

        [Description("")]
        [GridCategory("Parameters")]

        Comment


          #5
          Hello ScorpioTravis,

          Thanks for your post.

          If it does not stay enabled, that suggests there may be an error with the strategy.

          Please check the "Log" tab of the NinjaTrader control center for any error messages related to the strategy.

          I created a new strategy file rather than modify the shell of an existing file, based on the contents of post #1 and added your observed missing parts.

          I was able to run this with market replay, no issues to report.

          I used the default properties except for the start and end date and changed those to fit the market replay period.



          Paul H.NinjaTrader Customer Service

          Comment


            #6
            I didn't do a thing to it, yet after overnight I was able to enable and run it connected to Market Replay.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by CortexZenUSA, Today, 12:46 AM
            0 responses
            0 views
            0 likes
            Last Post CortexZenUSA  
            Started by usazencortex, Today, 12:43 AM
            0 responses
            2 views
            0 likes
            Last Post usazencortex  
            Started by sidlercom80, 10-28-2023, 08:49 AM
            168 responses
            2,262 views
            0 likes
            Last Post sidlercom80  
            Started by Barry Milan, Yesterday, 10:35 PM
            3 responses
            10 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by WeyldFalcon, 12-10-2020, 06:48 PM
            14 responses
            1,429 views
            0 likes
            Last Post Handclap0241  
            Working...
            X