Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SampleOnOrderUpdate

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

    SampleOnOrderUpdate

    I downloaded this strategy from the forum. When I try to enter conditions it is ignoring it. It is opening and closing positions non stop immediatley after the exit altough the condition is that Todays Open to be above Prior High .
    I try to put the following condition:

    protected override void OnBarUpdate()
    {
    // Submit an entry limit order if we currently don't have an entry order open
    if (entryOrder == null
    && CurrentDayOHL().CurrentOpen[0] > PriorDayOHLC().PriorHigh[0]);

    Can you tell me what is the problem?

    Here is the code

    //
    // Copyright (C) 2011, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //

    #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.Strategy;
    #endregion

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Sample demonstrating the use of the OnOrderUpdate() method.
    /// </summary>
    [Description("Sample strategy demonstrating a use case involving the OnOrderUpdate() method")]
    public class SampleOnOrderUpdate : Strategy
    {
    #region Variables
    private IOrder entryOrder = null; // This variable holds an object representing our entry order
    private IOrder stopOrder = null; // This variable holds an object representing our stop loss order
    private IOrder targetOrder = null; // This variable holds an object representing our profit target order
    #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()
    {
    // Submit an entry limit order if we currently don't have an entry order open
    if (entryOrder == null
    && CurrentDayOHL().CurrentOpen[0] > PriorDayOHLC().PriorHigh[0]);


    {
    /* The entryOrder object will take on a unique ID from our EnterLong()
    that we can use later for order identification purposes in the OnOrderUpdate() method. */
    entryOrder = EnterLong(1, "MyEntry");
    }

    /* If we have a long position and the current price is 4 ticks in profit, raise the stop-loss order to breakeven.
    We use (7 * (TickSize / 2)) to denote 4 ticks because of potential precision issues with doubles. Under certain
    conditions (4 * TickSize) could end up being 3.9999 instead of 4 if the TickSize was 1. Using our method of determining
    4 ticks helps cope with the precision issue if it does arise. */
    if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Position.AvgPrice + (80 * (TickSize / 2)))
    {
    // Checks to see if our Stop Order has been submitted already
    if (stopOrder != null && stopOrder.StopPrice < Position.AvgPrice)
    {
    // Modifies stop-loss to breakeven
    stopOrder = ExitLongStop(0, true, stopOrder.Quantity, Position.AvgPrice, "MyStop", "MyEntry");
    }
    }
    }

    /// <summary>
    /// Called on each incoming order event
    /// </summary>
    protected override void OnOrderUpdate(IOrder order)
    {
    // Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
    if (entryOrder != null && entryOrder == order)
    {
    // Reset the entryOrder object to null if order was cancelled without any fill
    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
    {
    entryOrder = null;
    }
    }
    }

    /// <summary>
    /// Called on each incoming execution
    /// </summary>
    protected override void OnExecution(IExecution execution)
    {
    /* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
    which ensures your strategy has received the execution which is used for internal signal tracking. */
    if (entryOrder != null && entryOrder == execution.Order)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {
    // Stop-Loss order 4 ticks below our entry price
    stopOrder = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 48 * TickSize, "MyStop", "MyEntry");

    // Target order 8 ticks above our entry price
    targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 168 * TickSize, "MyTarget", "MyEntry");

    // Resets the entryOrder object to null after the order has been filled
    if (execution.Order.OrderState != OrderState.PartFilled)
    {
    entryOrder = null;
    }
    }
    }

    // Reset our stop order and target orders' IOrder objects after our position is closed.
    if ((stopOrder != null && stopOrder == execution.Order) || (targetOrder != null && targetOrder == execution.Order))
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
    {
    stopOrder = null;
    targetOrder = null;
    }
    }
    }

    /// <summary>
    /// Called on each incoming position event
    /// </summary>
    protected override void OnPositionUpdate(IPosition position)
    {
    // Print our current position to the lower right hand corner of the chart
    DrawTextFixed("MyTag", position.ToString(), TextPosition.BottomRight);
    }

    #region Properties
    #endregion
    }
    }

    #2
    Hello flexi,

    Thank you for your post.

    This is because the entry condition is going to return true and each time it will take a new position. What are you trying to achieve? A larger distance for the Stop Loss from the entry price? One entry per day?

    Comment


      #3
      Yes I want just one position per day. Also I want to ad specific entry time. For example:

      && ToTime(Time[0]) > ToTime(8, 30, 0)
      && ToTime(Time[0]) < ToTime(8, 46, 0)

      Comment


        #4
        Hello flexi,

        Thank you for your response.

        You can use Bars.FirstBarOfSession to set a bool that allows you to trade only once per day. For example:
        Code:
        			if(Bars.FirstBarOfSession)
        				tradeBool = true;
        			
        			if(tradeBool
        				&& ToTime(Time[0]) > ToTime(8, 30, 0)
        				&& ToTime(Time[0]) < ToTime(8, 46, 0))
        			{
        				tradeBool = false;
        				// do something
        			}
        For information on Bars.FirstBarOfSession please visit the following link: http://www.ninjatrader.com/support/h...rofsession.htm

        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