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

{NinjaScript} NT8 - Unmanaged Approach - Set Target

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

    {NinjaScript} NT8 - Unmanaged Approach - Set Target

    Hello Everyone.
    Im trying to create a StopOrder and create his Profit Target on an unmanaged approach.
    But gives me this errors:

    Strategy 'PruebaOnOrderFill/206666250' submitted an order that generated the following error 'Unable to submit order'. Strategy has sent cancel requests, attempted to close the position and terminated itself.

    Order can't be submitted: The OCO ID 'OcoGroup1' cannot be reused. Please use a new OCO ID.

    Please find attached that errors.

    Here is my code:

    #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 PruebaOnOrderFill : Strategy
    {
    public bool tradeCreado;
    int contador;
    private Order entryOrder = null;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "PruebaOnOrderFill";
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 5;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = false;
    ExitOnSessionCloseSeconds = 0;
    IsFillLimitOnTouch = true;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = true;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 0;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    IsUnmanaged=true;
    tradeCreado=false;
    contador=1;
    }
    else if (State == State.Configure)
    {
    }
    }

    protected override void OnBarUpdate()
    {

    contador++;
    if(contador>20 && !tradeCreado)
    {
    SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, 10000, 0,Close[0]-0.0030,"OcoGroup","EntryName");
    tradeCreado=true;
    Print("PrecioEntrada: "+ (Close[0]-00.0030));
    }
    }


    protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
    Cbi.MarketPosition marketPosition, string orderId, DateTime time)
    {
    entryOrder= execution.Order;

    if (entryOrder != null && execution.Order == entryOrder)
    {
    SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Limit, 10000, Close[0]-0.0030, 0, "OcoGroup1", "profit target");
    Print("TargetsFilled!");
    }

    }




    }
    }


    Please, could you help me??
    Thank you!


    Attached Files

    #2
    Hi!

    1) I highly recommend you start with the very well reviewed and occasionally revised Unmanaged OCO samples produced by Jim and Chelsea.

    Start with these two pages

    1a) Example OCO from Jim https://ninjatrader.com/support/foru...10#post1142410

    1b) Unmanaged Profit Trail examples from Chelsea (cant find that page of four examples right now but ask the NT team for the link). One of the example files on that forum page is "ProfitChaseStopTrailExitOrdersExample"


    2) I highly recommend reviewing last years posts about Unmanaged by Jim https://www.google.com/search?q=Ninj...4dUDCA4&uact=5



    HedgePlay
    Last edited by hedgeplay; 04-18-2021, 06:46 PM.

    Comment


      #3
      Hello fscabrera03 and HedgePlay,

      The OCOIDs will need to be unique for each new pairing.
      I often suggest GetAtmStrategyUniqueId() and System.Guid.NewGuid() for unique ids. Using the DateTime in the string can also make unique strings.

      Below are links to ProfitChaseStopTrailUnmanagedExample_NT8 and UnmanagedOCOBracketExample_NT8.

      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Im still not seeing how it works OCO.
        the parameter ÖCO string is to link OCO order together, so im not understanding why cannot place 3 orders (entry, stop loss and take profit) with the identic OCO string.


        If I have 4 long trades and 4 short trades, how the system knows what SL or TP is for what entry trade?

        Conceptually should works in a way that if entry order was filled, later if any order is filled (SL or TP) the 3 trades should close, and this link is by the oco string, is that right?

        Comment


          #5
          Hello fscabrera03,

          You can have as many orders as you would like with the OCO group using the same OCO Id. If you want to group 4 orders that are all still in a working state, that will work.
          However, you cannot use an OCO Id that has already been used on an order that has filled, rejected, or been cancelled. You will only be able to group with working orders.

          Each new group must be using a new unique OCO Id. If an OCO Id is re-used, then it is group with the existing group using that Id.
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by cmtjoancolmenero, Yesterday, 03:58 PM
          10 responses
          36 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by DayTradingDEMON, Today, 09:28 AM
          4 responses
          24 views
          0 likes
          Last Post DayTradingDEMON  
          Started by geddyisodin, Yesterday, 05:20 AM
          9 responses
          50 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by George21, Today, 10:07 AM
          1 response
          15 views
          0 likes
          Last Post NinjaTrader_ChristopherJ  
          Started by Stanfillirenfro, Today, 07:23 AM
          9 responses
          24 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X