//
// Copyright (C) 2023, NinjaTrader LLC <www.ninjatrader.com>.
// NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
//
region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
//This namespace holds strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class MACrossOver : Strategy
{
private SMA smaFast;
private SMA smaSlow;
private int timeFrame = 3;
private double lastHeikenAshiClose;
private double lastHeikenAshiOpen;
private int lastBarTime;
private Series<double> ma;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = NinjaTrader.Custom.Resource.NinjaScriptStrategyDes criptionSampleMACrossOver;
Name = NinjaTrader.Custom.Resource.NinjaScriptStrategyNam eSampleMACrossOver;
Fast = 9;
Slow = 1;
Calculate = Calculate.OnBarClose;
IsOverlay = true;
DisplayInDataBox = true;
PaintPriceMarkers = true;
BarsRequired = 2;
// This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = false;
}
else if (State == State.Configure)
{
AddDataSeries(Data.BarsPeriodType.Minute, timeFrame);
AddHeikenAshi();
}
else if (State == State.DataLoaded)
{
smaFast = SMA(Fast);
smaSlow = SMA(Slow);
smaFast.Plots[0].Brush = Brushes.Goldenrod;
smaSlow.Plots[0].Brush = Brushes.SeaGreen;
AddChartIndicator(smaFast);
AddChartIndicator(smaSlow);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < BarsRequiredToTrade)
return;
if (CrossAbove(smaFast, smaSlow, 1))
EnterLong();
else if (CrossBelow(smaFast, smaSlow, 1))
EnterShort();
lastHeikenAshiClose = heikenAshi.Close;
lastHeikenAshiOpen = heikenAshi.Open;
}
private int GetActivationBarTime()
{
var currentTime = Time[0];
var activationBarTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, currentTime.Hour, (int)Math.Floor(currentTime.Minute / (double)timeFrame) * timeFrame, 0);
if (activationBarTime.Minute != lastBarTime)
return activationBarTime.Minute;
return 0;
region Properties
[Range(1, int.MaxValue), NinjaScriptProperty]
{[Display(ResourceType = typeof(Custom.Resource), Name = "Fast", GroupName = "NinjaScriptStrategyParameters", Order = 0)]}}
public int Fast;}}
{ get; set; }
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Name = "Slow", GroupName = "NinjaScriptStrategyParameters", Order = 1)]
public int Slow
{ get; set; }
#endregion
}
}
Anyways, the strategy idea is using the 9 and 1 MA crossover for a 3 min heikin ashi timeframe that begins at 0930 to 1600. The trade exits once another crossover takes place. I need help building this strategy code. As I'm still learning python, explanations would be great also.
Comment