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

How to Calculate SL = last Bar High/Low->Close?

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

    How to Calculate SL = last Bar High/Low->Close?

    Hello,

    i trade since 2 years, and i am learning C# since 2 weeks with an book, but its not for traders :-)
    I have an developed with the Wizzard an Indicator wich works nice.
    But i cant build a strategy out of it to 1 test it in strategy analyser, or to let it run as strategy, because i dont know how to calculate the SL or TP , but i think its very simple :-)

    My SL should be for:
    Long = (last bar bullish candle) the Low [1] till the Close[1]
    Short = (last bar bearish candle) the High [1] till the Close [1]

    SL is normal = Close +- High/Low, but i dont know how to describe it with Ninjascript:-)

    TP1 for Long or Short = The SL of the last candle.
    TP2 = TP1+TP1

    BE of the 2 Trades = after TP1 is reached.

    And i want to enter the Trade after last bar is closed, (signalbar)

    I watched BigMikesAdvanceStrategyVideo, and some others about the Wizzard from Ninjatrader.com, but i dont know how to calculate it in NinjaScript :-) I can not really find a lot of videos from where i can see people coding a different strategy than Xcrossing, which would help me to learn from it.

    Can you help me please? :-)
    Sorry for my English, its not my first language.

    Thanks a lot for your help.
    Last edited by xXTraderXx; 09-24-2015, 07:34 AM.

    #2
    Hello xXTraderXx,

    Thank you for your inquiry and welcome to the NinjaTrader Support Forum!

    Can you please clarify where you would like your stop losses to be placed? I am not exactly clear what you mean by "the Low [1] till the Close[1]" for a long position and "the High [1] till the Close [1]"for a short position.

    Unfortunately, any math logic would not be possible through the Strategy Wizard. Your strategy would need to be manually coded through the code editor to accomplish what you would like.

    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 proving 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

    I look forward to being of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Hallo Zachary,

      thank you very much for your answer! I didnt watched ALL the links so far you wrote me, but i still will do.
      I can send you already my picture of how my trades should be managed, i did it for my own to have clear what i need.

      In short, i want the last bar high (bearish candle), or the last bar low (bullish candle) als SL from the last bar close (signal bar).
      This distance (last bar high/low to close)is also my first TP in the same direction as the last bar was.
      And my second (or more TP) is multiplied by TP1 (TP2 == TP1+TP1). TP3 == TP1+TP1+TP1).

      As i said my indicator works nice. And i would need a xrossing EMA excample with SL of last bar H/L to Close, and TP of this distance of last bar H/L to Close, and that it opens direct after last bar closed. But all "Hello World (XrEMA examples) dont use a specific Stop or TP :-)

      I hope it helps you, so that you can help me too :-)

      Greetings Jasmine
      Attached Files

      Comment


        #4
        Hello,

        Thank you for the reply.

        What you have posted in the image seems like it should be possible using NinjaScript. While we don't have an exact example of what you had asked, there is a sample showing how to use CrossAbove and CrossBelow included with the platform, you can access it by going to Tools -> Edit NInjaScript -> Strategy -> SampleMACrossOver. This can be applied to Indicators, DataSeries or Price values.

        For other items, we have a full collection of samples located at this page: http://ninjatrader.com/support/forum...ead.php?t=3220

        If you need further assistance with a specific part of the question, I would be happy to provide any additional help or syntax where I can.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello Jesse,

          thanks for your answer.
          The links you gave me i studied already before. Also the Xcrossing ema isnt my problem. This i can do now without thinking :-)
          My problem is, how can i calculate the pricedifference of last bar high to close, or low to close, to have this difference (pips or ticks) and to be able to use it as my TP and my SL in wizzard? Or if i have to code it, how has the code to look?
          eg. My last bar (long) was from LOW to CLOSE = 15 pips. this i want to set as my TP and my SL when my signal comes. Can you maybe show me a way how i can calculate this? I tried to find examples over the weekend, but all examples use UserVariables. They dont calculate it.
          I was in hope my question was clear. Sorry for my bad english.

          Greetings Jasmine
          Last edited by xXTraderXx; 09-27-2015, 03:43 PM.

          Comment


            #6
            Hello,

            Thank you for the clarification.

            This would not be directly possible using the wizard for the simple fact that there is no way to add or subtract price values.

            Instead this would need to be manually coded, a simple way to do what you are looking for would be the following:

            If you mean to get the difference between the Close of a bar ago and the High of a bar ago it would be:

            Code:
            double difference = High[1] - Close[1];
            You could then use this to get an amount of Ticks, or a Price to set a StopLoss or ProfitTarget.

            Code:
            double difference = High[1] - Close[1];
            SetStopLoss(CalculationMode.Ticks, difference * TickSize);
            or for a specific price:

            Code:
            double difference = High[1] - Close[1];
            double price = Close[0] - difference * TickSize;
            SetStopLoss(CalculationMode.Price, price);

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thanks a lot Jesse, this exactly was, what i was searching for :-)

              Maybe one more question, because as an beginner, i tried to implement it.
              And it wont work so far :-) But its my problem, i know.
              Its because i only can find tutorials with #region Variables where privat int ... are used.

              For this i would like to ask, because here i wouldnt need a to set a Variable, because it calculates it self.

              How to do have to write it in the code?
              I know when i need SMA, and i want to set it to e.g = 28 later, i have to write private int sMASlowPeriod = 28; in the Variables region.

              But where i have to write this double difference = High[1] - Close[1]; in?
              Do i have to write it in only without "private"?

              I know what "double" means, but not if "difference" is a reserved word from NT?

              So, would this be the following code correct? I really ask, because i cant find tutorials which has a similar code in it.
              ================================================== =====
              #region Variables
              // Wizard generated variables
              private int sMASlowPeriod = 28; // Default setting for SMASlowPeriod
              private int sMAFastPeriod = 14; // Default setting for SMAFastPeriod
              .....
              double difference = High[1] - Close[1];
              or
              double differenceLong = Close[1]-Low[1];
              double differenceShort= High[1]-Cose[1];

              // User defined variables (add any user defined variables below)
              #endregion
              ================================================== ======
              Also because if the last bar was a Long, and i want to go long i have to calculate double difference = Low[1] - Close[1];

              So i have to define 2 different differences, right?
              Can i write to set it correct for long and short?
              double differenceLong = Close[1]-Low[1];
              double differenceShort= High[1]-Cose[1];


              I have now 4 NT-Coding books from Amazon, and a C# book, but all work with Xrossing-EMA example or somthing which uses predefined settings (=28), and its really hard to find some help for this problem, because my calculations doesent need it.

              Would it be possible, to give me a link for an example where it is already used, or an example how to fit your code in an example strategy?
              Or an very good tutorial to learn NinjaScript in different lessons, and not only always the same, means a XrossingEMA example code, which i can find everywhere, but which doesnt helps me to learn to code something different than Xrossing EMA :-)
              I mean i bought a full license of NT7, and i want to learn to code my trading ideas in NT7. It would really help me :-)

              Thanks a lot! Jasmine

              Comment


                #8
                Hello,

                Thank you for the questions.

                For the samples pasted, these can all go right in OnBarUpdate.

                The only time you need to define something as private would be if it is to be used in the Class in any Method meaning that if you define a Variable in the region, it could be used in OnBarUpdate, Initialize, OnMarketData and other methods in the script.

                Instead, these are local variables that would be calculated for each iteration of OnBarUpdate.

                So for the provided samples, they would look similar to the following when used in OnBarUpdate:

                Code:
                protected override void OnBarUpdate()
                {
                        if(CurrentBar < 1) return;
                
                	double difference = High[1] - Close[1];
                	double price = Close[0] - difference * TickSize;
                	SetStopLoss(CalculationMode.Price, price);
                			
                	if(Position.MarketPosition == MarketPosition.Flat)
                	{
                		EnterLong();
                	}
                }
                This would simply get the "difference" which is defined as a local variable of type double with the name of "difference" and then calculate the variable of type double "price" which is used in SetStopLoss(); Finally a simple check if you are currently Flat, if so enter long and the stoploss would be set to the price already so it is ready for the entry.

                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Brevo, Today, 01:45 AM
                0 responses
                4 views
                0 likes
                Last Post Brevo
                by Brevo
                 
                Started by aussugardefender, Today, 01:07 AM
                0 responses
                3 views
                0 likes
                Last Post aussugardefender  
                Started by pvincent, 06-23-2022, 12:53 PM
                14 responses
                241 views
                0 likes
                Last Post Nyman
                by Nyman
                 
                Started by TraderG23, 12-08-2023, 07:56 AM
                9 responses
                384 views
                1 like
                Last Post Gavini
                by Gavini
                 
                Started by oviejo, Today, 12:28 AM
                0 responses
                6 views
                0 likes
                Last Post oviejo
                by oviejo
                 
                Working...
                X