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

Strategy that allows for a percentage of events

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

    Strategy that allows for a percentage of events

    Is there a way to code a strategy that allows for a percentage of events to be true to start a trade?

    Example:

    If 3 out of the 5 events below are TRUE, open a long trade:

    (ToTime(Times[0][1]) >= 94500)
    (MACD(BarsArray[0], 10, 20, 9).Diff[1] > 0))
    (Stochastics(BarsArray[1], 14, 14, 5).K[1] >= 80)
    (EMA(BarsArray[2], 10)[1] < EMA(BarsArray[2], 10)[0])
    (RSI(BarsArray[0], 14, 1)[0] < 80)

    Thanks in advance.

    #2
    Hello ArmKnuckle,

    Thanks for your post.

    In looking at your conditions, I suspect that you would only want to enter an order if this condition
    (ToTime(Times[0][1]) >= 94500) were true in all cases, plus 2 others?

    If that was the case then you would want to bracket your logic within that condition:

    if ((ToTime(Times[0][1]) >= 94500)
    {
    // all other conditions in here
    }

    One way to accomplish your goal would be to use a counter to add up the number of conditions that are true.

    For example:

    if (ToTime(Times[0][1]) >= 94500)
    {

    int count = 0; // reset counter on each pass through.

    if (MACD(BarsArray[0], 10, 20, 9).Diff[1] > 0)
    count++; // increment the counter

    if (Stochastics(BarsArray[1], 14, 14, 5).K[1] >= 80)
    count++; // increment the counter

    if (EMA(BarsArray[2], 10)[1] < EMA(BarsArray[2], 10)[0])
    count++; // increment the counter

    if (RSI(BarsArray[0], 14, 1)[0] < 80)
    count++; // increment the counter

    if (count >=2)
    {
    // enter order here
    }
    }
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick response.

      If I didn’t want the time to be true in all cases, could please show me what the code would look like with 3 of the 5 events being true?

      You could change the “times” code to something else, if you want. It was just an example.

      Comment


        #4
        Hello ArmKnuckle,

        Thanks for your reply.

        It would be very much the same:

        int count = 0; // reset counter on each pass through.

        if (ToTime(Times[0][1]) >= 94500)
        count++; //increment the counter

        if (MACD(BarsArray[0], 10, 20, 9).Diff[1] > 0)
        count++; // increment the counter

        if (Stochastics(BarsArray[1], 14, 14, 5).K[1] >= 80)
        count++; // increment the counter

        if (EMA(BarsArray[2], 10)[1] < EMA(BarsArray[2], 10)[0])
        count++; // increment the counter

        if (RSI(BarsArray[0], 14, 1)[0] < 80)
        count++; // increment the counter

        if (count >=3)
        {
        // enter order here
        }

        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Here is twist on the original question.

          Can I assign a different worth (count amount) to each event?

          Have some events increment as one, another event increments as two. When I do a count total, I will have a result that reflects this new construct.

          Thanks again.
          Last edited by ArmKnuckle; 01-09-2018, 05:49 AM.

          Comment


            #6
            Hello ArmKnuckle,

            Thanks for your reply.

            Yes, where needed you could simply:

            count++; // increment once
            count++; // increment again

            or

            count += 2; // add two to the count

            Here is a reference: https://www.dotnetperls.com/increment
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              One last clarification:

              Please show me how to write this code in full. (The code includes Boolean logic and a counter.)

              I am trying to understand how to properly use the semicolons, brackets, and the relation of the code to OnBarUpdate.


              Code needed to start a Long trade:

              if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade || CurrentBars[2] < BarsRequiredToTrade)
              return;


              if (BarsInProgress == 0)


              (Stochastics(BarsArray[1], 3, 14, 3).K[1] <= 20
              &&
              Bollinger(BarsArray[2], 2, 20).Upper[1] > 20
              &&
              Bollinger(BarsArray[1], 2, 20).Lower[1] < 10)


              ALSO:

              If 3 out of the 5 events below are TRUE, open the trade:

              (ToTime(Times[0][1]) >= 94500)
              (MACD(BarsArray[0], 10, 20, 9).Diff[1] > 0))
              (Stochastics(BarsArray[1], 14, 14, 5).K[1] >= 80)
              (EMA(BarsArray[2], 10)[1] < EMA(BarsArray[2], 10)[0])
              (RSI(BarsArray[0], 14, 1)[0] < 80)

              Thanks in advance.
              Last edited by ArmKnuckle; 01-12-2018, 05:16 AM.

              Comment


                #8
                Hello ArmKnuckle,

                Thanks for your reply.

                We do not provide complete code writing as a service. If you want someone to code for you we can certainly provide references to 3rd party coders, please let us know if this is what you want.

                I've previously provided code segment samples that have provided solutions for you.

                OnBarUpdate() is called when according to the setting of the Calculate mode. If OnEachTick, then it is called on every incoming tick, if OnPriceChange, then on every price change, if OnBarClose, then once per bar at the close.

                When OnBarUpdate() is called the code that is between the braces {} will be executed:

                protected override void OnBarUpdate()
                {
                // all code between here is executed
                }

                Braces are a C# implementation that allow you to "bracket" a group of code to be executed when the method is called. The same thing is used for the If statements when there are multiple statements to execute when the conditions are true

                if (Conditions)
                {
                // execute all code in here when conditions true;
                }

                You may want to take advantage of the many on-line C# courses or references that are available by searching for C# programming language.
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Does this look correct if I want to:

                  1) Open a LONG TRADE based on the Boolean calculation
                  2) Print the counter in the "Output window"

                  Code:
                  			else if (State == State.Configure)
                  			{
                        			AddDataSeries(BarsPeriodType.Minute, 6);
                  			AddDataSeries(BarsPeriodType.Minute, 15);	
                  			}
                  		}
                  
                  		protected override void OnBarUpdate()
                  		{
                  	     // Checks to ensure all Bars objects contain enough bars before beginning
                  	     // If this is a strategy, use BarsRequiredToTrade instead of BarsRequiredToPlot
                  	     if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade || CurrentBars[2] < BarsRequiredToTrade)
                  	          return;
                  		 
                  		 // Condition set 1
                  			
                  			if ((BarsInProgress == 0)
                  			 
                  			&&
                  			 
                  			(Stochastics(BarsArray[1], 3, 14, 3).K[1] <= 20
                  			&&
                  			Bollinger(BarsArray[2], 2, 20).Upper[1] > 20 
                  			&&
                  			Bollinger(BarsArray[1], 2, 20).Lower[1] < 10))
                  				
                  				{
                  				int count = 0; // reset counter on each pass through.
                  
                  				if (MACD(BarsArray[0], 10, 20, 9).Diff[1] > 0)
                  				count++; // increment the counter
                  
                  				if (Stochastics(BarsArray[1], 14, 14, 5).K[1] >= 80)
                  				count++; // increment the counter
                  
                  				if (EMA(BarsArray[2], 10)[1] < EMA(BarsArray[2], 10)[0])
                  				count++; // increment the counter
                  
                  				if (RSI(BarsArray[0], 14, 1)[0] < 80)
                  				count++; // increment the counter
                  
                  				if (count >=0)
                  				{
                  				Print(Time[0].ToString()+" - Count =   "+count.ToString());
                  				}
                  				}				
                  				
                              {
                  				EnterLong("Long Trade");
                              }
                  Thanks

                  Comment


                    #10
                    Hello ArmKnuckle,

                    Thanks for your post.

                    Taking a quick look it appears that it would place a trade if the preceding conditions are true and will print the count value of the other conditions.
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      I have another question about using counters between trade OPEN and CLOSE.

                      I would like to use a counter to track certain events that only appear after the OPEN to decide which CLOSE code to trigger (based on the counter total.) Should I create/reset the counter to zero immediately after the trade is OPENED?

                      Code:
                      	{
                      		EnterLong(100, "Long Trade");
                      		int count = 0; // Establish counter after OPEN.
                      	}
                      And then should I reset the counter to zero immediately after the trade closes?

                      Code:
                      	{
                      	if (EMA(BarsArray[2], 10)[1] < EMA(BarsArray[1], 10)[0])
                      	count++; // increment the counter
                      
                      	if (count >= 2 && RSI(BarsArray[0], 14, 1)[0] < 80)
                      		{
                      		ExitLong ("Long Trade");
                      		int count = 0; // Reset counter after CLOSE.
                      		}
                      	}
                      Is this this the right thing to do if you only want the counter functioning between OPEN and CLOSE?

                      (I am not sure where to put the counter beginning and reset code.)
                      Last edited by ArmKnuckle; 05-20-2018, 09:33 PM.

                      Comment


                        #12
                        Hello ArmKnuckle,

                        Thanks for your reply.

                        In the previous work with counters, you wanted to enter a trade if at least 3 of 5 possible conditions were true, including a time condition. In that effort, the counters were reset before each pass through the code section where the conditions occurred and for each true condition found the counter was incremented and at the end of the code if the counter was at least 3 then an order was placed.

                        For the exit condition, if you want to do a similar thing, you would want to "bracket" the exit strategy underneath a check that says you are in a position, for a long example:

                        if (Position.MarketPosition == MarketPosition.Long) // only enter when long
                        {
                        int longCount = 0; // declare and reset counter
                        if (some condition)
                        longCount++; // increment counter

                        if (some other condition)
                        longCounter++; // increment counter

                        if (some final condition)
                        longCounter++; // increment counter

                        if (longCounter > 2) // if 2 conditions then exit long position.
                        {
                        ExitLong();
                        }
                        }
                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          Thank you for providing that code. It was a great help!

                          One last thing, could you show me where I should place a print statement so I can see, bar-by-bar what the counter number is?

                          Comment


                            #14
                            Hello ArmKnuckle,

                            Thanks for your reply.

                            for example:

                            if (Position.MarketPosition == MarketPosition.Long) // only enter when long
                            {
                            int longCount = 0; // declare and reset counter
                            if (some condition)
                            longCount++; // increment counter

                            if (some other condition)
                            longCounter++; // increment counter

                            if (some final condition)
                            longCounter++; // increment counter

                            Print ("Time: "+Time[0].TimeOfDay+" Count: "+longCounter);


                            if (longCounter > 2) // if 2 conditions then exit long position.
                            {
                            ExitLong();
                            }
                            }
                            Paul H.NinjaTrader Customer Service

                            Comment


                              #15
                              Thanks for the info on the print statement. It was a great help!

                              It seems the counter is showing a total of events that are true only for the current bar (multi-time frame). It then resets back to zero on every new bar.

                              <screen grab attached>

                              My goal is to get a running count for events that happen after the OPEN. (For example: An event happens four times over the course of the OPEN trade.)

                              Time: 11:33:00 Count: 0
                              Time: 11:36:00 Count: 1 New Event
                              Time: 11:36:00 Count: 1
                              Time: 11:39:00 Count: 1
                              Time: 11:42:00 Count: 1
                              Time: 11:42:00 Count: 1
                              Time: 11:45:00 Count: 2 New Event
                              Time: 11:45:00 Count: 2
                              Time: 11:48:00 Count: 3 New Event
                              Time: 11:48:00 Count: 3
                              Time: 11:51:00 Count: 4 New Event

                              Can you please provide some more wisdom on where to position the counter code so:
                              - The counter is created and set to zero once the trade OPENS
                              - The counter keeps a running count of events that occur during the OPEN trade, and
                              - The counter is not in use if there is no active trade.

                              The need for a running count is a big deal for me...
                              Thanks in advance!
                              Attached Files
                              Last edited by ArmKnuckle; 05-22-2018, 11:50 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by cre8able, Yesterday, 04:16 PM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by cre8able, Yesterday, 04:22 PM
                              1 response
                              13 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by stafe, 04-15-2024, 08:34 PM
                              5 responses
                              28 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by StrongLikeBull, Yesterday, 04:05 PM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by Mestor, 03-10-2023, 01:50 AM
                              14 responses
                              375 views
                              0 likes
                              Last Post z.franck  
                              Working...
                              X