Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Simple trailing stop from EMA of the Low

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

    Simple trailing stop from EMA of the Low

    I am transferring this code over from Amibroker (AB), so it's in C++. Recall, all the code in an AB indicator is run through about (default) every 10 seconds, so all of the code in an indicator is processed that way. (let's skip the theory about trading ticks - I work with minute bars!)


    There are two parameters:


    int emalen; //values e.g. 2-20
    double delta; //values e.g. 0-10


    The first thing that is done in the AB code is 3 arrays (myema, trail, trailstop) are initialized by being totally filled starting with an EMA of the Low with a lookback length of emalen. The commands are:



    //Initialize:

    Code:
    myema=EMA(L, emalen);  
    emastop=myema-delta; 
    trail=emastop;
    Then finally, there is a loop:
    Code:
    for( i = emalen+1; i < BarCount-1; i++ )
    {
        if(myema[i]> myema[i-1])
          trail[i]= emastop[i];
        if(myema[i]<= myema[i-1])
          trail[i]=trail[i-1];
      }
    My conversion of the above for NT8 is the following:

    Code:
    protectedoverridevoid OnBarUpdate()
    {
      
         if (CurrentBar<emalen+1)
         return;
      
         for( int i = emalen+1; i < Count-1; i++ )
         {
            doublevalue = EMA(Low, emalen)[Count-(Count-i)+1];
            myema[i]=value;   
            emastop[i]= emalow[i]-delta;
            trail[i]= emastop[i];
         }
      
         for( int i = emalen+1; i < Count-1; i++ )
         {
            if(myema[i]>myema[i-1])
               trail[i]= emastop[i];
            if(myema[i]<= myema[i-1])
               trail[i]=trail[i-1];
          }
    }
    Questions:
    1. Within OnBarUpdate, can I do math over all bars and fill the arrays using lookback lengths, or is OnBarUpdate essentially a loop which loops through the bars? If that’s true do I need the loops to do this?
    2. Within the second loop for NT8 code, do the [i] and [i-1] from AB need to be changed to [0] and [1] to pick off the ith and ith-1 elements, or is it ok as is?
    3. If emalen=10, I am not sure when I get the look back EMA for bar 10, whether my EMA(Low, emalen)[Count-(Count-i)+1] function in NT8 is actually getting the 10-period EMA for the 10th bar (the previous 10 bars?
    Last edited by pel11; 06-20-2018, 11:06 AM.

    #2
    Hello pel11,

    You can loop through all the bars on the very first OBU. To demonstrate this point, you could add the following to the top of OnBarUpdate, and look at the output window. On every OBU the output for number of bars is the same. For how to loop this, you can see a great example uploaded to the forum at the following link, see line 148.



    Regarding your second question, it would be best to add print statements to answer this question as I can’t give you the thumbs up on whether it’s okay, or I’d suggest doing a web search for C# for loops.

    I’ve provided a link to a youtube video which covers an example of using prints to understand behavior:
    Dive into manipulating C# code from within an unlocked NinjaScript strategy using the NinjaScript Editor.NinjaTrader 7 is an award winning end to end online ...


    I’ve provided a link covering debugging which you may find helpful.
    Debugging: http://ninjatrader.com/support/forum...ead.php?t=3418

    Looking at the EMA indicator in the editor, it looks like the EMA is going to be calculated after the first bar, whereas on current bar 0 its just set to the input series point.

    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Thanks - RESOLVED.


      I simply removed all the loops and just used [0] and [1] for the C++ [i] and [i-1] and let OBU fill in the array elements during its looping.



      Does this sound correct? It looks correct on the indicator.

      Comment


        #4
        Hello pel11,

        That might be the most straight forward way to accomplish what you're looking to do.

        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by CortexZenUSA, Today, 12:53 AM
        0 responses
        1 view
        0 likes
        Last Post CortexZenUSA  
        Started by CortexZenUSA, Today, 12:46 AM
        0 responses
        1 view
        0 likes
        Last Post CortexZenUSA  
        Started by usazencortex, Today, 12:43 AM
        0 responses
        5 views
        0 likes
        Last Post usazencortex  
        Started by sidlercom80, 10-28-2023, 08:49 AM
        168 responses
        2,265 views
        0 likes
        Last Post sidlercom80  
        Started by Barry Milan, Yesterday, 10:35 PM
        3 responses
        11 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Working...
        X