P.S. the boolean functions are just a wrapper for the approxcompare logic as a doubles extension (i.e. Close[0].EQ(somevalue)...etc) everything else is native ninjascript.
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies.MyStrategies {
public class MyBrac****rderTestStrategy : Strategy {
private double _priceLong = 0, _priceShort = 0;
private int _bi = 0, _biEntry = 0;
private int _fast = 10, _slow = 25;
protected override void OnStateChange() {
if (State == State.SetDefaults) {
Calculate = Calculate.OnBarClose;
Name = "MyBrac****rderTestStrategy";
}
else if (State == State.Configure) {
AddDataSeries(Data.BarsPeriodType.Tick, 1);
AddChartIndicator(EMA(_fast));
AddChartIndicator(EMA(_slow));
EMA(_fast).Plots[0].Brush = Brushes.Green;
EMA(_slow).Plots[0].Brush = Brushes.Blue;
SetProfitTarget(CalculationMode.Ticks, 4);
SetStopLoss(CalculationMode.Ticks, 12);
}
}
protected override void OnBarUpdate() {
if (CurrentBars[0] <= BarsRequiredToTrade || CurrentBars[1] <= BarsRequiredToTrade)
return;
if (BarsInProgress == 0) {
var c = Close[0];
_bi = CurrentBar;
if (CrossAbove(EMA(_fast), EMA(_slow), 1)) {
_biEntry = _bi;
_priceLong = c + TickSize;
_priceShort = Low[0] - TickSize;
BackBrush = Brushes.PowderBlue;
Print($"MIN Long bi: {_bi} close: {c} entry long: {_priceLong} entry short: {_priceShort}");
}
else if (CrossBelow(EMA(_fast), EMA(_slow), 1)) {
_biEntry = _bi;
_priceLong = High[0] + TickSize;
_priceShort = c - TickSize;
BackBrush = Brushes.PowderBlue;
Print($"MIN Short bi: {_bi} close: {c} entry short: {_priceShort} entry long: {_priceLong}");
}
if (_priceShort.GT(0) && _priceLong.GT(0)) {
Draw.Line(this, $"longentry" + CurrentBar, true, 1, _priceLong, 0, _priceLong, Brushes.Magenta, DashStyleHelper.Dot, 2);
Draw.Line(this, $"shortentry" + CurrentBar, true, 1, _priceShort, 0, _priceShort, Brushes.Magenta, DashStyleHelper.Dot, 2);
}
}
var notSameBarIndexAsEmaCrossover = _bi != _biEntry;
if (BarsInProgress == 1 && notSameBarIndexAsEmaCrossover) {
var onlyEnterOncePerEmaCrossover = _biEntry != 0;
if (Position.MarketPosition == MarketPosition.Flat && onlyEnterOncePerEmaCrossover) {
if (Close[0].EQ(_priceLong)) {
EnterLong(1, 1, $"EnterLong:{_biEntry}:{_priceLong}");
Print($"TIC bi: {CurrentBar} close: {Close[0]} MIN bi: {_bi} _priceLong: {_priceLong}");
_biEntry = 0;
}
else if (Close[0].EQ(_priceShort)) {
EnterShort(1, 1, $"EnterShort:{_biEntry}:{_priceShort}");
Print($"TIC bi: {CurrentBar} close: {Close[0]} MIN bi: {_bi} _priceShort: {_priceShort}");
_biEntry = 0;
}
}
}
}
}
}

Comment