Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Condition Builder - Calling previous and next trade

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

    Condition Builder - Calling previous and next trade

    Hi NT

    I'm trying to use the condition builder to have the size of each trade have a particular number of contracts traded, in the same strategy. For example, the first trade is a loser, then I'm flat for a while until the next trade triggers, I want that next trade to have a different contract size than the previous trade, sometimes lower, sometimes higher.

    Here would be the logic:

    If the realized PL of one trade ago is less than the realized PL from 2 trades ago

    Do the following

    enter the next trade with (more/less) contracts.

    I'm very new coding but I understand the syntax a bit, just keep it simple if you can

    Thanks!

    #2
    Hello EminiJalapenio,

    Thank you for your inquiry.

    Unfortunately, this would go beyond the Strategy Wizard.

    However, in code, this is what you would be able to do:
    Code:
    Trade oneTradeAgo = Performance.AllTrades[Performance.AllTrades.Count - 1];
    Trade twoTradesAgo = Performance.AllTrades[Performance.AllTrades.Count - 2];
    double oneTradeAgoTotalProfit = oneTradeAgo.ProfitCurrency * oneTradeAgo.Quantity;
    double twoTradesAgoTotalProfit = twoTradesAgo.ProfitCurrency * twoTradesAgo.Quantity;
    				
    if (oneTradeAgoTotalProfit < twoTradesAgoTotalProfit)
    	{
    		EnterLong(oneTradeAgo.Quantity - 1);
    	}
    In my example, if the total profit for the one trade ago is less than the total profit of two trades ago, the new long trade will be entered with one less contract.

    I would suggest reading through the NinjaTrader help guide for a reference guide of the numerous syntax you would be able to use: http://ninjatrader.com/support/helpG..._reference.htm

    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Thank you very much, can you tell me in what section of the cs file the this section of code will go in? I have added it into the code in various places, the file compiles but when I load the strategy to the chart, I can see the name and the settings but nothing shows in the results.

      Comment


        #4
        Hello EminiJalapenio,

        This code would most likely go into your OnBarUpdate() method as I do not know how your code is formatted.

        Please remember to enable your strategy once you have added it to the chart. To verify if it is enabled or not enabled, you will want to right-click on your chart and select Strategies. Select your strategy from the bottom section and ensure that the Enabled section under the General category in the parameters to the right is set to True.

        Please note that we do not provide programming services.

        I would suggest reading the Educational Resources section of the NinjaTrader help guide for more information about using NinjaScript: http://ninjatrader.com/support/helpG..._resources.htm

        Please, let us know if we may be of further assistance.
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          Gotcha, thanks! I wonder if I build an indicator that shows "previous trade" using the same logic, I would be able to call that on the condition builder, correct? I'm learning to code little by little Thanks for your patience.

          Comment


            #6
            Hello EminiJalapenio,

            Unfortunately the syntax I have provided will only work in strategies as the syntax utilizes the Strategy class. An indicator utilizes the Indicator class and not the Strategy class. While there is a way that you would be able to utilize Strategy class methods in an indicator, it does not seem that the Strategy Wizard can access these values and you would have to use the code editor regardless.

            Please, let us know if we may be of further assistance.
            Zachary G.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            558 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            324 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            545 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            547 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X