Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Moving Average Trailing Stop

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

    Moving Average Trailing Stop

    Hello,

    I am not a programmer, but I am looking to automate an exit 2points behind a simple moving average. I did a search but could not find anything that fit what I am looking for. Would anyone be able to provide any information on how I could go about doing so? A simple code or potentially an indicator that would do so? I tried to just use an ATM trailing stop 4,5, and 6 points back from the current price to attempt to compensate for the indicator value but it wasn't to my liking.

    Thanks in advance for any help provided.

    #2
    Hello eminiman414,

    Thank you for your inquiry.

    Here is a simple example that will cause a strategy to exit from a long position if the Close price of the current bar is two points behind a 20 period SMA line:
    Code:
    #region Variables
    private double points = 2;
    private double exitPrice = 0;
    private int smaPeriod = 20;
    #endregion
    
    protected override void OnBarUpdate()
    {
         // entry logic here
    
         exitPrice = SMA(smaPeriod)[0] - points;
    
         // exit logic below
         if (Close[0] == exitPrice) // if the Close equals the exitPrice
         {
              ExitLong(); // exit long position
         }
    }
    Please, remember in this case that the closing price has to exactly equal the exitPrice variable in the example I've provided above. If you wanted to add a cushion, say, exit if the price is equal to or greater than that exitPrice variable, you would use the "=>" operator.

    We do have resources to help you begin creating NinjaScript Strategies/Indicators.

    The best way to begin learning NinjaScript is to use the Strategy Wizard. With the Strategy Wizard you can setup conditions and variables and then see the generated code in the NinjaScript Editor by clicking the View Code button.

    I'm also providing a link to a pre-recorded set of videos 'Strategy Wizard 301' and 'NinjaScript Editor 401' for you to view at your own convenience.
    Strategy Wizard 301
    NinjaScript Editor 401

    There are a few Sample Automated Strategies which come pre-configured in NinjaTrader that you can use as a starting point. These are found under Tools--> Edit NinjaScript--> Strategy. You will see locked strategies where you can see the details of the code, but you will not be able to edit (you can though always create copies you can later edit via right click > Save as)

    We also have some Reference samples online as well as ‘Tips and Tricks’ for both indicators and strategies:
    Click here to see our NinjaScript Reference Samples
    Click here to see our NinjaScript Tips

    These samples can be downloaded, installed and modified from NinjaTrader and hopefully serve as a good base for your custom works.

    Further, the following link is to our help guide with an alphabetical reference list to all supported methods, properties, and objects that are used in NinjaScript.
    Alphabetical Reference

    We also have a few tutorials in our help guide for both Indicators and Strategies.
    Indicator tutorials
    Strategy tutorials

    Please let me know if I can be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Shansen, 08-30-2019, 10:18 PM
    24 responses
    938 views
    0 likes
    Last Post spwizard  
    Started by Max238, Today, 01:28 AM
    0 responses
    3 views
    0 likes
    Last Post Max238
    by Max238
     
    Started by rocketman7, Today, 01:00 AM
    0 responses
    2 views
    0 likes
    Last Post rocketman7  
    Started by wzgy0920, 04-20-2024, 06:09 PM
    2 responses
    27 views
    0 likes
    Last Post wzgy0920  
    Started by wzgy0920, 02-22-2024, 01:11 AM
    5 responses
    32 views
    0 likes
    Last Post wzgy0920  
    Working...
    X