Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simple Flatten NT script - please help

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

    Simple Flatten NT script - please help

    Hello all,
    I am attempting my first Ninja Script , with very no programming background .
    Objective is to create a simple script that will flatten and close pending orders once unrealized PL target is reached .
    The position( with Stop and target bracket) is opened by a custom NT dll API .

    Example : If unrealized PL is >= 100 flatten the position . Assuming only one instrument will be open at a time.

    I used the NT wizard to create a script , refer the screenshots:


    Then I manually edited the script.
    Based on the research on this forum. ExitShort () was replaced with
    NinjaTrader.Gui.SuperDom.SuperDom.FlattenEverythin g() ;

    I keep NT Super DOM running because above command has that word in it .
    Custom API does not need DOM to be running , though order and bracket created by API are displayed on NT dom.

    Complete script code :

    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Flatten Script L tgt
    /// </summary>
    [Description("Flatten Script L tgt")]
    public class zLtargetexit : Strategy
    {
    #region Variables
    // Wizard generated variables
    private double ltargetUip = 100; // Default setting for LtargetUip
    // 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>
    protected override void Initialize()
    {
    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) >= LtargetUip)
    {
    NinjaTrader.Gui.SuperDom.SuperDom.FlattenEverythin g() ;
    }

    }

    #region Properties
    [Description("Tgt L user define input")]
    [GridCategory("Parameters")]
    public double LtargetUip
    {
    get { return ltargetUip; }
    set { ltargetUip = Math.Max(1, value); }
    }
    #endregion
    }
    }


    Not surprisingly the script is not working
    please help !

    Thanks in advance .

    #2
    nina2020a, I would not expect that command to trigger. Since you're not entering any position via the script itself, the strategy would only know about it's own positions here.

    Comment


      #3
      oh..

      Is there any other way to flatten positions when a predefined unrealized PL is reached ?

      Thanks

      Comment


        #4
        Unfortunately not directly, only by time (Tools > Options > Misc).

        Your script could access those account level items though which includes the realized pnl -

        Comment


          #5
          Is there a way to make a call from this script to 'NinjaTrader.Custom' XML located in bin>custom folder ?
          May be the 'UnrealizedProfitLoss' class there can provide Unrealized PL to the script ? .


          thanks

          Comment


            #6
            Hi NinjaTrader_Bertrand,
            Was wondering if this was possible?

            Thanks

            Originally posted by nina2020a View Post
            Is there a way to make a call from this script to 'NinjaTrader.Custom' XML located in bin>custom folder ?
            May be the 'UnrealizedProfitLoss' class there can provide Unrealized PL to the script ? .


            thanks

            Comment


              #7
              nina2020a, you could unfortunately not directly call the column into your script - however you could look into concepts used to acquire the needed unrealized account position pnl for your script potentially. For example consider this snippet below - here we get the position object first for our account (Sim101) and instrument in the OnConnectionStatus. In OnBarUpdate() we then access the GetProfitLoss of this position.

              Please note though this code is provided as a courtesy for you to get going in this area, officially this would not be a documented area of our NinjaScript.

              Code:
              private string accountName = "Sim101";
              private Position	position = null;
              private ConnectionStatus dataFeed = ConnectionStatus.Connected;
              		
              protected override void Initialize()
              {
                     CalculateOnBarClose = true;
              }
              
              protected override void OnBarUpdate()
              {
              	if (position != null && !Historical)
              		Print(position.Instrument.FullName + " " + position.GetProfitLoss(Close[0], PerformanceUnit.Currency));
              }
              		
              protected override void OnConnectionStatus(ConnectionStatus orderStatus, ConnectionStatus priceStatus)
              {
              			dataFeed = priceStatus; 
              			
              			if (dataFeed == ConnectionStatus.Connected)
              			{
              				foreach (Account account in Cbi.Globals.Accounts)
              						if (account.Name == accountName)
              							lock (account.Positions)
              								foreach (Position positionTmp in account.Positions)
              									if (positionTmp.Instrument.IsEqual(Instrument))
              										position = positionTmp;
                                       }
              }

              Comment


                #8
                NinjaTrader_Bertrand,
                That helps . Appreciate your help
                cheers

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                558 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                324 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                545 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                547 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X