Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Attempt at renko trail stop

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

    Attempt at renko trail stop

    I am very new to Ninja Script and not a programmer. I have tried to build a simple trailing stop strategy that would trigger trades and exits at points of bar close only (not intrabar like normal stops etc.). Predictably, I am running into some problems and have included the code shown below. Any suggestions would be greatly appreciated. Thanks.

    Code:
     
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Enter the description of your strategy here
    /// </summary>
    [Description("Auto adjusted stops. ProfTgt1BE=level Stop reset to BE. ProfTgt2= level Stop reset to Stop2.")]
    public class demaRenkoTrlStp : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int dEMA1 = 1; // Default setting for DEMA1
    private int dEMA2 = 1; // Default setting for DEMA2
    private int trailStop = 1; // Default setting for ProfTgt1BE
    private int maxPriceLong = 0;
    private int maxPriceShort = 500;
    // User defined variables (add any user defined variables below)
    #endregion
    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    Add(DEMA(DEMA1));
    Add(DEMA(DEMA2));
    
    
    CalculateOnBarClose = true;
    }
    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Resets the stop loss to the original value when all positions are closed
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    maxPriceLong = 0;
    maxPriceShort = 500;
    }
    
    // If a long position is open, begin calculation for trail stop
    else if (Position.MarketPosition == MarketPosition.Long)
    {
    // Update maxPrice
    if (Close[0] > maxPriceLong)
    {
    maxPriceLong = Math.Max(maxPriceLong, Close[0]);
    } 
    
    // Once the close gets to or below trail stop, exit
    if (Close[0] <= maxPriceLong - trailStop * TickSize)
    {
    ExitLong(DefaultQuantity,"") ;
    }
    } 
    
    // If a short position is open, begin calculation for trail stop exit
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    // Update maxPrice
    if (Close[0] < maxPriceShort)
    {
    maxPriceShort = Math.Min(Close[0], maxPriceShort);
    }
    
    // Once the clsoe gets above position trail stop, exit
    if (Close[0] >= maxPriceShort + trailStop * TickSize)
    {
    ExitShort(DefaultQuantity, "");
    }
    } 
    
    // Condition set 1
    if (CrossAbove(DEMA(DEMA1), DEMA(DEMA2), 1))
    {
    EnterLong(DefaultQuantity, "RTS");
    }
    // Condition set 2
    if (CrossBelow(DEMA(DEMA1), DEMA(DEMA2), 1))
    {
    EnterShort(DefaultQuantity, "RTS");
    }
    }
    #region Properties
    [Description("")]
    [GridCategory("Parameters")]
    public int DEMA1
    {
    get { return dEMA1; }
    set { dEMA1 = Math.Max(1, value); }
    }
    [Description("")]
    [GridCategory("Parameters")]
    public int DEMA2
    {
    get { return dEMA2; }
    set { dEMA2 = Math.Max(1, value); }
    }
    [Description("")]
    [GridCategory("Parameters")]
    public int TrailStop
    {
    get { return trailStop; }
    set { trailStop = Math.Max(1, value); }
    } 
    
    
    #endregion
    }
    }

    #2
    billr, what issues are you seeing exactly with this script? If the stops are not picked up at all, I would suggest using those calls -


    Code:
     
    ExitLong(DefaultQuantity,"LongExit", "RTS") ;
     
    ExitShort(DefaultQuantity,"ShortExit", "RTS") ;

    Comment


      #3
      I just noticed that I failed to include the programming errors. Sorry about that.

      The issues I am having at this point are simply trying to figure out what the CS codes are refering to. I have apparently implied a conversion of an 'int' to a 'string' but I don't know what to do about it. I have read the notes etc. but cannot figure out my syntax error. Thanks.


      Click image for larger version

Name:	programmingerrors.JPG
Views:	1
Size:	66.2 KB
ID:	858038
      Last edited by billr; 08-19-2010, 11:12 AM.

      Comment


        #4
        Please doubleclick on the error to be taken directly to the offensive code portion, then use Intellisense to guide you what the expected parameters are -

        Comment


          #5
          Bertrand, that is where I get into trouble. I am simply guessing what to do when I see the message. For example, one of my problem spots is in the following line:

          maxPriceLong = Math.Max(mathPriceLong, Close[0]);

          The red item, Math, is what is called out. The bullet shown when holding the cursor over it shows: "classSystem.Math". What do I do with that?

          Comment


            #6
            Another and possibly more confusing message relates to this line:
            ExitLong(DefaultyQuantity, "") ;

            Again the red highlight is the problem identified by Intellisense. The message bubble for this one is "IOrderStrategyBase.ExitLong() (+5 overloads)". I don't know why the IOrder is suggested and have no idea what the 'overload' means.
            Sorry for the lack of knowledge on my end....

            Comment


              #7
              billr, correct this given you the class description - what you want to do is typing in Math.Max followed by the open brace ( - this will give you the parameters the compiler expects for this overload, to switch to a different overload use the arrows keys.

              Comment


                #8
                Thanks Bertrand, but I am obviously in over my head. I have no idea what you are talking about. I don't know what you mean by 'open brace' and I don't know about overload and therefore wouldnt know how or why to change it.
                Where should I read about all this?

                Comment


                  #9
                  You would find this info explained in the 'Method Descriptions and Signatures' in this link - http://www.ninjatrader-support.com/H...ellisense.html

                  You just type in your class / method and then add the opening brace (Math.Max( ) - Instellisense then shows you what kind of parameters it would expect, for example if a value is an int, string or double.

                  Comment


                    #10
                    Bertrand, I appreciate your input. Unfortunately, I am simply too novice to understand your references. Your referral pages seem to go from very basic and straight forward within the Ninja help guide to the SoftSteel which is hyper-esoteric and I get lost in the short-hand writing style of programmers. Plus the subject is covered so broadly I don't think it is doing me much good at this point.

                    I have actually successfully written a few strategies and indicators so far, but I posted here because I am simply stumped with a very specific problem. I know this is very basic for you and the answers you post surely make plenty of sense for some, but from my perspective it seems equivalent to me asking for directions to a good restaurant and in response I am handed a phone book.

                    Please don't take this as anything but constructive, but I feel like my questions are being answered with clues and hints written in the very language I am trying to decipher. Everything eludes to the general topic of my error codes, but nothing I can identify helps me connect the dots. I understand there is a no-programming line you cannot cross, so if the answer has to be 'go hire a programmer', no problem, just let me know and I will not press any further.

                    Thanks.
                    Last edited by billr; 08-20-2010, 12:59 AM.

                    Comment


                      #11
                      Bertrand, I re-read my post and wanted to reiterate that I do appreciate you attempts to help me resolve this matter. I am just frustrated that my knowledge is so limited that I can't even understand your answers.

                      Comment


                        #12
                        billr, no worries - please remember you're dealing with a 'real' programming language here and thus to receive the benefits of its underlying power and flexibility getting used to the syntax and style may take a bit more time and study as you intitially may have estimated.

                        If you're looking for a 'shortcut' to a working customized solution the consultants may be the best way to go - you can always plan on studying their final code later to improve your own skills.

                        If you like to continue working this out on your own, perhaps our indicator tutorials and the wizard are the best places to start -





                        The wizard will let you create strategies via a point and click approach, while you constantly have the option to view the underlying C# / NinjaScript source code it generated, this will offer a solid path to get familiar with the syntax used.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        633 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        364 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        105 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        567 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        568 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X