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 determine if Last Trade was Long or Short

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

    How to determine if Last Trade was Long or Short

    Hello,

    I need to determine the direction of the last closed trade. I thought I should be able to determine this using the AllTrades collection. But after investigating the methods and properties associated with this collection I don't see how this can be determined.

    From documentation :
    TradeCollection myTrades = SystemPerformance.AllTrades.GetTrades("MSFT", "myEntrySignal", 1);

    Thought that there would be a property on the trade like
    myTrades[1].TradeDirection

    But no type property. Only properties like Entry, Exit, ProfitTicks .. I could look at these to determine if Long or Short Trade.. But you would think there would be property for said direction?

    So, other approach would be to use the OnPositionUpdate like :

    Code:
    [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]
    [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]protected[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] OnPositionUpdate([/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Position[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] position,[/COLOR] [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] averagePrice,[/COLOR] [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] quantity,[/COLOR] [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]MarketPosition[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] marketPosition)[/COLOR]
     {
        [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]      lastTradeDirection = marketPosition == [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]MarketPosition[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].Long ?[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af] MarketPosition[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].Long : [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]MarketPosition[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].Short; 
    [/SIZE][/FONT][/SIZE][/FONT][/SIZE][/FONT][/SIZE][/FONT]
     [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]}
     [/SIZE][/FONT][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]

    Am I missing a way to do this with AllTrades or AllTrades.LosingTrades and AllTrades.WinningTrades?

    Thanks,

    Last edited by tornadoatc; 12-05-2015, 11:42 AM.

    #2
    Code:
    [LEFT]protected override  void OnBarUpdate()
    
    a simple bool would be the easiest way.
    { if (Position.MarketPosition = MarketPosition.Long){ long=true; short=false; } if (Position.MarketPosition = MarketPosition.Short){ short=true; long=false; } if(Position.MarketPosition = MarketPosition.Flat && long) // do stuff if(Position.MarketPosition = MarketPosition.Flat && short) // do different stuff }
    [/LEFT]
    Last edited by BigRo; 12-06-2015, 12:34 PM.

    Comment


      #3
      Hello tornadoatc,

      Thank you for writing in. There is unfortunately no property in the collection that I am aware of for this purpose. Here is an additional method which you can use right inside OnBarUpdate():
      Code:
      bool lastTradeLong;
      if(Performance.LongTrades.Count > 1 && Performance.ShortTrades.Count > 1)
      	lastTradeLong = (DateTime.Compare(Performance.LongTrades[Performance.LongTrades.Count - 1].ExitExecution.Time, Performance.ShortTrades[Performance.ShortTrades.Count - 1].ExitExecution.Time) > 0) ? false : true;
      Please let me know if I may be of further assistance.
      Michael M.NinjaTrader Quality Assurance

      Comment


        #4
        IMO, this really should be provided as a property. How can this be requested for the development team to evaluate?

        Thanks,

        Comment


          #5
          Hello,

          I have submitted a feature request regarding a TradeDirection property.

          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            In case you are still interested in trying this, you can solve this with some of the building blocks NT gives you though it will take a bit of work.

            Access the last trade with the following code and then determine the direction with a few if statements.

            Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
            double lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;
            int q1 = Performance.AllTrades[Performance.AllTrades.Count - 1].Entry.Order.Quantity;
            double Exitprice = Performance.AllTrades[Performance.AllTrades.Count -1].Exit.Price;
            double Enterprice = Performance.AllTrades[Performance.AllTrades.Count -1].Entry.Price;
            string Name = Performance.AllTrades[Performance.AllTrades.Count -1].Entry.Name;


            So if last trade was a loss and entry position relative to exit position was < or >, you can tell if the last trade was long or short.

            Let me know if this helps.

            Ian

            Comment


              #7
              So has the TradeDirection property been added to NT8 yet? I was looking for something similar but not just the last trade but the last 2 or 3 trade directions. Like do not take a long if the last 3 trades in a row were long and vice versa for shorts.

              Comment


                #8
                Hi Trader17, thanks for posting.

                The SystemPerformance.AllTrades collection is a collection of all the Trade objects a strategy has generated. The Trade object has an Entry and Exit execution where you can read what type of execution the Entry was (long or short)

                https://ninjatrader.com/support/help...?alltrades.htm - Documentation here.
                Also see the Trade object: https://ninjatrader.com/support/help...html?trade.htm

                Kind regards,
                -ChrisL
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks Chris. But won't SystemPerformance.AllTrades just give me a total of "all" long trades? I am looking specifically for the last 2 or 3 consecutive trades to be long or short. Unless I missed something. Where do I find the Trade Object execution type? Maybe I missed it in the Guide.
                  Thanks!!

                  Comment


                    #10
                    Hi Trader17,

                    SystemPerformance.AllTrades is a TradeCollection of all trades made by the strategy (a collection of all trades). e.g. The last 2 trades:

                    Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
                    Trade secondLastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 2];
                    Chris L.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks Chris. And how does this tell us if they were long/short?
                      Thanks.

                      Comment


                        #12
                        Hi Trader17, thanks for your reply.

                        Please read the Trade documentation that I sent earlier. It has an Entry and Exit Execution object that holds all information about the two executions that make up the trade. The Execution.Position property will tell you if the execution was long or short.

                        Kind regards,
                        -ChrisL

                        Chris L.NinjaTrader Customer Service

                        Comment


                          #13
                          So if I am correct, MarketPosition will tell me is last execution was long/short? So check back on last X executions to see if they were long/short. Examples anywhere? Can this feature be added in Strategy Builder as Last Trade Direction? Have not seen that in there yet.
                          Thanks.

                          Comment


                            #14
                            Hi Trader17, thanks for your reply.

                            This, unfortunately, is not available for use in the Builder. The script will need to be written in the NinjaScript editor to test/print out the values from SystemPerformance trades. Each documentation page I sent has an example of using the SystemPerformance collection. We also have a couple of full examples here:




                            Kind regards,
                            -ChrisL
                            Chris L.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by judysamnt7, 03-13-2023, 09:11 AM
                            4 responses
                            59 views
                            0 likes
                            Last Post DynamicTest  
                            Started by ScottWalsh, Today, 06:52 PM
                            4 responses
                            36 views
                            0 likes
                            Last Post ScottWalsh  
                            Started by olisav57, Today, 07:39 PM
                            0 responses
                            7 views
                            0 likes
                            Last Post olisav57  
                            Started by trilliantrader, Today, 03:01 PM
                            2 responses
                            21 views
                            0 likes
                            Last Post helpwanted  
                            Started by cre8able, Today, 07:24 PM
                            0 responses
                            10 views
                            0 likes
                            Last Post cre8able  
                            Working...
                            X