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
namespace NinjaTrader.Strategy
{
[Description("Demonstration of AutoTrendH Indicator")]
//
public class AutoTrendHDemo : Strategy
{
#region Variables
// User Variables
//User variables for AutoTrendH indicator settings
private bool alert =true;
private bool showHistory =true;
private int strength =25; // Good number for 15 minute time frame (my preferred trading period)
// User variables used for Stop strategys (not implimented in this strategy)
private int stopEven = 4; // Move exit to breakeven after profitable stopEven pips/ticks
private int stopProfit = 16; // Move stop up after profitable move of stopProfit pips/ticks
private int stopShock = 4; // Exit trade is sudden negative excursion of stopShock pips/ticks. NOTE: Use tick or 1 minute secondary time frame to monitor
private int stopLoss = 16; // Initial stopLoss setting upon trade entry.
private int stopReverse = 0; // Reverse trade after stopReverse pips/ticks
private int quantity = 1; // DefaultQuantity to trade (one full lot in Forex)
private int eMAslowslow = 200; // Default setting for EMASlowSlow
private int eMAslowfast = 180; // Default setting for EMASlowFast
private int starttime = 100000;
private int stoptime = 120000;
private int startdate = 20180101;
private int stopdate = 20181231;
// Constants
int cBreakUp=1; int cBreakDown =-1;
int cRising =1; int cFalling =-1; int cFlat=0;
int cLong =1; int cShort =-1;
bool debug = false;
#endregion
//
protected override void Initialize()
{ Add(EMA(Low, EMAslowfast));
Add(EMA(Low, EMAslowslow));
DefaultQuantity = Quantity;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
CalculateOnBarClose = true;
Add(AutoTrendH(alert,showHistory,strength));
if (stopLoss>0) SetStopLoss(CalculationMode.Ticks,stopLoss);
if (stopProfit>0) SetProfitTarget(CalculationMode.Ticks,stopProfit);
//if (stopShock>0) Add(PeriodType.Minute, 1); // If we are monitoriing for price shocks, add lower time bar/tick for higher resolution detection
//if (stopLoss>0) SetStopLoss(CalculationMode.Ticks,stopLoss);
}//ENDInitialize()
//
protected override void OnBarUpdate()
{
if (ToDay(Time[0])>= startdate && ToDay(Time[0]) <= stopdate)
{
if ((ToTime(Time[0]) >= starttime && ToTime(Time[0]) <= stoptime))
{
//STOP Price Shock
if (stopShock>0)
{// If negative Intrabar excursion more then stopShock pips, exit trade
}
if (BarsInProgress != 0) return; //We only want the primary bars processing from this point on.
//
//PRELOAD
//I use this as my NT is not set to account for the tick price difference in the JPY's
if ( (Historical) && (Instrument.FullName == "$USDJPY") || (Instrument.FullName=="$EURJPY") )
{ DefaultQuantity=(int)(quantity*.01); }
//Preload the AutoTrendH values this bar for later use in the strategy
int trendDirection = AutoTrendH(alert,showHistory,strength).Direction; //1=TrendUp, -1=TrendDown, 0=New trend not yet determined
double trendPrice = AutoTrendH(alert,showHistory,strength).TrendPrice; //Tick value at rightmost bar of current trend line
int trendSignal = AutoTrendH(alert,showHistory,strength).Signal; //1=resistance break, -1=support break
//ENTRYS
// If price breaks through trend, reverse position.
if ( (trendDirection==cFalling) && (Close[0]>trendPrice)
&& EMA(Low, EMAslowfast)[0] > EMA(Low, EMAslowslow)[0])
EnterLong("long");
if ( (trendDirection==cRising) && (Close[0]<trendPrice)
&& EMA(Low, EMAslowfast)[0] < EMA(Low, EMAslowslow)[0])
EnterShort( "short");
}
}
}
//USER DEFINED METHODS
//
private int myPosition(int posNbr)
{// A cleaner way of coding position direction. Use posNbr if writing strategy with multiple position entries on different insturments/timeframes
if (Positions[posNbr].MarketPosition==MarketPosition.Long) return cLong;
if (Positions[posNbr].MarketPosition==MarketPosition.Short) return cShort;
if (Positions[posNbr].MarketPosition==MarketPosition.Flat) return cFlat;
return cFlat;
}//endMyPosition()
//
#region Properties
//
[Description("Sets audible and logs alert if set to true")]
[GridCategory("Parameters")]
public bool Alert
{ get { return alert; }
set { alert = value; }
}
//
[Description("Saves trendlines of auto-generated Trends")]
[GridCategory("Parameters")]
public bool ShowHistory
{ get { return showHistory; }
set { showHistory = value; }
}
//
[Description("Sets the granularity of trend detection (smaller # = finer trend detection")]
[GridCategory("Parameters")]
public int Strength
{ get { return strength; }
set { strength = Math.Max(1, value); }
}
//
[Description("Lock in profits after StopProfit pips/ticks")]
[GridCategory("Parameters")]
public int StopProfit
{ get { return stopProfit; }
set { stopProfit = value; }
}
//
[Description("1 Minute timeframe catastrophic loss stop")]
[GridCategory("Parameters")]
public int StopShock
{ get { return stopShock; }
set { stopShock = value; }
}
//
[Description("Move stop to breakeven after StopEven pips/ticks")]
[GridCategory("Parameters")]
public int StopEven
{ get { return stopEven; }
set { stopEven = value; }
}
//
[Description("Initial StopLoss on entry")]
[GridCategory("Parameters")]
public int StopLoss
{ get { return stopLoss; }
set { stopLoss = value; }
}
//
[Description("Reverse position after StopReverse pips/ticks")]
[GridCategory("Parameters")]
public int StopReverse
{ get { return stopReverse; }
set { stopReverse = value; }
}
//
[Description("Number of shares/contracts/Lots to buy")]
[GridCategory("Parameters")]
public int Quantity
{ get { return quantity; }
set { quantity = Math.Max(0, value); }
}
public int Startdate
{
get { return startdate; }
set { startdate = Math.Max(1, value); }
}
[Description("trend filter")]
[GridCategory("Parameters")]
public int EMAslowslow
{
get { return eMAslowslow; }
set { eMAslowslow = Math.Max(1, value); }
}
[Description("trend filter")]
[GridCategory("Parameters")]
public int EMAslowfast
{
get { return eMAslowfast; }
set { eMAslowfast = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int Stoptdate
{
get { return stopdate; }
set { stopdate = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int Starttime
{
get { return starttime;}
set { starttime = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int Stoptime
{
get { return stoptime; }
set { stoptime = Math.Max(1, value); }
}
#endregion
}
}
Comment