Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem

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

    Problem

    I am having problems with the following strategy when testing it live on the simulator, however historical trades all look correct. Entry is supposed to occur on the crossover of the MACD on the close of the bar. Somehow entries are occuring seemingly at random with an "Order Pending" status...

    #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

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Enter the description of your strategy here
    /// </summary>
    [Description("Enter the description of your strategy here")]
    public class dpMACD : Strategy
    {
    #region Variables
    // Wizard generated variables
    // User defined variables (add any user defined variables below)
    private int fastlength = 12;
    private int slowlength = 26;
    private int smooth = 9;

    private double stopamount = 3;
    private double profittarget = 2;

    private int maxdailylosses = 2;
    private int maxdailytrades = 6;

    private double starttime = 93000;
    private double stoptime = 160000;

    private bool tradingtime = false;

    private DataSeries macd = null;
    private DataSeries macdavg = null;

    private int tradestoday = 0;
    private int lossestoday = 0;

    private IOrder StopOrder = null;


    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    CalculateOnBarClose = true;

    macd = new DataSeries(this);
    macdavg = new DataSeries(this);
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    macd.Set(MACD(fastlength,slowlength,smooth)[0]);
    macdavg.Set(MACD(fastlength,slowlength,smooth).Avg[0]);

    // check for new day
    if (ToDay(Time[0])!=ToDay(Time[1]))
    {
    tradestoday = 0;
    lossestoday = 0;
    }

    tradingtime=ToTime(Time[0])>starttime && ToTime(Time[0])<stoptime;

    if (tradingtime)
    {
    // entries
    if (Positions[0].MarketPosition==MarketPosition.Flat && tradestoday<maxdailytrades && lossestoday<maxdailylosses)
    {
    if (macd[0]>macdavg[0] && macd[1]<=macdavg[1])//(CrossAbove(macd,macdavg,1))
    {
    EnterLong();
    tradestoday++;
    }

    if (macd[0]<macdavg[0] && macd[1]>=macdavg[1])//(CrossBelow(macd,macdavg,1))
    {
    EnterShort();
    tradestoday++;
    }
    }

    // long trade risk management
    if (Positions[0].MarketPosition==MarketPosition.Long)
    {
    // stop order
    double stopprice = Positions[0].AvgPrice-stopamount;
    if (Close[0]>stopprice)
    StopOrder=ExitLongStop(stopprice);
    else
    StopOrder=ExitLong();

    // profit order
    double profitprice = Positions[0].AvgPrice+profittarget;
    ExitLongLimit(profitprice);
    }

    // short trade risk management
    if (Positions[0].MarketPosition==MarketPosition.Short)
    {
    // stop order
    double stopprice = Positions[0].AvgPrice+stopamount;
    if (Close[0]<stopprice)
    StopOrder=ExitShortStop(stopprice);
    else
    StopOrder=ExitShort();

    // profit order
    double profitprice = Positions[0].AvgPrice-profittarget;
    ExitShortLimit(profitprice);
    }
    }
    else
    {
    if (Positions[0].MarketPosition==MarketPosition.Long)
    ExitLong();

    if (Positions[0].MarketPosition==MarketPosition.Short)
    ExitShort();
    }
    }

    /// <summary>
    /// Called on each incoming execution
    /// </summary>
    protected override void OnExecution(IExecution execution)
    {
    if (StopOrder!=null && execution.Order.Token==StopOrder.Token)
    lossestoday++;
    }

    #region Properties

    [Description("MACD Fast Length")]
    [Category("Parameters")]
    public int FastLength
    {
    get { return fastlength; }
    set { fastlength = value; }
    }

    [Description("MACD Slow Length")]
    [Category("Parameters")]
    public int SlowLength
    {
    get { return slowlength; }
    set { slowlength = value; }
    }

    [Description("MACD Smooth Length")]
    [Category("Parameters")]
    public int Smooth
    {
    get { return smooth; }
    set { smooth = value; }
    }

    [Description("Stop Loss Amount (Points)")]
    [Category("Parameters")]
    public double StopAmount
    {
    get { return stopamount; }
    set { stopamount = value; }
    }

    [Description("Profit Target Amount (Points)")]
    [Category("Parameters")]
    public double ProfitTarget
    {
    get { return profittarget; }
    set { profittarget = value; }
    }

    [Description("Maximum daily losses")]
    [Category("Parameters")]
    public int MaxDailyLosses
    {
    get { return maxdailylosses; }
    set { maxdailylosses = value; }
    }

    [Description("Maximum daily trades")]
    [Category("Parameters")]
    public int MaxDailyTrades
    {
    get { return maxdailytrades; }
    set { maxdailytrades = value; }
    }

    [Description("Time to start trading")]
    [Category("Parameters")]
    public double StartTime
    {
    get { return starttime; }
    set { starttime = value; }
    }

    [Description("Time to stop trading")]
    [Category("Parameters")]
    public double StopTime
    {
    get { return stoptime; }
    set { stoptime = value; }
    }

    #endregion
    }
    }

    #2
    absolute,

    What you want to do is use TraceOrders = true to track your orders. Then add yourself Print() throughout your code to track which code blocks are being executed. Isolate out the behavior and then you will be able to find when and where your orders are executing from.

    Please see these tips: http://www.ninjatrader-support2.com/...ead.php?t=3418
    Josh P.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    574 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    333 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    553 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    551 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X