Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to recognize the exit

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

    How to recognize the exit

    Hello

    I need to ask you how could I recognize (in NinjaScript) when the strategy exit any position on stoploss. Because when the strategy exit on stoploss I need to do something - i need to call MyMethod()...

    so is there any way (any trigger) how to realize that on every StopLoss-exit the NinjaTrader will call MyFunction()?

    And another question - is there any function which returns profit of last executed trade? I need to have exact profit of last executed trade..

    Thank you very much for your help

    #2
    Hello Zooinek,

    Thank you for your post.

    If you want to trigger something for when your StopLoss is filled you will have to work with IOrder objects and check the status within the OnExecution() method.

    You can work with the Trade Performance Class to get PnL from the last trade.

    This reference sample can help when using Trade Performance.
    Using trade performance statistics for money management
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Thank you that is exactly what i was looking for..
      I have to say you hare really good and very useful and helpful support..

      so if I understand when I want to MarketPosition of currently executing trade i will use..:

      Performance.AllTrades[0].Entry.MarketPosition

      And when I need to know unrealized profit/loss (because of move to be) of the currently executing trade I will use..:

      double currentTheoreticalProfit = Position.GetProfitLoss(Close[0], PerformanceUnit.Points)

      or (I guess it is the same)

      double currentTheoreticalProfit;
      if (Position.MarketPosition==MarketPosition.Long) {
      currentTheoreticalProfit = Close[0] - Position.AvgPrice;
      }
      if (Position.MarketPosition==MarketPosition.Short) {
      currentTheoreticalProfit = Position.AvgPrice - Close[0];
      }


      Note: when I am writing "currently executing trade" I guess it means that Performance.AllTrades[0] gives me my last executed trade I have done and I have just exited (in case I am in FLAT). But in case I am in for example LONG, the Performance.AllTrades[0] code will return the current executing trade I am doing right now.... right?

      And another question:
      Imagine I have function which returns profit of last executed trade

      public double profitOfLastExecutedTrade() {
      return Performance.AllTrades[0].ProfitPoints *
      Performance.AllTrades[0].Quantity;
      }

      In case I will call this method when I am in FLAT it will return the real profit of my last executed trade. OK. But what will happend if I am in LONG and I will call the method.
      So I am still in open position and there is no profit yet. (just the unrealized profit)
      Will it return the unrealized profit in that case?


      Thank you very much for your answers it really helps me lots of to understant the NinjaScript language and the principels. I am Java programmer so I know the points of OOP and I have to say it is really easy to understand this NinjaScript - it is really great work this language in compare with other languages..

      Comment


        #4
        The Trade Performance class will only collect information for completed trades.

        You'll have to work with Position.GetProfitLoss() for your current unrealized PnL.
        Ryan M.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by CortexZenUSA, Today, 12:53 AM
        0 responses
        1 view
        0 likes
        Last Post CortexZenUSA  
        Started by CortexZenUSA, Today, 12:46 AM
        0 responses
        1 view
        0 likes
        Last Post CortexZenUSA  
        Started by usazencortex, Today, 12:43 AM
        0 responses
        5 views
        0 likes
        Last Post usazencortex  
        Started by sidlercom80, 10-28-2023, 08:49 AM
        168 responses
        2,265 views
        0 likes
        Last Post sidlercom80  
        Started by Barry Milan, Yesterday, 10:35 PM
        3 responses
        11 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Working...
        X