• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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 martyn73, Today, 02:29 PM
        0 responses
        7 views
        0 likes
        Last Post martyn73  
        Started by cls71, Today, 12:28 PM
        0 responses
        6 views
        0 likes
        Last Post cls71
        by cls71
         
        Started by NeverDownMoney, Today, 12:19 PM
        0 responses
        2 views
        0 likes
        Last Post NeverDownMoney  
        Started by NT-Roland, Today, 11:24 AM
        0 responses
        6 views
        0 likes
        Last Post NT-Roland  
        Started by NeverDownMoney, Today, 11:05 AM
        0 responses
        3 views
        0 likes
        Last Post NeverDownMoney  
        Working...
        X