Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with Strategy order script

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

    Help with Strategy order script

    I have the following code, BarsToWait is set to 10 on a 3 minute ES chart.


    if (ToTime(Time[0])> 095100)
    && ((BarsSinceExitExecution() > BarsToWait) || (BarsSinceExitExecution() ==-1))
    && Position.MarketPosition == MarketPosition.Flat
    && CurrentBar > BarTraded
    && Close[0] < EMA(length)[0]​

    However when I run the code in stategy analyzer I see 2 problems

    1. BarsSinceExitExecution does not work. It takes the trades anyways

    However when I run the code in stategy analyzer I see 2 problems
    Click image for larger version

Name:	2023-05-05_0-22-20.jpg
Views:	132
Size:	86.1 KB
ID:	1250107

    2. I have specified to not take trades before 9:51 am

    I see some trades taken at 9:36 am.

    I am not sure what I am doing wrong here. Can you please help?


    #2
    I would suspect that possible you are running Calculate == Calculate.OnPriceChange or Calculate == Calculate.OnEachTick rather than Calculate.OnBarClose, or that you are placing orders that are LiveUntilCanceled = true and thus they are saving up and executing together later. We would probably need more information to assist with this such as what comes after this "if" statement to place the orders and the Calculate state at the least. Additionally, if you are not trying to scale in, I would recommend that you set EntriesPerDirection to 1.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      Hello Graci117,

      Thank you for your post.

      I suggest adding print statements to your strategy both outside of your condition as well as when the orders are submitted to understand the values being used for your condition when trades are taken. To address both of your questions, you can print the value of BarsSinceExitExecution() and compare it to the value of BarsToWait, and you can also print Time[0] to see what time value is being used when the condition should be checking if it is past 9:51 or not. For more details on using prints to debug your script:


      After adding prints, review the output in the NinjaScript Output window. If you are unsure of the meaning of the output, you could share your print statements here along with the output. To save the output, right-click the NinjaScript Output window and select Save As. You can save the .txt file and then attach it here.

      Please let us know if we may be of further assistance.

      Comment


        #4
        Originally posted by NinjaTrader_Emily View Post
        Hello Graci117,

        Thank you for your post.

        I suggest adding print statements to your strategy both outside of your condition as well as when the orders are submitted to understand the values being used for your condition when trades are taken. To address both of your questions, you can print the value of BarsSinceExitExecution() and compare it to the value of BarsToWait, and you can also print Time[0] to see what time value is being used when the condition should be checking if it is past 9:51 or not. For more details on using prints to debug your script:


        After adding prints, review the output in the NinjaScript Output window. If you are unsure of the meaning of the output, you could share your print statements here along with the output. To save the output, right-click the NinjaScript Output window and select Save As. You can save the .txt file and then attach it here.

        Please let us know if we may be of further assistance.
        Thanks so much!! The Print statements helped tremendously. The reason it was not working was because I had an 'Or' condition and may parenthesis were not correctly placed. I have yet another question, and would appreciate if you could help. I'd like to know the profit or loss on the previous trade. If the previous trade was completely stopped out before the breakeven hit then I'd like to stop trading for that day. How do I get the p/l for the previous trade?

        Comment


          #5
          It would end up looking something like:
          Code:
          double LastTradeProfitCurrency = (SystemPerformance.AllTrades.Count > 0) ? SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].ProfitCurrency : double.NaN;
          Bruce DeVault
          QuantKey Trading Vendor Services
          NinjaTrader Ecosystem Vendor - QuantKey

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          52 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          130 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          70 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          44 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          49 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X