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

¿how can i force a strategy to use a larger period for an sma1 than another sma2?

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

    ¿how can i force a strategy to use a larger period for an sma1 than another sma2?

    good day to everyone, i had not posted to this forum in a while as i had been busy for a number of weeks trying to get some work done on another platform.




    i could use some assistance with a matter that should be very simple for the experts on the nt support staff. i have been doing some tests and evaluations on the nt optimization engine and also trying to evaluate some simple strategies derived from the sample strategies that come with the nt platform.




    when i run an optimization process over the parameters for the sample sma cross over strategy, i like to use the same range of values for both parameters (1 to 50) and often times the best results from the optimization are such that what should be the "fast" sma is calculated for a larger period than the "slow" sma. this means the "fast" sma ends up in fact being the "slow" sma and the strategy tries to counter every trend instead of going with it.





    i want these strategies to only follow trends, not try to counter them, so i would like to force the nt platform to optimize both parameters over the same range but only consider the results for the cases where the period for sma2 ("slow") is in fact larger than the period for sma1 ("fast").




    Code:
    
    protected override void OnBarUpdate()
            {
                if (CurrentBar < BarsRequiredToTrade)
                    return;
    
                if (CrossAbove(smaFast, smaSlow, 1))
                    EnterLong();
                else if (CrossBelow(smaFast, smaSlow, 1))
                    EnterShort();
            }

    my best guess is that the following modification to the line (¿filter?) before the conditions for the entries should work. ¿is this correct?



    if (CurrentBar < BarsRequiredToTrade OR Fast > Slow) return;




    ¿could there be any other ways to do this that worked better for optimization purposes?





    i also have two further questions regarding how i could impose conditions similar to this on a strategy:





    - if i wanted to use more than 2 smas and wanted to use similar conditions to the one mentioned before, ¿would inserting this line before the conditions for the entries work?



    if (CurrentBar < BarsRequiredToTrade OR Sma1 > Sma2 OR Sma2 > Sma3) return;




    - and, ¿how could i do if i wanted to invert these conditions so that instead of disabling the entire strategy if any of the statements was true as in the previous cases, i now wanted the strategy to only be enabled if all the conditions i listed were true simultaneously? something like the following line:



    {only} if (CurrentBar >= BarsRequiredToTrade && Sma1 < Sma2 && Sma2 < Sma3) {then go on with the subsequent commands}




    very well, thanks, regards.
    Last edited by rtwave; 06-08-2018, 02:05 PM.

    #2
    Hello rtwave,

    Thank you for your post.

    You are correct on each of your questions for performing these tasks.

    Are you not seeing these conditions work on your backtests?

    I look forward to your response.

    Comment


      #3
      i haven't tried these kind of conditions on nt yet. these were solutions i implemented on another platform so i wanted to make sure similar logic would work on nt and i could structure it acceptably.




      nt is a more powerful platform so maybe there could be other more effective ways to create filters like the ones i'm interested in. i'm thinking these conditions could be established much earlier in the strategy to completelyto disable in case they weren't false and possibly this could improve the results the optimization engine generates.







      also, i am missing essential code in the final line i asked about; {only} if (CurrentBar >= BarsRequiredToTrade && Sma1 < Sma2 && Sma2 < Sma3) {then go on with the subsequent commands}.




      i now understand it would indeed be possible to structure a filter like this with positive statements, i would like to know how to properly implement this kind of structure. ¿do commands like - only, begin, end - exist in nt? ¿how would this kind of conditions work with the sample sma cross over strategy?
      Last edited by rtwave; 06-08-2018, 03:52 PM.

      Comment


        #4
        Hello rtwave,

        Thank you for your response.

        There is no other means other than through the OnBarUpdate() function for the optimization.

        You would use 'return;' when the condition is true that should not process.

        Code you wish to have occur when a condition is true you would put inside the brackets. Example:
        Code:
        if (MyConditionToTrade)
        {
        // my actions
        }
        Please let me know if you have any questions.

        Comment


          #5
          Originally posted by rtwave View Post
          i haven't tried these kind of conditions on nt yet. these were solutions i implemented on another platform so i wanted to make sure similar logic would work on nt and i could structure it acceptably.




          nt is a more powerful platform so maybe there could be other more effective ways to create filters like the ones i'm interested in. i'm thinking these conditions could be established much earlier in the strategy to completelyto disable in case they weren't false and possibly this could improve the results the optimization engine generates.







          also, i am missing essential code in the final line i asked about; {only} if (CurrentBar >= BarsRequiredToTrade && Sma1 < Sma2 && Sma2 < Sma3) {then go on with the subsequent commands}.




          i now understand it would indeed be possible to structure a filter like this with positive statements, i would like to know how to properly implement this kind of structure. ¿do commands like - only, begin, end - exist in nt? ¿how would this kind of conditions work with the sample sma cross over strategy?
          That is certainly one way to do it, and is simple to write.

          However, if you wanted to, you could always force the Fast period to be smaller than the Slow period by using Math.Max() and Math.Min().

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Barry Milan, Today, 10:35 PM
          1 response
          8 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by WeyldFalcon, 12-10-2020, 06:48 PM
          14 responses
          1,428 views
          0 likes
          Last Post Handclap0241  
          Started by DJ888, Yesterday, 06:09 PM
          2 responses
          9 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by jeronymite, 04-12-2024, 04:26 PM
          3 responses
          40 views
          0 likes
          Last Post jeronymite  
          Started by bill2023, Today, 08:51 AM
          2 responses
          16 views
          0 likes
          Last Post bill2023  
          Working...
          X