View Full Version : Hma rising and falling arrows
djkiwi
08-30-2010, 09:28 AM
Hi All, I am trying to put an arrow when the HMA changes color from red to green and green to red using the rising and falling. I'm using the HMA Code code below called the (HMA color change indicator) which changes the color of the HMA to green when rising and red when falling. I put in the following code but it inserts a number of arrows not just a single arrow when the HMA changed from falling to rising. I was able to do the arrows for SAR crossovers but not this one. Any ideas would be appreciated. Thanks. DJ
1. My code
// Condition set 1
if (HMAColourChange(14).FallingPlot[0] < HMAColourChange(14).RisingPlot[1])
{
DrawArrowDown("My down arrow" + CurrentBar, false, 0, Close[0] + 20 * TickSize, Color.Red);
}
// Condition set 2
if (HMAColourChange(14).RisingPlot[0] > HMAColourChange(14).FallingPlot[1])
{
DrawArrowUp("My up arrow" + CurrentBar, false, 0, Close[0] + -20 * TickSize, Color.Lime);
}
2. HMA Code
//
// Copyright (C) 2009, GreatTradingSystems <www.greattradingsystems.com>.
//
//
#region Using declarations
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
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
{
/// <summary>
/// This is a sample indicator plotting a HMA in three colors.
/// </summary>
[Description("Sample HMA plotted with three colors.")]
[Gui.Design.DisplayName("HMAColourChange")]
public class HMAColourChange : Indicator
{
#region Variables
private int period = 14;
#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()
{
// Add one plot for every color you wish to use.
Add(new Plot(Color.LimeGreen, PlotStyle.Line, "Rising"));
Add(new Plot(Color.Red, PlotStyle.Line, "Falling"));
Add(new Plot(Color.Yellow, PlotStyle.Line, "Neutral"));
CalculateOnBarClose = true;
Overlay = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Checks to make sure we have at least 1 bar before continuing
if (CurrentBar < 1)
return;
// Plot green if the HMA is rising
// Rising() returns true when the current value is greater than the value of the previous bar.
if (Rising(HMA(Period)))
{
// Connects the rising plot segment with the other plots
RisingPlot.Set(1, HMA(Period)[1]);
// Adds the new rising plot line segment to the line
RisingPlot.Set(HMA(Period)[0]);
}
// Plot red if the HMA is falling
// Falling() returns true when the current value is less than the value of the previous bar.
else if (Falling(HMA(Period)))
{
// Connects the new falling plot segment with the rest of the line
FallingPlot.Set(1, HMA(Period)[1]);
// Adds the new falling plot line segment to the line
FallingPlot.Set(HMA(Period)[0]);
}
// Plot yellow if the HMA is neutral
else
{
// Connects the neutral plot segment with the rest of the line
NeutralPlot.Set(1, HMA(Period)[1]);
// Adds the new neutral plot line segment to the line
NeutralPlot.Set(HMA(Period)[0]);
NinjaTrader_Bertrand
08-30-2010, 09:44 AM
Are there any errors in the log tab of your Control Center?
Likely you miss a CurrentBars check at the start of your indicator's OnBarUpdate() -
http://www.ninjatrader.com/support/forum/showthread.php?t=3170
djkiwi
08-30-2010, 10:11 AM
Thanks Bertrand, well I'm not seeing any errors. I have attached a screenshot and the full code. You can see from the screenshot it is drawing multiple arrows I'm not exactly sure what that link you sent me is supposed to do because I never had to do it for the SAR crossover. I can only do very basic things at this point. I also tried crosses under or over instead of greater than or less than and still same problem. Thanks. DJ
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("Enter the description of your strategy here")]
public class HMAdj : Strategy
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
if (HMAColourChange(14).FallingPlot[0] < HMAColourChange(14).RisingPlot[1])
{
DrawArrowDown("My down arrow" + CurrentBar, false, 0, Close[0] + 20 * TickSize, Color.Red);
}
// Condition set 2
if (HMAColourChange(14).RisingPlot[0] > HMAColourChange(14).FallingPlot[1])
{
DrawArrowUp("My up arrow" + CurrentBar, false, 0, Close[0] + -20 * TickSize, Color.Lime);
}
}
NinjaTrader_Bertrand
08-30-2010, 10:42 AM
djkiwi, thanks for clarifying the issue, I see what you mean now - to resolve just access the HMA base indicator value directly -
// Condition set 1
if (HMA(14)[0] < HMA(14)[1] && HMA(14)[1] > HMA(14)[2])
{
DrawArrowDown("My down arrow" + CurrentBar, false, 0, Close[0] + 20 * TickSize, Color.Red);
}
// Condition set 2
if (HMA(14)[0] > HMA(14)[1] && HMA(14)[1] < HMA(14)[2])
{
DrawArrowUp("My up arrow" + CurrentBar, false, 0, Close[0] - 20 * TickSize, Color.Lime);
}
djkiwi
08-30-2010, 05:43 PM
Hi Bertrand still no luck with lots of iterations. Same problem with lots of arrows. So I tried to say rising and then close above the HMA and still doesnt work. Any other ideas? Thanks. DJ
Here is the code and new image:
[Description("Enter the description of your strategy here")]
public class HMAdj : Strategy
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
if (Falling(DefaultInput) == true
&& CrossBelow(Close, HMA(14), 1))
{
DrawArrowDown("My down arrow" + CurrentBar, false, 0, High[0] + 5 * TickSize, Color.Red);
}
// Condition set 2
if (Rising(HMA(14)) == true
&& CrossAbove(Close, HMA(14), 1))
{
DrawArrowUp("My up arrow" + CurrentBar, false, 0, Low[0] + -5 * TickSize, Color.Lime);
}
DrawTriangleDown("My triangle down" + CurrentBar, false, 0, Low[0], Color.Red);
}
NinjaTrader_Bertrand
08-31-2010, 05:37 AM
Sorry I don't follow you, with the code change I posted I get this result for example on the FDAX 7R chart, this looks like what you're after?
djkiwi
08-31-2010, 09:15 PM
Thanks Bertrand works perfectly! Greatly appreciate the help. I missed the code you typed.
Cheers
DJ
djkiwi
09-06-2010, 04:32 PM
Hi Bertrand, I am trying to modify the strategy you helped me with to include swing lows and highs but does not seem to work. I am live trading with ninja 7 and would like to incorporate swing highs and lows in an automated strategy I'm working on. Basically it should short when the HMA crosses (5 as an example) AND the Closing bar once the cross occurs is below the last swing high as follows. I've used the following cde that you will see below:
&&Close[0] < Swing(Swing1).SwingHighBar(0,2,100))
The opposite for going long. The problem is it is basically ignoring the code and drawing an arrow wnytime the HMA crosses. Also the short signal is being ignored totally. I also tried it with this code and it didn't work either.
&& Swing(5).SwingHighBar(0,1,100) < Swing(5).SwingHighBar(0,2,100)
I've attached a screenshot. I'm not sure why it is drawing the up arrows when it is clearly closing below a swing high or not drawing down arrows at all even though there are many instances that it is clearly above the last swing low. Any help on this one would be appreciated!
Thanks
DJ
#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
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("Enter the description of your strategy here")]
public class swingdj : Strategy
{
#region Variables
// Wizard generated variables
private int h1 = 5; // Default setting for H1
private int eMA1 = 10; // Default setting for EMA1
private int eMA2 = 30; // Default setting for EMA2
private int eMA3 = 10; // Default setting for EMA3
private int target1 = 20; // Default setting for Target1
private int target2 = 40; // Default setting for Target2
private int target3 = 60; // Default setting for Target3
private int trail = 15; // Default setting for Trail
private int Swing1 = 5; // Default setting for Swing
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
SetProfitTarget("Short1", CalculationMode.Ticks, Target1);
SetProfitTarget("Short2", CalculationMode.Ticks, Target2);
SetProfitTarget("Short3", CalculationMode.Ticks, Target3);
SetTrailStop("Short1", CalculationMode.Ticks, Trail, false);
SetTrailStop("Short2", CalculationMode.Ticks, Trail, false);
SetTrailStop("Short3", CalculationMode.Ticks, Trail, false);
SetProfitTarget("Long1", CalculationMode.Ticks, Target1);
SetProfitTarget("Long2", CalculationMode.Ticks, Target2);
SetProfitTarget("Long3", CalculationMode.Ticks, Target3);
SetTrailStop("Long1", CalculationMode.Ticks, Trail, false);
SetTrailStop("Long2", CalculationMode.Ticks, Trail, false);
SetTrailStop("Long3", CalculationMode.Ticks, Trail, false);
CalculateOnBarClose = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
if (HMA(H1)[0] < HMA(H1)[1]
&&HMA(H1)[1] > HMA(H1)[2]
&&Close[0] < Swing(Swing1).SwingHighBar(0,2,100))
{
DrawArrowDown("My down arrow" + CurrentBar, false, 0, High[0] + 15 * TickSize, Color.Red);
Alert("MyAlert48", Priority.High, "", @"C:\Program Files\NinjaTrader 7\sounds\TNA below S2.wav", 60, Color.White, Color.Black);
EnterShort(DefaultQuantity, "Short1");
EnterShort(DefaultQuantity, "Short2");
EnterShort(DefaultQuantity, "Short3");
}
// Condition set 2
if (HMA(H1)[0] > HMA(H1)[1]
&&HMA(H1)[1] < HMA(H1)[2]
&&Close[0] > Swing(Swing1).SwingLowBar(0,2,100))
{
Alert("MyAlert49", Priority.High, "", @"C:\Program Files\NinjaTrader 7\sounds\TNA below S2.wav", 60, Color.White, Color.Black);
DrawArrowUp("My up arrow" + CurrentBar, false, 0, Low[0] + -15 * TickSize, Color.Lime);
EnterLong(DefaultQuantity, "Long1");
EnterLong(DefaultQuantity, "Long2");
EnterLong(DefaultQuantity, "Long3");
}
}
#region Properties
[Description("")]
[GridCategory("Parameters")]
public int H1
{
get { return h1; }
set { h1 = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int EMA1
{
get { return eMA1; }
set { eMA1 = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int EMA2
{
get { return eMA2; }
set { eMA2 = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int EMA3
{
get { return eMA3; }
set { eMA3 = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int Target1
{
get { return target1; }
set { target1 = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int Target2
{
get { return target2; }
set { target2 = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int Target3
{
get { return target3; }
set { target3 = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int Trail
{
get { return trail; }
set { trail = Math.Max(1, value); }
}
#endregion
}
}
NinjaTrader_Bertrand
09-07-2010, 03:40 AM
DJ, what the Swing indicator offers programmatically is not the same as what it would show visually on the charts - it's more meant a visual decision support here showing the swing structure.
To understand the realtime behavior you would see, I suggest you start off by plotting the Swing High and Low values in an indicator.
djkiwi
09-09-2010, 10:17 PM
Thanks Bertand, I have found a work around. There is one real headache I was hoping you could help me with. When I log in to Interactive Brokers in the morning or at night I am always missing the day or overnight data on the charts (see attached screenshot).You will see I logged 11 pm on thhe 9th and to the left on the ES chart is the 8th of Sep and it then it starts the 9th from like 11 pm at night. I'm not sure why I have this problem with IB data not giving me the previous day/night data, maybe I have a setting wrong somewhere . So what I have to do every day is put in the free license key and log on to the demo of zenfire so it fills in the data that is missing then I have to put in the live license key and then log back into IB so IB can continue with the live feed and I can trade live. The other problem is I just downloaded the new v 21 software and now I get an error trying to log into zenfire (see attached screenshot error). Any ideas how to fix this problem with the IB data and any idea why it doesn't show up? Thanks. DJ
NinjaTrader_Bertrand
09-10-2010, 09:39 AM
DJ, if you're using tick based chart intervals with IB, there's unfortunately no backfill for the time you're not connected live, as the connection does not provide this feature.
For the ZenFire issue in B21, please uninstall B21 from the Control Panel, delete the NT7 folder in Program Files on your harddrive and then reinstall it completely fresh - as this should clearup the issues you've ran into unfortunately.
djkiwi
09-13-2010, 03:46 PM
Hi Bertrand, I have reinstalled and all ok now apart from the tick data issue which continues to be annoying. The solution would be to include my broker AMP as an option on my live trading connection tab but have it inactive for live trading(or active and I simply don't use it for trading). This would save the hassle having to log in and out of the demo/live trading account day in and day out. Another issue I'm grappling with is turning a strategy into an indicator. I have done this before but does not seem to work now. For example I have copied this code from a strategy and turned it into an indicator. Although there were no errors the arrows don't show up even though they show up when a strategy is run. Would be grateful if you could take a quick look on this one!
Thanks
DJ
#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
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
[Description("Enter the description of your new custom indicator here")]
public class DJTF7 : Indicator
{
#region Variables
// Wizard generated variables
private int h1 = 5; // Default setting for H1
private int eMA1 = 10; // Default setting for EMA1
private int eMA2 = 20; // Default setting for EMA2
private int eMA3 = 50; // Default setting for EMA3
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
if (HMA(H1)[0] < HMA(H1)[1]
&& HMA(H1)[1] > HMA(H1)[2]
&& EMA(EMA1)[0] < EMA(EMA2)[0]
&& EMA(EMA2)[0] < EMA(EMA3)[0])
{
DrawArrowDown("My down arrow" + CurrentBar, false, 0, High[0] + 15 * TickSize, Color.Yellow);
}
// Condition set 2
if (HMA(H1)[0] > HMA(H1)[1]
&& HMA(H1)[1] < HMA(H1)[2]
&& EMA(EMA1)[0] > EMA(EMA2)[0]
&& EMA(EMA2)[0] > EMA(EMA3)[0])
{
DrawArrowUp("My up arrow" + CurrentBar, false, 0, Low[0] + -15 * TickSize, Color.Yellow);
}
}
#region Properties
[Description("")]
[GridCategory("Parameters")]
public int H1
{
get { return h1; }
set { h1 = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int EMA1
{
get { return eMA1; }
set { eMA1 = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int EMA2
{
get { return eMA2; }
set { eMA2 = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int EMA3
{
get { return eMA3; }
set { eMA3 = Math.Max(1, value); }
}
#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 DJTF7[] cacheDJTF7 = null;
private static DJTF7 checkDJTF7 = new DJTF7();
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public DJTF7 DJTF7(int eMA1, int eMA2, int eMA3, int h1)
{
return DJTF7(Input, eMA1, eMA2, eMA3, h1);
}
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public DJTF7 DJTF7(Data.IDataSeries input, int eMA1, int eMA2, int eMA3, int h1)
{
if (cacheDJTF7 != null)
for (int idx = 0; idx < cacheDJTF7.Length; idx++)
if (cacheDJTF7[idx].EMA1 == eMA1 && cacheDJTF7[idx].EMA2 == eMA2 && cacheDJTF7[idx].EMA3 == eMA3 && cacheDJTF7[idx].H1 == h1 && cacheDJTF7[idx].EqualsInput(input))
return cacheDJTF7[idx];
lock (checkDJTF7)
{
checkDJTF7.EMA1 = eMA1;
eMA1 = checkDJTF7.EMA1;
checkDJTF7.EMA2 = eMA2;
eMA2 = checkDJTF7.EMA2;
checkDJTF7.EMA3 = eMA3;
eMA3 = checkDJTF7.EMA3;
checkDJTF7.H1 = h1;
h1 = checkDJTF7.H1;
if (cacheDJTF7 != null)
for (int idx = 0; idx < cacheDJTF7.Length; idx++)
if (cacheDJTF7[idx].EMA1 == eMA1 && cacheDJTF7[idx].EMA2 == eMA2 && cacheDJTF7[idx].EMA3 == eMA3 && cacheDJTF7[idx].H1 == h1 && cacheDJTF7[idx].EqualsInput(input))
return cacheDJTF7[idx];
DJTF7 indicator = new DJTF7();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
indicator.Input = input;
indicator.EMA1 = eMA1;
indicator.EMA2 = eMA2;
indicator.EMA3 = eMA3;
indicator.H1 = h1;
Indicators.Add(indicator);
indicator.SetUp();
DJTF7[] tmp = new DJTF7[cacheDJTF7 == null ? 1 : cacheDJTF7.Length + 1];
if (cacheDJTF7 != null)
cacheDJTF7.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheDJTF7 = tmp;
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>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.DJTF7 DJTF7(int eMA1, int eMA2, int eMA3, int h1)
{
return _indicator.DJTF7(Input, eMA1, eMA2, eMA3, h1);
}
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Indicator.DJTF7 DJTF7(Data.IDataSeries input, int eMA1, int eMA2, int eMA3, int h1)
{
return _indicator.DJTF7(input, eMA1, eMA2, eMA3, h1);
}
}
}
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.DJTF7 DJTF7(int eMA1, int eMA2, int eMA3, int h1)
{
return _indicator.DJTF7(Input, eMA1, eMA2, eMA3, h1);
}
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Indicator.DJTF7 DJTF7(Data.IDataSeries input, int eMA1, int eMA2, int eMA3, int h1)
{
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.DJTF7(input, eMA1, eMA2, eMA3, h1);
}
}
}
#endregion
NinjaTrader_RyanM
09-13-2010, 04:09 PM
Hello djkiwiw,
You will want to be sure you have the CurrentBar check:
if (CurrentBar < 1)
return;
Make sure you have enough bars in the data series you are accessing (http://www.ninjatrader.com/support/forum/showthread.php?t=3170)
djkiwi
09-13-2010, 05:46 PM
Hi Ryan, not sure what you mean about current bar check, where does that go in the code? Thanks. DJ
djkiwi
09-13-2010, 06:05 PM
Also this worked as a strategy without any problems. The problem appeared when I copied the code and turned it into an indicator.
NinjaTrader_Bertrand
09-14-2010, 04:58 AM
DJ, yes the strategy would cover you normally against those issues (BarsRequired 'pause' unitl OnBarUpdate() is called) - for indicators you want to add the check Ryan outlined at the OnBarUpdate() start to ensure you don't try to access bars that are not available right at the start of the chart.
djkiwi
09-15-2010, 02:39 PM
Hi Bertrand, I tried this piece and put it at onbarupdate but still doesn't work as follows (bolded the attached piece although compiles ok). What should happen is arrows should show up in the right places same as the strategy. I have also attached the entire code. This works on a strategy so I was wondering if there is something in the code when transferred from a strategy to an indicator causes it not to work. I also copied all the wizard bits at the bottom of the strategy so not sure if that is needed or not. I have about 10 different strategies that I'd like to make indicators so I don't have to start them up each time so once I know how to do one all should work ok. Thanks. DJ
// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
{if (CurrentBar < 1)
return;
}
// Condition set 1
if (HMA(H1)[0] < HMA(H1)[1]
&& HMA(H1)[1] > HMA(H1)[2]
&& EMA(EMA1)[0] < EMA(EMA2)[0]
&& EMA(EMA2)[0] < EMA(EMA3)[0])
{
DrawArrowDown("My down arrow" + CurrentBar, false, 0, High[0] + 15 * TickSize, Color.Yellow);
}
NinjaTrader_RyanM
09-15-2010, 03:57 PM
Hello djkiwi,
Sorry for not catching this initially, but it looks like you are accessing index values 2 bars back, so the 1 bar check isn't sufficient. Strategy logic uses a default value of 20 (MinimumBarsRequired).
You can probably use a value of 20 and not notice any difference. It just means there won't be indicator processing for the first 20 bars.
if (CurrentBar < 20)
return;
Let us know if you're still having issues with this.
djkiwi
09-19-2010, 12:20 PM
Thanks for the help guys that worked well perfectly. I have another one I'm grappling with as well I'd be grateful if you could provide some pointers. What I am doing is triggering a sound alert once the tick hits 800 or 900 or 1000 etc. Example is below so it gives a sound alert each time the tick exceeds the value. I did it this way because that's all I could figure out how to do it with limited knowledge.
What I'd really like to do though is trigger the alert every time the tick increases 100 or decreases 100 from a base of 100. So if the tick is 600 and it hits say 740 then it should alert, alternatively if it then hits 800 it should alert again because it has increased over the threshold of 100. I'd like to do it this way because I'd like to apply this to other instruments as well by just changing the 100 threshold and without have to specify the value triggers each time which is proving to be very time consuming. Any help on this one would be appreciated. Thanks. DJ
// Condition set 7
if (CrossAbove(Close, 1000, 1))
{
Alert("MyAlert1134", Priority.High, "", @"C:\Program Files\NinjaTrader 7\sounds\tick 1000.wav", 10, Color.White, Color.Black);
}
// Condition set 8
if (CrossBelow(Close, -1000, 1))
{
Alert("MyAlert1132", Priority.High, "", @"C:\Program Files\NinjaTrader 7\sounds\tick -1000.wav", 5, Color.White, Color.Black);
}
// Condition set 9
if (CrossAbove(Close, 900, 1))
{
Alert("MyAlert49", Priority.High, "", @"C:\Program Files\NinjaTrader 7\sounds\tick 900.wav", 10, Color.White, Color.Black);
}
// Condition set 10
if (CrossBelow(Close, -900, 1))
{
Alert("MyAlert49", Priority.High, "", @"C:\Program Files\NinjaTrader 7\sounds\tick -900.wav", 10, Color.White, Color.Black);
}
NinjaTrader_Josh
09-19-2010, 01:29 PM
djkiwi,
To clarify, when you say tick you mean the latest price? When you say 800, 900, etc. you mean price becomes that?
So you are trading an instrument where it is possible to move 100 pts. fairly easily?
What you need to do is define out a starting point for the value you are at. Perhaps the opening price of the day. Then you can just add 100 on top of that for your alert price. When the alert is triggered, change the alert price to reflect the new price afterwards.
Untested code to give you some hints
if (FirstTickOfBar && Bars.FirstBarOfSession)
{
alertPriceUp = Close[0] + 100;
alertPriceDown = Close[0] - 100;
}
if (CrossAbove(Close, alertPriceUp, 1))
{
Alert(...);
alertPriceUp = Close[0] + 100;
}
if (CrossBelow(Close, alertPriceDown, 1))
{
Alert(...);
alertPriceDown = Close[0] - 100;
}
djkiwi
09-20-2010, 12:09 PM
Thaks Josh, well I tried this one and get an error that te alertpriceup does not exist in the current context. I then tried to define it in variables using private int = alert price up and got another error. Have I put something in the wrong place? Thanks. DJ
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
[Description("Enter the description of your new custom indicator here")]
public class djpricechange : Indicator
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
#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()
{
Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
Overlay = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (FirstTickOfBar && Bars.FirstBarOfSession)
{
alertPriceUp = Close[0] + 100;
alertPriceDown = Close[0] - 100;
}
if (CrossAbove(Close, alertPriceUp, 1))
{
Alert("MyAlert1137", Priority.High, "", @"C:\Program Files\NinjaTrader 7\sounds\tick 1300.wav", 10, Color.White, Color.Black);
alertPriceUp = Close[0] + 100;
}
if (CrossBelow(Close, alertPriceDown, 1))
{
Alert("MyAlert1137", Priority.High, "", @"C:\Program Files\NinjaTrader 7\sounds\tick -1300.wav", 10, Color.White, Color.Black);
alertPriceDown = Close[0] - 100;
}
}
NinjaTrader_Brett
09-20-2010, 12:43 PM
Hello,
You need to add a variable declartion for both alertPriceUp and alertPriceDown. In the #region Variables.
Best to use doubles here since your dealing with price.
double alertPriceUp = 0;
double alertPriceDown =0;
Let me know if I can be of further assistance.
djkiwi
09-21-2010, 05:36 PM
Hi Brett, thanks for that input. It got rid of the compile error and this is working but not entirely correctly. I have enclosed a chart so you can see what I'm trying to achieve. I can see the problem now. So if you look at the chart you can see the signals (via partially obscured arrow heads) in the first half hour trading as it fell the required 100. Then at 7.14 am another signal as it fell another 100 to -817 then another at 10.40 am as it ht -917. Then another signal was triggered at 11.36 am. This triggered because it was above the very first one of the day.
So what I'm trying to achieve is that the 100 is triggered when it moves up OR down from the last signal. So in this case at 7.10 am when there was a trigger at 6.42 am of about -707 there should have been a signal at 6.46 am as it rose 100.
So I'm guessing the first issue is the first line of the code:
if (FirstTickOfBar && Bars.FirstBarOfSession).
Do you see the problem now? Thanks. DJ
PS: I just thought the fact this Symbol has negative numbers which maybe causing some issue.
Code attached:
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
[Description("Enter the description of your new custom indicator here")]
public class djpricechange : Indicator
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
double alertPriceUp = 0;
double alertPriceDown =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()
{
Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
Overlay = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (FirstTickOfBar && Bars.FirstBarOfSession)
{
alertPriceUp = Close[0] + 100;
alertPriceDown = Close[0] - 100;
}
if (CrossAbove(Close, alertPriceUp, 1))
{
Alert("MyAlert1137", Priority.High, "", @"C:\Program Files\NinjaTrader 7\sounds\tick 1300.wav", 10, Color.White, Color.Black);
DrawArrowDown("My down arrow" + CurrentBar, false, 0, Close[0] + 20 * TickSize, Color.Red);
alertPriceUp = Close[0] + 100;
}
if (CrossBelow(Close, alertPriceDown, 1))
{
Alert("MyAlert1137", Priority.High, "", @"C:\Program Files\NinjaTrader 7\sounds\tick -1300.wav", 10, Color.White, Color.Black);
alertPriceDown = Close[0] - 100;
DrawArrowDown("My down arrow" + CurrentBar, false, 0, Close[0] + 20 * TickSize, Color.Red);
}
}
#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries Plot0
{
get { return Values[0]; }
}
[Description("")]
[GridCategory("Parameters")]
public int MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
#endregion
}
}
NinjaTrader_Bertrand
09-22-2010, 05:52 AM
DJ, I suggest you add prints throughout your code to check what values are calculated by your indicator so you can compare and deduct where it does not return as you would expect - the FirstTickOfBar would only be of value if running in realtime or Market Replay with CalculateOnBarClose = false.
djkiwi
10-03-2010, 09:21 AM
Hi Bertrand, still struggling with this one. I am trying to add the print command although am getting an error. I assume this prints an "up" or "down" on the chart when the condition is met? Please correct me if I'm wrong. Anyway the problem is it says the 'printwithtimestamp' does not exist in the current context. I copied this in from code produced by the strategy wizard and seems to work ok in the wizard but when I copy it to unlocked program shows an error. Please see code below. Thanks. DJ
PrintWithTimeStamp("UP");
PrintWithTimeStamp("DOWN");
[Description("Enter the description of your new custom indicator here")]
public class djpricechange : Indicator
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
double alertPriceUp = 0;
double alertPriceDown =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()
{
Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
Overlay = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (FirstTickOfBar && Bars.FirstBarOfSession)
{
alertPriceUp = Close[0] + 100;
alertPriceDown = Close[0] - 100;
}
if (CrossAbove(Close, alertPriceUp, 1))
{
Alert("MyAlert1137", Priority.High, "", @"C:\Program Files\NinjaTrader 7\sounds\tick 1300.wav", 10, Color.White, Color.Black);
DrawArrowUp("My up arrow" + CurrentBar, false, 0, Low[0] + -15 * TickSize, Color.Yellow);
alertPriceUp = Close[0] + 100;
PrintWithTimeStamp("UP");
}
if (CrossBelow(Close, alertPriceDown, 1))
{
Alert("MyAlert1137", Priority.High, "", @"C:\Program Files\NinjaTrader 7\sounds\tick -1300.wav", 10, Color.White, Color.Black);
DrawArrowDown("My down arrow" + CurrentBar, false, 0, High[0] + 15 * TickSize, Color.Yellow);
alertPriceDown = Close[0] - 100;
PrintWithTimeStamp("DOWN");
}
NinjaTrader_Austin
10-03-2010, 01:17 PM
DJ, the Print() command prints to the output window (Tools -> Output Window), not to the chart. What is the error you are receiving?
djkiwi
10-03-2010, 03:58 PM
Hi Austin, the error is CS0103 and says:
PrintWithTimeStamp("UP") does not exist in the current context.
What I'm try to show is the actual value. It's hard to explain at this point. So instead of UP what would I put in something like:
PrintWithTimeStamp(Close[0]);
That had same error.
So not really sure how to use this print command. So what happens here is when the the price increases by 100 then it should give an alert and then when it decreases by 100 it should give another alert. It seems to work sometimes but there is a problem. Bertrand suggested putting in this print command to test the values coming through. So help with this print command would be appreciated. Thanks. DJ
NinjaTrader_Austin
10-03-2010, 09:18 PM
DJ, please just use Print() to print out all the information, like this:
// this will print out the bar's timestamp, close price, and some text at the end
Print(Time[0].ToString() + " " + Close[0] + " " + "some text here at the end");
djkiwi
10-04-2010, 09:17 AM
Thanks Austin, that worked well. Ok, so what this code should do is give an alert each time the amount moves +100 or -100 using the last amount as the base. So what it is actually doing is moving up ok (as per this example) BUT will not alert when it moves down 100. It only alerts the -100 (See-924 below) when the the amount down exceeds the lowest amount posted in this case it was at the open (around -820). See attached graph. Also the next -100 recorded was at 7.48 am. I have posted the code below as well so you can see what is going on. Any ideas on this one would be appreciated. Thanks. DJ
10/4/2010 6:34:00 AM -592 some text here at the end
10/4/2010 6:36:00 AM -467 some text here at the end
10/4/2010 6:40:00 AM -352 some text here at the end
10/4/2010 6:40:00 AM -240 some text here at the end
10/4/2010 6:41:00 AM -136 some text here at the end
10/4/2010 6:45:00 AM -21 some text here at the end
10/4/2010 6:45:00 AM 110 some text here at the end
10/4/2010 6:47:00 AM 214 some text here at the end
10/4/2010 7:02:00 AM -924 some text here at the end
10/4/2010 7:48:00 AM -1045 some text here at the end
Code:
protected override void OnBarUpdate()
{
if (FirstTickOfBar && Bars.FirstBarOfSession)
{
alertPriceUp = Close[0] + 100;
alertPriceDown = Close[0] - 100;
}
if (CrossAbove(Close, alertPriceUp, 1))
{
Alert("MyAlert1137", Priority.High, "", @"C:\Program Files\NinjaTrader 7\sounds\tick 1300.wav", 10, Color.White, Color.Black);
DrawArrowUp("My up arrow" + CurrentBar, false, 0, Low[0] + -15 * TickSize, Color.Yellow);
alertPriceUp = Close[0] + 100;
Print(Time[0].ToString() + " " + Close[0] + " " + "some text here at the end");
}
if (CrossBelow(Close, alertPriceDown, 1))
{
Alert("MyAlert1137", Priority.High, "", @"C:\Program Files\NinjaTrader 7\sounds\tick -1300.wav", 10, Color.White, Color.Black);
DrawArrowDown("My down arrow" + CurrentBar, false, 0, High[0] + 15 * TickSize, Color.Yellow);
alertPriceDown = Close[0] - 100;
Print(Time[0].ToString() + " " + Close[0] + " " + "some text here at the end");
}
}
NinjaTrader_Bertrand
10-04-2010, 11:54 AM
DJ, the outcome you see would mean you fail to update the new amount base then as needed as it still takes the old one as reference for the calculations.
djkiwi
10-04-2010, 12:04 PM
Hi Bertrand, sorry I'm not sure what you mean here. Can you please explain? So are you saying this isn't possible to do? :( Thanks. DJ
NinjaTrader_RyanM
10-04-2010, 01:40 PM
You are setting the value to the first level. You're not incrementing it at each condition. The lines highlighted below say, "Add/subtract 100 to the value of alert price up". It's equivalent to writing
alertPriceUp = alertPriceUp + 100
if (CrossAbove(Close, alertPriceUp, 1))
{
Alert("MyAlert1137", Priority.High, "", @"C:\Program Files\NinjaTrader 7\sounds\tick 1300.wav", 10, Color.White, Color.Black);
DrawArrowUp("My up arrow" + CurrentBar, false, 0, Low[0] + -15 * TickSize, Color.Yellow);
alertPriceUp += 100;
Print(Time[0].ToString() + " " + Close[0] + " " + "some text here at the end");
}
if (CrossBelow(Close, alertPriceDown, 1))
{
Alert("MyAlert1137", Priority.High, "", @"C:\Program Files\NinjaTrader 7\sounds\tick -1300.wav", 10, Color.White, Color.Black);
DrawArrowDown("My down arrow" + CurrentBar, false, 0, High[0] + 15 * TickSize, Color.Yellow);
alertPriceDown -= 100;
Print(Time[0].ToString() + " " + Close[0] + " " + "some text here at the end");
}
djkiwi
12-02-2010, 11:39 PM
Hi Guys, still having problems with drawing things. I'm trying to insert the closing volume bar value at the top of each candle. I am able to draw text no problem but not sure how I go about getting the value of an indicator above or below the candle.
DrawText("My text" + CurrentBar, "150", 0, Low[0] - 8 * TickSize, Color.Lime);
Amy ideas on this one? Thanks. DJ
NinjaTrader_Bertrand
12-03-2010, 05:04 AM
You could just convert the indicator value to a string and then DrawText it as needed -
DrawText("My text" + CurrentBar, Volume[0].ToString(), 0, Low[0] - 8 * TickSize, Color.Lime);
djkiwi
12-03-2010, 10:16 AM
Thanks Bertrand, you know I spent forever trying to find how to do that, and you do it in 1 minute. :( Now I am trying to put it in the candle which I can do ok but when I try to put a background behind it says method requires 10 arguments. I checked against the drawtext() in the manual but the example doesn't have all the items in it. Any ideas on this one? Thanks. DJ
private Font textFontMed = new Font("Arial", 8, FontStyle.Bold);
DrawText("My text" + CurrentBar, (GomDeltaVolume().UpVolume[0] + GomDeltaVolume().DownVolume[0]).ToString(), 0, ((Open [0] + Close[0])/2), Color.Lime,textFontMed, StringAlignment.Center, Color.Black, Color.Pink, 5);
NinjaTrader_Bertrand
12-03-2010, 10:22 AM
DJ, if you do the extended overload on NT7 you would need set the autoScale property, too - just go through each parameter with Intellisense, autoScale would be the second one in your snippet.
djkiwi
12-03-2010, 02:19 PM
Hi Bertrand, still researching this problem. Have another baffling problem as well. I have a condition that once satisfied is supposed to draw the value at the low of the candle (Output1) and draw another value in the middle of the candle (Output 2). Problem is it will only draw one and not the other. If I remove draw text output 1 it will draw output 2. If I remove output 2 it will draw output 1 BUT will never draw both at the same time. I was wondering then if you can draw two things around a candle at the same time? Thanks.
Output 1
DrawText("My text" + CurrentBar, (((GomDeltaVolume().UpVolume[0] + GomDeltaVolume().DownVolume[0])*100)/Volume[0]).ToString("N0"), 0, Low[0] -2 * TickSize, Color.Yellow);
Output 2
DrawText("My text" + CurrentBar, (GomDeltaVolume().UpVolume[0] + GomDeltaVolume().DownVolume[0]).ToString(), 0, ((Open [0] + Close[0])/2), Color.Lime);
NinjaTrader_RyanM
12-03-2010, 02:56 PM
Hello djkiwi,
The issue is that you're using the same tag for both objects. Note unique tags below:
DrawText("My text" + CurrentBar, (((GomDeltaVolume().UpVolume[0] + GomDeltaVolume().DownVolume[0])*100)/Volume[0]).ToString("N0"), 0, Low[0] -2 * TickSize, Color.Yellow);
DrawText("My text2" + CurrentBar, (GomDeltaVolume().UpVolume[0] + GomDeltaVolume().DownVolume[0]).ToString(), 0, ((Open [0] + Close[0])/2), Color.Lime);
djkiwi
12-03-2010, 04:10 PM
Thanks Bertrand, I really need to learn this stuff faster :(
Anyway the other issue is still causing a problem. I went back through it all and still doesn't work. I found this code from Josh on another post. I see I had missed out false on my version and have no inserted it. The problem is when I compile with the one from Josh and comment out mine (below) I get the same error with his one. No overload method draw text takes 11 arguments. I have checked it and has all of the items that is needed? Any other ideas on this one? Thanks. DJ
1. Josh
DrawText("test", false, "Test Text", 5, MAX(High, 20)[0], Color.Red, new Font("Arial", 12), StringAlignment.Far, Color.Transparent, Color.Transparent, 50);
2. Mine
//DrawText("My text8" + CurrentBar, false, (GomDeltaVolume().UpVolume[0] + GomDeltaVolume().DownVolume[0]).ToString(), 0, ((Open [0] + Close[0])/2), Color.Lime,textFontMed, StringAlignment.Center, Color.Black, Color.Pink, 5);
NinjaTrader_RyanM
12-03-2010, 04:24 PM
You can use intellisense to help generate the right syntax. Just type DrawText( (http://www.ninjatrader.com/support/helpGuides/nt7/drawtext.htm) and then cycle through the available overloads with your up and down arrows. Advance through each field with a comma ,
DrawText(string tag, bool autoScale, string text, int barsAgo, double y, int yPixelOffset, Color textColor, Font font, StringAlignment alignment, Color outlineColor, Color areaColor, int areaOpacity)
hunter548
01-08-2011, 09:57 PM
Just converted to NT7 and I notice that most indicator arrows seem to have a white outline around them that did not occur in NT6.5. I prefer a black outline. For candles, I can change the outline color using CandleOutlineColor. Is there a similar way to change the arrow outline color? If so, does this method apply to other drawing objects (triangles, dots, etc) as well?
Ok, with sufficient digging through posts, I found where it was explained that the arrow and other object outline colors are determined by the axis color. Explains the white halo, but still makes the objects look like crap when used with a dark background and a light color axis.
You really should consider changing this in a future revision.
NinjaTrader_Bertrand
01-10-2011, 03:04 AM
This is unfortunately correct hunter - thanks for the feedback and suggestion.