Let me start off by saying that I have no idea how to code or use NinjaScript, but I have been getting the below error in the log when I try to enable my strategy.
Error on calling 'OnStateChange' method: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
I want to create a bot that does the following:
The bot should continuously check if the previous candle crossed above or below the 20 day SMA. If it crossed above, I want to place a buy limit order at the high of that candle; if it crossed below, then I want the to short at the low of the candle. When the trade is entered, I want to set my stop loss at the high (if crossing below) or the low (if crossing above) of the previous candle. The profit target should be set to a 1:1 risk reward ratio.
I've tried to follow the other numerous forum suggestions for this exact issue, but none of them have worked for me. I have no idea how to use Visual Studio or debug code, so that isn't particularly helpful either. I began using the strategy builder, but could not figure out how to set the profit target equal to my stop loss, so I tried to code it manually from there.
My code is as follows:
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.Gui.Tools;
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 MyCustomStrategy : Strategy
{
private SMA SMA1;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "MyCustomStrategy";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
SMA1 = SMA(Close, 20);
SetStopLoss(@"BuyHigh", CalculationMode.Currency, Low[1], false);
SetStopLoss(@"SellLow", CalculationMode.Currency, High[1], false);
SetProfitTarget(@"BuyHigh", CalculationMode.Price, Position.AveragePrice + (Position.AveragePrice - Low[1]));
SetProfitTarget(@"SellLow", CalculationMode.Price, Position.AveragePrice - (High[1] - Position.AveragePrice));
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if (CrossAbove(Close, SMA1[0], 1)
&& (Position.MarketPosition == MarketPosition.Flat))
{
EnterLongLimit(Convert.ToInt32(DefaultQuantity), High[1], @"BuyHigh");
}
// Set 2
if (CrossBelow(Close, SMA1[0], 1)
&& (Position.MarketPosition == MarketPosition.Flat))
{
EnterShortLimit(Convert.ToInt32(DefaultQuantity), Low[1], @"SellLow");
}
}
}
}
Thanks for any help you can provide!
Comment