Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trade lastTrade =

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

    Trade lastTrade =

    Hallo,

    I found in the forum the following code:

    Trade lastTrade = Performance.RealtimeTrades[0];


    However, when I code this in my strategy, after the OnbarUpdate(), the word "Trade" does not turn blue. The script compiles ok, but the strategy does not execute any trades.

    Do I have to write some more code somewhere else for the "Trade" thing to turn blue?


    #2
    It should turn blue automatically. Please ensure this is indeed in a strategy and that you are on NT6.5.1000.8.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      I thought so too, but it doesnt turn blue and the strategy does not execute trades, even though it compiles ok.

      Yes, the version is the one you mentioned

      Any hints?

      Comment


        #4
        Please see your Control Center logs for any errors. It may not necessarily turn blue, but if you can compile then it should still work. If you are not making any trades you may want to check your strategy's logic.

        Please see this reference sample for using the Performance class: http://www.ninjatrader-support2.com/...ead.php?t=4084
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Oh, the log, I always forget to check.

          It says that the index must be no negative and less than...

          What i want to achieve it the Print the PnL of the last trade every time a trade is closed.

          My code is:

          private double lastTradeProfit = 0;

          lastTradeProfit = Performance.AllTrades[0].ProfitCurrency;

          How can I achieve to print out the PnL?

          Thanks!!

          Comment


            #6
            Please see the example here: http://www.ninjatrader-support.com/H...radeClass.html
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Hallo again,

              I just did that, copy the text from the example:

              the Trade word didnt turn blue in my strategy

              and the Print() is:

              The last trade's profit is 4949,99999999997
              The last trade's profit is 4949,99999999997
              The last trade's profit is 4949,99999999997
              The last trade's profit is 4949,99999999997
              The last trade's profit is 4949,99999999997

              and always like that

              Any ideas?

              Thanks

              Comment


                #8
                That is the value of your last trade. As you do additional trades it should change.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  I know, there are like 100 trades, but the value does not update itself.

                  I think that the Trade word not turning blue has something to do with it...

                  Comment


                    #10
                    No, the word does not necessarily need to be blue. If it is not updating it is because you are not setting the Trade object to take on new Performance trades. Please set it to take on different trades.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      i just copied the example you mentioned. It should work shouldn´t it?

                      I have no idea what is going on. I will keep looking.

                      Thanks anyway

                      Juan

                      Comment


                        #12
                        Please post up your whole code and we can do a spot check for you. Thanks.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Hallo again,

                          This is the code. The strategy has been working for a long time, so it just the added lines about the Performance that do not work:

                          publicclass XXXX: Strategy
                          {
                          #region Variables
                          privatedouble lastProfit = 0;





                          #endregion
                          protectedoverridevoid Initialize()
                          {

                          EntriesPerDirection = MM;
                          EntryHandling = EntryHandling.AllEntries;
                          ExitOnClose =
                          false;
                          TimeInForce = Cbi.TimeInForce.Gtc;
                          ExcludeWeekend =
                          true;
                          SessionBegin = DateTime.Parse(
                          "08:00 AM");
                          SessionEnd = DateTime.Parse(
                          "22:00 PM");

                          ExitOnClose =
                          false;
                          CalculateOnBarClose =
                          true;
                          }
                          ///<summary>
                          /// Called on each bar update event (incoming tick)
                          ///</summary>
                          protectedoverridevoid OnBarUpdate()
                          {

                          if (Performance.AllTrades.Count > 0)
                          {
                          // Get the last completed trade(at index 0)
                          Trade lastTrade = Performance.AllTrades[0];
                          // Calculate the PnL for the last completed trade
                          double lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;
                          // Pring the PnL to the Output window
                          Print("The last trade's profit is " + lastProfit);
                          }

                          If you spot any errors, please let me know

                          Thanks


                          I

                          Comment


                            #14
                            jriverac,

                            You have no trade logic in this code. Please try with some very basic trade logic.

                            Code:
                            if (Close[0] > Open[0])
                                 EnterLong();
                            else if (Close[0] < Open[0])
                                 ExitLong();
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Ok, this simple strategy:
                              protectedoverridevoid OnBarUpdate()
                              if (Performance.AllTrades.Count > 0)
                              {
                              // Get the last completed trade(at index 0)
                              Trade lastTrade = Performance.AllTrades[0];
                              // Calculate the PnL for the last completed trade
                              double lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;
                              // Pring the PnL to the Output window
                              Print("The last trade's profit is " + lastProfit);
                              }



                              if (Close[0] > Open[0])
                              EnterLong();
                              elseif (Close[0] < Open[0])
                              ExitLong();
                              }

                              It yields 2452 trades, so I get 2452 lines in the output window. All of them with the result of the first trade.

                              What can be wrong?

                              Thanks for the help

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by fredrikn, 05-08-2019, 04:01 AM
                              57 responses
                              4,389 views
                              2 likes
                              Last Post Momentum  
                              Started by Blairski, Yesterday, 09:19 AM
                              2 responses
                              11 views
                              0 likes
                              Last Post Blairski  
                              Started by tonynt, Today, 09:33 AM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by minotti1, Today, 08:31 AM
                              1 response
                              13 views
                              0 likes
                              Last Post NinjaTrader_Jim  
                              Started by SmartArtsStudio, 05-15-2021, 10:46 AM
                              1 response
                              15 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X