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

Entry Handling and Grid strategy troubleshoot

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

    Entry Handling and Grid strategy troubleshoot

    Hello

    I am trying to create a range base grid strategy and I receive the error message "'Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties" in the output frequently.

    The concept I'm trying to program is to have the fill of a buy or sell limit order create new buy and sell limits above and below in discrete intervals. This creates a grid of buys and sells as the price moves in the range of the grid. The issue I'm having is that eventually new limit orders will not be created and instead give the error message above. Also depending on the EntriesPerDirection quantity my initial entry order quantity will be a variety of factors larger than what is set in the strategy. Ideally the strategy maintains a position until the position is manually closed, having various position quantities depending on the prices location in the grid.

    Here is my script for reference, I appreciate any help offered.
    Erik









    #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 GridStrategy : Strategy
    {

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "GridStrategy";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 5;
    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.StopCancelClose;
    StopTargetHandling = StopTargetHandling.ByStrategyPosition;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    GridSize = 50;
    EntryPrice = 16200;

    }



    }

    protected override void OnBarUpdate()
    {
    if (State == State.Historical)
    return;

    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    EnterLongLimit(1, EntryPrice, "Level0");
    }


    }
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
    Print(order.Name);
    Print(order.OrderState);

    }
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {

    Print(execution);
    Print(execution.Name);
    Print(Position.Quantity);

    if(execution.Name == "LevelM2")
    {

    {
    ExitLongLimit(0, true, 1, EntryPrice - GridSize * TickSize, "LevelM1", "LevelM2");


    }
    }

    if (execution.Name == "LevelM1")
    {

    {
    EnterLongLimit(0, true, 1, EntryPrice - GridSize * 2 * TickSize, "LevelM2");
    ExitLongLimit(0, true, 1, EntryPrice, "Level0", "LevelM1");

    }

    }


    if (execution.Name == "Level0")
    {


    {


    ExitLongLimit(0, true, 1, EntryPrice + GridSize * TickSize, "Level1", "Level0");
    EnterLongLimit(0, true, 1, EntryPrice - GridSize * TickSize, "LevelM1");


    }
    }


    if (execution.Name == "Level1")
    {

    {


    ExitLongLimit(0, true, 1, EntryPrice + GridSize * 2 * TickSize, "Level2", "Level1");
    EnterLongLimit(0, true, 1, EntryPrice, "Level0");

    }
    }

    if (execution.Name == "Level2");

    {

    {
    EnterLongLimit(0, true, 1, EntryPrice + GridSize * TickSize, "Level1");


    }
    }




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

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



    }
    }

    #2
    Hi kosmo, thanks for posting. The entries per direction property needs to be set to the maximum number of entries per direction the strategy will take. You will need to figure out what the maximum entries per direction possible is, or set the EntrierPerDirection so high that the limit can never be reached.
    Best regards,
    -ChrisL
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      I'm following. I'm interested in multiple entry positions (adding to winners) but I havent yet thought of how to implement it. Over my head. Is there a way to do it in the ATM? thanks
      Last edited by ezrollin; 11-24-2021, 11:21 PM.

      Comment


        #4
        Hey Chris
        Thank you for your reply. If i set the Entry per direction to 50 for instance it will buy me in 50 contracts at every possible buy limit in my OnExecutionUpdate section above the current price even though my initial entry command is set to 1 or a number much smaller. I figured it must be something about how OnExecutionUpdate was processing my lines

        For the sake of knowledge I wish to know what was causing this to improve my skill, however I managed to get the results I wanted by doubling the entry orders with EnterLimits that have no signal names. This seems like a programming bandaid instead of finding the source of the behavior.
        Erik

        Comment


          #5
          Hello kosmo,

          Thanks for your reply.

          This is Paul responding for Chris who is out of the office today.

          Entries per direction would be used to limit the number of entries in the same direction at the same time. Entry handling qualifies that to be either all entries or Unique entries. Unique entries are created by using entry names.
          References:



          I would suggest using prints to understand what specific orders are being placed and when as this may help clarify what is actually happening. In addition, keep an eye on the "Log" tab of the Ninjatrader control center for any related order errors.
          Paul H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by GLFX005, Today, 03:23 AM
          0 responses
          1 view
          0 likes
          Last Post GLFX005
          by GLFX005
           
          Started by XXtrader, Yesterday, 11:30 PM
          2 responses
          11 views
          0 likes
          Last Post XXtrader  
          Started by Waxavi, Today, 02:10 AM
          0 responses
          6 views
          0 likes
          Last Post Waxavi
          by Waxavi
           
          Started by TradeForge, Today, 02:09 AM
          0 responses
          11 views
          0 likes
          Last Post TradeForge  
          Started by Waxavi, Today, 02:00 AM
          0 responses
          2 views
          0 likes
          Last Post Waxavi
          by Waxavi
           
          Working...
          X