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

EnterLongStop And EnterShortStop Together

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

    EnterLongStop And EnterShortStop Together

    I'm wanting to code a strategy that submits both an EnterLongStopMarket and an EnterShortStopMarket at the same time.

    If price goes up enough I get stopped in long. If price goes down enough I get stopped in short.

    However, it appears that the internal order handling rules are preventing this.

    This page seems to discuss this under "Internal Order Handling Rules to Reduce Unwanted Positions."I've included my code sample below.

    Is it possible to achieve what I'm wanting to do with a managed approach or does it require the unmanaged approach? Is there a way to tie the orders together ala OCO?

    I'm testing on DAY bars, though I'm not sure that matters.

    Thanks,
    Steve


    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 Using declarations
    
    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies.Barz
    {
        public class TestEntryStopsStrategy : Strategy
        {
            protected override void OnBarUpdate()
            {
                if (CurrentBar < BarsRequiredToTrade) { return; }
    
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                    if (Range()[1] > Range()[2] && ADX(15)[0] > 20)
                    {
                        EnterLongStopMarket(MAX(High, 10)[0]);
                        EnterShortStopMarket(MIN(Low, 10)[0]);
                    }
                }
                else
                {
                    if (BarsSinceEntryExecution() > 10)
                    {
                        ExitLong();
                        ExitShort();
                    }
                }
            }
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = @"Test simultaneous EnterLongStopMarket and EnterShortStopMarket.";
                    Name = "Test - Entry Stops";
                    Calculate = Calculate.OnBarClose;
                    EntriesPerDirection = 10;
                    EntryHandling = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy = false;
                    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 = 50;
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
                    IsInstantiatedOnEachOptimizationIteration = true;
                    TraceOrders = true;
                }
                else if (State == State.Configure)
                {
                }
            }
        }
    }
    Last edited by Steve L; 06-01-2019, 02:27 PM.
    Steve L
    NinjaTrader Ecosystem Vendor - Ninja Mastery

    #2
    Hello Steve,

    Bracketing working opposing entry orders with OCO is too advanced for the managed approach.

    The unmanaged approach would need to be used.

    Below is a link to an example.
    https://ninjatrader.com/support/foru...926#post486926
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks, Chelsea. That's the confirmation I needed.

      Couple of questions about the example and the unmanaged approach:

      1) The example says it is not coded to work historically. I would like to be able to backtest and I'll only be working with minute bars or larger. Is there anything keeping me from testing unmanaged bracketed orders historically for my scenario?

      2) OrderStatus.PartFilled. Is this an interim status before "Filled" or is it always a final status for the order indicating that a portion of the quantity was filled and there will be no additional executions associated with this order?

      Thanks,
      Steve


      Steve L
      NinjaTrader Ecosystem Vendor - Ninja Mastery

      Comment


        #4
        Hello Steve,

        I've coded the script to only place orders in real-time because its easier to demonstrate that way.

        (With the historical orders we wouldn't see the cancelled orders on a chart, so it wouldn't be clear whats going on)

        OCO works historically too. You would just place the orders in OnBarUpdate.

        An order can be part filled. If its filled in parts, each part fill before being fully filled would cause the order state to be OrderState.PartFilled until the order is fully filled and then it will be OrderState.Filled.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by stantenlee, Today, 04:32 AM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_JustinD  
        Started by jbmiguel, 08-11-2022, 08:48 AM
        6 responses
        52 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by ezrollin, Yesterday, 08:58 PM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_ChrisL  
        Started by futureszone, Yesterday, 04:01 PM
        1 response
        14 views
        0 likes
        Last Post NinjaTrader_Kate  
        Started by flybuzz, Today, 09:05 AM
        0 responses
        5 views
        0 likes
        Last Post flybuzz
        by flybuzz
         
        Working...
        X