Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need Help with incrementing Order Quantity

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

    Need Help with incrementing Order Quantity

    NinjaTrader Support,

    I am trying to implement the following in a strategy:
    • Increase my position size (Forex), with each winning trade by 10000 units. The starting size is also 10000.
    • After three consecutive wins, reset the position size to 10000. If a loss occurs, also reset the position size to 10000.
    I can get the position to increment to 20000 and reset after a loosing trade, but for some reason it does not get to 30000.

    I can't seem to figure this out.

    Please see my code below:

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;



    // Set 1
    if ((CrossAbove(RSI1.Default, 10, 1))
    && (Close[0] > SMA1[0]))
    {

    EnterLong(10000*size);

    }

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

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

    if (lastProfitCurrency > 0)
    {
    size++;
    temp=size;
    }



    if (lastProfitCurrency < 0 || temp==3)
    {
    size=1;
    }







    #2
    Hello omermirza,

    Thank you for the post.

    To better understand what is happening in your logic I would suggest using a Print to see what the values are in the use case where this code is being executed.

    An example print could be like the following:

    Code:
    Print("size " + size + " temp " + temp );

    I look forward to being of further assistance.

    Comment


      #3
      Ok that was added, where would I see that in the strategy?

      Comment


        #4
        Hello omermirza,

        Prints appear in the NinjaScript output window. You can open it using the control center new menu.

        You can customize the print as needed to better explore the logic you made. Another item which can be helpful for the print is adding the time so you can associate the time with a specific bar in the chart.

        Code:
        Print(Time[0] + "  size " + size + " temp " + temp );
        I look forward to being of further assistance.

        Comment


          #5
          Got it, thanks.

          As I suspected, size never gets to 3. Not sure why though.

          Comment


            #6
            Hello omermirza,

            If the size is not getting to the target amount that would indicate your increment condition is not happening. You could check that by adding a print for that condition:

            Code:
            Print(Time[0] + " lastProfitCurrency " + astProfitCurrency);
            if (lastProfitCurrency > 0)
            {
            size++;
            temp=size;
            }
            I look forward to being of further assistance.

            Comment


              #7
              So basically the TradeQuantity and size don't match.

              If the TradeQuantity is 20000, then size should be two but it's not incrementing properly:

              6/3/2021 4:00:00 PM lastTradeQuantity 20000
              6/3/2021 4:00:00 PM size 1 temp 1
              6/3/2021 5:00:00 PM lastTradeQuantity 20000
              6/3/2021 5:00:00 PM size 1 temp 1
              6/3/2021 6:00:00 PM lastTradeQuantity 20000
              6/3/2021 6:00:00 PM size 1 temp 1

              Any ideas?

              -Omer

              Comment


                #8
                Hello omermirza,

                I would still suggest trying the print I had shown in post 6. You increment the size inside the lastProfitCurrency condition. If the size is not being incremented then that conditions is not true.


                I look forward to being of further assistance.

                Comment


                  #9
                  Ok, here is what that is coming back with:

                  1/14/2021 8:00:00 PM lastProfitCurrency 20
                  1/14/2021 8:00:00 PM size 1 temp 3

                  1/14/2021 9:00:00 PM lastProfitCurrency 20
                  1/14/2021 9:00:00 PM size 2 temp 2
                  1/14/2021 10:00:00 PM lastProfitCurrency 20
                  1/14/2021 10:00:00 PM size 1 temp 3

                  1/14/2021 11:00:00 PM lastProfitCurrency 20
                  1/14/2021 11:00:00 PM size 2 temp 2
                  1/15/2021 12:00:00 AM lastProfitCurrency -20
                  1/15/2021 12:00:00 AM size 1 temp 2
                  1/15/2021 1:00:00 AM lastProfitCurrency -20
                  1/15/2021 1:00:00 AM size 1 temp 2
                  1/15/2021 2:00:00 AM lastProfitCurrency -20
                  1/15/2021 2:00:00 AM size 1 temp 2
                  1/15/2021 3:00:00 AM lastProfitCurrency -20
                  1/15/2021 3:00:00 AM size 1 temp 2
                  1/15/2021 4:00:00 AM lastProfitCurrency -20
                  1/15/2021 4:00:00 AM size 1 temp 2
                  1/15/2021 5:00:00 AM lastProfitCurrency -20
                  1/15/2021 5:00:00 AM size 1 temp 2
                  1/15/2021 6:00:00 AM lastProfitCurrency -20
                  1/15/2021 6:00:00 AM size 1 temp 2
                  1/15/2021 7:00:00 AM lastProfitCurrency -20
                  1/15/2021 7:00:00 AM size 1 temp 2

                  Interesting here that temp got to 3 but size stayed at 1.

                  Comment


                    #10
                    Hello omermirza,

                    From the prints it appears to be working as you wrote it.

                    The orange prints show that the size got incremented 1, 2 and then due to the order of your logic on 3 it resets to 1 based on your condition and temp being 3:

                    Code:
                    if (lastProfitCurrency < 0 || temp==3)
                    {
                    size=1;
                    }
                    That happens twice and then following that lastProfitCurrency = -20 so the increment is no longer going to happen.




                    I look forward to being of further assistance.

                    Comment


                      #11
                      Ok, if II take that out then size increments for every bar if the last trade was profitable. For example, if the last successful trade was 20 bars ago, size will increment 20 times.

                      How can I get it to only increment once if the last trade was successful?

                      Comment


                        #12
                        Hello omermirza,

                        Increase my position size (Forex), with each winning trade by 10000 units. The starting size is also 10000.
                        After three consecutive wins, reset the position size to 10000. If a loss occurs, also reset the position size to 10000.
                        The logic you have is essentially doing what you described already, we can see it happened twice in the prints. The size variable got incremented to 3 meaning there were two increments. It then reset to 1 after the conditions you specified. It seems that it just got stuck based on the temp variable. You may need to reset that as well when you reset size so the logic can start over. Your condition uses the or statement so as long as temp is 3 it will continue to reset size.

                        I look forward to being of further assistance.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by NullPointStrategies, Yesterday, 05:17 AM
                        0 responses
                        61 views
                        0 likes
                        Last Post NullPointStrategies  
                        Started by argusthome, 03-08-2026, 10:06 AM
                        0 responses
                        134 views
                        0 likes
                        Last Post argusthome  
                        Started by NabilKhattabi, 03-06-2026, 11:18 AM
                        0 responses
                        75 views
                        0 likes
                        Last Post NabilKhattabi  
                        Started by Deep42, 03-06-2026, 12:28 AM
                        0 responses
                        45 views
                        0 likes
                        Last Post Deep42
                        by Deep42
                         
                        Started by TheRealMorford, 03-05-2026, 06:15 PM
                        0 responses
                        50 views
                        0 likes
                        Last Post TheRealMorford  
                        Working...
                        X