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

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.
    BertrandNinjaTrader Customer Service

    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 Aviram Y, Today, 05:29 AM
      0 responses
      2 views
      0 likes
      Last Post Aviram Y  
      Started by quantismo, 04-17-2024, 05:13 PM
      3 responses
      25 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by ScottWalsh, 04-16-2024, 04:29 PM
      7 responses
      34 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by cls71, Today, 04:45 AM
      0 responses
      6 views
      0 likes
      Last Post cls71
      by cls71
       
      Started by mjairg, 07-20-2023, 11:57 PM
      3 responses
      217 views
      1 like
      Last Post PaulMohn  
      Working...
      X