Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trying to Build An Example System

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

    Trying to Build An Example System

    Hello, I´m learning how to use the strategy editor from NT7, but I don´t really know if what I inserted in the editor is actually what I had in mind when the strategy is compiled.
    For example, I tried to write a very simple system just to see if I would get any result out of it, but when I backtest it, the results come blank as if no strategy was actually tested.
    What I tried to write is the following:

    1)If market opens above yesterday´s close, buy 1 contract.
    2)If market opens below yesterday´s close, sell 1 contract.

    From there on, at the close of every bar:

    3) If long and price goes 1 tick below the last bar, sell 2 contracts( meaning "stop and reverse"), otherwise, hold long.
    4) If short and price goes 1 tick above the last bar, buy 2 contracts (meaning "stop and reverse", otherwise, hold short.

    5) At the close of Regular Trading Hours, close any position

    I imagine this must be fairly easy to do it. Can anyone help me?

    #2
    Hello NeoTrader86,

    What do you have in your script so far?

    To export your script do the following:
    1. Click File -> Utilities -> Export NinjaScript
    2. Enter a unique name for the file in the value for 'File name:'
    3. Select the strategy from the objects list on the left -> click the right facing arrow ">" to add the strategy to the export
    4. Click the 'Export' button -> click 'yes' to add any referenced indicators to the export -> click OK to clear the export location message


    By default your exported file will be in the following location:
    • (My) Documents/NinjaTrader 7/bin/Custom/ExportNinjaScript/<export_file_name.zip>


    Below is a link to the help guide on Exporting NinjaScripts.
    http://www.ninjatrader.com/support/h...nt7/export.htm


    So the first conditions should look for the Session Break to be true. If true look for the Close[0] to be greater than (likely you need greater than or equal to one of these) PriorDayOHLC().CurrentClose[0]. If that is also true call EnterLong. A second condition would check the session break and look for Close[0] to be less than PriorDayOHLC().PriorClose[0]. If that is true call EnterShort.

    Then in a third set you would need to check that Close[0] is less than Close[1] (1 for the Bars ago value) + -1 ticks in the offset (Close[1] + -1 * TickSize). Using an offset with -1 ticks would subtract a tick from the Close[1].

    The 4th set would look for the Close to be greater than Close[1] plus 1 tick in the offset (Close[1] + 1 * TickSize).

    Then the Exit on close could exit your position and cancel any trades at the end of the session. (You could also hard code a time to exit all positions and cancel all orders using a time filter with the Time objects)
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you very much! I´ll try to follow your instructions and then I´ll export the file to show it here and see if I got it right! But if you want to see what I did so far... It´s embarassing, because I´m just learning C++ and don´t know anything about programming yet... Here´s the file:
      Attached Files
      Last edited by NeoTrader86; 11-12-2015, 02:14 PM.

      Comment


        #4
        Hello I´m trying to code according to the instructions given, but I couldn´t find the commands to generate this functions: "PriorDayOHLC().PriorClose[0]" and "PriorDayOHLC().CurrentClose[0]". I looked in options of the Conditions Menu and each parameter of them, especially in Price Data, but didn´t find it. I tried this instead for the first and second set:

        Bars.SessionBreak == true;
        Open[] >= Close[1];

        EnterLong(1,"");

        and

        Bars.SessionBreak == true;
        Open[] < Close[1];

        EnterShort(1,"");

        But I don´t know if this will work, because I don´t know if after considering Session Break to be true, with this declarations, the program would automatically consider the close of the last bar of the last day.
        Last edited by NeoTrader86; 11-13-2015, 04:37 AM.

        Comment


          #5
          Hi NeoTrader86,

          The Session break is true on the first bar close of the new session or the first tick of the new session.

          The Misc -> Session Break in the Wizard writes Bars.FirstBarOfSession in the code.
          http://ninjatrader.com/support/helpG...rofsession.htm

          Also, attached is a screenshot on comparing the Close[0] to yesterday's close using the PriorOHLC indicator.
          Attached Files
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            OK...Thank you.... What about the "Exit on close" to close any and all positions or orders at the end of the Regular Trading Hours? Where do I find that?

            Comment


              #7
              Hello NeoTrader86,

              The Exit on close by default is enabled 30 seconds before the close of the session template of the chart or the session template chosen when the strategy was added to the Strategies tab of the Control Center.

              Attached is a screenshot.

              You can also enable/disable this by code and change the amount of seconds.
              http://ninjatrader.com/support/helpG...xitonclose.htm
              http://ninjatrader.com/support/helpG...oseseconds.htm

              The session template cannot be changed by code because this will come from the chart or the strategy setting when adding the strategy.
              Equities that come pre-made in NinjaTrader will have the US Equities RTH template as the default template. If you have new instruments that you have added to the Instrument Manager, be sure these are also using an RTH template.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Ok.. Thank you very much.... Here´s what I did...
                Set 1) Enter on the first tick of the session. If equals to or above the close of the prior session, Go Long 1 contract,
                Set 2) if below, Go Short 1 Contract.
                Then, throughout the whole day:
                Set 3) if Long and there is a Current Ask 1 tick below the previous bar, Sell 2 Contracts. (Stop and Reverse)
                Set 4) in Short and there is a Current Bid 1 tick above the previous bar, Buy 2 Contracts. (Stop and Reverse)

                Then, when I run the backtest, I´ll set it to Exit on close (the default 30 seconds is fine).

                Could you please take a look at the script to see if I got this instructions right in the code?
                Attached Files

                Comment


                  #9
                  Hello NeoTrader86,

                  From your description the code looks correct.

                  Have you tested the code? Does this follow the behavior you would like?
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Yes... Thank you... It gives me some results at least... Before it showed no change in the stats.. but as I look at the graphs where it plots the executions, they´re not happening as they should. Almost never when a high of the previous bar is crossed up it buys and the opposite (when the low of the previous buy is crossed down, it sells). Maybe this is because I used Bid and Ask values as conditions and not the last trade?
                    Last edited by NeoTrader86; 11-13-2015, 01:26 PM.

                    Comment


                      #11
                      Hi NeoTrader86,

                      You can unlock the script and add prints to the code and print the values of the conditions to see what the values are on each bar to see if the trades match the prints.

                      For example:
                      Print(string.Format("{0} | SessionBreak: {1} == true & Open[0]: {2} >= PriorDayOHLC().PriorClose[0]: {3}", Time[0], Bars.SessionBreak, Open[0], PriorDayOHLC().PriorClose[0]));

                      Prints appear in the output window (Tools -> Output Window...)

                      One thing I am noticing is that you did not add/subtract the offset to either the high or the low.
                      Attached Files
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Damn... LOL... This is still a little too much for me... I think the beginning of the session is taken care of... Meaning: It´ll go long or short 1 contract.
                        So, all that´s missing is: how do I set the condition to: IF LONG AND THE PRICE GOES 1 TICK BELOW THE PREVIOUS BAR, SELL 2 CONTRACTS, IF IT DOESN´T, HOLD LONG and WHEN THE CURRENT BAR CLOSES, THE SAME IS VALID FOR IT´S LOW, AND SO ON? (Like a trailing stop and reverse always adjusted to 1 tick below the last bar)

                        Comment


                          #13
                          Hello NeoTrader86,

                          To subtract or add a tick this is done with the offset.

                          In my previous post I included a screenshot of how to set the offset.
                          Did this not work when you tried it?

                          The Print is a basic part of debugging to understand behavior. This would require that you unlock the script, add the print, then compare the output with the prints.
                          Otherwise you may just look at the buys and sells and see if they follow the rules that you would have done manually.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by NinjaTrader_ChelseaB View Post
                            Hello NeoTrader86,

                            To subtract or add a tick this is done with the offset.

                            In my previous post I included a screenshot of how to set the offset.
                            Did this not work when you tried it?
                            I did... but unfortunately, it didn´t work. Every fill is always far away from the areas where I would have manually placed the orders.

                            Comment


                              #15
                              But it´s OK... You are always very helpful to me and I was fooling myself that I could learn how to build strategies without knowing how to program... LOL The Editor sure is helpful, but if one doesn´t know how to program, it´s not possible to use it because you have to have the mindset of a programmer to build the flow of statements... Otherwise, you´ll end up trying to give orders to a computer as if you were giving them to a human being... Which obviously will never work. Bottom line: I have to keep learning how to program and when I know enough, come back...Thank you very much for the attention!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Today, 05:17 AM
                              0 responses
                              41 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              124 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              64 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              41 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              46 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X