Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Programming chase

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

    Programming chase

    Hello I need programming an strategy that chase if touch the limit price, I have found only one sample in the support forum that chase at any change of the last price not only if touch the limit price,any help to change from "Chase" to "Chase if Touch"?:

    protectedoverridevoid OnMarketData(MarketDataEventArgs e)
    {

    //We only need to be looking at the Bid/Ask if we actually have an unfulled order:
    if (entryOrder != null && entryOrder.OrderState != OrderState.Filled ) {

    //Then if we have another bid, and it has changed, and we have a long order, we need to change it:
    if (e.MarketDataType == MarketDataType.Bid && BidPrice != e.Price && entryOrder.OrderAction == OrderAction.Buy) {

    BidPrice = e.Price;
    //Print ("NewBid: A:" + AskPrice + " B:" + BidPrice);
    //Print ("Changing " + entryOrder.OrderAction + Instrument.FullName + " Order.... NewLimitPrice =" + MyLimitPrice);
    entryOrder = EnterLongLimit(Quantity, BidPrice + Offset, "LongEntry");

    }
    //then the reverse: if we have an ask, and it has changed, and we have a short order:
    elseif (e.MarketDataType == MarketDataType.Ask && AskPrice != e.Price && entryOrder.OrderAction == OrderAction.SellShort ) {

    AskPrice = e.Price;
    //Print ("NewAsk: A:" + AskPrice + " B:" + BidPrice);
    //Print ("Changing " + entryOrder.OrderAction + Instrument.FullName + " Order.... NewLimitPrice =" + MyLimitPrice);
    entryOrder = EnterShortLimit(Quantity, AskPrice - Offset, "ShortEntry");

    }
    }

    }


    #2
    Welcome to our forums - a touch would be for example if the Bid / Ask is the same as the order price but you would not go into partial fill state then, in this case you might want to set a timer and then change the order price after your desired patience.

    Comment


      #3
      Hello,
      I have programming the solution long time ago and I have forgotten to posted ,I hope that everyone can benefit:

      #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.Gui.Chart;
      using NinjaTrader.Strategy;
      #endregion

      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      /// <summary>
      /// Enter the description of your strategy here
      /// </summary>
      [Description("Enter the description of your strategy here")]
      public class ChaseIfTouchForum : Strategy
      {
      #region Variables
      // Wizard generated variables


      private int stoptick = 12; // Default setting for Stoptick
      private int profittick = 8; // Default setting for Profittick
      public double LastPrice;
      public double AskPrice;
      public double BidPrice;
      private IOrder entryOrder1 = null;
      private IOrder entryOrder2 = null;
      private int chaseExitShort = 0;// if change to 1 allows the chase if touch
      private int chaseExitLong = 0;// if change to 1 allows the chase if touch
      private double filledExitShort = 0;
      private double filledExitLong = 0;
      private int limitChaseExit = 1;// maximum number of ticks that you allow the chase



      // User defined variables (add any user defined variables below)
      #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()
      {
      SetProfitTarget("", CalculationMode.Ticks, Profittick);
      SetStopLoss("", CalculationMode.Ticks, Stoptick, false);
      CalculateOnBarClose = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Condition set 1

      //place here an enter condition for long orders
      {
      entryOrder1 = EnterShortLimit(0, true, DefaultQuantity, GetCurrentAsk(), "Short");

      }

      // Condition set 2

      //place here an enter condition for long orders



      {
      entryOrder2 = EnterLongLimit(0, true, DefaultQuantity, GetCurrentBid(), "Long");

      }









      }
      protected override void OnMarketData(MarketDataEventArgs e)
      {


      // CHASE IF TOUCH


      // SHORT POSITION

      // here we allow the chase if touch when e.Price == Position.AvgPrice -(profittick * TickSize)

      if (e.MarketDataType == MarketDataType.Last && Position.MarketPosition == MarketPosition.Short && chaseExitShort == 0 && e.Price == Position.AvgPrice -(profittick * TickSize)
      )
      {
      chaseExitShort = 1;
      filledExitShort = e.Price;
      BidPrice = e.Price;
      }


      //Then if we have another bid, and it has changed, and we have a profit short order, we need to change it:
      if ( e.MarketDataType == MarketDataType.Bid && chaseExitShort == 1 && Position.MarketPosition == MarketPosition.Short && BidPrice != e.Price && e.Price <= filledExitShort + (limitChaseExit * TickSize)
      )
      {

      BidPrice = e.Price;
      SetProfitTarget(CalculationMode.Price, e.Price );

      }





      // LONG POSITION

      // here we allow the chase if touch when e.Price == Position.AvgPrice + (profittick * TickSize)

      if (e.MarketDataType == MarketDataType.Last && Position.MarketPosition == MarketPosition.Long && chaseExitLong == 0 && e.Price == Position.AvgPrice +(profittick * TickSize)
      )
      {
      chaseExitLong = 1;
      filledExitLong = e.Price;
      AskPrice = e.Price;
      }




      //Then if we have another ask, and it has changed, and we have a profit long order, we need to change it:
      if (e.MarketDataType == MarketDataType.Ask && chaseExitLong == 1 && Position.MarketPosition == MarketPosition.Long && AskPrice != e.Price && e.Price >= filledExitLong - (limitChaseExit * TickSize)
      )
      {

      AskPrice = e.Price;
      SetProfitTarget(CalculationMode.Price, e.Price );

      }




      // reset profit target when is Flat


      if (Position.MarketPosition == MarketPosition.Flat)
      {
      SetProfitTarget( CalculationMode.Ticks, Profittick);

      }



      }



      protected override void OnOrderUpdate(IOrder order)
      {
      // Checks for all updates to entryOrder.
      if (entryOrder1 != null && entryOrder1.Token == order.Token)
      {
      // Check if entryOrder is Filled.
      if (order.OrderState == OrderState.Filled)
      {
      // Reset entryOrder back to null
      entryOrder1 = null;
      chaseExitShort = 0; // we reset the variable
      }
      // Check if entryOrder is Cancelled.
      if (order.OrderState == OrderState.Cancelled)
      {
      // Reset entryOrder back to null
      entryOrder1 = null;
      chaseExitShort = 0;// we reset the variable
      }
      }
      // Checks for all updates to entryOrder.
      if (entryOrder2 != null && entryOrder2.Token == order.Token)
      {
      // Check if entryOrder is Filled.
      if (order.OrderState == OrderState.Filled)
      {
      // Reset entryOrder back to null
      entryOrder2 = null;
      chaseExitLong = 0;// we reset the variable

      }
      // Check if entryOrder is Cancelled.
      if (order.OrderState == OrderState.Cancelled)
      {
      // Reset entryOrder back to null
      entryOrder2 = null;
      chaseExitLong = 0;// we reset the variable
      }
      }

      }


      #region Properties




      [Description("")]
      [GridCategory("Parameters")]
      public int Stoptick
      {
      get { return stoptick; }
      set { stoptick = Math.Max(1, value); }
      }

      [Description("")]
      [GridCategory("Parameters")]
      public int Profittick
      {
      get { return profittick; }
      set { profittick = Math.Max(1, value); }
      }




      [Description("")]
      [GridCategory("Parameters")]
      public int LimitChaseExit
      {
      get { return limitChaseExit; }
      set { limitChaseExit = Math.Max(0, value); }
      }

      #endregion
      }
      }

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      627 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      359 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      562 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      568 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X