I'm writing an indicator script which draws an arrow on a chart and a calculated number based on the price of that bar when various conditions are true. This all works fine, but what I would now like to do is also display on the chart some info about the candles prior to the one that meets my conditions.
There are a number of things but I think once I get one done the others will be easy to add on.
The first is to display in text on the chart, how many previous candles were bullish.
I was thinking to check the previous candle to see ((IsRising(Close) == true) and then increment a counter until ((IsRising(Close) == false) and then display text on my chart "There are [x] previous bull candles"
I'm struggling to get the loop to work and would appreciate any help.
Thanks
Tim
Below is my code prior to adding in the loop.
namespace NinjaTrader.NinjaScript.Indicators
{
public class indFTB0 : Indicator
{
private Double dStoreHigherOpenOrClose; // variable to store whether price close or open is higher value
private Double dOpenClosePct; // variable to store % of bar open/close happens
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Indicator to show potential FTB setups";
Name = "_indFTB0";
Calculate = Calculate.OnBarClose;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = true;
dStoreHigherOpenOrClose =0;
dOpenClosePct =0;
;
}
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 2)
return;
else if (State == State.Configure)
{
}
}
protected override void OnBarUpdate()
{
//Add your custom indicator logic here.
// Set 1
dStoreHigherOpenOrClose = Close[0];
if (Open[0] > Close[0])
{
dStoreHigherOpenOrClose = Open[0];
}
// (higher of open/close less low) / (high less low)
dOpenClosePct = ((dStoreHigherOpenOrClose - Low[0]) / (High[0] - Low[0]));
Print ("dOpenClosePct " + dOpenClosePct.ToString());
Print ("Open[0] " + Open[0].ToString());
Print ("Close[0] " + Close[0].ToString());
Print ("Low[0] " + Low [0].ToString());
Print ("High[0] " + High[0].ToString());
if ((High[1] > High[2])
&& (dOpenClosePct <= 0.5)
&& (High[0] > High[1])
&& (Times[0][0].TimeOfDay >= new TimeSpan(6, 0, 0))
&& (Times[0][0].TimeOfDay <= new TimeSpan(10, 0, 0))
&& (Close[1] > Close[2]))
{
Draw.ArrowDown(this, @"FTB0 " + CurrentBars[0].ToString(), true, 0, (High[0] + (1 * (TickSize * 10))) , Brushes.Orange); //draw a red down arrow over the candle that could be a potential FTB setup
//Draw.Text(this, CurrentBars[0].ToString() + @" Text_1", dOpenClosePct.ToString(), 0, (High[0] + (75 * TickSize)) ); //draw the dOpenClosePct value so we can see which <= 50% value for that candle
string myText = string.Format("{0,0:N2}", (dOpenClosePct + (5 * (TickSize * 10))));
Draw.Text(this, CurrentBars[0].ToString() + @" Text_1a",myText , 0, Low[0] - 30 * TickSize);
}
}
}
}

Comment