Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Scalling in positions when losing

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

    Scalling in positions when losing

    Hi,

    Is there a way to open new positions every 20 ticks from initial position (same direction) when it is still open and losing?

    Example:
    If my initial position is still open and 20 ticks losing, then open additional position 1, if my initial position is still open and 40 ticks losing, then open additional position 2, etc...

    Thank you very much in advance.

    #2
    Hello YakusaTrader,

    Thanks for your post.

    There are some internal rules to opening positions in the opposite direction when you are already in position, however these rules would not effect entering another position in the same direction.

    The strategy would just have to have Entries Per Direction and EntryHandling to be set to allow multiple entries in the same direction. The logic would then need to be defined to control your entries and subsequent entries.

    I've included documentation resourced on these items as well the Managed Approach's Internal Rules for further reading.

    Entry Handling - https://ninjatrader.com/support/help...ryhandling.htm

    Entries Per Direction - https://ninjatrader.com/support/help...rdirection.htm

    Managed Approach Internal Rules - https://ninjatrader.com/support/help...antedPositions

    Please let us know if we can be of further assistance.
    Last edited by NinjaTrader_Jim; 09-13-2018, 12:33 PM.
    JimNinjaTrader Customer Service

    Comment


      #3
      I appreciate your help.

      I put something together -- it's working thank you!

      #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 ScallingUp : Strategy
      {
      private double Counter;


      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "ScallingUp";
      Calculate = Calculate.OnBarClose;
      EntriesPerDirection = 100;
      EntryHandling = EntryHandling.UniqueEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
      OrderFillResolution = OrderFillResolution.High;
      OrderFillResolutionType = BarsPeriodType.Minute;
      OrderFillResolutionValue = 1;
      Slippage = 0;
      StartBehavior = StartBehavior.WaitUntilFlatSynchronizeAccount;
      TimeInForce = TimeInForce.Gtc;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 20;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;
      Distance = 20;
      QTY = 1;
      Profit = 10;
      }
      else if (State == State.Configure)
      {
      }
      }

      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;

      if (CurrentBars[0] < 20)
      return;



      // Set 1
      if (Close[0] > Close[1] && (Position.MarketPosition == MarketPosition.Flat))
      {
      EnterLong(QTY, "");
      Counter = 1;
      }

      // Set 2
      if ((Position.MarketPosition != MarketPosition.Flat)
      && (Close[0] <= (Position.AveragePrice - (Distance * TickSize * Counter))))
      {
      EnterLong(QTY, "");
      Counter += 1;
      }

      if ((Position.MarketPosition != MarketPosition.Flat)
      && (Close[0] >= (Position.AveragePrice + (Profit * TickSize))))
      {
      ExitLong();
      }



      if (Position.MarketPosition == MarketPosition.Flat)
      {
      Counter = 0;
      }

      Print("Counter is : " + Counter);
      Print(Time[0]);
      Print("Quantity is : " + Position.Quantity);

      }

      #region Properties
      [NinjaScriptProperty]
      [Range(0, int.MaxValue)]
      [Display(Name="Distance", Order=1, GroupName="Parameters")]
      public int Distance
      { get; set; }

      [NinjaScriptProperty]
      [Range(1, int.MaxValue)]
      [Display(Name="QTY", Order=2, GroupName="Parameters")]
      public int QTY
      { get; set; }

      [NinjaScriptProperty]
      [Range(1, int.MaxValue)]
      [Display(Name="Profit", Order=3, GroupName="Parameters")]
      public int Profit
      { get; set; }

      #endregion

      }
      }

      Comment


        #4
        Position.AveragePrice is the average entry price of all opened positions, is there a way to identify the initial entry position?

        Comment


          #5
          Hello YakusaTrader,

          Thank you for your response.

          You would need to manually track the first execution through OnExecutionUpdate() as there is not a direct access to the initial entry for the position.

          Please visit the following link for details on OnExecutionUpdate(): https://ninjatrader.com/support/help...tionupdate.htm

          Please let me know if you have any questions.

          Comment


            #6
            Tnx, I was working on it.
            The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by DJ888, 04-16-2024, 06:09 PM
            6 responses
            18 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by Jon17, Today, 04:33 PM
            0 responses
            1 view
            0 likes
            Last Post Jon17
            by Jon17
             
            Started by Javierw.ok, Today, 04:12 PM
            0 responses
            6 views
            0 likes
            Last Post Javierw.ok  
            Started by timmbbo, Today, 08:59 AM
            2 responses
            10 views
            0 likes
            Last Post bltdavid  
            Started by alifarahani, Today, 09:40 AM
            6 responses
            41 views
            0 likes
            Last Post alifarahani  
            Working...
            X