NexusFi: Find Your Edge


Home Menu

 





Debugging a BarsType help


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one zacharydw00 with 6 posts (3 thanks)
    2. looks_two MXASJ with 3 posts (0 thanks)
    3. looks_3 eman with 2 posts (0 thanks)
    4. looks_4 DavidHP with 1 posts (1 thanks)
    1. trending_up 7,339 views
    2. thumb_up 7 thanks given
    3. group 2 followers
    1. forum 13 posts
    2. attach_file 2 attachments




 
Search this Thread

Debugging a BarsType help

  #11 (permalink)
 eman 
Galveston ,TX
 
Experience: Intermediate
Platform: NT7
Broker: Zaner
Trading: Futures
Posts: 386 since Mar 2010
Thanks Given: 364
Thanks Received: 435

tweaked your code ... here's what i came up with:

 
Code
        /// <summary>
        /// write debug to log file
        /// </summary>
        /// <param name="time"></param>
        /// <param name="bar"></param>
        /// <param name="msg"></param>
        void Debug(DateTime time, int bar, string msg)
        {
            // local variables
            string path = string.Concat(Cbi.Core.UserDataDir.ToString(), @"log\YourFileName.log");
            FileInfo file = new FileInfo(path);

            // ensure file exists
            if (!file.Exists)
            {
                using (StreamWriter sw = file.CreateText())
                {
                    try
                    {
                        sw.WriteLine(string.Concat("===== ", DateTime.Now.ToString(), " ====="));
                        sw.Flush();
                    }
                    catch (Exception ex) { }

                    sw.Close();
                }
            }

            // write debug entry
            using (StreamWriter sw = File.AppendText(path))
            {
                try
                {
                    sw.WriteLine("{0} : {1} \r\n{2}", time, bar, msg);
                    sw.Flush();
                }
                catch (Exception ex) { }

                sw.Close();
            }
        }
to invoke from a bar-type, do something like this:
 
Code
                    string msg = string.Concat("myValue1: ", myValue1.ToString(), "\r\nmyValue2: ", myValue2.ToString());
                    Debug(time, bars.Count, msg);
logs are posted in <my docs>\NinjaTrader7\log and look like this:

 
Code
2011-04-28 07:30:00 : 475 
myValue1: 8
myValue2:12616
2011-04-28 07:30:03 : 476 
myValue1: 8
myValue2:12624
2011-04-28 07:32:17 : 477 
myValue1: 8
myValue2:12616
2011-04-28 07:50:11 : 478 
myValue1: 8
myValue2:12624
note: i haven't tested this in ChartStyles or Indicators or Strategies. should work just fine, but my primary use (for now) is debugging a bar type, which is the point of this thread.

cheers,
-e

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
ZombieSqueeze
Platforms and Indicators
Cheap historycal L1 data for stocks
Stocks and ETFs
How to apply profiles
Traders Hideout
REcommedations for programming help
Sierra Chart
MC PL editor upgrade
MultiCharts
 
  #12 (permalink)
 
zacharydw00's Avatar
 zacharydw00 
Idaho
 
Experience: Intermediate
Platform: NinjaTrader,ToS
Broker: Amp Futures, ToS
Trading: ES, E7, CL, GC
Posts: 149 since Aug 2009
Thanks Given: 87
Thanks Received: 180

No wiki yet. I've been offline for a while. will post what i've been using so far. I did get the debugging output file to work. will post it here later today, for a quick post, then post wiki later. Thanks for the suggestion @ eman.

Milk-a-What?
Started this thread Reply With Quote
  #13 (permalink)
 
zacharydw00's Avatar
 zacharydw00 
Idaho
 
Experience: Intermediate
Platform: NinjaTrader,ToS
Broker: Amp Futures, ToS
Trading: ES, E7, CL, GC
Posts: 149 since Aug 2009
Thanks Given: 87
Thanks Received: 180


 
Code
using System.IO;
using System.Windows.Forms;

   
 public class LineBreakWickedTestBarsType : BarsType
    {
        private static bool        registered            = Register(new LineBreakWickedTestBarsType());
        private double            switchPrice            = double.MinValue;
        private double            anchorPrice            = double.MinValue;
        ....

        #region Debug StreamWriter
        private string                    path = Cbi.Core.UserDataDir+"LBWickedTest "+DateTime.Now.ToString("_yyyy.MM.dd")+".txt";  // CHANGE LB WICKEDTEST to the file name you want.
        private System.IO.StreamWriter    sw;
        public void Debug(DateTime time, String line)    // This will print the Time specified given
        {
            try        {    sw = new StreamWriter(path, true);    sw.AutoFlush = true;
                        sw.WriteLine(String.Format("{0} | {1}", time.ToString("HH:mm:ss:fff"), line));
                    }
            catch (NullReferenceException e) 
                    {    MessageBox.Show(e.ToString());        }
            finally    {    sw.Close();        }
        }
        public void Debug(String line)    // This will print the Time now
        {
            try        {    sw = new StreamWriter(path, true);    sw.AutoFlush = true;
                        sw.WriteLine(String.Format("{0} | {1}", DateTime.Now.ToString("HH:mm:ss:fff"), line));
                    }
            catch (NullReferenceException e) 
                    {    MessageBox.Show(e.ToString());        }
            finally    {    sw.Close();        }
        }
        #endregion

        public override void Add(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isRealtime)
        {
            int barcnt = bars.Count;
            bool endOfBar = true;

Debug("_");
Debug(String.Format("BarTime: {0:yyyy/MM/dd HH:mm:ss} :  Barcnt= {1:00}, O_{2:000.0}, H_{3:000.0}, L_{4:000.0}, C_{5:000.0}", time, barcnt, open, high, low, close));
           ...
        }
This will create a file in ...\My Documents\NinjaTrader 7\ directory.

Milk-a-What?
Started this thread Reply With Quote
  #14 (permalink)
 
WolfieWolf's Avatar
 WolfieWolf 
Charlottetown, Prince Edward Island
 
Experience: Advanced
Platform: Ninja
Broker: Optimus - Rithmic
Trading: GC
Posts: 232 since Jul 2010
Thanks Given: 100
Thanks Received: 272

Hi Guys,

I know this is a really old thread but I came across it about eight months ago when I started developing a custom bars type. Just now, after messing with streamwriters all this time I figured it out. Hopefully this helps someone who's developing down in the guts of NinjaTrader where you're not supposed to be.

using NinjaTrader.code;

public void Print(string output)
{
OutputEventArgs.ProcessEventArgs(new OutputEventArgs(output + "\r\n"));
}

Then from anywhere in that namespace you can do the same thing you've always done in Indicators or Strategies.

Print ("This is my value: " + _myValue);

The Ninja Output window will now happily give you the output you want.

I hope that helps.

/W

Reply With Quote
Thanked by:




Last Updated on November 20, 2014


© 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