• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to define if an event happened during an open trade

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

    How to define if an event happened during an open trade

    I am looking for guidance in creating some code that references an event during the period of a live trade.

    Below is some basic example code that has one entrance and two exits (else if). One exit requires price analysis in the active trade.

    Exit #1: Stochastic K crosses below D
    and
    Price > 63 at some point after the trade was entered.

    Exit #2: Time >= 1557.

    Code:
    // Condition set 1
      if (CrossAbove(Stochastics(7, 14, 3).K[1], Stochastics(7, 14, 3).D[1]))
      {
         EnterLong(100);
      }
                 
                
    // Condition set 2
      if (CrossBelow(Stochastics(7, 14, 3).K[1], Stochastics(7, 14, 3).D[1])  
         //&&
         //What is the code for "PRICE WAS GREATER THAN 63 AT SOME POINT AFTER THE TRADE WAS ENTERED"
         )
      {
         ExitLong(100);
      }
      else if (ToTime(Times[0][0]) >= 155700)
      {
         ExitLong(100);
    }
    Any help will be greatly appreciated.

    Thanks in advance.

    #2
    Hello ArmKnuckle,

    Thanks for your post.

    For the exit side, you would want to first test to see that you are (in the example), a long position.

    if (Position.MarketPosition == MarketPosition.Long) // only true when long
    {
    if (Close[0] > 63.0)
    {
    ExitLong(100)
    }
    else if (ToTime(Times[0][0]) >= 155700)
    {
    ExitLong(100)
    }
    }

    Alternatively, you could write like:

    if (Position.MarketPosition == MarketPosition.Long && (Close[0] > 63.0 || ToTime(Times[0][0]) >= 155700)))
    {
    ExitLong(100);
    }


    Edit: changed time to match original example
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      That is a start, it is not the full answer I need.

      Exit #1 has two elements that need to be addressed in this "else if" close. Exit #2 is time only.

      To clarify:


      EXIT #1:

      Element A) Stochastic K crosses below D
      (This can only happen at a single bar.)

      AND

      Element B) Price > 63 at some point after the trade was entered.
      (This can in any bar once the trade is opened. 1st bar, 8th bar, 46th bar, etc.
      I am looking for away to analyze if this particular event has occurred after the open.)



      EXIT #2:

      Element A) Time >= 1557.


      Any continued help will be greatly appreciated.

      Thanks in advance.

      Comment


        #4
        Hello ArmKnuckle,

        Thanks for your reply and clarification.

        if (Position.MarketPosition == MarketPosition.Long) // only true when long
        {
        if (CrossBelow(Stochastics(7, 14, 3).K[1], Stochastics(7, 14, 3).D[1] && Close[0] > 63.0)
        {
        ExitLong(100)
        }
        else if (ToTime(Times[0][0]) >= 155700)
        {
        ExitLong(100)
        }
        }

        Alternatively, you could write like:

        if (Position.MarketPosition == MarketPosition.Long && ((CrossBelow(Stochastics(7, 14, 3).K[1], Stochastics(7, 14, 3).D[1]Close[0] > 63.0) || ToTime(Times[0][0]) >= 155700)))
        {
        ExitLong(100);
        }
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          That only references the price > 63 for the most recent bar.

          It needs to check if the price is greater that 63 for all bars that exist while the trade is OPEN.

          Comment


            #6
            Hello ArmKnuckle,

            Thanks for your reply.

            So your conditions for exit are

            1) CrossBelow(Stochastics(7, 14, 3).K[1], Stochastics(7, 14, 3).D[1]

            OR

            2) Close[0] > 63.0

            OR

            3)ToTime(Times[0][0]) >= 155700

            Do I understand this correctly?
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              My conditions for exit are:

              1A) CrossBelow(Stochastics(7, 14, 3).K[1], Stochastics(7, 14, 3).D[1] (ONE BAR ONLY)

              AND

              1B) Price > 63 at some point after the trade was entered. (NOT JUST ONE BAR)

              OR

              2)ToTime(Times[0][0]) >= 155700
              Last edited by ArmKnuckle; 02-06-2017, 03:11 PM.

              Comment


                #8
                Hello ArmKnuckle,

                Thanks for your reply.

                If the Cross condition occurs and price is 63.0 when the cross occurs then you do not exit?

                if the cross condition occurs and price is 63.0 when the cross occurs you do not exit but if price later is greater than 63, you exit? This would be like needing condition A to always have occured and condition B only if A has happened. Does that match what you are looking for?
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Let's say the cross conditions happens 100 bars after the trade opened.

                  The price at 100 bars after the trade opened is $3.95.

                  But, since the price was $64.00 at 16 bars after the trade opened, we can close on the cross.

                  If the price NEVER exceeds $63.00, we wait until the time is 1557 to close.

                  I cannot be much clearer than I have been in previous posts #1, 3, 5, and 7.
                  Last edited by ArmKnuckle; 02-06-2017, 03:24 PM.

                  Comment


                    #10
                    Hello ArmKnuckle,

                    Thanks for your reply.

                    I appreciate your patience in trying to clarify what I am not following.

                    Based on your latest post, my understanding is that exceeding the price level of 63.0 is the critical condition. Please confirm that this statement matches your needs: If price exceeds 63.0 then exit if there is a cross after price exceeds 63.0, otherwise exit on time.

                    I will be responding to your e-mail as well.
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      If price exceeds 63.0 then exit if there is a cross after price exceeds 63.0, otherwise exit on time.

                      The above sentence is correct.

                      Thanks for the help.

                      Comment


                        #12
                        Hello ArmKnuckle,

                        Thanks for your reply and confirmation.

                        The text "If price exceeds 63.0 then exit if there is a cross after price exceeds 63.0, otherwise exit on time." can be coded with the use of a bool variable that is either true or false. In this case, for example, we can use a bool named priceExceeded just to make it readable. The bool would need to be created and initialized false in the #region variables, example:

                        private bool priceExceeded = false;

                        In the OnBarUpdate() area you would have the code that checks for a long position and then test for price > 63.0 which will then set the bool priceExceeded true which is then used in the cross condition test, if neither condition comes true then the time element will exit the long position. Here is the example using your code example:

                        f (Position.MarketPosition == MarketPosition.Long) // only true when long
                        {
                        if (Close[0] > 63.0)
                        {
                        priceExceeded = true; // set bool/flag to indicate exit when cross
                        }
                        if (CrossBelow(Stochastics(7, 14, 3).K[1], Stochastics(7, 14, 3).D[1] && priceExceeded)
                        {
                        ExitLong(100)
                        }
                        if (ToTime(Times[0][0]) >= 155700)
                        {
                        ExitLong(100)
                        }
                        }

                        Later in your code, you will need this example code to reset the bool:

                        if (Position.MarketPosition == MarketPosition.Flat)
                        {
                        priceExceeded = false; // reset for next use
                        }
                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          Thank you for the help.
                          Last edited by ArmKnuckle; 02-21-2017, 02:33 AM.

                          Comment


                            #14
                            Hello ArmKnuckle,

                            Thanks for your post.

                            My error, in NT8 there is not a specific defined #region variables (although you can add one).

                            Here is an example of an indicator with several private variables defined under the public class name:

                            Code:
                            namespace NinjaTrader.NinjaScript.Indicators
                            {
                            	public class BSTVolume : Indicator
                            	{
                            		private double	buys 		= 0;
                            		private double	sells 		= 0;
                            		private double	ratio		= 2.0;
                            		private bool 	showTotal 	= true;
                            		private bool 	showMarker	= true;
                            		private int		activeBar;	
                            
                            		
                            		protected override void OnStateChange()
                            Paul H.NinjaTrader Customer Service

                            Comment


                              #15
                              I have an addition to the original question.


                              If price exceeds 63.0 ONCE, then exit if there is a cross after price exceeds 63.0.

                              otherwise,
                              if price exceeds 63.0 MORE THAN ONCE, then exit on time.

                              Basically, how do I create a counter to track the number of events? The number in the counter can then be used to trigger trade choices.

                              Thanks again for the help.
                              Last edited by ArmKnuckle; 03-08-2017, 08:54 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by ajay8000, Yesterday, 11:31 PM
                              3 responses
                              11 views
                              0 likes
                              Last Post NinjaTrader_Marco  
                              Started by simjs, Yesterday, 11:23 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post simjs
                              by simjs
                               
                              Started by akuntysh, Yesterday, 10:41 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post akuntysh  
                              Started by Wolf1, Yesterday, 08:26 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post Wolf1
                              by Wolf1
                               
                              Started by pparmley, Yesterday, 04:30 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post pparmley  
                              Working...
                              X