![]() |
|
|||||||
| Indicator Development Support for the development of custom indicators using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Nov 2004
Location: , ,
Posts: 110
Thanks: 1
Thanked 1 time in 1 post
|
I was able to change the back ground to blue for an up trend but the code to set back ground to magenta for a down trend make it goes blank.
Code:
//
// Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
// NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
//
#region Using declarations
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.Xml.Serialization;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
#endregion
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// The TRIX (Triple Exponential Average) displays the percentage Rate of Change (ROC) of a triple EMA. Trix oscillates above and below the zero value. The indicator applies triple smoothing in an attempt to eliminate insignificant price movements within the trend that you're trying to isolate.
/// </summary>
[Description("The TRIX (Triple Exponential Average) displays the percentage Rate of Change (ROC) of a triple EMA. Trix oscillates above and below the zero value. The indicator applies triple smoothing in an attempt to eliminate insignificant price movements within the trend that you're trying to isolate.")]
public class TRIXc : Indicator
{
#region Variables
private int period = 8;
private int signalPeriod = 1;
private string sellText = @"SELL";
private Font pfont = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point);
private Color sColor = Color.Red;
private string buyText = @"BUY";
private Color bColor = Color.Black;
private int yPixels = 10;
private string upArrow = "Y";
#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.Black, "Default"));
Add(new Plot(Color.Red, "Signal"));
Add(new Line(Color.DarkGray, 0, "Zero line"));
// Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
// Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "Plot1"));
}
/// <summary>
/// Calculates the indicator value(s) at the current index.
/// </summary>
protected override void OnBarUpdate()
{
if (CurrentBar == 0)
{
Value.Set(Input[0]);
return;
}
EMA tripleEma = EMA(EMA(EMA(Input, period), period), period);
double trix = 100 * ((tripleEma[0] - tripleEma[1]) / tripleEma[0]);
Default.Set(trix);
// if ((trix > 0) && (trix > ( 100 * ((tripleEma[1] - tripleEma[2]) / tripleEma[1]))))
Signal.Set(EMA(Default, signalPeriod)[0]);
if (trix < 0) upArrow = "Y";
if (trix > 0)
if (trix > 100 * ((tripleEma[1] - tripleEma[2]) / tripleEma[1]))
BackColor = Color.LightSeaGreen; // set back ground color
else if (upArrow == "Y")
{
DrawArrowDown("MyArrowDown"+CurrentBar, 0, High[0]+2* TickSize, Color.Red);
// DrawDot(CurrentBar.ToString(), 0, High[1] + 10 * TickSize, Color.Red);
// DrawText("MyText"+CurrentBar, true,"Sell", 0, High[0] + 12 * TickSize, Color.Red,pfont);
DrawText("V"+CurrentBar, true, sellText, 0, High[0] + 8 * TickSize, yPixels, sColor, pfont,
StringAlignment.Center, Color.Empty, Color.Empty, 10) ;
upArrow = "N";
}
// *********************** trouble code here ******************************
// if (trix < 0)
// if (trix > 100 * ((tripleEma[1] - tripleEma[2]) / tripleEma[1]))
// BackColor = Color.LightPink; // set back ground color
// *************************************************************************
// if (CrossBelow(TRIX(period, signalPeriod).Default, TRIX(period, signalPeriod).Signal, 1))
/* if (CrossBelow(TRIX(period, signalPeriod).Default, 0,1))
{
DrawArrowDown("MyArrowDown"+CurrentBar, 0, High[0]+2* TickSize, Color.Red);
// DrawDot(CurrentBar.ToString(), 0, High[1] + 10 * TickSize, Color.Red);
// DrawText("MyText"+CurrentBar, true,"Sell", 0, High[0] + 12 * TickSize, Color.Red,pfont);
DrawText("V"+CurrentBar, true, sellText, 0, High[0] + 8 * TickSize, yPixels, sColor, pfont,
StringAlignment.Center, Color.Empty, Color.Empty, 10) ;
}
//if (CrossAbove(TRIX(period, signalPeriod).Default, TRIX(period, signalPeriod).Signal, 1))
if (CrossAbove(TRIX(period, signalPeriod).Default, 0,1))
{
DrawArrowUp("MyArrowUp"+CurrentBar, 0, Low[0] -2* TickSize, Color.Orange);
// DrawText("MyText"+CurrentBar, "Buy", 0, Low[0] - 6 * TickSize, Color.Green);
DrawText("V"+CurrentBar, true, buyText, 0, Low[0] - 10 * TickSize, yPixels, bColor, pfont,
StringAlignment.Center, Color.Empty, Color.Empty, 10) ;
}
*/
}
#region Properties
/// <summary>
/// </summary>
[Description("Numbers of bars used for calculations.")]
[Category("Parameters")]
public int Period
{
get { return period; }
set { period = Math.Max(1, value); }
}
[Description("Period for the signal line.")]
[Category("Parameters")]
[Gui.Design.DisplayNameAttribute("Signal period")]
public int SignalPeriod
{
get { return signalPeriod; }
set { signalPeriod = Math.Max(1, value); }
}
/// <summary>
/// </summary>
[Browsable(false)]
[XmlIgnore]
public DataSeries Signal
{
get { return Values[1]; }
}
/// <summary>
/// </summary>
[Browsable(false)]
[XmlIgnore]
public DataSeries Default
{
get { return Values[0]; }
}
#endregion
}
}
|
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,377
Thanks: 252
Thanked 966 times in 949 posts
|
Hi nkhoi, do you see any errors in the Log tab of the Control Center when this happens?
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Nov 2004
Location: , ,
Posts: 110
Thanks: 1
Thanked 1 time in 1 post
|
thanks for remind me to look at the log, I added current bar > 1 and it worked.
![]() Error on calling the 'OnBarUpdate' method for indicator 'TRIXc' on bar 1: Index was out of range. Must be non-negative and less than the size of the collection. |
|
|
|
|
|
#4 |
|
Junior Member
Join Date: Apr 2009
Posts: 5
Thanks: 0
Thanked 0 times in 0 posts
|
I'm having the same problem in retrieving a DataSeries of anything larger than [1] -- same "out of range" error in log. However, I'm not clear about how and where to enter the "CurrentBar > 1" statement. Should it be entered at the beginning of the 'OnBarUpdate' or within the IF statement calling for the DataSeries? What is the syntax? Thanks!
|
|
|
|
|
|
#5 |
|
Senior Member
Join Date: Nov 2004
Location: , ,
Posts: 110
Thanks: 1
Thanked 1 time in 1 post
|
change
(CurrentBar == 0) to (CurrentBar > 1) |
|
|
|
|
|
#6 |
|
Junior Member
Join Date: Apr 2009
Posts: 5
Thanks: 0
Thanked 0 times in 0 posts
|
Perfect ! Thanks Nkhoi
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need Help with KST Indicator code | rwwoods | Indicator Development | 2 | 06-29-2012 08:52 PM |
| How to import indicator code | TheAnalyst13 | Indicator Development | 19 | 04-01-2010 03:16 PM |
| Indicator code | bulegila | Indicator Development | 1 | 04-26-2008 10:42 PM |
| An improved RsiStoch indicator code | Hank M | Indicator Development | 4 | 02-06-2008 12:52 AM |
| Wizard vs Code for indicator on indicator | Don44 | NinjaScript File Sharing Discussion | 5 | 01-24-2008 02:41 PM |