Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

AllTrades Object Issue

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

    AllTrades Object Issue

    Hi,

    I have developed a strategy, in the strategy the trade volume would updated based on the result of previous order (winning/losing). At the beginning, I would initialize everything because I don't want the strategy be affected by the performance of the last order in the previous day. So here is my code:

    //This is to get the total # of trade in the previous day
    if (Times[0][0].TimeOfDay < Times[0][1].TimeOfDay)
    {
    AccmTrades = SystemPerformance.AllTrades.Count;
    }

    // This is to control Trade Volumn
    if (SystemPerformance.AllTrades.Count - AccmTrades >= 1)
    {
    if (SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].ProfitCurrency < 0
    && TradeNum != SystemPerformance.AllTrades.Count - 1
    && TradeVol < 10)
    {
    TradeVol++;
    TradeNum = SystemPerformance.AllTrades.Count - 1;
    Print("New Trades: " + (SystemPerformance.AllTrades.Count - AccmTrades));
    Print("Trade Volumn = " + TradeVol);

    }

    if (SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].ProfitCurrency > 0
    && TradeNum != SystemPerformance.AllTrades.Count - 1)
    {
    TradeVol = 1;
    TradeNum = SystemPerformance.AllTrades.Count - 1;
    }
    }

    The default value of TradeVol (Trade Volume) is 1, and it is supposed to start from 1. TradeNum is the trade number that label the last order as the strategy is run on each tick and I only want it update the volume once. However, when I actually start running the strategy, the first order's trade volume is always 10, and according to the log, the trade volume update code has already been triggered 10 times even there is no order at all in the new day. So I wonder how to use the AllTrades array correctly.

    Thank you very much for your help!

    Click image for larger version

Name:	image.png
Views:	171
Size:	1,006.2 KB
ID:	1217198

    #2
    Hello RandomTrader,

    Thank you for your post.

    Please clarify; which method is calling this block of code? For example, is it inside of OnBarUpdate(), OnPositionUpdate(), etc?

    Additionally, I see you have a condition that when it is met, TradeVol = 1. It appears you are using this to reset the TradeVol. In order to see if this condition is being met, please add a Print statement inside of the condition such as the following:

    if (SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].ProfitCurrency > 0
    && TradeNum != SystemPerformance.AllTrades.Count - 1)
    {
    TradeVol = 1;
    Print(Time + "TradeVol reset to 1");
    TradeNum = SystemPerformance.AllTrades.Count - 1;
    }

    After adding this print, do you see it in the NinjaScript Output window? If not, this means that the conditions are not evaluating to true in order to trigger the actions.

    I look forward to your reply.​

    Comment


      #3
      Hi Emily,

      Thank you for your response.

      The block of code is called in OnBarUpdate().
      I can see the volume is reset to 1 as I see that trade volume after the first order is reset to 1 and stayed as 1 when there is a winning trade and increases when there is a losing trade, but the first order submitted is always wrong.
      Click image for larger version

Name:	image.png
Views:	142
Size:	20.7 KB
ID:	1217214
      Thank you for your help! ​

      Comment


        #4
        Hello RandomTrader,

        Thanks for your reply.

        You mentioned that the first order submitted is always wrong; what is wrong with the order? What is it that you are expecting vs. what you are experiencing?

        Additionally, what output are you expecting to see?

        I also would like to note that with my suggested print statement, I forgot to include the bar index so the actual time was not printed properly per your screenshot. This can be resolved by adding the bar index to the print statement:
        Print(Time[0] + "TradeVol reset to 1");

        I look forward to your reply.​

        Comment


          #5
          Hi Emily,

          My expectation is the first trade volume is always 1, then, based on the performance of the orders, i.e. winning or losing trades, the volume increases by 1, and the maximum volume is 10. It is not supposed to start from 10 and then reset to 1. That is the problem.

          Thanks.

          Comment


            #6
            Hello RandomTrader,

            However, when I actually start running the strategy, the first order's trade volume is always 10, and according to the log, the trade volume update code has already been triggered 10 times even there is no order at all in the new day
            Your code will execute for historical bars as well as the current day. If you are trying to do a reset based on the session you should use Bars.IsFirstBarOfSession. There is a sample which uses trade performance and resets variables for use with the current day. That would be helpful to program your script in a similar way so that each new session it can zero out any variables you use to start fresh. https://ninjatrader.com/support/help...nce_statis.htm

            My expectation is the first trade volume is always 1, then, based on the performance of the orders, i.e. winning or losing trades, the volume increases by 1, and the maximum volume is 10. It is not supposed to start from 10 and then reset to 1. That is the problem.
            From the first image you provided of the output it appears that it is starting at 1 and moving up to 10. The timestamp in the prints shows the oldest at the top and newest at the bottom.

            From the print that Emily suggested I see that you are checking the condition to reset is happening. That would be due to the conditions you used there being true. The ProfitCurrency was greater than 0 and TradeNum was not equal to the count when that condition was checked. If the reset needs to happen at a different point you would need to look into that condition and modify it.

            One item that I see is that you are increasing the volume when it was not profitable and you are doing the reset when it was profitable:

            Code:
            ProfitCurrency < 0
            
            ProfitCurrency > 0

            Did you mean to increase the volume if the trade was profitable and then reset when it was not profitable? if so you need to reverse the conditions you created so that the first condition checks Greater than 0 and the second condition checks Less than 0.




            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Yesterday, 05:17 AM
            0 responses
            58 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            133 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            73 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