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

New-code problems

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

    New-code problems

    I am testing a simple system in AAPL, 365 days of data but I get just one trade that is closed at the last day, meaning that profit target PT and stop-loss SL of 2% do not work
    Thanks


    Code:
    #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 POL : Strategy
        {
            #region Variables
            // Wizard generated variables
            private double pT = 2.000; // Default setting for PT
            private double sL = 2.000; // Default setting for SL
            // 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()
            {
                SetProfitTarget("", CalculationMode.Percent, PT);
                SetStopLoss("", CalculationMode.Percent, SL, true);
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Condition set 1
                if (Close[0] > Close[10])
                {
                    EnterLong(DefaultQuantity, "");
                }
    			
    			
            }
    
            #region Properties
            [Description("profit target")]
            [GridCategory("Parameters")]
            public double PT
            {
                get { return pT; }
                set { pT = Math.Max(1, value); }
            }
    
            [Description("stop-loss")]
            [GridCategory("Parameters")]
            public double SL
            {
                get { return sL; }
                set { sL = Math.Max(1, value); }
            }
            #endregion
        }
    }

    #2
    Hello ricko,

    Thank you for your post.

    So I may investigate this matter further please answer the following questions.

    What version of NinjaTrader are you using? You can check under Help -> About (Example: 7.0.1000.30)

    Who are you connected to? This is displayed by green on lower left corner of the Control Center window.

    What interval is selected for your chart(s)? (Example: Tick, Minute, Day, etc.)

    I look forward to your response.

    Comment


      #3
      Thanks. v 7 1000.30 Connected to yahoo. Interval day.

      Note that your moving average cross example works with the AAPL chart and I get a backtest with signals, etc. My strategy I quoted does not work. The first entry signal is correct but then no exit until the last bar..

      Comment


        #4
        Hello ricko,

        Thank you for your response.

        Are you trying to do 200%, or 2%? If 2%, try 0.02 instead of 2.00.

        Comment


          #5
          Hello Partick,

          I wanted 2% profit target and 2% stop-loss. I changed the 2 to 0.02 but again same thing: first entry point and then no exit until last bar. I used AAPL daily data 365 bars. Here is the code.

          #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>
          /// Percent, open, long
          /// </summary>
          [Description("Percent, open, long")]
          public class PercOL : Strategy
          {
          #region Variables
          // Wizard generated variables
          private double pT = 0.02; // Default setting for PT
          private double sL = 0.02; // Default setting for SL
          // 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()
          {
          SetProfitTarget("", CalculationMode.Percent, PT);
          SetStopLoss("", CalculationMode.Percent, SL, true);

          CalculateOnBarClose = true;
          }

          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          {
          // Condition set 1
          if (Close[0] > Close[10])
          {
          EnterLong(DefaultQuantity, "");
          }
          }

          #region Properties
          [Description("Profit taregt")]
          [GridCategory("Parameters")]
          public double PT
          {
          get { return pT; }
          set { pT = Math.Max(0.02, value); }
          }

          [Description("Stop loss")]
          [GridCategory("Parameters")]
          public double SL
          {
          get { return sL; }
          set { sL = Math.Max(0.02, value); }
          }
          #endregion
          }
          }

          Comment


            #6
            I think I found the problem. It appears that I have to put a name for the signal and put that also in the profit target and stop-loss order.

            Comment


              #7
              Hello ricko,

              That is actually how it should be done. Although I would recommend an actual value rather than a blank string of "". So try "Long" for example.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by swestendorf, Today, 11:14 AM
              2 responses
              5 views
              0 likes
              Last Post NinjaTrader_Kimberly  
              Started by xiinteractive, 04-09-2024, 08:08 AM
              4 responses
              12 views
              0 likes
              Last Post xiinteractive  
              Started by Mupulen, Today, 11:26 AM
              0 responses
              1 view
              0 likes
              Last Post Mupulen
              by Mupulen
               
              Started by Sparkyboy, Today, 10:57 AM
              1 response
              5 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by TheMarlin801, 10-13-2020, 01:40 AM
              21 responses
              3,917 views
              0 likes
              Last Post Bidder
              by Bidder
               
              Working...
              X