PDA

View Full Version : Working code to Read Text but I want to move to the next stage


suprsnipes
03-09-2010, 02:10 AM
Hi,

I need a little bit of help with the following code. The code is reading a text file and finds the next highest value when referenced against the private int num = 4800; It works as expected.



[Description("Displays Levels")]
public class MyCustomIndicator2 : Indicator
{
#region Variables
// Wizard generated variables
// User defined variables (add any user defined variables below)
private string path = Cbi.Core.UserDataDir.ToString() + "PIn.txt";
private int[] levels;
private int count = 0;
private string readText = "";

private int num = 4800;
private int tmp, res = 0;
private int? diff = null;

#endregion

/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
Overlay = true;
PriceTypeSupported = false;
levels = new int[1000];
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.

readText = File.ReadAllText("C:\\PIn.txt");

string [] split = readText.Split(new Char [] {';'});
foreach (string s in split)
{
count++;
tmp = int.Parse(s);
levels[count] = tmp;

if (tmp > num)
{
if(diff == null)
diff = num;
if ((tmp - num) < diff)
{
res = tmp;
diff = tmp - num;
}
}
}
Print(res);
}

#region Properties

#endregion
}
}

All I want to do is now is instead of using the private int num = 4800;
I want to use the Close[0] but I am getting the following error on lines 67 & 71;

Cannot implicitly convert type 'double' to 'int?'. An explicit conversion exists (are you missing a cast?)


#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;
using System.IO;
#endregion

// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// Displays Levels
/// </summary>
[Description("Displays Levels")]
public class MyCustomIndicator2 : Indicator
{
#region Variables
// Wizard generated variables
// User defined variables (add any user defined variables below)
private string path = Cbi.Core.UserDataDir.ToString() + "PIn.txt";
private int[] levels;
private int count = 0;
private string readText = "";

private int tmp, res = 0;
private int? diff = null;

#endregion

/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
Overlay = true;
PriceTypeSupported = false;
levels = new int[1000];
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.

readText = File.ReadAllText("C:\\PIn.txt");

string [] split = readText.Split(new Char [] {';'});
foreach (string s in split)
{
count++;
tmp = int.Parse(s);
levels[count] = tmp;

if (tmp > Close[0])
{
if(diff == null)
diff = Close[0];
if ((tmp - Close[0]) < diff)
{
res = tmp;
diff = tmp - Close[0];
}
}
}
Print(res);
}

#region Properties

#endregion
}
}

#region NinjaScript generated code. Neither change nor remove.
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
public partial class Indicator : IndicatorBase
{
private MyCustomIndicator2[] cacheMyCustomIndicator2 = null;

private static MyCustomIndicator2 checkMyCustomIndicator2 = new MyCustomIndicator2();

/// <summary>
/// Displays Levels
/// </summary>
/// <returns></returns>
public MyCustomIndicator2 MyCustomIndicator2()
{
return MyCustomIndicator2(Input);
}

/// <summary>
/// Displays Levels
/// </summary>
/// <returns></returns>
public MyCustomIndicator2 MyCustomIndicator2(Data.IDataSeries input)
{

if (cacheMyCustomIndicator2 != null)
for (int idx = 0; idx < cacheMyCustomIndicator2.Length; idx++)
if (cacheMyCustomIndicator2[idx].EqualsInput(input))
return cacheMyCustomIndicator2[idx];

MyCustomIndicator2 indicator = new MyCustomIndicator2();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
indicator.Input = input;
indicator.SetUp();

MyCustomIndicator2[] tmp = new MyCustomIndicator2[cacheMyCustomIndicator2 == null ? 1 : cacheMyCustomIndicator2.Length + 1];
if (cacheMyCustomIndicator2 != null)
cacheMyCustomIndicator2.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheMyCustomIndicator2 = tmp;
Indicators.Add(indicator);

return indicator;
}

}
}

// This namespace holds all market analyzer column definitions and is required. Do not change it.
namespace NinjaTrader.MarketAnalyzer
{
public partial class Column : ColumnBase
{
/// <summary>
/// Displays Levels
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.MyCustomIndicator2 MyCustomIndicator2()
{
return _indicator.MyCustomIndicator2(Input);
}

/// <summary>
/// Displays Levels
/// </summary>
/// <returns></returns>
public Indicator.MyCustomIndicator2 MyCustomIndicator2(Data.IDataSeries input)
{
return _indicator.MyCustomIndicator2(input);
}

}
}

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// Displays Levels
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.MyCustomIndicator2 MyCustomIndicator2()
{
return _indicator.MyCustomIndicator2(Input);
}

/// <summary>
/// Displays Levels
/// </summary>
/// <returns></returns>
public Indicator.MyCustomIndicator2 MyCustomIndicator2(Data.IDataSeries input)
{
if (InInitialize && input == null)
throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

return _indicator.MyCustomIndicator2(input);
}

}
}
#endregion

Appreciate the help. Kind Regards,
suprsnipes

NinjaTrader_Bertrand
03-09-2010, 04:08 AM
Your tmp variable is an integer you're attempting to compare to the double value Close[0], this will throw an error when you attempt to compile it. You could try casting it, or declaring tmp as double and then parsing double values into it later on...

suprsnipes
03-09-2010, 05:24 AM
Hi Bertand,

Can you please provide me an example (if it's not too much trouble)?

Regards,
suprsnipes

NinjaTrader_Bertrand
03-09-2010, 05:27 AM
As a start for example, see the private int declaration you have for tmp > define it as double, private double tmp instead.

suprsnipes
03-09-2010, 08:08 AM
I have made some changes to the code as follows and the output screen shows that the text file is being read correctly, except it is rounding down...I thought I had the rounding up issue resolved yet the Math.Ceiling is not working.


#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;
using System.IO;
#endregion

// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// Displays Levels
/// </summary>
[Description("Displays Levels")]
public class MyCustomIndicator2 : Indicator
{
#region Variables
// Wizard generated variables
// User defined variables (add any user defined variables below)
private string path = Cbi.Core.UserDataDir.ToString() + "PIn.txt";
private int[] levels;
private int count = 0;
private string readText = "";

private int roundedValue;
private int diff = int.MaxValue;
private int tmp, res = 0;


#endregion

/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
Overlay = true;
PriceTypeSupported = false;
levels = new int[100000];
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.

readText = File.ReadAllText("C:\\PIn.txt");

int roundedValue = ((int)Math.Ceiling(Close[0]));

string [] split = readText.Split(new Char [] {';'});
foreach (string s in split)
{
count++;
tmp = int.Parse(s);
levels[count] = tmp;

if (tmp > roundedValue)
{
if(diff == null)
diff = roundedValue;
if ((tmp - roundedValue) < diff)
{
res = tmp;
diff = tmp - roundedValue;
}
}
}
Print(res);
}

#region Properties

#endregion
}
}


Thanks in advance to anyone who can provide assistance.

NinjaTrader_Bertrand
03-09-2010, 08:30 AM
Have you printed out your roundedValue to check into? A quick test for me here worked out as expected rounding up.