Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Martingale strategy - suddenly Short entry without command!?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Martingale strategy - suddenly Short entry without command!?

    Hi all,
    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:
    Click image for larger version

Name:	martingale_phase_1.jpg
Views:	66
Size:	82.9 KB
ID:	1325006

    When the first Buy limit order hits (quantity = 1), I create a target and stoploss order:
    Click image for larger version

Name:	martingale_phase_2.jpg
Views:	76
Size:	102.1 KB
ID:	1325002
    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:
    Click image for larger version

Name:	martingale_phase_3.jpg
Views:	67
Size:	97.0 KB
ID:	1325003
    So far so good. But suddenly the order quantity changes from 4 to 3 (order, target and stop), see:
    Click image for larger version

Name:	martingale_phase_4.jpg
Views:	65
Size:	93.3 KB
ID:	1325007
    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:
    Click image for larger version

Name:	martingale_phase_log_entry.jpg
Views:	67
Size:	219.4 KB
ID:	1325005

    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


    }
    }
    Attached Files

    #2
    Hello theo,

    From the Log tab this is showing the order 'LongTPall' with Id: 'bda22221ba9e40bfa3f0b0e8be2dc62a' part filled for a quantity of 1.

    To confirm, your code is calling an exit long order with the name LongTPall​?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello theo,

      From the Log tab this is showing the order 'LongTPall' with Id: 'bda22221ba9e40bfa3f0b0e8be2dc62a' part filled for a quantity of 1.

      To confirm, your code is calling an exit long order with the name LongTPall​?
      I overlooked the part filled order you mentioned, because in the chart I could not see the part filled wick of the candle.
      You are right. My code is calling an Exit Long order, correct.
      So if the order is part filled, I have also to taking this into account. I will try that, Thank you for having a look on it.

      theo

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      62 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      134 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      75 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      45 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X