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

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:



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



    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.



    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:



            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:



            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 Kaledus, Today, 01:29 PM
            0 responses
            3 views
            0 likes
            Last Post Kaledus
            by Kaledus
             
            Started by PaulMohn, Today, 12:36 PM
            1 response
            16 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by yertle, Yesterday, 08:38 AM
            8 responses
            36 views
            0 likes
            Last Post ryjoga
            by ryjoga
             
            Started by rdtdale, Today, 01:02 PM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Started by alifarahani, Today, 09:40 AM
            3 responses
            19 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Working...
            X