Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position Open as Point of Reference

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

    Position Open as Point of Reference

    Hi, I have a strategy where after other logic is taken into account, Long positions will be taken while the EMA(5) is above the EMA(20) and Shorts will be taken while the EMA(5) is below the EMA(20). All of that logic is coded and works; however, this is will normally produce several signals, especially in a trend. I want to do the following:

    1. If a Long position is taken during the trading session, another Long should only be taken if the EMA(5) crosses below and back above the EMA(20).

    2. If a Short position is taken during the trading session, another Short should only be taken if the EMA(5) crosses above and back below the EMA(20).

    The idea is that the first signal in the trend is usually the most consistent with my results and I want to ignore later signals unless the above happens.

    Can I pick a point in a trading session (a Position opening in this case) and code logic that says if "this" has happened since "this particular point", then "do this"?

    #2
    Hello manten808,

    Thanks for your post and welcome to the forums!

    Yes, you can certainly code as you have written. Here is a brief example using bools I called doitOnceLong and doitOnceShort (declared as true initially)

    Code:
    if (Crossabove (EMA(5), EMA(20), 1) && your other conditions && doitOnceLong)
    {
    [COLOR="Green"]// order entry here[/COLOR]
    doitOnceLong = false; [COLOR="green"]// set so we don't do this again until crossbelow[/COLOR]
    doitOnceShort = true; [COLOR="green"]// since we cross above we can now go short when we cross below[/COLOR]
    }
    
    
    if (CrossBelow (EMA(5), EMA(20), 1) && your other conditions && doitOnceShort)
    {
    [COLOR="green"]// order entry here[/COLOR]
    doitOnceLong = true; [COLOR="green"]// since we have crossed below, we can now go long when cross above[/COLOR]
    doitOnceShort = false;[COLOR="green"] // set so we don't do this again until crossabove[/COLOR]
    }

    Comment


      #3
      Thanks Paul, and that's kinda the direction I want to go in. However, after thinking more about what you wrote, I think I'm looking more for the following:

      I want to evaluate the following statement since the initial Long position is taken
      if (MACD is positive && EMA(5) cross below EMA(20) && EMA(5) cross above EMA(20))
      {
      EnterLong():
      }

      I can code the rest using the Bool statements you suggested, but I think its the "since initial Long" I'm stuck on. How can I identify the point at which a Long position is taken and then evaluate that period to see if the crosses occur during that time? If the next entry signal is given 9 bars after the initial Long position is taken, I want to evaluate that 9 bar period only and add that to the overall condition statements.

      Comment


        #4
        Hello manten808,

        Thanks for your reply.

        Your intent is not quite clear to me. How could the ema 5 cross above and below at the same time? Are you saying that while in a long position long you want to enter another long contract?

        Perhaps you might want to post a chart showing what you are trying to do. (use the go advanced option below, then manage attachments).

        Comment


          #5
          Sure, this is just a reference, but assumes the entry signals are given starting after 11:00AM.

          EMA(5) is the Green Line
          EMA(20) is the Red Line

          The entry signal would be the following

          11:30AM, 12:40PM, 14:40PM where the candle is closing below the EMA(5)

          The 11:30AM signal would be valid. The 12:40PM signal would NOT be because the EMA(5) has not crossed below and back above EMA(20). The 14:40PM signal would be valid because EMA(5) has crossed below and then back above EMA(20).

          Make sense?
          Attached Files

          Comment


            #6
            Hello manten808,

            Thanks for your reply.

            I think I was getting hung up on the crossover as being your entry condition but in review I think you are wanting to use that as your "reset". The code provided earlier will still help you with that as long as you incorporate the bools into your entry conditions.

            So when you enter a position you set the one bool false and the other direction bool true. Using the true/false states you can prevent any further same side entries until the "reset" of the crossover has occurred and is where you would flip the bool states.

            Comment


              #7
              Yeah, that makes sense. One last question.

              What is the best way to record the time a position is taken if I want to use the value later in a condition statement?

              Comment


                #8
                Hello manten808,

                Thanks for your reply.

                If you are not looking for very precise timing a quick way is to save the bar number of the bar when you placed the entry order. For example int enterBar = CurrentBar; // save the entry bar

                You can get the bar time from Time[CurrentBar-enterBar]. Gets the time of the bars-ago bar.


                You may also want to review: http://ninjatrader.com/support/helpG...t7/?totime.htm

                Comment


                  #9
                  Awesome!! Thanks Paul, that info should definitely help.

                  Comment


                    #10
                    Hello manten808,

                    Thanks for your feedback.

                    I was thinking you might also want to look at: http://ninjatrader.com/support/helpG...sinceentry.htm and its brother: http://ninjatrader.com/support/helpG...ssinceexit.htm

                    Comment


                      #11
                      Yeah, those may also come in handy. Thanks again.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      656 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      371 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      109 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      574 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      579 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X