some time ago i found this sample strategy for nt7 in this post at morbidly obese mike's forum:
https://futures.io/ninjatrader/2512-...-strategy.html
the strategy compiles and works without problem in nt7. this sample strategy helped me to begin putting some ideas into ninjascript and i have used it as a platform to create some simple strategies of my own.
[INDENT]#region Variables
[/INDENT]private int smalength = 120;
private int emalength = 120;
private int hmalength = 120;
private int target1 = 12;
private int target2 = 12;
private int target3 = 12;
private int stop = 12;
private bool be2 = false;
private bool be3 = false;
#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;
EntryHandling = EntryHandling.UniqueEntries;
}
private void GoLong()
{
SetStopLoss("target1", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
SetStopLoss("target2", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
SetStopLoss("target3", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
SetProfitTarget("target1", CalculationMode.Price, Close[0] + (Target1*TickSize));
SetProfitTarget("target2", CalculationMode.Price, Close[0] + ((Target1+Target2)*TickSize));
SetProfitTarget("target3", CalculationMode.Price, Close[0] + ((Target1+Target2+Target3)*TickSize));
EnterLong("target1");
EnterLong("target2");
EnterLong("target3");
}
private void GoShort()
{
SetStopLoss("target1", CalculationMode.Price, Close[0] + (Stop*TickSize), false);
SetStopLoss("target2", CalculationMode.Price, Close[0] + (Stop*TickSize), false);
SetStopLoss("target3", CalculationMode.Price, Close[0] + (Stop*TickSize), false);
SetProfitTarget("target1", CalculationMode.Price, Close[0] - (Target1*TickSize));
SetProfitTarget("target2", CalculationMode.Price, Close[0] - ((Target1+Target2)*TickSize));
SetProfitTarget("target3", CalculationMode.Price, Close[0] - ((Target1+Target2+Target3)*TickSize));
EnterShort("target1");
EnterShort("target2");
EnterShort("target3");
}
private void ManageOrders()
{
if (Position.MarketPosition == MarketPosition.Long)
{
if (BE2 && High[0] > Position.AvgPrice + (Target1*TickSize))
SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice, false);
if (BE3 && High[0] > Position.AvgPrice + ((Target1+Target2)*TickSize))
SetStopLoss("target3", CalculationMode.Price, Position.AvgPrice, false);
}
if (Position.MarketPosition == MarketPosition.Short)
{
if (BE2 && Low[0] < Position.AvgPrice - (Target1*TickSize))
SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice, false);
if (BE3 && Low[0] < Position.AvgPrice - ((Target1+Target2)*TickSize))
SetStopLoss("target3", CalculationMode.Price, Position.AvgPrice, false);
}
}
protected override void OnBarUpdate()
{
EntryHandling = EntryHandling.UniqueEntries;
SMA smav = SMA(smalength);
EMA emav = EMA(emalength);
HMA hmav = HMA(hmalength);
ManageOrders();
if (Position.MarketPosition != MarketPosition.Flat) return;
if (Rising(smav) && Rising(emav) && Rising(hmav))
GoLong();
else if (Falling(smav) && Falling(emav) && Falling(hmav))
GoShort();
}
- as an exercise, i am now trying to convert morbidly obsese mike's strategy from nt7 to nt8. this is as far as i have been able to go on my own:
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "morbidly obese mike's ema hma sma.";
Name = "sampleemahmasma";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 3;
EntryHandling = EntryHandling.UniqueEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 50;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 3;
StartBehavior = StartBehavior.AdoptAccountPosition;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelCloseIgnoreRejects;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 200;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
Smalength = 120;
Emalength = 120;
Hmalength = 120;
Target1 = 12;
Target2 = 12;
Target3 = 12;
Stop = 12;
Be2 = false;
Be3 = false;
}
else if (State == State.DataLoaded)
{
Smav[0] = SMA(Smalength)[0];
Emav[0] = EMA(Emalength)[0];
Hmav[0] = HMA(Hmalength)[0];
}
}
protected override void OnBarUpdate()
{
ManageOrders();
if (Position.MarketPosition != MarketPosition.Flat) return;
if ( IsRising(Smav) && IsRising(Emav) && IsRising(Hmav))
GoLong();
else if ( IsFalling(Smav) && IsFalling(Emav) && IsFalling(Hmav))
GoShort();
}
i haven't been able to include the manage orders, go long and go short private voids as i have no idea how to define and where to include them in nt8. ¿in which of the nt8 sections should i place them? ¿and how would the initialize and onbarupdate sections in nt7 correspond to the onstatechange, setdefaults, dataloaded and onbarupdate sections on nt8?
very well, thanks, regards.

Comment