NexusFi: Find Your Edge


Home Menu

 





Debug Ninjascript in Visual Studio on Live or Replay data


Discussion in NinjaTrader

Updated
    1. trending_up 2,548 views
    2. thumb_up 0 thanks given
    3. group 1 followers
    1. forum 0 posts
    2. attach_file 0 attachments




 
Search this Thread

Debug Ninjascript in Visual Studio on Live or Replay data

  #1 (permalink)
 
spinnybobo's Avatar
 spinnybobo 
Crete, IL/USA
 
Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
Posts: 173 since Aug 2009
Thanks Given: 105
Thanks Received: 61

Hello Programmers

I have noticed that using replay data is actually very valuable in terms of strategy development and making sure I programmed the IOrders correctly.

I noticed that sometimes when I do a "Managed Approach" using multiple exit targets, I then need to create multiple IOrders for each leg if I am correct.

So if I need 2 targets, I need to create 2 entry IOrders, 2 stop IOrders, and 2 target IOrders.

I noticed, however, that for some reason when I use replay data, on the first trade it will work properly, however, as soon as the 2nd short trade happens, it will only put on 1 of my stop IOrders and the other one does not even get into an Accepted state and remains null.

I wanted to know if using Visual Studio to debug each bar would be helpful. However, I noticed that while attaching to a process, putting in my breakpoint, and then refreshing NinjaTrader chart: I can then see my breakpoint go from red to yellow and I see the stack fill up with the first bar.

However, I don't want historical data. So, it is fine, I just delete tick data from the tick data folder --- because I use tick granularity for order entry, so when I load the strategy, if there is no historical tick data, it starts only on new data.

So, the problem is when I start a breakpoint from this point, I cannot have access to "new incoming data" from the replay data. As soon as I start the replay data, it shows NinjaTrader as "not responding" until I hit "Stop debugging" and detach process from Visual Studio.

So here is my delima: How do I debug on "Replay Data" so I can see what is going on with my program on Live Data or Replay Data that "Streams" and is not historical?

Historical data acts different than real live or replay data in Ninja.

Here is the code I am trying to debug. For some reason, the 2nd IOrder: stop2Order which is linked to "StopShort2" and "Sort2" from entry2Order does not get filled when you play it on 10/01 or 10/02 on the 2nd order.

