Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strange behavior on a strategy

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

    Strange behavior on a strategy

    I found this simple strategy which I fixed, and run, but it doesn't do what I expect it to do.

    Here is what I assume and find:
    1. Looking at the GoLong(), I would expect the strategy to create 3 orders (target1, target2, target3), but looking at the chart I see only 1.
    2. I using teh ES 5min chart and target1=4, target2=4, target3=4, I would expect the Sell order of 19:05 to have a traget of 1pt, which is achieved at 20:55, but the instead the order goes all the way up and exit at 1754.0, which is not the stop (12t its 1756.5).

    Can you help to explain.

    Attached are the chart, and orders, trades & Excutions.

    Code:
    		
    private void GoLong()
    		{
    			SetStopLoss("target1", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
    			SetStopLoss("target2", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
    			SetStopLoss("target3", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
    			
    			SetProfitTarget("target1", CalculationMode.Price, Close[0] + ((Target1)*TickSize));
    			SetProfitTarget("target2", CalculationMode.Price, Close[0] + ((Target1+Target2)*TickSize));
    			SetProfitTarget("target3", CalculationMode.Price, Close[0] + ((Target1+Target2+Target3)*TickSize));
    		
    			EnterLong("target1");
    			EnterLong("target2");
    			EnterLong("target3");			
    		}
    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>
        /// Big Mike - 1/26/10 - https://www.bigmiketrading.com/beginners-introductions-tutorials/2512-video-tutorial-how-create-advanced-ninjatrader-strategy.html
        /// </summary>
        [Description("Make me millions Jan 27 2010")]
        public class TargetCCI : Strategy
        {
            #region Variables
    		
    		private bool	useRealData		= false;
    		
    		private int		target1			= 12;
    		private int		target2			= 10;
    		private int		target3			= 24;
    		
    		private int		stop			= 12;
    		
    		private bool	be2				= true;
    		private bool	be3				= true;
    		
    		private int		cciLength		= 14;
    		private int		cciLevelH		= 250;
    		private int		cciLevelL		= -250;
    		
    		private int		cciLookBackBars	= 1;
    		
    		#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;
    			EntryHandling		= EntryHandling.UniqueEntries;
    			
    			
            }
    		
    		private void GoLong()
    		{
    			SetStopLoss("target1", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
    			SetStopLoss("target2", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
    			SetStopLoss("target3", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
    			
    			SetProfitTarget("target1", CalculationMode.Price, Close[0] + ((Target1)*TickSize));
    			SetProfitTarget("target2", CalculationMode.Price, Close[0] + ((Target1+Target2)*TickSize));
    			SetProfitTarget("target3", CalculationMode.Price, Close[0] + ((Target1+Target2+Target3)*TickSize));
    		
    			EnterLong("target1");
    			EnterLong("target2");
    			EnterLong("target3");
    			
    		}
    
    		private void GoShort()
    		{
    			SetStopLoss("target1", CalculationMode.Price, Close[0] + (Stop*TickSize), false);
    			SetStopLoss("target2", CalculationMode.Price, Close[0] + (Stop*TickSize), false);
    			SetStopLoss("target3", CalculationMode.Price, Close[0] + (Stop*TickSize), false);
    			
    			SetProfitTarget("target1", CalculationMode.Price, Close[0] - ((Target1)*TickSize));
    			SetProfitTarget("target2", CalculationMode.Price, Close[0] - ((Target1+Target2)*TickSize));
    			SetProfitTarget("target3", CalculationMode.Price, Close[0] - ((Target1+Target2+Target3)*TickSize));
    		
    			EnterShort("target1");
    			EnterShort("target2");
    			EnterShort("target3");
    			
    		}
    
    		private void ManageOrders()
    		{
    
    			if (Position.MarketPosition == MarketPosition.Long)
    			{
    				if (BE2 && High[0] > Position.AvgPrice + ((Target1)*TickSize))
    					SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice, false);
                    
    				if (BE3 && High[0] > Position.AvgPrice + ((Target1+Target2)*TickSize))
    					SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice, false);
    				
    			}
                    
    			if (Position.MarketPosition == MarketPosition.Short)
    			{
    				if (BE2 && Low[0] < Position.AvgPrice - ((Target1)*TickSize))
    					SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice, false);
                    
    				if (BE3 && Low[0] < Position.AvgPrice - ((Target1+Target2)*TickSize))
    					SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice, false);
    				
    			}
    			
    		}
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
    		protected override void OnBarUpdate()
    		{			
    		if (useRealData && Historical) 
    			return; 
    
    		EntryHandling = EntryHandling.UniqueEntries;
    
    		ManageOrders();
    
    		if (CrossBelow(CCI(cciLength), cciLevelH, cciLookBackBars))
    			EnterLong();
    		if (CrossAbove(CCI(cciLength), cciLevelL, cciLookBackBars))
    			EnterShort();
    		}
    
            #region Properties
    		[Description("")]
            [Category("Parameters")]
            public int CciLength
            {
                get { return cciLength; }
                set { cciLength = Math.Max(1, value); }
            }
    
    		[Description("")]
            [Category("Parameters")]
            public int CciLookBackBars
            {
                get { return cciLookBackBars; }
                set { cciLookBackBars = Math.Max(0, value); }
            }
    
    		[Description("")]
            [Category("Parameters")]
            public int CciLevelH
            {
                get { return cciLevelH; }
                set { cciLevelH = Math.Max(1, value); }
            }
    
    		[Description("")]
            [Category("Parameters")]
            public int CciLevelL
            {
                get { return cciLevelL; }
                set { cciLevelL = Math.Min(-1, value); }
            }
    
    		[Description("")]
            [Category("Parameters")]
            public int Target1
            {
                get { return target1; }
                set { target1 = Math.Max(1, value); }
            }
    
    		[Description("")]
            [Category("Parameters")]
            public int Target2
            {
                get { return target2; }
                set { target2 = Math.Max(1, value); }
            }
    
    		[Description("")]
            [Category("Parameters")]
            public int Target3
            {
                get { return target3; }
                set { target3 = Math.Max(1, value); }
            }
    
    		[Description("")]
            [Category("Parameters")]
            public int Stop
            {
                get { return stop; }
                set { stop = Math.Max(1, value); }
            }
    
    		[Description("")]
            [Category("Parameters")]
            public bool BE2
            {
                get { return be2; }
                set { be2 = value; }
            }
    
    		[Description("")]
            [Category("Parameters")]
            public bool BE3
            {
                get { return be3; }
                set { be3 = value; }
            }
    
    		[Description("")]
            [Category("Parameters")]
            public bool UseRealData
            {
                get { return useRealData; }
                set { useRealData = value; }
            }				
    		
            #endregion
        }
    }
    Attached Files
    Last edited by Shai Samuel; 11-21-2013, 11:34 AM.

    #2
    Shai, look to me like an issue with resetting the stop / target prices once flat, this would need to happen if you're using the price based. The reset would be done then in strategy flat state by setting to a default amount in ticks for example.

    This sample here would show the concept at work - http://www.ninjatrader.com/support/f...ead.php?t=3222

    For further debugging, best usage would be working with TraceOrders, as they would show exactly what's going on 'under the hood' with your order placements / amendments.

    BertrandNinjaTrader Customer Service

    Comment


      #3
      Dear Bertrand, thank you for your kind reply.

      I looked at the example you linked and I also added the TraceOrders and I attached here the relevant result, related to this post. I still don't understand why:
      1. When entering the market on GoShort (10/31/2013 7:00:00 PM) there is only 1 order with the name "FromEntrySignal", instead of 3 orders: Traget1, Target2, Target3.
      2. This order exit at 1754.0 (11/1/2013 12:15:00 AM), which has no relation to any stop or target. The stops for these 3 orders are 12 ticks, which should make it 1755.50, and the targets are: target1=4 target2=4 target3=4. This should place the target prices to be target1=1752.50 (which meet at 20:55), target2=1751.50 (which meet at 21:00).


      Everything is the same even if I set BE2 and BE3 to false, so ManageOrder does nothing.

      10/31/2013 7:00:00 PM Entered internal PlaceOrder() method at 10/31/2013 7:00:00 PM: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
      11/1/2013 12:15:00 AM Entered internal PlaceOrder() method at 11/1/2013 12:15:00 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
      11/1/2013 6:40:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='target2' Mode=Price Value=1754 Currency=0 Simulated=False
      11/1/2013 6:45:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='target2' Mode=Price Value=1754 Currency=0 Simulated=False
      11/1/2013 6:45:00 AM Entered internal PlaceOrder() method at 11/1/2013 6:45:00 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
      11/1/2013 6:45:00 AM Ignored PlaceOrder() method at 11/1/2013 6:45:00 AM: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Buy' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'
      11/1/2013 6:50:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='target2' Mode=Price Value=1754 Currency=0 Simulated=False
      ...

      Comment


        #4
        Shai, rechecking your first snippet you never seem to call the GoLong, GoShort at all?

        Would change to

        if (CrossBelow(CCI(cciLength), cciLevelH, cciLookBackBars))
        GoLong();
        if (CrossAbove(CCI(cciLength), cciLevelL, cciLookBackBars))
        GoShort();

        and recheck then...
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Thank you so much, it's my mistake. It's now working as expected.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by arvidvanstaey, Today, 02:19 PM
          4 responses
          11 views
          0 likes
          Last Post arvidvanstaey  
          Started by samish18, 04-17-2024, 08:57 AM
          16 responses
          61 views
          0 likes
          Last Post samish18  
          Started by jordanq2, Today, 03:10 PM
          2 responses
          9 views
          0 likes
          Last Post jordanq2  
          Started by traderqz, Today, 12:06 AM
          10 responses
          18 views
          0 likes
          Last Post traderqz  
          Started by algospoke, 04-17-2024, 06:40 PM
          5 responses
          48 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Working...
          X