Debug Ninjascript in Visual Studio on Live or Replay data - NinjaTrader | futures io social day trading
futures io futures trading


Debug Ninjascript in Visual Studio on Live or Replay data
Updated: Views / Replies:1,260 / 0
Created: by spinnybobo Attachments:0

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 
Thread Tools Search this Thread
 

Debug Ninjascript in Visual Studio on Live or Replay data

  #1 (permalink)
Elite Member
Crete, IL/USA
 
Futures Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker/Data: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Favorite Futures: ES, CL, EUR/USD, TF
 
spinnybobo's Avatar
 
Posts: 171 since Aug 2009
Thanks: 99 given, 48 received

Debug Ninjascript in Visual Studio on Live or Replay data

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


Last edited by spinnybobo; October 27th, 2013 at 08:36 PM.
Reply With Quote

Reply



futures io > > > > Debug Ninjascript in Visual Studio on Live or Replay data

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Jigsaw Trading: TBA

Elite only

FuturesTrader71: TBA

Elite only

NinjaTrader: TBA

Jan 18

RandBots: TBA

Jan 23

GFF Brokers & CME Group: Futures & Bitcoin

Elite only

Adam Grimes: TBA

Elite only

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Profile your code using Visual Studio 2010 gomi NinjaTrader Programming 21 January 13th, 2014 10:08 AM
Webinar - ACSIL with Visual Studio yonatan Sierra Chart Programming 1 August 16th, 2012 06:05 AM
Using Visual Studio (Express) w/ NT LostTrader NinjaTrader Programming 3 July 28th, 2010 08:48 PM
Visual Studio 08 NT programming symphys NinjaTrader Programming 5 March 8th, 2010 05:45 AM
Will Visual Studio 2008(5, 10?) help? MXASJ NinjaTrader Programming 2 November 8th, 2009 08:55 AM


All times are GMT -4. The time now is 10:04 AM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts
Page generated 2017-12-17 in 0.08 seconds with 19 queries on phoenix via your IP 107.20.115.174