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

Quantity Issue (Last Trade vs. Last Contract)

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

    Quantity Issue (Last Trade vs. Last Contract)

    I am trying to access the full number of contracts used in my last trade, but I am only ever able to access the last executed quantity in the sequence. Sometimes the last executed quantity = the total number of contracts, but often times there is a split and it does not. For example it will match up if we have 4 contracts sold in the entry and 4 contracts bought with a profit target in one execution, however the issue lies when I have my 4 contracts get executed like this: Sold 1, Sold 3 as entry, then bought 2 bought 2 as an exit. I have tried a number of things and nothing is giving me the full number of contracts for the entire trade, I am only ever seeing the last executed quantity.

    In the code below I am attempting to access the number of contracts used in my previous trade and increment them by 1:

    Here is what I have tried:

    int Test = Positions[0].Quantity: This does nothing. It won't increment at all, it just stays at 1. I have tried moving the index to 1, -1, etc, but nothing seems to work here.

    int Test2 = lastTrade.Quantity; This just takes the last executed part of the trade, as I mentioned it works fine until there are splits then it only looks at the last executed quantity in the sequence.

    int Test3 = lastTrade.Entry.Quantity; This one takes the first entry quantity, which reduces the number of splits since most of the splits occur on the exit, but again, when I splits on the entry, it takes the first part and not the whole thing.

    Here is the working code below. Please let me know how to accomplish accessing the full number of contracts from my previous trade. I appologize if this has been fielded somewhere else on the board, but I have looked for hours and could not find anything.




    if (Performance.RealtimeTrades.Count > 0)
    {
    Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
    double lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;
    Print("The last trade's profit is " + lastProfit);

    int Test = Positions[0].Quantity ;
    int Test2 = lastTrade.Quantity;
    int Test3 = lastTrade.Entry.Quantity;



    // Condition set 1
    if (Close[0] > Open[0]
    && Close[1] > Open[1]
    && Close[2] < Open[2]
    && Position.MarketPosition == MarketPosition.Flat
    )
    {


    EnterLong(Test +1,"");
    }
    }


    Thanks,

    Ian

    #2
    Hello iantg,

    Thank you for your patience.

    Can you supply the full strategy used?

    You can attach your strategy to your response by going to File > Utilities > Export NinjaScript > Export selected source files > select your strategy > select the right arrow > Export. The file will be located under (My) Documents\NinjaTrader 7\bin\Custom\ExportNinjaScript.

    Comment


      #3
      Patrick,

      Thanks for getting back to me and offering to help me with this. I was actually able to resolve this recently though. The issue was in using the lastTrade.Quantity. Now I am using something to the effect of: Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1].Order.Quantity

      And it is working fine.

      Thanks,

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by r68cervera, Today, 05:29 AM
      0 responses
      2 views
      0 likes
      Last Post r68cervera  
      Started by geddyisodin, Today, 05:20 AM
      0 responses
      3 views
      0 likes
      Last Post geddyisodin  
      Started by JonesJoker, 04-22-2024, 12:23 PM
      6 responses
      33 views
      0 likes
      Last Post JonesJoker  
      Started by GussJ, 03-04-2020, 03:11 PM
      12 responses
      3,239 views
      0 likes
      Last Post Leafcutter  
      Started by AveryFlynn, Today, 04:57 AM
      0 responses
      6 views
      0 likes
      Last Post AveryFlynn  
      Working...
      X