PDA

View Full Version : Help with my first script (3 bar reversal)


ClydeMacLeod
10-16-2007, 06:06 PM
Hi Guys,

I’m hoping you can help me. I originally wrote this in VB for another trading platform and have converted it to C#. (I’m brand new to C#, but think I have done this properly). When I compile I get a whole load of error messages that do not refer to any lines in my code. I really don’t have any easy way of debugging this.

I had thought about compiling the base template before I started adding code and then at each major step. However, if I try and compile the brand new template, as is, I get errors as well.

I’d really appreciate any help you can give me on this. My intention is to turn this into a system that will automatically move my profit stops on Forex trades at night.

Thanks,
Clyde



Code logic -- 3 bar reversal stop A three bar reversal stop looks three valid bars back and finds either the lowest low (for longs) or highest high (for shorts) of the prior 3 bars. [/font]

A valid bar is defined as being a bar that is not an inside bar (a bar completely within the trading range of the prior bar).[/font]

<snippet - bbs would not premit me to post the whole thing>

protectedoverridevoid Initialize()
{
Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
CalculateOnBarClose = true;
Overlay = true;
PriceTypeSupported = false;
}

///<summary>
/// Called on each bar update event (incoming tick)
///</summary>

protectedoverridevoid OnBarUpdate()
{
int constOneBarAgo = 1;
int CurrentBarToReview = 1; // Trade bar we are evaluating
int OneBarPriorToCurrentBar = 2;
double RangeLL = Low[0]; // the cumulative Lowest Low of the back test range (start with current bar)
double RangeHH = High[0]; // the cumulative Highest High of the back test range
int ValidBarCount = 0; // We need 3 valid look back bars

while (ValidBarCount < 3)
{
// Loop for three valid bars back
// is this a valid bar for testing?
// A valid bar must be either higher or lower (or both) than the prior bar (not an inside day)

if (High[CurrentBarToReview] > High[OneBarPriorToCurrentBar] | Low[CurrentBarToReview] < Low[OneBarPriorToCurrentBar])
{
if (High[CurrentBarToReview] > RangeHH) RangeHH = High[CurrentBarToReview];
if (Low[CurrentBarToReview] < RangeLL) RangeLL = Low[CurrentBarToReview];
ValidBarCount ++;
}
//Increment loop and index pointer variables
CurrentBarToReview ++;
OneBarPriorToCurrentBar ++;
}

if (longDirection == true)
Plot0.Set(RangeHH);
else
Plot0.Set(RangeLL);
}

#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("")]
[Category("Parameters")]

publicbool LongDirection
{
get { return longDirection; }
set { longDirection = value; }
}
#endregion
}
}

NinjaTrader_Josh
10-17-2007, 12:23 AM
Hi ClydeMacLeod,

If you could describe some of the errors or even upload the .cs file we could provide you with some more assistance.

A quick thing I noticed so far is this:
if (High[CurrentBarToReview] > High[OneBarPriorToCurrentBar] | Low[CurrentBarToReview] < Low[OneBarPriorToCurrentBar])

The OR operator is done with two horizontal bars '||'.

See if it allows you to compile after that change.
if (High[CurrentBarToReview] > High[OneBarPriorToCurrentBar] || Low[CurrentBarToReview] < Low[OneBarPriorToCurrentBar])

ClydeMacLeod
10-18-2007, 12:41 PM
Hi Josh,

Thanks for your reply. I changed the "or" to the correct symbol as you suggested and the code compiles.

However, no indicator results (no line) is displayed when I put this on my chart and I'm not sure why?

Thanks for your help.
Clyde

NinjaTrader_Josh
10-18-2007, 02:28 PM
Hi Clyde,

Please check your logs for errors. I suspect you are running into the issue outlined here: http://www.ninjatrader-support.com/vb/showthread.php?t=3170

Also please check out this tip for debugging:
http://www.ninjatrader-support.com/vb/showthread.php?t=3418

Good luck.

KBJ
10-21-2007, 05:58 AM
Clyde,

You might also want to take a look at the MAX (http://www.ninjatrader-support.com/HelpGuide/helpguide.html?MaximumMAX) and MIN (http://www.ninjatrader-support.com/HelpGuide/helpguide.html?MinimumMIN) functions in the NinjaTrader help, as they are a much easier way to do what the while loop in your code is attempting.

KBJ