I need your help coding a simple strategy.
I have zero knowledge about coding, but I was happy to learn that Ninjatrader had a "Strategy Builder" that you can program an strategy with no coding skills.
So I finally made the following simple strategy
#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 Prueba2 : Strategy
{
private BuySellVolume BuySellVolume1;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "Prueba2";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = true;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelCloseIgnoreRejects;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 3;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
StopLoss = 150;
TakeProfit = 50;
Value = 20000;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
BuySellVolume1 = BuySellVolume(Close);
BuySellVolume1.Plots[0].Brush = Brushes.DarkCyan;
BuySellVolume1.Plots[1].Brush = Brushes.Crimson;
AddChartIndicator(BuySellVolume1);
SetStopLoss("", CalculationMode.Ticks, StopLoss, false);
SetProfitTarget("", CalculationMode.Ticks, TakeProfit);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if ((BuySellVolume1.Buys[0] == Value)
&& (Open[1] < Open[0]))
{
EnterLongStopMarket(Convert.ToInt32(DefaultQuantity), (High[1] + (6 * TickSize)) , "");
}
// Set 2
if ((BuySellVolume1.Buys[0] == Value)
&& (Open[1] > Open[0]))
{
EnterShortStopMarket(Convert.ToInt32(DefaultQuantity), (Low[1] + (-6 * TickSize)) , "");
}
}
#region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="StopLoss", Order=1, GroupName="Parameters")]
public int StopLoss
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="TakeProfit", Order=2, GroupName="Parameters")]
public int TakeProfit
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Value", Order=3, GroupName="Parameters")]
public int Value
{ get; set; }
#endregion
}
}
I made my reasearch and found that the main problem is that all pending orders (limit and stop) are cancelled by default when a new bar starts, and there is no option in strategy builder to change that behavior without manual coding.
So, my idea is to cancel the pending orders if they are not filled in X bars.
Again, I made my reasearch and found the following post from 2009

It seems someone had the same problem as me, and came out with the solution, but the problem is that I do not have any coding skills, so I really do not understand the procedure of it.
He said that the following code worked for him;
private IOrder myEntryOrder = null;
private int barNumberOfOrder = 0;
protected override void OnBarUpdate()
{
// Submit an entry order at the low of a bar
if (myEntryOrder == null)
{
myEntryOrder = EnterLongLimit(0, true, 1, Low[0], "Long Entry");
barNumberOfOrder = CurrentBar;
}
// If more than 5 bars has elapsed, cancel the entry order
if (myEntryOrder != null && CurrentBar > barNumberOfOrder + 5)
CancelOrder(myEntryOrder);
}

So I really ask for help if somehow can fix my code in order to make it work.
On the other hand, I started to be interested with coding, I learnt that Ninjascript is C# based. I need to start from zero, do you have any hints for that?
Manyt thanks in advance, if you can help me I will be eternally gratefull.
Regards,

Comment