Moving Averages/Trendlines/Time Intervals

Discussion in 'Strategy Building' started by Spectre2007, Sep 2, 2018.

  1. There is a surge in orderflow between 10:00-12:00 EST USA, the direction of the orderflow is not known. So you place EMA's /trendlines and give validation to enter trades for any signals generated. Once trade is placed a stop loss/profit target 1:1 ratio. Or 16 ticks for ES 200/200 dollars. Most of the trades are from breaks of trendlines/EMA crossovers.
     
    #11     Sep 9, 2018
  2. ah.PNG

    updated algo working well.
     
    #12     Sep 13, 2018
  3. autotrend2.PNG
     
    #13     Oct 28, 2018
  4. original code non modified did better.

    autotrend3.PNG
     
    #14     Oct 28, 2018
  5. fixed a error regarding start date input.
     
    #15     Oct 28, 2018
  6. param.PNG table.PNG grph.PNG
     
    #16     Oct 28, 2018
    beginner66 likes this.
  7. #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); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    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
    }
    }
     
    #17     Oct 28, 2018
    tommcginnis likes this.
  8. System generated a outlier .. after posting and giving it away.

    anamoly.PNG
     
    #18     Nov 10, 2018
  9. anoma.PNG
     
    #19     Nov 10, 2018
  10. tommcginnis

    tommcginnis

    Given that your average win is nearly your limit at $200 (+$195)
    and your average loss is dang close to -$200 at -$185, it'd be a sweet thing to bump that upside TP to $250 and dump that SL down to $175, and see what happens. I know your playing, so if I missed some rule or R:R convention to which your ascribing that was mentioned above, "Apologies."

    But, "Nice Thread!"
     
    #20     Nov 10, 2018