Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trades don't happen when they should.

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

    Trades don't happen when they should.

    I am running a very simple ma crossover auto strategy. In sim right now. I see a great problem on any time bar I try. When a crossover happens it may take from 2 to 6 bars later before a trade is made. Here is the strategy. Can someone tell me why the trade doesnt happen as soon as the crossover is made?

    //
    // Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //

    #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.Strategy;
    #endregion

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Simple moving average cross over strategy.
    /// </summary>
    [Description("moving average cross over strategy.")]
    public class MACrossOver : Strategy
    {
    #region Variables
    private int fast = 10;
    private int slow = 25;
    #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()
    {
    SMA(Fast).Plots[0].Pen.Color = Color.Red;
    SMA(Slow).Plots[0].Pen.Color = Color.Green;
    Add(SMA(Fast));
    Add(SMA(Slow));
    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick).
    /// </summary>
    protected override void OnBarUpdate()
    {

    if (CrossAbove(SMA(5), SMA(25), 10))
    EnterLong(1);
    if (CrossBelow(SMA(5), SMA(25), 10))
    EnterShort(1);


    }

    #region Properties
    /// <summary>
    /// </summary>
    [Description("Period for fast MA")]
    [GridCategory("Parameters")]
    public int Fast
    {
    get { return fast; }
    set { fast = Math.Max(1, value); }
    }

    /// <summary>
    /// </summary>
    [Description("Period for slow MA")]
    [GridCategory("Parameters")]
    public int Slow
    {
    get { return slow; }
    set { slow = Math.Max(1, value); }
    }
    #endregion
    }
    }
    Attached Files

    #2
    your strategy is working correctly; you are entering AFTER the bar closes, which would be the next bar's open.

    p/s - in real-time trading you can set the calculate on bar close to false to reduce the lag.

    Comment


      #3
      Look at the last trade. The cross was at 119,168 or so but the trade did not happen until 119,145 5 bars later, how is the correct when the strategy is supposed to trade on the bar close which should have been119, 155 or 119,150, so I changed it to false and it trades 4 bars after the cross over still 1 - 2 points late.. Its was set to trade on the bar CLOSE not anytime after the close and it was set to market trade which should have been sometime on the next bar after the cross over.

      Comment


        #4
        Hello,

        Thanks for your note.

        This is a historical trade on the chart or a backtest correct.

        In this case we dont open the position at the close instead open it at the open price of the next bar and this is the way NinjaTrader is setup to backtest. When you run on live data it will close at the bar close price but when you run on backtest or historical it will open or close on the open of the next bar. We do this for realism of what an actual fill would be like after the signal to buy or sell. In the case of the last bar the open price where the trade was opened at was a little higher then the close price of the signal bar. Therefor this is where you got filled at this price point.

        Let me know if I can be of further assistance.

        Comment


          #5
          No that was live trades using the sim101 account.
          Last night I started another with trade on close false. I will make a picture of the chart and send it later.

          Comment


            #6
            Here is a chart I have been running with the sim101 live trading and the moving average with the trigger on bar close false. I marked 2 extreme examples of trades happing way after the cross over. Can someone tell me why? This is not historical data the chart and strategy has been running.
            Attached Files

            Comment


              #7
              larrylwill,

              This is accurate behavior. Trades for cross overs will occur on the next tradeable location which is the next bar's open when looking at historical trades.

              Let us look at the second marked example. The signal bar that determines a cross over happened on the big green up bar. That is the bar the cross over happened. This signal is evaluated at the end of that bar and since it was evaluated when that bar was already closed no trading can be done until the next bar following it. That is where you see the trade being executed and that represents the correct time and location of the trade.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                You picked the best example. The crossover looks like it occurred on the small red bar, since it was set to buy on signal NOT the close why did it wait 2 bars. Even so lets look at another example.

                The first blue arrow the sell short cross occurred the same place that the previous buy was made, by the time the buy was made the signal had already turned around and should have immediately sold, instead it waited 10 bars. There was a whipsaw that generated a buy then a sell within 5 minuets.
                So why did it take 10 bars of 20min each to sell short after the signal? If I had a profit stop in the strategy I would have missed the move completely.

                Why doesn't the buy or sell trigger at the same time the bar crosses.These are 20 min bars if a signal crosses during the bar it should buy or sell within the same bar not several bars after. It is NOT set to buy on the bar close. If I was trading manually I would have bought as soon as I saw the cross and after the 2nd bar I would have sold on the 3th bar after 2:20 instead of being long. Then the cross over on the red bar between the 2 long green bars I would have reversed. Are you saying that a buy or sell is not generated untill after the 20 min bar closes no matter how its set up?
                I do not recall trade station having this problem. I used a strategy that was similar and it bought or sold at exactly the cross over not bars later.
                Last edited by larrylwill; 12-28-2010, 06:10 PM.

                Comment


                  #9
                  Larry, please recheck at the code you initially posted - your lookback is set for 10 bars on the crossovers, please set this to just 1, recompile the code and recheck the results you get. Trades should be placed one bar after the signal triggered with this change, which is accurate, conservative backtesting behavior.

                  Thanks,
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Ok thanks, I started a new run last night with the old one and now it appears to be trading on the next bar.
                    Its been running since midnight last night and the trade is right after the crossover. Thank you

                    Comment


                      #11
                      Great to hear Larry, thanks for reporting back.
                      BertrandNinjaTrader Customer Service

                      Comment


                        #12
                        Well its happing again. This is the same strategy I started 2 days ago.
                        Look at the last 2 trades. One traded without a crossover and the 2nd took 10 bars to trade after the crossover.
                        What is going on?
                        Attached Files

                        Comment


                          #13
                          Larry,

                          This is a live chart with a live trade,

                          Most likely your have COBC = False. Where this strategy runs on every tick. You must have had this set as this is the only way this trade would have been taken. As you can tell the bar it traded on was a down bar, most likely where the crossover did occur during that bar. Except before the bar was completed the crossover was invalidated since price action moved back up.

                          Let me know if I can be of further assistance.

                          Comment


                            #14
                            First I had it set to True and it was still up to 10 bars late trading so I was advised to set it to false, so its still trading up to 10 bars late. I understand what your saying about the first trade, that makes sense but the 10 bar late trade does not make sense. The 2nd trade should have happen during the next bar after the crossover or before. Not 10 bars later.

                            Comment


                              #15
                              Please explain the last 2 trades, this chart was copied at 1:10am, what is that at 1am and at 3am.
                              Could the chart be screwing up? Minimum bars = 1, MA = 5,25. The 1st 2 trades look correct, the 2nd to the last should not have happened looking at the 1 minute chart there is no way that a 20min moving average crossed over on that bar. The last one is in the future. If its correct then I have found the Holy Grail.
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by mjairg, 07-20-2023, 11:57 PM
                              3 responses
                              213 views
                              1 like
                              Last Post PaulMohn  
                              Started by TheWhiteDragon, 01-21-2019, 12:44 PM
                              4 responses
                              544 views
                              0 likes
                              Last Post PaulMohn  
                              Started by GLFX005, Today, 03:23 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post GLFX005
                              by GLFX005
                               
                              Started by XXtrader, Yesterday, 11:30 PM
                              2 responses
                              12 views
                              0 likes
                              Last Post XXtrader  
                              Started by Waxavi, Today, 02:10 AM
                              0 responses
                              7 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Working...
                              X