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

Skip next trade after winning trade

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

    Skip next trade after winning trade

    Hello. I am in the process of backtesting a strategy and I would like to find out if there is a code snippet to skip the next trade after a winning trade but take all trades after losing trades. I have tried adapting the sample strategy 'SampleTradeObjects' but could not get it to do what I described above. Is there a way to do it without using looping i.e. a single line of code in an IF statement that would skip the trade after a win?

    #2
    Skip next trade after winning trade

    Hello homey,

    Thanks for writing in to our Support team.

    To check if the last trade was a loser, we will have to save the last trade to a variable and pull the profit it made and save it to a variable. Then, we can set a bool to true or false which will cause us to skip the next trade after a winning trade or take all trades after losing trades.

    For example:
    Code:
    protected override void Initialize()
    {
    bool canTrade = true;
    }
    protected override void OnBarUpdate()
    {
    // Check to make sure there is at least one trade in the collection
    if (Performance.AllTrades.Count > 0)
    {
    Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
    double lastProfit = lastTrade.profitCurrency;
    // if the last trade was a winner, set canTrade to false - change to < to check if the last trade was a loser and set canTrade to true
    if(lastProfit > 0)
    canTrade = false;
    // order entry logic
    if (your conditions && canTrade == true)
    // enter trades
    }
    }
    You can find more information on the Trade objects in our help guide here:
    http://ninjatrader.com/support/helpG...collection.htm

    Please let me know if I may be of any further assistance.
    Alan S.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_AlanS View Post
      Hello homey,

      Thanks for writing in to our Support team.

      To check if the last trade was a loser, we will have to save the last trade to a variable and pull the profit it made and save it to a variable. Then, we can set a bool to true or false which will cause us to skip the next trade after a winning trade or take all trades after losing trades.

      For example:
      Code:
      protected override void Initialize()
      {
      bool canTrade = true;
      }
      protected override void OnBarUpdate()
      {
      // Check to make sure there is at least one trade in the collection
      if (Performance.AllTrades.Count > 0)
      {
      Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
      double lastProfit = lastTrade.profitCurrency;
      // if the last trade was a winner, set canTrade to false - change to < to check if the last trade was a loser and set canTrade to true
      if(lastProfit > 0)
      canTrade = false;
      // order entry logic
      if (your conditions && canTrade == true)
      // enter trades
      }
      }
      You can find more information on the Trade objects in our help guide here:
      http://ninjatrader.com/support/helpG...collection.htm

      Please let me know if I may be of any further assistance.
      Hi Alan S. Thanks for your reply. The code looks great. It compiles in Ninjatrader, however, there is no output in the strategy analyzer after a backtest. How does the code take the initial trade and further subsequent trades from then on? Am I missing something? Cheers

      Comment


        #4
        Hello homey,

        Thanks for your reply.

        There is no order entry logic in the code sample that I provided. You will need to include your order entry logic after the if(your conditions && canTrade == true) check and include your conditions in that check in place of 'your conditions'. You will also want to ensure you are creating another check to set canTrade == true after a losing trade so that trades are being placed.

        You can find more information on all the different order methods in our help guide here:
        http://ninjatrader.com/support/helpG...er_methods.htm

        Please let me know if I may be of any further assistance.
        Alan S.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_AlanS View Post
          Hello homey,

          Thanks for your reply.

          There is no order entry logic in the code sample that I provided. You will need to include your order entry logic after the if(your conditions && canTrade == true) check and include your conditions in that check in place of 'your conditions'. You will also want to ensure you are creating another check to set canTrade == true after a losing trade so that trades are being placed.

          You can find more information on all the different order methods in our help guide here:
          http://ninjatrader.com/support/helpG...er_methods.htm

          Please let me know if I may be of any further assistance.
          Hi Alan S.

          I have another question. It seems that the code will skip the next trade after a winner; but what happens from that point on is that it skips all following trades. What I am after is to skip 1 trade after a winner and take the next trade and then again always skip 1 trade after the next winner.

          I would appreciate your comments.

          Comment


            #6
            Hello homey,

            Thanks for your reply.

            This code sample was meant to be taken as an example to demonstrate what you are trying to achieve - in the code sample I mention that you will need to change the condition to check if(lastProfit < 0) and set canTrade to true when this occurs. If you do not include this in your code then you will get exactly the behavior you are describing as in the sample I provided I only wrote the if(lastProfit > 0) check. You must set the bool to true so that when you are checking if it is true later in your code, the condition can be met.

            Please let me know if I may be of any further assistance.
            Alan S.NinjaTrader Customer Service

            Comment


              #7
              Hi Alan S.

              The strategy seems to be doing what it is meant to for the most part. Thanks for your help so far.

              I have encountered another problem and I will try to describe it. The strategy is a long only strategy that uses a couple of indicators. It skips or takes 1 trade when the initial entry conditions are met using the indicators - so that is not a problem; however, while that initial position is open, subsequent entries are ignored when a 2nd, 3rd, or 4th entry condition is active when using the indicators. When a 'grouping' of such valid entries take place together, I would like to treat all 4 possible entries as a 'single' result and then move on to the next trade, and depending on whether the last result was a net winner/loser, I would skip/enter long.

              I have looked at the difference between using 'All entries' and 'Unique entries' but toggling between either of them gives the same result. Could you point me in the right direction or suggest any relevant code snippets that might help? Cheers.

              Comment


                #8
                Hello homey,

                With Order Handling>Entry Handling set to Unique Entries, are you uniquely naming your entries?

                For example,

                Code:
                EnterLong("Long1");
                EnterLong("Long2");
                I look forward to your reply.
                Alan P.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Alan P.

                  Yes I have uniquely named my entries "long1", "long2" etc. And I also set EntriesPerDirection and EntriesHandling.UniqueEntries in the Initialize section. What happens is that all entries take place on the initial bar when the initial entry conditions are met, but subsequent entries on different bars are ignored. Was there anything else that I may have missed?

                  Is there a way to compare 2 groups of trades in the case when there is more than 1 valid entry condition (as in my previous post)? And also, is it possible to compare the net profit or loss for each group of trades to determine if the next trade OR group of trades should be skipped or taken?

                  Thanks. Look forward to your input

                  Comment


                    #10
                    Hello homey,

                    You have EntriesHandling.UniqueEntries, what is your Entries Per Direction set to?

                    When you are referring to comparing groups of trades, are you looking to do this from within the strategy? If so you could see the following example; otherwise could you please provide more information so I can best assist?


                    I look forward to your reply.
                    Alan P.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Alan P.

                      I have tried setting Entries per direction at 1 and also 5, however, the result is not what I expected. I am looking to get the strategy to enter on each signal as per the indicator.

                      Thanks for sending the link for the Sample PnL strategy. I have had a look at it but it is not quite what I'm after. When comparing groups of trades, I would look to do this from within the strategy.

                      What I want to achieve is to skip the next trade after a winning trade and take the next trade after a losing trade. I have run some tests for serial correlation and believe my strategy exhibits this property. Why I need to compare groups of trades at times is that there can be a few open trades that exit at the same time. So for the purposes of determining if the last 'trade' was a winner or a loser, I want to evaluate the collective net profit or loss on that result to signal whether I would enter or stay out of the next signal.

                      Hope I'm describing what I'm after with clarity. Cheers.

                      Comment


                        #12
                        Hello homey,

                        You could use the following reference sample for an example of how you could write logic to take or not take action based off whether or not the last trade was a winner or loser.


                        Regarding why subsequent trades are not taken, could you please send an email to platformsupport[at]ninjatrader[dot]com with Attn: Alan P in the Subject line. Also within the email please include a link to this thread, and attach the log and trace files for the day in subject which you can find in My Documents>NinjaTrader7>Log and My Documents>NinjaTrader7/Trace folders.

                        I look forward to receiving your email.
                        Last edited by NinjaTrader_AlanP; 06-08-2017, 10:04 AM.
                        Alan P.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by RookieTrader, Today, 09:37 AM
                        3 responses
                        15 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by kulwinder73, Today, 10:31 AM
                        0 responses
                        7 views
                        0 likes
                        Last Post kulwinder73  
                        Started by terofs, Yesterday, 04:18 PM
                        1 response
                        24 views
                        0 likes
                        Last Post terofs
                        by terofs
                         
                        Started by CommonWhale, Today, 09:55 AM
                        1 response
                        4 views
                        0 likes
                        Last Post NinjaTrader_Erick  
                        Started by Gerik, Today, 09:40 AM
                        2 responses
                        8 views
                        0 likes
                        Last Post Gerik
                        by Gerik
                         
                        Working...
                        X