I am new to c# and ninjatrader.. and i tried to make some simple strategy work. Now i just to long SPY when sma(10)>sma(20) and sell the other way.
1. About Add(some indicators), could i also specify the color?
2. In my code , I first want to Add(sma10), it looks like i can't do this. So how could i initialize a indicator object?
3. In my code, I tried to call BackColorAll = Color.Green; Yet the backtest result does not show any green color on the chart though the trading log did execute some selling orders.
4. In general , my logic is to first check if my position is empty, if so, check the long condition. if my position is not empty, check the short condition.
Could someone help check where i goes wrong?
public class Crossover01 : Strategy { #region Variables // Wizard generated variables private int myInput0 = 1; // Default setting for MyInput0 // 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> private IOrder entryOrder = null; protected override void Initialize() { CalculateOnBarClose = true; //global stop loss SetStopLoss(CalculationMode.Percent, 0.07); SetProfitTarget(CalculationMode.Percent,0.1); //Add(sma10); //Add(sma20); Add(SMA(10)); Add(SMA(20)); } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { var sma10 = SMA(10); var sma20 = SMA(20); var sma50 = SMA(50); if(entryOrder == null) { if(CrossAbove(sma10,sma20,1)&& Rising(sma50) == true) { EnterLong(); BackColorAll = Color.Red; } } else if (entryOrder != null) { if (CrossBelow(sma10,sma20,1)|| Falling(sma50) == true) { EnterShort(); BackColorAll = Color.Green; } entryOrder = null; } } #region Properties [Description("")] [GridCategory("Parameters")] public int MyInput0 { get { return myInput0; } set { myInput0 = Math.Max(1, value); } } #endregion }
Comment