Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy not executing

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

    Strategy not executing

    Hi,
    I have strategy that is not executing. Since I am new the coding I am not sure where the problem is . Can someone check it and let me know why it does not place order etc...It is simple SMA crossover (10, 25) to place the order. I use stop loss and training stops also. Here is the code.

    thanks in advance
    Bill

    #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>
    /// Cross simple MA 10 and 25
    /// </summary>
    [Description("Cross simple MA 10 and 25")]
    public class BillMACross : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int pos1 = 2; // Default setting for Pos1
    private int pos2 = 2; // Default setting for Pos2
    private int fastMA = 10; // Default setting for FastMA
    private int slowMA = 25; // Default setting for SlowMA
    private int psize = 1; // Default setting for Psize
    // User defined variables (add any user defined variables below)
    #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()
    {
    SetStopLoss("LLong1", CalculationMode.Ticks, 4, false);
    SetTrailStop("LLong2", CalculationMode.Ticks, 5, false);
    SetStopLoss("Short1", CalculationMode.Ticks, 4, false);
    SetTrailStop("Short2", CalculationMode.Ticks, 5, false);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(Close, SMA(10), 1)
    && CrossAbove(SMA(10), SMA(25), 1))
    {
    EnterLong(Pos1, "Long1");
    EnterLong(Pos2, "Long2");
    }

    // Condition set 2
    if (CrossBelow(Close, SMA(10), 1)
    && Position.Quantity > Psize)
    {
    ExitLong("", "");
    }

    // Condition set 3
    if (CrossBelow(Close, SMA(10), 1)
    && CrossBelow(SMA(14), SMA(25), 1))
    {
    EnterLong(Pos1, "Short1");
    EnterLong(Pos2, "Short2");
    }

    // Condition set 4
    if (CrossAbove(Close, SMA(10), 1)
    && Position.Quantity > Psize)
    {
    ExitShort("", "");
    }
    }

    #2
    Hi billsingh,

    Thanks for your post.

    Have you added any prints to your code to ensure that the entry condition is evaluating as true?

    For example:
    if (CrossAbove(Close, SMA(10), 1)
    && CrossAbove(SMA(10), SMA(25), 1))
    {
    Print(Time[0]+" - close crossed sma 10 and sma 10 crossed sma 25");
    EnterLong(Pos1, "Long1");
    EnterLong(Pos2, "Long2");
    }

    Prints will show in the Output window. (Tools -> Output Window...)

    May we see the output of this print?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I will do it when I get home and post the output here

      Thank you.

      Comment


        #4
        Here is the output for last few day. The condition occurs few times even in an hour but I am not sure why NT does not show that. I am using NQ as an instrument.

        **NT** Enabling NinjaScript strategy 'BillMACross/7b1e9eb4592e4636bee735cceed47a26' : On starting a real-time strategy - StrategySync=WaitUntilFlat SyncAccountPosition=False EntryHandling=AllEntries EntriesPerDirection=10 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=True/ triggering 30 before close Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=False CancelExitOrdersOnDisable=True CalculateOnBarClose=True MaxRestarts=4 in 5 minutes
        10/31/2014 12:07:00 PM - close crossed sma 10 and sma 10 crossed above sma 25
        10/31/2014 1:56:00 PM - close crossed sma 10 and sma 10 crossed above sma 25
        11/2/2014 3:25:00 PM - close crossed sma 10 and sma 10 crossed below sma 25
        11/2/2014 4:51:00 PM - close crossed sma 10 and sma 10 crossed above sma 25
        11/2/2014 7:01:00 PM - close crossed sma 10 and sma 10 crossed above sma 25
        11/2/2014 10:20:00 PM - close crossed sma 10 and sma 10 crossed above sma 25
        11/3/2014 1:43:00 AM - close crossed sma 10 and sma 10 crossed above sma 25
        11/3/2014 4:53:00 PM - close crossed sma 10 and sma 10 crossed above sma 25
        11/3/2014 5:09:00 PM - close crossed sma 10 and sma 10 crossed below sma 25
        11/3/2014 7:57:00 PM - close crossed sma 10 and sma 10 crossed below sma 25
        11/4/2014 12:09:00 AM - close crossed sma 10 and sma 10 crossed above sma 25
        11/4/2014 5:01:00 PM - close crossed sma 10 and sma 10 crossed below sma 25
        11/5/2014 12:01:00 AM - close crossed sma 10 and sma 10 crossed above sma 25

        Comment


          #5
          Hi billsingh,

          May I test your script on my end?

          If so, please attach an export of the script to your next post.

          To export your script do the following:
          1. Click File -> Utilities -> Export NinjaScript
          2. Enter a unique name for the file in the value for 'File name:'
          3. Select the strategy from the objects list on the left -> click the right facing arrow ">" to add the strategy to the export
          4. Click the 'Export' button -> click 'yes' to add any referenced indicators to the export -> click OK to clear the export location message


          By default your exported file will be in the following location:
          • (My) Documents/NinjaTrader 7/bin/Custom/ExportNinjaScript/<export_file_name.zip>


          Below is a link to the help guide on Exporting NinjaScripts.
          http://www.ninjatrader.com/support/h...nt7/export.htm
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Here it is. Thank you
            Attached Files

            Comment


              #7
              Hello billsingh,

              I have added your strategy to a chart.

              I am showing trades on every bar that there is a print for.

              If you right-click the chart -> select Strategy Performance -> BillMACross -> Historical & Real-Time, what number is in the Total # of Trades row?
              Attached Files
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Hi Chelsea,
                But there these conditions are true lot more times. If you draw the SMA on your screen you will see. I am also using Heikashi2 as candles and NQ as an instrument with 2 ticks. Will that make any difference ????

                Comment


                  #9
                  Hi billsingh,

                  Just to clarify, you are seeing trades on your end, however, you believe there should be more trades, is this correct?

                  I would recommend that you print the values in your conditions.

                  For example to check for a cross above on an SMA:

                  Print(Time[0]+" - Close[1]: "+Close[1]+" < SMA(10)[1]: "+SMA(10)[1]+" && Close[0]: "+Close[0]+" > SMA(10)[0]: "+SMA(10)[0]);

                  This prints the value of the close of the previous bar and the SMA(10) of the previous bar, and also prints the current bars close value and the value of the SMA(10) on the current bar. The close should be less than the SMA on the previous bar and greater than the SMA on the current bar for a crossabove to be detected.

                  Do this for all of the crosses you are attempting to detect in your code. This will explain why a cross was detected or not detected.

                  If it is not making sense, post the output.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi Chelsea,
                    I have attached the output. I have also attached the Jpeg, where NT suppose to place the orders but (green for long and red for short) what it did not happen ???

                    Thanks in advance
                    Bill
                    Attached Files

                    Comment


                      #11
                      Hi billsingh,

                      Place this print outside of the condition, so that you can see what the values are when the condition is not true.

                      Also, in the screenshot, I am not seeing any crossing moving averages where the circles are. What should the code be detecting in that circle?
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Hi Chelsea,
                        I will put that code in the condition and send the output. I guess I will have to change the code what I am trying to show you on the picture. So if you can help me with this.

                        What will be the code to check if I have long or short position already ???

                        What will be the code to check if I do not have any position ???

                        Thanks
                        Bill

                        Comment


                          #13
                          Hi billsingh,

                          To check to see if there is a long position:

                          if (Position.MarketPosition == MarketPosition.Long)

                          To check for no position:

                          if (Position.MarketPosition == MarketPosition.Flat)

                          http://www.ninjatrader.com/support/h...etposition.htm


                          Again, be sure you move the print to outside of any conditions.



                          Also, I do not understand what you are trying to show me in the picture.
                          What rules are you trying to highlight?
                          Why should there be a trade in the circle?
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Awesome Chelsea,

                            another quick question ?

                            what is the code to test if RSI > 50 is it RSI(0)>50 ???
                            and MACD Ossilator > 0 ???

                            thanks
                            Bill

                            Comment


                              #15
                              Hello billsingh,

                              if (RSI(14, 3)[0] > 50)
                              http://www.ninjatrader.com/support/h..._index_rsi.htm

                              if (MACD(12, 26, 9)[0] > 0)
                              http://www.ninjatrader.com/support/h...gence_macd.htm

                              Let me know if I may still assist.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by funk10101, Today, 09:43 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post funk10101  
                              Started by pkefal, 04-11-2024, 07:39 AM
                              11 responses
                              37 views
                              0 likes
                              Last Post jeronymite  
                              Started by bill2023, Yesterday, 08:51 AM
                              8 responses
                              44 views
                              0 likes
                              Last Post bill2023  
                              Started by yertle, Today, 08:38 AM
                              6 responses
                              26 views
                              0 likes
                              Last Post ryjoga
                              by ryjoga
                               
                              Started by algospoke, Yesterday, 06:40 PM
                              2 responses
                              24 views
                              0 likes
                              Last Post algospoke  
                              Working...
                              X