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

How to detect a Close order?

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

    How to detect a Close order?

    Hi, I looked through the documentation and searched here but cant seem a way to detect specifically when someone uses the Close button? Is this not sent as an order type with a specific flag I can identify? I am coding an add on and want to detect when users alter positions, close positions, etc.
    Thanks,
    -R

    #2
    Hello RaygunWizzle,

    To detect a close order you would need to subscribe to the addon framework account class OnOrderUpdate and OnExecutionUpdate events to see the order that is submitted to the account in realtime. The order would be submitted as a market order with the name Close. This would not be visible from a Strategy, you would need to use an indicator and add the addon framework account events.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Yes as I stated I am coding this in an add on. Here is my current OnExecutionUpdate and OnOrderUpdate classes:
      private void OnExecutionUpdate(object sender, ExecutionEventArgs e)
      {
      try
      {
      Dispatcher.InvokeAsync(() =>
      {
      ExecutionInstrument = e.Execution.Instrument;
      ExecutionQuantity = e.Quantity;
      ExecutionPrice = e.Price;
      outputBox6.Clear();
      outputBox6.AppendText("Execution:");
      outputBox6.AppendText(string.Format("{0}Instrument : {1}{0}Quantity: {2}{0}Price: {3}{0}MarketPosition: {4}{0}Time: {5}{0}",
      Environment.NewLine,
      e.Execution.Instrument.FullName,
      e.Quantity,
      e.Price,
      e.MarketPosition,
      e.Time));
      TradeAnalysis();
      });
      }
      catch (Exception error)
      {
      Dispatcher.InvokeAsync(() =>
      {
      outputBox6.Text = "TCFramework - OnExecutionUpdate Exception: " + error.ToString();
      });
      }

      }
      private void OnOrderUpdate(object sender, OrderEventArgs e)
      {
      try
      {
      Dispatcher.InvokeAsync(() =>
      {
      outputBox6.Clear();
      outputBox6.AppendText("Order:");
      outputBox6.AppendText(string.Format("{0}Instrument : {1}{0}OrderAction: {2}{0}OrderType: {3}{0}Quantity: {4}{0}LimitPrice: {5}{0}StopPrice: {6}{0}Time: {7}{0}",
      Environment.NewLine,
      e.Order.Instrument.FullName,
      e.Order.OrderAction,
      e.Order.OrderType,
      e.Quantity,
      e.LimitPrice,
      e.StopPrice,
      e.Time));

      if (e.Order.OrderType == OrderType.Market && (e.Order.OrderAction == OrderAction.BuyToCover || e.Order.OrderAction == OrderAction.Sell))
      {
      profitTarget = 0;
      stopLoss = 0;
      return;
      }

      if (e.Order.OrderType == OrderType.Limit)
      {
      if (CurrentMarketPosition == MarketPosition.Long && e.Order.LimitPrice > CurrentAveragePrice)
      {
      profitTarget = e.Order.LimitPrice;
      }
      else if (CurrentMarketPosition == MarketPosition.Short && e.Order.LimitPrice < CurrentAveragePrice)
      {
      profitTarget = e.Order.LimitPrice;
      }
      }

      else if (e.Order.OrderType == OrderType.StopMarket)
      {
      if (CurrentMarketPosition == MarketPosition.Long && e.Order.StopPrice < CurrentAveragePrice)
      {
      stopLoss = e.Order.StopPrice;
      }
      else if (CurrentMarketPosition == MarketPosition.Short && e.Order.StopPrice > CurrentAveragePrice)
      {
      stopLoss = e.Order.StopPrice;
      }
      }

      TradeAnalysis();
      });
      }
      catch (Exception error)
      {
      Dispatcher.InvokeAsync(() =>
      {
      outputBox6.Text = "TCFramework - OnOrderUpdate Exception: " + error.ToString();
      });
      }
      }​
      So under Execution update I can have it look for e.ExecutionType.Close ?

      Comment


        #4
        Hello RaygunWizzle,

        I would suggest adding a print that just outputs the Order and Execution objects so you can see the values for any type of order. The way to check for a close order is to look at the orders Name property which is equal to "Close".
        JesseNinjaTrader Customer Service

        Comment


          #5
          If you're trying to figure out when someone closes a trade using the "Close" button, it might not be straightforward to spot it directly. What you could do is keep an eye on changes in how many stocks or money they have. If that number goes down without them making any new buys or sells, chances are they hit that "Close" button.

          Another idea is to look at the history of their trades. Sometimes, you can see a note or label saying a trade was closed. This could be a clue that they closed their position.​

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by fx.practic, 10-15-2013, 12:53 AM
          5 responses
          5,404 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by Shai Samuel, 07-02-2022, 02:46 PM
          4 responses
          95 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by DJ888, Yesterday, 10:57 PM
          0 responses
          7 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by MacDad, 02-25-2024, 11:48 PM
          7 responses
          159 views
          0 likes
          Last Post loganjarosz123  
          Started by Belfortbucks, Yesterday, 09:29 PM
          0 responses
          8 views
          0 likes
          Last Post Belfortbucks  
          Working...
          X