Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

EMA cross

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

    EMA cross

    Below is the code for a simple EMA_cross stop and reverse strategy. It's always in the market and takes positions upon a cross of two EMAs. Is there a way (without hardcoding) to add an input that would move entry/exit price +/- certain amount of ticks from the price I'm getting with this code ?

    protectedoverridevoid Initialize()
    {
    Add(EMA(
    8));
    Add(EMA(
    21));
    CalculateOnBarClose =
    true;
    }
    ///<summary>
    /// Called on each bar update event (incoming tick)
    ///</summary>
    protectedoverridevoid OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(EMA(8), EMA(21), 1))
    {
    ExitShort(
    "", "");
    EnterLong(DefaultQuantity,
    "");
    }
    // Condition set 2
    if (CrossBelow(EMA(8), EMA(21), 1))
    {
    ExitLong(
    "", "");
    EnterShort(DefaultQuantity,
    "");

    #2
    Hello,

    Thanks for the forum post:

    When you say this:

    Is there a way (without hardcoding) to add an input that would move entry/exit price +/- certain amount of ticks from the price I'm getting with this code ?
    To get your strategy to do anything you would need to hardcode it, either with the strategy wizard or NinjaScript.

    TO do what your asking would require a code change, however I'm not sure what exactly you mean, can you please further clarify what your trying to do. Your wanting to use a limit order instead of a market order if I understand you correctly?

    I look forward to assisting you further.
    BrettNinjaTrader Product Management

    Comment


      #3
      By no hardcoding I mean using of strategy wizard itself. I understand that with the code in its current shape I'm getting opening of the next bar (in the backtest). I want to see what would happened if I enetered at the limit price few pips below the opening of the next bar. Testing would obviously have to be conducted with look inside bar feature on

      Comment


        #4
        Hello,

        Right, what I would try first without a code change is too change the parameter named slippage when you start your back test. This may do what you’re looking for without code change. If this isn’t what you are looking for let me know and I can send you what you will need to change to change this in code.

        I look forward to assisting you further.
        BrettNinjaTrader Product Management

        Comment


          #5
          Strategy in its current shape is getting killed by slippage. The reason I'm trying to play around with entries is to see wheter I can squeeze out few more pips on each position and that way cover my slippage. Without slippage I'm getting a nice upward equity curve

          Comment


            #6
            Hello,

            You would need to switch over to EnterLongLimit() in this case. Change out your EnterLong() for this and specify as the entry price as the Low price and you can use an offset of -1 or -2 to subtract 2 points off that value. You may want to store this limit price in a variable however so it stays constant.

            Let me know if I can be of further assistance.
            BrettNinjaTrader Product Management

            Comment


              #7
              Doesn't look like it can be done with just a wizard. Am I right ?

              Comment


                #8
                Hello,

                Yes can be done. You just change your enterlong into enterlong with a limit order/EnterLongLimit in the Do The Following section of the strategy wizard. You will want to use one of the set tabs to set Variable0 to the price you want the limit order to submit too. Then in the setup of EnterLongLimit you will set the price as Variable0.

                Let me know if I can be of further assistance.
                BrettNinjaTrader Product Management

                Comment


                  #9
                  So if I want to go long 3 pips below the opening of the next bar I should put:
                  -enter long position by a limit order
                  -limit price: open
                  -bars ago:-1
                  -offset: -3
                  Is this correct ?

                  Comment


                    #10
                    mateo321, from the concept correct but you could unfortunately not access the next bar's open price as at the time of order submission it would not be known - please try using the close + / - an offset for example as substitute here.

                    Comment


                      #11
                      Should it look like this ? I want to be able to optimize offset value so I created an input (offset)?

                      // Condition set 1
                      if (CrossAbove(EMA(8), EMA(21), 1))
                      {
                      ExitShort(
                      "", "");
                      EnterLongLimit(DefaultQuantity, Close[
                      0] + Offset * TickSize, "");
                      }
                      // Condition set 2
                      if (CrossBelow(EMA(8), EMA(21), 1))
                      {
                      ExitLong(
                      "", "");
                      EnterShortLimit(DefaultQuantity, Close[
                      0] + Offset * TickSize, "");
                      }

                      Comment


                        #12
                        You would use a negative offset for the long, and a positive offset for the short. Assuming that your offset value if limited to positive values, then your long entry would be:

                        Code:
                        EnterLongLimit(DefaultQuantity, Close[0] [COLOR="Red"][B]- [/B][/COLOR]Offset * TickSize, "");
                        while your short entry would be correct as you wrote it.

                        Comment


                          #13
                          Ok, thanks. My strategy works on 5 min chart. How do I obtain 1 min (or better) resolution ? Does Ninja have look inside bar feature ?

                          Comment


                            #14
                            Originally posted by mateo321 View Post
                            Ok, thanks. My strategy works on 5 min chart. How do I obtain 1 min (or better) resolution ? Does Ninja have look inside bar feature ?
                            Not sure that I understand. You mean that you want to use 5-min bars, but take signals off a 1-min chart granularity? Then you need to use a multi-series indicator.

                            If you just want to take act off of 1-min bars, I guess you just need to change your chart period to a 1-min chart.

                            Comment


                              #15
                              Strategy takes signals based on EMA crosses on 5 min chart. With offset set to zero the price I'm getting (in the backtest) is the close of the bar on which the cross has occured. With offset set to lets say to -3 pips I'll still be getting the close of that bar (or opening of the next one because it's the same thing) because alghoritm can give you only one of those four prices (open,high, low, close) and you're getting whichever is closer to the price where your signal fired off.
                              That's why you need to look inside the bar

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              668 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              377 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              110 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              575 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              580 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X