• 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

A few code line requests

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

    A few code line requests

    Hi NT8 support,

    I am having trouble finding these code lines:

    1. What is the code for for "Total PnL" that shows on the "accounts" tab in the control center. I have the realized/unrealized PnL for the strategy, but want the code for the account PnL total

    2. In the same image above, what is the code for Net Liquidation

    3. What is the code line as an if statement for IF(last bar experienced an entry Long/Short) so instead of replicating the conditions of entry, it would be something like "if in the last bar I entered a trade" ... then I could continue with my 'Then' script.


    Further, I am trying to understand what my current PnL is for a when I am achieving interim targets in a strategy. For instance, if I enter at a price of 100, then enter again at a price of 50, the position average price is now 75 and the PnL is calculated from there. However, if I take an interim target from the second entry at a price of 60, I have gained $10 in my PnL, but the trade is calculated from the Avg price of 75. When I do this over the course of several trades, my account PnL/the realized/unrealized for the strategy don't reflect what my account shows after I close a trade - I understand they are calculating through different methods. In the case of a series of trades like the above that take interim targets before going flat again, I have trouble determining what my actual PnL is at a given time. I am not asking for a bunch of code, just a few more pointers on how to calculate this in a script. I found the below scripts in the help guide and I think can be used as a reference, as I believe this is where to start.



    protected override void OnBarUpdate()
    {
    if (SystemPerformance.RealTimeTrades.Count > 0)
    {
    // Check to make sure there is at least one trade in the collection
    Trade lastTrade = SystemPerformance.RealTimeTrades[SystemPerformance.RealTimeTrades.Count - 1];

    // Calculate the PnL for the last completed real-time trade
    double lastProfitCurrency = lastTrade.ProfitCurrency;

    // Store the quantity of the last completed real-time trade
    double lastTradeQty = lastTrade.Quantity;

    // Pring the PnL to the NinjaScript Output window
    Print("The last trade's profit in currency is " + lastProfitCurrency);
    // The trade profit is quantity aware, we can easily print the profit per traded unit as well
    Print("The last trade's profit in currency per traded unit is " + (lastProfitCurrency / lastTradeQty));
    }
    }
    Last edited by Austiner87; 06-29-2020, 11:12 AM.

    #2
    Hello Austiner87,

    Thank you for your note. As to your first question, the total pnl is just the unrealized PNL + the realized PNL and those as well as the Net Liquidation can be found in the AccountItem class:

    https://ninjatrader.com/support/help...ccountitem.htm

    For checking if there was a trade entered on the last bar you'd want to use BarsSinceEntryExecution:

    https://ninjatrader.com/support/help...yexecution.htm

    For sim accounts, you can change the PNL to be calculated based on FIFO instead of average entry price which may be closer to what you're expecting. For a live account you'd need to check with your broker to see if they can change that on their end. This is found under Tools > Options > Trading > Calculations.

    https://ninjatrader.com/support/help...ns_trading.htm

    As far as calculating this yourself yes, the above snippet is a good starting point, you could then add the PNL of each trade together to get a running total.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Thank you, this is very helpful, looking for a bit more help - please see the error attached, what do I need to correct to fill the variable with the account net liquidation?

      I've set it up as private double = NetLiquidationVariable and separately NetLiquidationVariable = 0; in the correct parts of the code, but still get the error. Just looking to fill this variable with my account net liquidation.
      Attached Files

      Comment


        #4
        Hello Austiner87,

        Thank you for your reply.

        Looks like you'll need to explicitly cast that value to a double:

        Code:
        NetLiquidationVariable = (double)AccountItem.NetLiquidation;
        Please let us know if we may be of further assistance to you.

        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hi NT8,

          I appreciate the support, this has been very helpful. However, I am still not getting the result I am looking for. In the attached snap, what I am trying to do is establish an account target at a currency total that when met, all positions are closed out - this is so that when I run this strategy across multiple instruments, the positions close as long as my account net liquidation reaches it's currency target. This is intended to allow for me to take acceptable losses in positions on other instrument as my net gain is positive. However, during a test, I could not get this result. Please help me correct this code.

          On a separate note, how do I enter a position on a different instrument when the strategy is turned on for another instrument? For instance, entering on YM if my conditions are met for a strategy on an NQ chart. If it is simply addition to the entry syntax, can you help by changing it below:

          EnterLong(Convert.ToInt32(DefaultQuantity), "");
          Attached Files

          Comment


            #6
            Hello Austiner87,

            Thank you for your reply.

            Based on what you have there it looks like you're not locking the account and subscribing to updates for the OnAccountItemUpdate as in the example on this page:

            https://ninjatrader.com/support/help...ount_class.htm

            I'd try something like what's in that example.

            For your other question, it sounds like you're looking to have a multi-instrument strategy - if a condition is met on NQ, you want to enter on the YM. In that case you would want to use AddDataSeries to add an additional YM Series and then submit your orders to that.

            Here's a link to our help guide on Multi-series scripts:

            https://ninjatrader.com/support/help...nstruments.htm

            There is also a script that comes installed with NinjaTrader that is called "Sample MultiInstrument" that you can look at for some guidance on how to include multiple instruments in a script.

            Please let us know if we may be of further assistance to you.
            Kate W.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by digibob, Today, 03:45 AM
            1 response
            10 views
            0 likes
            Last Post NinjaTrader_Jim  
            Started by waldoalvarez, Yesterday, 04:59 PM
            1 response
            15 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by jayfutures23, Today, 07:06 AM
            1 response
            12 views
            0 likes
            Last Post NinjaTrader_Melissa  
            Started by MojoJojo, Today, 05:59 AM
            4 responses
            11 views
            0 likes
            Last Post MojoJojo  
            Started by Spark68, Yesterday, 07:48 PM
            1 response
            19 views
            0 likes
            Last Post NinjaTrader_PaulH  
            Working...
            X