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

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 love2code2trade, Yesterday, 01:45 PM
                              4 responses
                              28 views
                              0 likes
                              Last Post love2code2trade  
                              Started by funk10101, Today, 09:43 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post funk10101  
                              Started by pkefal, 04-11-2024, 07:39 AM
                              11 responses
                              37 views
                              0 likes
                              Last Post jeronymite  
                              Started by bill2023, Yesterday, 08:51 AM
                              8 responses
                              45 views
                              0 likes
                              Last Post bill2023  
                              Started by yertle, Today, 08:38 AM
                              6 responses
                              26 views
                              0 likes
                              Last Post ryjoga
                              by ryjoga
                               
                              Working...
                              X