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

Last Trade Type

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

    Last Trade Type

    hello

    I am back testing historical data, want to know how can I get the last trade type (Long or Short) that completed after strategy exit trade due to either reversing of crossover or SetStoploss() or SetProfitTarget().

    I have Partial success in getting that in case of crossover reversing cause it will be opposite of new generated signal, but unable to get when position get flat due to stop loss or profit target.

    #2
    Welcome to our forums - easiest would be likely just setting a flag when you enter your trades, as it would persist it's value once set unless you reset it on another entry / condition occurring. Then you can simply check for that value state once you're flat.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      MarketPosition.Long , Short and Flat are already available of these flag type, but they do not solve my purpose IMO.

      I want to capture some calculations just after strategy exited any trade . those calculation depends on which type of trade just completed Long or Short.

      what u have suggested I hav tried but it do not capture those trade which exit due to StopLoss and ProfitTarget and position gets flat after that.

      Comment


        #4
        There's a lot more specific infos available per the IOrder interface for example, however for your Set() those returns would not exist per default, but could be custom captured to provide you with the details desired - http://www.ninjatrader.com/support/f...ead.php?t=5790

        BertrandNinjaTrader Customer Service

        Comment


          #5
          I am new to Not and c#,

          will look in what u suggested, but I am back testing historical data. Do IOrder and IExecution interface work in this case ?

          Let me simplify my need. Say all I want to print in output window for each trade

          "Long (or Short )" + "StopLoss( or ClosePosition or ProfitTarget)"

          just like back testing report shows. How can I do that from my strategy ?

          Comment


            #6
            You can for example access the strategy's trade and performance statistics as well in your code -



            To get the last trade completed realtime direction, you could work with this snippet -

            Code:
            if (Performance.RealtimeTrades.Count > 0)
            	{
            		Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
            		
            		string lastEntryType = lastTrade.EntryExecution.MarketPosition.ToString();
            		
            		PrintWithTimeStamp("Bar " + CurrentBar + " The last trade's dir " + lastEntryType);
            	}
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Thanks Bertrand.

              I will try as you suggested.

              Comment


                #8
                hi bertrand,

                i am unable to produce what i want , as i suspected those Performance.Realtime and IOrder,IExecution stuff works only on Live and Market Replay. They are not producing any thing with BackTesting of Historical Data , as my prime request.

                Can u suggest any other method by which i can get this Print in OutPut window just as NT do in BackTest report,
                Attached Files

                Comment


                  #9
                  That's correct as in the snippet the real-time trades collection would be worked with, however there's a corresponding .AllTrades collection as well which will let you work with historical and real-time trades from the script, so activity done.
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Hi Bertrand,

                    I am happy that with some trial i could produce what i wanted with this code snippets, it looks me simplest yet. Strange thing for me is that i have to reverse check execution.MarketPosition for the trade type which was exited. As seems execution.MarketPosition posses the opposite trade in case of Exit events. your comment is sought in this.


                    Code:
                    protected override void OnExecution(IExecution execution)
                    			{
                    				if(execution != null)
                    				{
                    					if(execution.Name == "Stop loss" || execution.Name == "Close position" || execution.Name == "Profit target")
                    					{
                    						if(execution.MarketPosition.ToString() == "Short")     //need to check opposite of exceution
                    							Print("Long"+"  "+execution.Name);
                    						else if(execution.MarketPosition.ToString() == "Long")
                    							Print("Short"+"  "+execution.Name);
                    					}
                                    
                    				}
                                }
                    Attached Files

                    Comment


                      #11
                      Glad to hear that spaceTrader. Yes I would expect that, since there's no flat state here or matching trade done, it's just an execution. If you have a long exit, you would actually sell > so market position short given. However since you were long before that would produce a flat state then if the pairing is done.
                      BertrandNinjaTrader Customer Service

                      Comment


                        #12
                        being on execution style working, how do i track individual open position and get reported which individual open position got stop out or profit target.

                        What i want to do , say i have two open position - "Long1" and "Long2" entry signal name.

                        "Long2" might have been trigger after "Long1" say on some higher price and later time. Stop for both order will be submitted by strategy. Suppose stop for "Long2" executed and then stop for "Long1" executed after some time.

                        How can i programmatically check , when first stop executed ( i manually know it is of "Long2" ) that which position exited "Long1" or "Long2" ? and how Ninja Trader performance report will assign Trade Number for this case. I see NT Backtesting report give each trade a trade number , how can i get trade number of these two completed trade as they happen one by one in sequence ?
                        Last edited by spaceTrader; 10-13-2013, 04:47 AM.

                        Comment


                          #13
                          Originally posted by spaceTrader View Post
                          being on execution style working, how do i track individual open position and get reported which individual open position got stop out or profit target.

                          What i want to do , say i have two open position - "Long1" and "Long2" entry signal name.

                          "Long2" might have been trigger after "Long1" say on some higher price and later time. Stop for both order will be submitted by strategy. Suppose stop for "Long2" executed and then stop for "Long1" executed after some time.

                          How can i programmatically check , when first stop executed ( i manually know it is of "Long2" ) that which position exited "Long1" or "Long2" ? and how Ninja Trader performance report will assign Trade Number for this case. I see NT Backtesting report give each trade a trade number , how can i get trade number of these two completed trade as they happen one by one in sequence ?
                          No answer ?
                          No solution ?

                          Comment


                            #14
                            spaceTrader, I'm sorry. Thanks for ringing my bell here. This is something that the Trade class would provide as well, each trade would have an entry and exit IExecution object to offer to check for example the execution time and compare - http://www.ninjatrader.com/support/h.../nt7/trade.htm

                            The pairing itself on the strategy level is done by signal name, however in the account performance reports you would see it strictly done on the FIFO principle.
                            BertrandNinjaTrader Customer Service

                            Comment


                              #15
                              Thank you Bertrand,

                              Being totally new to NT and C#, i find myself crouching through stones...but i am progressing. Your help is really appreciated. I will try again and will ask some more of my dead lock in this learning process.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by traderqz, Today, 09:44 AM
                              1 response
                              1 view
                              0 likes
                              Last Post traderqz  
                              Started by rocketman7, Today, 09:41 AM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by rocketman7, Today, 02:12 AM
                              7 responses
                              31 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by guillembm, Yesterday, 11:25 AM
                              3 responses
                              16 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by junkone, 04-21-2024, 07:17 AM
                              10 responses
                              149 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Working...
                              X