Here is the strategy
 
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
{
   
    [Description("A Test Strategy used for testing IOrders using a Managed Approach")]
    public class sdIBTest : Strategy
    {
        #region Variables
      	
		// indicator
		private sdEMAandTrigger tradeSignal;
		private bool initialize = false;
		
		// IOrders
		private IOrder entry1Order = null;
		private IOrder entry2Order = null;
		private IOrder stop1Order = null;
		private IOrder stop2Order = null;
		private IOrder target1Order = null;
		private IOrder target2Order = null;
		private IOrder exit1Order = null;
		private IOrder exit2Order = null;
		
		//Stop and Target Values
		private double stopOffset = 0.15;
		private double stopValue;
		private double target1Offset = 0.35;
		private double target2Offset = 0.50;
		
		// Times to trade
		private double beginTrading = 100;
		private double stopTrading = 1430;
		
		// Main Program
		private int orderSeries = 2;
		private int quantity = 20;
		
        #endregion

        protected override void Initialize()
        {
		BarsRequired = 0;
		EntryHandling = EntryHandling.UniqueEntries;
           	ExitOnClose = true;
		ExitOnCloseSeconds = 600;
		TimeInForce = Cbi.TimeInForce.Gtc;
			
		Add(sdEMAandTrigger(8, 34));
			
		CalculateOnBarClose = true;
		Add(PeriodType.Minute, 1); // 1
		Add(PeriodType.Tick, 1); // 2
        }
      
        protected override void OnBarUpdate()
        {	
		if (!initialize)
		{
			tradeSignal = sdEMAandTrigger(8, 34);
			initialize = true;
		}
			
		if (!TimeToTrade && CurrentBars[orderSeries] > 0)
		{	
			if (!Flat)
			{
				if (Long)
				{
					exit1Order = ExitLong(Position.Quantity, "ExitLong1", "Long1");
					exit2Order = ExitLong(Position.Quantity, "ExitLong2", "Long2");						
				}
				if (Short)
				{
					exit1Order = ExitShort(Position.Quantity, "ExitShort1", "Short1");
					exit2Order = ExitShort(Position.Quantity, "ExitShort2", "Short2");
				}
			}
		}
			
		if (TimeToTrade && CurrentBars[orderSeries] > 0)
		{	
			BackColor = Color.LightGray;
				
			if (BarsInProgress == 0)
			{		
				int qty = (quantity/2);
				
				if (entry1Order == null && entry2Order == null && (tradeSignal.LongSignal[0] == 1) && Flat)
				{
					stopValue = Low[0] - stopOffset;
					entry1Order = EnterLong(orderSeries, (quantity / 2), "Long1");
					entry2Order = EnterLong(orderSeries, (quantity / 2), "Long2");
				}
				if (entry1Order == null && entry2Order == null && (tradeSignal.ShortSignal[0] == -1) && Flat)
				{
					stopValue = High[0] + stopOffset;
					entry1Order = EnterShort(orderSeries, (quantity / 2), "Short1");
					entry2Order = EnterShort(orderSeries, (quantity / 2), "Short2");
				}
			}
		}
        }
		
	#region Event Driven Functions
	protected override void OnOrderUpdate(IOrder order)
	{
		if (entry1Order != null && entry1Order.Token == order.Token)
			if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
				entry1Order = null;
			
		if (entry2Order != null && entry2Order.Token == order.Token)
			if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
				entry2Order = null;
			
		if (exit1Order != null && exit1Order.Token == order.Token)
			if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
				exit1Order = null;
			
		if (exit2Order != null && exit2Order.Token == order.Token)
			if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
				exit2Order = null;
			
		if (stop1Order != null && stop1Order.Token == order.Token)
			if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
				stop1Order = null;
			
		if (stop2Order != null && stop2Order.Token == order.Token)
			if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
				stop2Order = null;
			
		if (target1Order != null && target1Order.Token == order.Token)
			if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
				target1Order = null;
			
		if (target2Order != null && target2Order.Token == order.Token)
			if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
				target2Order = null;
	}
	protected override void OnExecution(IExecution execution)
	{
		if ((entry1Order != null && entry1Order.Token == execution.Order.Token)||
				(entry2Order != null && entry2Order.Token == execution.Order.Token))
		{
			if (execution.Order.OrderState == OrderState.Filled)
			{	
				int qty = (quantity/2);
					
				if (Long)
				{
					if (execution.Order == entry1Order)
					{
						stop1Order = ExitLongStop(orderSeries, true, execution.Order.Filled, stopValue, "StopLong1", "Long1");
						target1Order = ExitLongLimit(orderSeries, true, execution.Order.Filled, entry1Order.AvgFillPrice + target1Offset, "TargetLong1", "Long1");
					}
					if (execution.Order == entry2Order)
					{
						stop2Order = ExitLongStop(orderSeries, true, execution.Order.Filled, stopValue, "StopLong2", "Long2");
						target2Order = ExitLongLimit(orderSeries, true, execution.Order.Filled, entry2Order.AvgFillPrice + target2Offset, "TargetLong2", "Long2");
					}
				}
				if (Short)
				{
					if (execution.Order == entry1Order)
					{
						stop1Order = ExitShortStop(orderSeries, true, execution.Order.Filled, stopValue, "StopShort1", "Short1");
						target1Order = ExitShortLimit(orderSeries, true, execution.Order.Filled, entry1Order.AvgFillPrice - target1Offset, "TargetShort1", "Short1");
					}
					if (execution.Order == entry2Order)
					{	
						stop2Order = ExitShortStop(orderSeries, true, execution.Order.Filled, stopValue, "StopShort2", "Short2");
						target2Order = ExitShortLimit(orderSeries, true, execution.Order.Filled, entry2Order.AvgFillPrice - target2Offset, "TargetShort2", "Short2");
					}
				}
				if (execution.Order == entry1Order)
					entry1Order = null;
				if (execution.Order == entry2Order)
					entry2Order = null;
			}
		}
		if (exit1Order != null && exit1Order.Token == execution.Order.Token)
		{
			if (execution.Order.OrderState == OrderState.Filled)
			{
				exit1Order = null;
			}
		}
		if (exit2Order != null && exit2Order.Token == execution.Order.Token)
		{
			if (execution.Order.OrderState == OrderState.Filled)
			{
				exit2Order = null;
			}
		}
		if (stop1Order != null && stop1Order.Token == execution.Order.Token)
		{
			if (execution.Order.OrderState == OrderState.Filled)
			{
		   	       if (target1Order != null)
					CancelOrder(target1Order);
				stop1Order = null;
			}
		}
		if (target1Order != null && target1Order.Token == execution.Order.Token)
		{
			if (execution.Order.OrderState == OrderState.Filled)
			{
				if (stop1Order != null)
					CancelOrder(stop1Order);
				target1Order = null;
			}
		}
		if (stop2Order != null && stop2Order.Token == execution.Order.Token)
		{
			if (execution.Order.OrderState == OrderState.Filled)
			{
				if (target2Order != null)
					CancelOrder(target2Order);
				stop2Order = null;
			}
		}
		if (target2Order != null && target2Order.Token == execution.Order.Token)
		{
			if (execution.Order.OrderState == OrderState.Filled)
			{
				if (stop2Order != null)
					CancelOrder(stop2Order);
				target2Order = null;
			}
		}
	}
	#endregion
	
	#region Market Position
	private bool Long { get { return Position.MarketPosition == MarketPosition.Long; }}
	private bool Short { get { return Position.MarketPosition == MarketPosition.Short; }}
	private bool Flat { get { return Position.MarketPosition == MarketPosition.Flat; }}
	#endregion
		
	#region Time To Trade
	private bool TimeToTrade
	{
		get { return (ToTime(Time[0]) > beginTrading * 100 && ToTime(Time[0]) <= stopTrading * 100)? true : false; }
	}
	#endregion

        #region Properties
        
        #endregion
    }
}
Here is the indicator

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

// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
    [Description("Spencer's simple EMA trend and reversal bar trigger")]
    public class sdEMAandTrigger : Indicator
    {
		private int fastPeriod = 8;
		private int slowPeriod = 34;
		private DataSeries longSignal;
		private DataSeries shortSignal;
		private bool drawArrows = true;
		
        protected override void Initialize()
        {
		Add(new Plot(Color.Green, "Fast")); // Values[0]
		Add(new Plot(Color.Red, "Slow")); // Values[1]
		Plots[0].Pen.Width = 2;
		Plots[1].Pen.Width = 2;
			
		longSignal = new DataSeries(this);
		shortSignal = new DataSeries(this);
			
                Overlay	= true;
		CalculateOnBarClose = true;
        }

        /// Called on each bar update event (incoming tick)
        protected override void OnBarUpdate()
        {
			if (CurrentBar < 5) return;
			
			bool longReversal = (Open[1] > Close[1] && Open[0] < Close[0] ? true : false);
			bool shortReversal = (Open[1] < Close[1] && Open[0] > Close[0] ? true : false);
			
			Fast.Set(EMA(FastPeriod)[0]);
			Slow.Set(EMA(SlowPeriod)[0]);

			longSignal.Set((EMA(FastPeriod)[0] > EMA(SlowPeriod)[0] && longReversal)? 1 : 0);
			shortSignal.Set((EMA(FastPeriod)[0] < EMA(SlowPeriod)[0] && shortReversal)? -1 : 0);

			if (drawArrows)
			{
				if (longSignal[0] == 1)
					DrawArrowUp("ArrowUp"+CurrentBar, 0, Low[0] - TickSize, Color.Blue);
				if (shortSignal[0] == -1)
					DrawArrowDown("ArrowDown"+CurrentBar, 0, High[0] + TickSize, Color.Red);
			}
        }

      #region Properties
       [Browsable(false)]
       [XmlIgnore()]
       public DataSeries LongSignal
       {
	get { return longSignal; }
        }
        [Browsable(false)]
        [XmlIgnore()]
        public DataSeries ShortSignal
        {
	get { return shortSignal; }
	}
		
        [Browsable(false)]	
        [XmlIgnore()]		
        public DataSeries Fast
        {
            get { return Values[0]; }
        }
		[Browsable(false)]	
        [XmlIgnore()]		
        public DataSeries Slow
        {
            get { return Values[1]; }
        }
        [Description("Slow Period")]
        [GridCategory("Parameters")]
        public int SlowPeriod
        {
            get { return slowPeriod; }
            set { slowPeriod = Math.Max(1, value); }
        }
		[Description("Fast Period")]
        [GridCategory("Parameters")]
        public int FastPeriod
        {
            get { return fastPeriod; }
            set { fastPeriod = Math.Max(1, value); }
        }
        #endregion
    }
}
So when install indicator then strategy, and compile, and download 10/01 and 10/02 replay data from CQG, and open 1 chart and turn on chart trader (so I can see the stops and targets) and then open another chart and open the sdIBTest.cs strategy, when I start replay data on 500 speed, it does first short trade fine, but the next one instead of having 20 contracts for the Stop, it only has 10 contracts.

When I look at the log, the StopShort2 is not even listed as "Accepted"
When I try to trace when is going in with stop2Order at any point.
I put it in OnExecution, or OnOrderUpdate, and nothing happens when that 2nd order comes in.

I am totally not understanding.

 
Code
if (stop2Order != null)
   Print("stop2Order: " + stop2Order.ToString());
The only thing I can think of is moving to a "Unmanaged Approach" and using 2 target IOrders, but using 1 entry IOrder and 2 stop IOrder, and then just use the "ChangeOrder(.......) " method to change the amount of contracts when something gets hit. I can override OCO functionality this way.

However, the other problem that gets created is: What are the things I now need to handle when I use an "Unmanaged Approach"?

Order Rejection? and ConnectionStatus type things?

Basically I want to be able to not have the strategy totally turn off when an order gets rejected. Just go flat but keep the strategy running. I think I can handle this with OnOrderUpdate and using the

order.OrderStatus == OrderStatus.OrderRejected
Flatten(......) ;

There is something I need to say also in Initialize() to declare that I intend to take control over this.
I think I understand Order Rejected, but how does the "Unmanaged approach" handle connection loss issues?

I, of course, just want this:
1. if I lose a connection, when I re-establish a connection, then I want the strategy to go back to work
2. If Ninja crashes or the computer crashes, when I restart, I want the strategy to pick up where it left off.

I do not want it to go flat and then start fresh again.

so a lot of input here
1. Live and Replay Visual Studio debugging
2. Did I program something wrong in my strategy concerning IOrders?
3. If using an Unmanaged Approach, how do I handle connection loss issues so it always picks up where it left off and becomes a pretty bulletproof or close to bulletproof strategy?

thanks
Spencer

Follow me on Twitter Started this thread Reply With Quote




Last Updated on October 27, 2013


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts