Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Sma crossing

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

    Sma crossing

    I'm looking to find code for entering long on each bar close above the fast ma when the fast is above the slow.

    Also please clarify how to exit a position after consecutive up closes.

    #2
    Hello G-Force,

    Thank you for your inquiry.

    Here is simple code that will check if the close is above a fast SMA (let's say a period of 20) and the fast SMA is above a slow SMA (let's say a period of 100) and enter long:
    Code:
    if (Close[0] > SMA(20)[0] && SMA(20)[0] > SMA(100)[0])
         EnterLong();
    For more information about the Close DataSeries, SMA() method call, and EnterLong() please take a look at the following links below:




    To close a position after consecutive up closes, what you can do is establish a counter that will keep track of consecutive up closes. If there is a down close before your consecutive condition is met, you can reset the counter.

    Here is an example for five consecutive up closes:
    Code:
    #region Variables
    private int counter = 0; // create the counter
    #endregion
    
    protected override void OnBarUpdate()
    {
         if (Close[0] > Close[1])
              counter++; // if the current close is greater than the previous, increment the counter
         else
              counter = 0; // reset the counter back to 0 because the current close isn't greater than the previous
    
         if (counter == 5)
              ExitLong(); // if there have been five consecutive up closes, exit long position
    }
    For more information about ExitLong(), please take a look at this link: http://ninjatrader.com/support/helpG.../?exitlong.htm

    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by terofs, Yesterday, 04:18 PM
    1 response
    21 views
    0 likes
    Last Post terofs
    by terofs
     
    Started by CommonWhale, Today, 09:55 AM
    1 response
    3 views
    0 likes
    Last Post NinjaTrader_Erick  
    Started by Gerik, Today, 09:40 AM
    2 responses
    7 views
    0 likes
    Last Post Gerik
    by Gerik
     
    Started by RookieTrader, Today, 09:37 AM
    2 responses
    13 views
    0 likes
    Last Post RookieTrader  
    Started by alifarahani, Today, 09:40 AM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Working...
    X