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

Main strategy timeframe and secondary timeframe for take profit and stoploss

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

    Main strategy timeframe and secondary timeframe for take profit and stoploss

    Hello, I´ve coded a strategy for 240minute timeframe, but I have a stoploss and take profit for a certain amount of ticks. In initialize I have added:

    Code:
     Add(PeriodType.Tick, 1);
    Add(PeriodType.Minute, 240);
    SetStopLoss("", CalculationMode.Ticks, Mi_SL, false);
    SetProfitTarget(misenal, CalculationMode.Ticks, Mi_TP);
    This way it´s suppose to load both timeframes. But even hitting the Takeprofit value, does not close the trade. Am I missing anything?

    The complete code:

    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 AVANTEtp : Strategy
        {
            #region Variables
            // Wizard generated variables
            private int mi_TP = 20; // Default setting for Mi_TP
            private int mi_SL = 286; // Default setting for Mi_SL
            private int mi_QT = 10; // Default setting for Mi_QT
            private int myBar;
    	private bool Conditions = false;
    	private string misenal;
            // 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()
            {
                            Add(PeriodType.Tick, 1);
    			Add(PeriodType.Minute, 240);
    			SetStopLoss("", CalculationMode.Ticks, Mi_SL, false);
    			SetProfitTarget(misenal, CalculationMode.Ticks, Mi_TP);
    
                CalculateOnBarClose = true;
    			//ExitOnClose = true;
    			//ExitOnCloseSeconds = 30;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Condition set 1
                if (Close[2] < Open[0]
                    && Low[2] > Open[0])
                	{
                    	EnterLong(Mi_QT,"misenal");
    				
    					myBar = CurrentBar;
    				
                	}
    
                // Condition set 2
                if(myBar == CurrentBar - 1)
    					{
      						myBar = 0; //The next bar has occurred.
    						ExitLong("", "");
    					}
            }
    
            #region Properties
            [Description("mi valor de Take Profit")]
            [GridCategory("Parameters")]
            public int Mi_TP
            {
                get { return mi_TP; }
                set { mi_TP = Math.Max(1, value); }
            }
    
            [Description("mi valor de Stop Loss")]
            [GridCategory("Parameters")]
            public int Mi_SL
            {
                get { return mi_SL; }
                set { mi_SL = Math.Max(1, value); }
            }
    
            [Description("mi valor para cantidad")]
            [GridCategory("Parameters")]
            public int Mi_QT
            {
                get { return mi_QT; }
                set { mi_QT = Math.Max(1, value); }
            }
            #endregion
        }
    }
    Thank you in advanced.

    #2
    Hello,

    Thank you for the inquiry.

    Could you provide more detail on what you mean by But even hitting the Takeprofit value, does not close the trade. Are you seeing the StopLoss not being filled and remaining active, or something else?

    I had also noted that you are not checking the BarsInProgress. You are adding multiple series but not specifying which series executes logic. If you had intended that the logic gets executed for each tick, each 240 minute bar and each primary bar, the script would execute that way currently. To separate the logic further, you would need to use the BarsInProgress property: http://ninjatrader.com/support/helpG...barsinprogress

    Additionally, you may want to enable TraceOrders to see if you are running into any manage order handling rules. One example is trying to use an Exit order while a SetStopLoss is active, the Exit would be ignored.



    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you very much. What I meant was that my strategy timeframe is 240min, but meanwhile the next 240 minute bar finishes, if price moves 10 ticks I want to take profit, but never did it.

      I have a lot to learn. I know understand BarsInProgress property.

      So if I add these lines:

      Code:
      if (BarsInProgress == 0) 
          	{ 
       		SetProfitTarget(misenal, CalculationMode.Ticks, Mi_TP);
          	}
      Inside OnBarUpdate:

      Code:
       protected override void OnBarUpdate()
              {
                  // Condition set 1
                  if (Close[2] < Open[0]
                      && Low[2] > Open[0])
                  	{
                      	EnterLong(Mi_QT,"misenal");
      				
      					myBar = CurrentBar;
      					
      					[B]if (BarsInProgress == 0) 
          					{ 
       						SetProfitTarget(misenal, CalculationMode.Ticks, Mi_TP);
          					} [/B]
      				
                  	}
      Will my strategy close the trade if take profit value is reached?

      Thank you very much for your help.

      Comment


        #4
        Hello tomaslolo,

        Thanks for writing back.

        BarsInProgress starts with the primary data series that the strategy/indicator gets applied to and gets iterated for each data series added with Add().

        So BarsInProgress would equal 0 for the primary data series, 1 for the first data series added with Add(), and 2 for the second data series added with Add(). If your 1 tick data series is added as the first added data series, you would reference it with (BarsInProgress == 1).

        Please let me know if you have any additional questions.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by bmartz, 03-12-2024, 06:12 AM
        3 responses
        25 views
        0 likes
        Last Post NinjaTrader_Zachary  
        Started by Aviram Y, Today, 05:29 AM
        2 responses
        7 views
        0 likes
        Last Post Aviram Y  
        Started by gentlebenthebear, Today, 01:30 AM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by cls71, Today, 04:45 AM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by TradeForge, Today, 02:09 AM
        1 response
        23 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X