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>
/// Just seeing how this stuff works!!!
/// </summary>
[Description("Just seeing how this stuff works!!!")]
public class RivTestNumber1 : Strategy
{
#region Variables
// Wizard generated variables
private bool closeAboveMidBand = false; // Default setting for CloseAboveMidBand
private bool openBelowMidBand = false; // Default setting for OpenBelowMidBand
private bool lowBelowBottomBand1 = false; // Default setting for LowBelowBottomBand1
private bool lowBelowBottomBand2 = false; // Default setting for LowBelowBottomBand2
private int initialSimpleMovingAverage = 1; // Default setting for InitialSimpleMovingAverage
private int justOne = 1; // Default setting for JustOne
private bool tradeFinalized = false;
private bool strategyActive = false;
private bool lookForLows = false;
private int lowCount = 0;
private int barChangeCount = 0;
// 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()
{
Print( "In Initialize()" );
//set period to 3 minutes (3 minute chart, instead of every tick change)
//Add(PeriodType.Minute, 3);
//set initial SMA to current SMA (used to determine when OPEN and/or CLOSE and/or HIGH and/or LOW
//are above/below it (for current run of strategy)
InitialSimpleMovingAverage = SMA(14)[0];
Print("InitialSimpleMovingAverage = SMA(14)[0]: " + SMA(14)[0] );
//set trafeFinalized to false, once a successful sell/buy is executed, this will be set to true
tradeFinalized = false;
Log("tradeFinalized = false", LogLevel.Information);
//set runStrategy to false, indicating that the OPEN/CLOSE/HIGH/LOW are not all above the SMA
strategyActive = false;
Log("strategyActive = false", LogLevel.Information);
lookingForLows = false;
Log("lookingForLows = false", LogLevel.Information);
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
//only process if this is a change in the primary bar data
//if (BarsInProgress != 0)
// return;
//if onBarUpdate the tradeFinalized bool is true, need to call Initialize() again
if( tradeFinalized )
{
Initialize();
Log("call Initialize from OnBarUpdate", LogLevel.Information);
}
//if this strategy is not yet active, check to see if we want to make it active
if( !strategyActive )
{
Log("strategyActive check was false", LogLevel.Information);
//make strategy active when the entire candlestick is completely above the SMA
if( High[0]>InitialSimpleMovingAverage && Low[0]>InitialSimpleMovingAverage && Open[0]>InitialSimpleMovingAverage && Close[0]>InitialSimpleMovingAverage )
{
strategyActive = true;
PlaySound(@"C:\Program Files\NinjaTrader 6.5\sounds\Alert4.wav");
Log("NOW ACTIVE - candlestick body below the SMA", LogLevel.Information);
}
}
//run the strategy logic only if it is active
if( strategyActive )
{
Log("strategyActive = true", LogLevel.Information);
//only want to run this piece of code if the candlestick has not dropped below the
//SMA for the first time
if( !lookForLows )
{
Log("not looking for lows yet", LogLevel.Information);
//look for the entire body of the candlestick to drop below SMA
if( Open[0] < SMA(14)[0] && Close[0] < SMA(14)[0] && Low < SMA(14)[0] )
{
//now we are looking for 2 lows to be below the bottom of the bollinger band so we
//can buy
lookForLows = true;
Log("NOW we are looking for lows!!!", LogLevel.Information);
}
return;
}
//once we enter into this code, we are looking for 2 lows below bollinger low
//(when close < open) in 3 changes
if( lookForLows )
{
//only counting lows that go through bollinger low if they have a close < high
if( Close[0] < Open[0] )
{
//increment for every bar change
barChangeCount++;
//increment the bar changes when the low is less than bollinger low
if( Low[0] < Bollinger(2, 14).Lower[0] )
{
lowCount++;
}
}
//complete strategy by selling
//only
if( lowCount == 2 && barChangeCount <= 3 )
{
//enter a short (single contract)
EnterShort( 1 );
//set profit target
SetProfitTarget( CalculationMode.Ticks, 8 );
//set stop loss
SetTrailStop("Short", CalculationMode.Ticks, 3, true);
//set tradeFinalized to true
tradeFinalized = true;
}
else if( barChangecount > 3 )
{
Initialize();
}
}
}
}
#region Properties
[Description("Ensures current low is above mid band")]
[Category("Parameters")]
public bool CloseAboveMidBand
{
get { return closeAboveMidBand; }
set { closeAboveMidBand = value; }
}
[Description("Marks the move from the top of bollinger band to the bottom")]
[Category("Parameters")]
public bool OpenBelowMidBand
{
get { return openBelowMidBand; }
set { openBelowMidBand = value; }
}
[Description("Looks for first low below absolute bottom band")]
[Category("Parameters")]
public bool LowBelowBottomBand1
{
get { return lowBelowBottomBand1; }
set { lowBelowBottomBand1 = value; }
}
[Description("Looks for second low below absolute bottom band")]
[Category("Parameters")]
public bool LowBelowBottomBand2
{
get { return lowBelowBottomBand2; }
set { lowBelowBottomBand2 = value; }
}
[Description("Grab the initial SMA so we can determine when we drop below it")]
[Category("Parameters")]
public int InitialSimpleMovingAverage
{
get { return initialSimpleMovingAverage; }
set { initialSimpleMovingAverage = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int JustOne
{
get { return justOne; }
set { justOne = Math.Max(1, value); }
}
#endregion
}
}
Comment