I am a newbee in programming Ninjatrader scripts.
I tried to run an martingale strategy and it starts as I want. In my example I opening a long trade wird 3 long limit orders:
When the first Buy limit order hits (quantity = 1), I create a target and stoploss order:
After that, when the second buy limit order is hit (quantity = 3), I change the target and stoploss quantity to 4 and the target level is changed to the old entry level:
So far so good. But suddenly the order quantity changes from 4 to 3 (order, target and stop), see:
But it has been done somehow automatically without a line of code from my side.
In the log file I can see the entry of a short statement:
I tried a lot, but now I am at the end of my ideas.
my code at the end
Hope someone can help me.
best regards,
theo
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 MyMartingale : Strategy
{
private bool Started;
private bool StartedLimit;
private int Ordersize;
private bool Hit0p62;
private bool Hit0p78;
private bool Hit0p38;
private bool HitEnd;
private Order entryLimit1 = null;
private Order entryLimit2 = null;
private Order entryLimit3 = null;
private Order ExitTP = null;
private Order ExitSL = null;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "MyMartingale";
Calculate = Calculate.OnPriceChange;
EntriesPerDirection = 5;
EntryHandling = EntryHandling.UniqueEntries;
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;
IsUnmanaged = true;
Fibrange = 60;
Fibmin0p05 = 21222;
Fib0p62 = 21224.5;
Fib0p78 = 21223.5;
Fib1p27 = 21229;
Entryprice = 21226;
Started = false;
StartedLimit = false;
Ordersize = 1;
Hit0p62 = false;
Hit0p78 = false;
Hit0p38 = false;
HitEnd = false;
}
else if (State == State.Configure)
{
}
else if (State == State.Realtime)
{
if (entryLimit1 != null)
entryLimit1 = GetRealtimeOrder(entryLimit1);
if (entryLimit2 != null)
entryLimit2 = GetRealtimeOrder(entryLimit2);
if (entryLimit3 != null)
entryLimit3 = GetRealtimeOrder(entryLimit3);
if (ExitTP != null)
ExitTP = GetRealtimeOrder(ExitTP);
if (ExitSL != null)
ExitSL = GetRealtimeOrder(ExitSL);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
// Set 1
if ((State == State.Realtime)
&& (Started == false)
&& (Position.MarketPosition == MarketPosition.Flat))
{
Started = true;
Print("started = true");
entryLimit1 = EnterLongLimit(0, true, 1, Entryprice, @"LongLimitEntryPrice");
Print("EntryLongLimit Entryprice: " + Entryprice);
entryLimit2 = EnterLongLimit(0, true, 3, Fib0p62, @"LongLimitEntry0p62");
Print("EntryLongLimit Fib0p62: " + Fib0p62);
entryLimit3 = EnterLongLimit(0, true, 5, Fib0p78, @"LongLimitEntry0p78");
Print("EntryLongLimit Fib0p78: " + Fib0p78);
}
if (CurrentBars[0] < 1)
return;
}
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
{
region Exitcommands
if (ExitSL != null && ExitSL == order)
{
if (order.OrderState == OrderState.Filled)
{
if (entryLimit1 != null)
{
CancelOrder(entryLimit1);
Print("Cancel entryLimit1");
}
if (entryLimit2 != null)
{
CancelOrder(entryLimit2);
Print("Cancel entryLimit2");
}
if (entryLimit3 != null)
{
CancelOrder(entryLimit3);
Print("Cancel entryLimit3");
}
if (ExitTP != null)
{
CancelOrder(ExitTP);
Print("Cancel ExitTP");
}
}
}
if (ExitTP!=null && ExitTP == order)
{
if (order.OrderState == OrderState.Filled)
{
if (entryLimit1 != null)
{
CancelOrder(entryLimit1);
Print("Cancel entryLimit1");
}
if (entryLimit2 != null)
{
CancelOrder(entryLimit2);
Print("Cancel entryLimit2");
}
if (entryLimit3 != null)
{
CancelOrder(entryLimit3);
Print("Cancel entryLimit3");
}
if (ExitSL != null)
{
CancelOrder(ExitSL);
Print("Cancel ExitSL");
}
}
}
#endregion
}
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
region reach0p38
if (entryLimit1 != null && entryLimit1 == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
Print("Hit0p38 " + execution.Order.Name);
Ordersize = 2;
ExitSL = ExitLongStopMarket(0, true,Convert.ToInt32(Ordersize), Fibmin0p05, @"LongSLall", @"LongLimitEntryPrice");
Print("ExitLongStopMarket mit " + Ordersize);
ExitTP = ExitLongLimit(0, true,Convert.ToInt32(Ordersize), Fib1p27, @"LongTPall", @"LongLimitEntryPrice");
Print("ExitLongLimit mit " + Ordersize);
//entryLimit1 = null;
}
}
// Resets the entryOrder object to null after the order has been filled
#endregion
region reach0p62
if (entryLimit2 != null && entryLimit2 == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
Print("Hit0p62 " + execution.Order.Name);
Ordersize = 4;
ChangeOrder(ExitSL,Convert.ToInt32(Ordersize), 0, Fibmin0p05);
Print("ExitLongStopMarket mit: " + Ordersize);
ChangeOrder(ExitTP,Convert.ToInt32(Ordersize), Entryprice, 0);
Print("ExitLongLimit mit: " + Ordersize);
//entryLimit2 = null;
}
}
#endregion
region reach0p78
if (entryLimit3 != null && entryLimit3 == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
Print("Hit0p78 " + execution.Order.Name);
Ordersize = 9;
ChangeOrder(ExitSL,Convert.ToInt32(Ordersize), 0, Fibmin0p05);
Print("ExitLongStopMarket mit: " + Ordersize);
ChangeOrder(ExitTP,Convert.ToInt32(Ordersize), Entryprice, 0);
Print("ExitLongLimit mit: " + Ordersize);
//entryLimit3 = null;
}
}
#endregion
}
region Properties
[NinjaScriptProperty]
[Range(20, double.MaxValue)]
[Display(Name="Fibrange", Order=1, GroupName="Parameters")]
public double Fibrange
{ get; set; }
[NinjaScriptProperty]
[Range(10000, double.MaxValue)]
[Display(Name="Fibmin0p05", Order=2, GroupName="Parameters")]
public double Fibmin0p05
{ get; set; }
[NinjaScriptProperty]
[Range(10000, double.MaxValue)]
[Display(Name="Fib0p62", Order=3, GroupName="Parameters")]
public double Fib0p62
{ get; set; }
[NinjaScriptProperty]
[Range(10000, double.MaxValue)]
[Display(Name="Fib0p78", Order=4, GroupName="Parameters")]
public double Fib0p78
{ get; set; }
[NinjaScriptProperty]
[Range(10000, double.MaxValue)]
[Display(Name="Fib1p27", Order=5, GroupName="Parameters")]
public double Fib1p27
{ get; set; }
[NinjaScriptProperty]
[Range(1, double.MaxValue)]
[Display(Name="Entryprice", Order=6, GroupName="Parameters")]
public double Entryprice
{ get; set; }
#endregion
}
}

Comment