Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Function to wait for next consecutive crossover?

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

    Function to wait for next consecutive crossover?

    I seem to be having issues implementing a strategy which waits for the consecutive time that an indicator crosses over. Is there a Ninjascript function that does that? For example, let's say I want to use the two consecutive crossovers of the stochastic indicator as a buy/sell signal... I am using multiple "counter" variables like this and it works right now, but it seems messy:

    Code:
    // Some sell condition triggers Variable0 = 0;
    // Some buy condition triggers Variable0 = 1;
    
    // Sell signal is triggered, wait for first crossover
    if (Variable0 == 0 && CrossBelow(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1)) {
    	Variable1 = 1;
    }
    
    // Buy signal is triggered, wait for first crossover
    if (Variable1 == 1 && CrossAbove(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1)) {
    	Variable1 = 1;
    }
    			
    if (Variable1 == 1 && Variable0 == 0 && CrossAbove(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1)) {
    	Variable2 = 1;
    }
    
    if (Variable1 == 1 && Variable0 == 1 && CrossBelow(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1) && (Time[0].DayOfWeek != DayOfWeek.Monday)) {
    	Variable2 = 1;
    }
    
    // Wait for next crossover to sell
    if (Variable1 == 1 && Variable2 == 1 && Variable0 == 0 && CrossBelow(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1)) {
    	EnterShort(1, "sell");
    	Variable0 = 0;
    	Variable1 = 0;
    	Variable2 = 0;
    }
    
    // Wait for next crossover to buy
    if (Variable1 == 1 && Variable2 == 1 && Variable0 == 1 && CrossBelow(Stochastics(7, 14, 3).K, Stochastics(7, 14, 3).D, 1)) {
    	EnterShort(1, "buy");
    	Variable0 = 0;
    	Variable1 = 0;
    	Variable2 = 0;
    }
    Thanks!

    #2
    Hello,

    There is not a current NinjaScript method that would do this for you. The only way to accomplish this is by using a counter or a flag and ask your conditions to look for that value to arm the strategy and wait for the next condition to complete.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Got it! Thanks a lot Matt. I really wish to extend my sincerest gratitude to you and the rest of the support team; thanks to you guys, my strategy development has been going on well and I intend to upgrade to the multi-broker version soon (barely a month after getting the single-broker version). Have a great day!

      Comment


        #4
        Happy to help and we welcome your kind words! I'll be sure to share your comments to the rest of the team.

        Have a great day!
        MatthewNinjaTrader Product Management

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by WalterSkinner, Today, 01:39 PM
        0 responses
        2 views
        0 likes
        Last Post WalterSkinner  
        Started by babouin77, Yesterday, 06:04 PM
        1 response
        17 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by Daddy_Trader21, Yesterday, 05:04 PM
        3 responses
        29 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by ernest93, Today, 01:08 PM
        0 responses
        11 views
        1 like
        Last Post ernest93  
        Started by Unsuitable, Yesterday, 02:54 PM
        1 response
        24 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X