Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Tradestation to NT Intrabar Orders

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

    Tradestation to NT Intrabar Orders

    I am writing a simple strategy in TS that fires orders to NT using the DLL interface. I want my strategy to set a range based on the high and low of the first bar of the day, and then fire off an order the first time either side of the range is taken out. It only executes once per day. The order cannot wait for the bar to close. It must execute as soon as the range is violated.

    The following code does this just fine, except TS sends more than one order for the triggering bar when intrabarordering is set to true. This raises two questions regarding this interface:

    How can I get just a single order processed?
    Why can't NTMarketPosition see that the first intrabar order is placed and prevent the second from being generated.





    Code:
    [IntraBarOrderGeneration = TRUE] 
    {DefineDLLFunc: "NTDirect.dll", int, "Setup", lpstr, int;}
     
    inputs: numContracts(2);
    variables:  nHigh(9999999), nLow(-1);
    variables:  success(0);
     
    if  Date = {CurrentDate} 1071012  then begin  
    if Date <> Date[1] then 
    begin
     nHigh = High;
     nLow = Low;
     end;
    if NTConnected(1) then 
     begin
      {go long if price > high  of 1st bar }
      if High > nHigh and NTMarketPosition("") = 0 then
      begin
       success = NTBuyMarket("FirstBar",numContracts); 
       nHigh = 99999999;
       nLow = -1;
       end;
     
     
      if Low < nLow and NTMarketPosition("") = 0  then
       begin
         success = NTSellMarket("FirstBar",numContracts);     
       Print(File("c:\mydata.txt"), "SELL ",CurrentBar, "Date " ,Date, Time, Close, "High: ", High, " LOW: ", Low," NHigh: ", nHigh, " NLOW: ", nLow );
       nHigh = 99999999;
       nLow = -1;
      end;
     
    end;
     
    end;  {currentdate}

    #2
    The reason NTMarketPosition can't stop it is because even though the order is sent you won't have a position right away. In a fast moving market there will be several more ticks that will violate the threshold before you get filled on your first order thus the submission of subsequent orders.

    To workaround this you could work with a simple bool variable. Do something similar to this pseudo-code.

    if thisBool is true
    then
    submit orders and set bool to false
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks for taking a look. I have already done essentially that by setting nHigh to 99999999 - so the next time around, High will not be greater than nHigh so another order should not fire off. It looks as if the new value for the variable is not set until after the bar is closed. Not sure though. If that is the case, does it mean intrabar orders are impossible? There must be a way.

      Comment


        #4
        I am unfamiliar with the way TS executes its code. Your code should work from what I can tell. Perhaps try using some print commands with time outputs to see when exactly the variable values are set.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Looks like each bar is processed four times and the variables set after the fourth. Perhaps its time to learn Ninja code. I would still be interested in hearing from anyone who has a solution though.

          Thanks for the help.

          Comment


            #6
            So, would this be the equivalent NinjaScript code? Just play the breakout of the first bar, and you are done for the day. Or are there any gotcha's I need to worry about.

            Code:
            [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]#region[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#000000] Using declarations[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#000000][/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]#endregion[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#000000] NinjaTrader.Strategy[/COLOR][/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
            [FONT=Courier New][SIZE=2][COLOR=#808080]///[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#808080]<summary>[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#808080]///[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000] First Bar Breakout[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#808080]///[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#808080]</summary>[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][Description([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000]"First Bar Breakout"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2])][/SIZE][/FONT]
            [SIZE=2][FONT=Courier New][Gui.Design.DisplayName([/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800000]"First Bar"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2])][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] FirstBar : Strategy[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]#region[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] Variables[/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#008000]// Wizard generated variables[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] numContracts = [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]2[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]; [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000]// Default setting for NumContracts[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#008000]// User defined variables (add any user defined variables below)[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] nHigh = [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]99999999[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] nLow = -[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] success = [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]bool[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] DoneForTheDay = [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]#endregion[/COLOR][/SIZE][/FONT]
             
             
            [FONT=Courier New][SIZE=2][COLOR=#808080]///[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#808080]<summary>[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#808080]///[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000] This method is used to configure the strategy and is called once before any strategy method is called.[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#808080]///[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#808080]</summary>[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]protected[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] Initialize()[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
            [SIZE=2][FONT=Courier New]CalculateOnBarClose = [/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
             
            [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
            [FONT=Courier New][SIZE=2][COLOR=#808080]///[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#808080]<summary>[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#808080]///[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000] Called on each bar update event (incoming tick)[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#808080]///[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#808080]</summary>[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]protected[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] OnBarUpdate()[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
            [FONT=Courier New][SIZE=2][COLOR=#008000]// Condition set 1[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (Position.MarketPosition == MarketPosition.Flat && ! DoneForTheDay) [/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (Bars.FirstBarOfSession)[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]{ nHigh = High[[/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]];[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]nLow = Low[[/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]]; }[/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2]{ [/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (High[[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]] > nHigh)[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]{ DoneForTheDay = [/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]AtmStrategyCreate(Action.Buy, OrderType.Market,[/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],TimeInForce.Day,GetAtmStrategyUniqueId(),[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000]"FirstBar"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],GetAtmStrategyUniqueId());[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]} [/FONT][/SIZE]
             
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (Low[[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]] < nLow)[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
            [SIZE=2][FONT=Courier New]DoneForTheDay = true[/FONT][/SIZE][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]AtmStrategyCreate(Action.Sell, OrderType.Market,[/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],TimeInForce.Day,GetAtmStrategyUniqueId(),[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000]"FirstBar"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],GetAtmStrategyUniqueId());[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
            [SIZE=2][FONT=Courier New]} [/FONT][/SIZE]
            [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
            [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]#region[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] Properties[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New][Description([/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2])][/SIZE][/FONT]
            [SIZE=2][FONT=Courier New][Category([/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800000]"Parameters"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2])][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] NumContracts[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]get[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] { [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] numContracts; }[/SIZE][/FONT]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]set[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] { numContracts = Math.Max([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2], value); }[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
            [FONT=Courier New][SIZE=2][COLOR=#0000ff]#endregion[/COLOR][/SIZE][/FONT]
            [FONT=Courier New][SIZE=2]}[/SIZE][/FONT]
            [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
            [/COLOR][/SIZE][/FONT]

            Comment


              #7
              Seems all correct to me.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                I am still working on my TS version while learning about Ninja, and I have worked out the TS problems. I have now modified my code to use the ntcommand function so that I can place the order and activate an existing NT strategy to manage it. The strategy name in the example is FirstBar.

                success = ntCommand("PLACE", "Sim101", "SELL", numContracts, "MARKET", 0,0 , "DAY", "", "FirstBarID", "FirstBar", "");


                This shows up in the Ninja log as:
                10/14/2007 9:08:04 AM,ATI,AT, 'PLACE;Sim101;@ER2;SELL;2;MARKET;0;0;DAY;;FirstBar ID;FirstBar;' processing ,

                but it doesnt execute. What exactly is happening here? I have reviewed the documentation and a number of forum entries, but to no avail.

                Comment


                  #9
                  Try passing in "ER 12-07" and see if that makes a difference.
                  RayNinjaTrader Customer Service

                  Comment


                    #10
                    Try passing in "ER2 12-07" and see if that makes a difference.
                    RayNinjaTrader Customer Service

                    Comment


                      #11
                      I tried that but it is not clear how. There is nowhere in the ntCommand syntax to pass an instrument as far as I can see, and in TS the symbol is ER2Z07, which I tried using on the chart, with the same result.

                      When I use NTBuyMarket with @ER in TS, NT can make the conversion to ER2 12-07 automatically. But ntBuyMarket doesn't let me run my strategy, so that is of no use.

                      Your help guide is actually in error on this issue. If you go to the TradeStation Functions page, the syntax shown for NTCommand does not provide for an instrument parameter. If you then click the link in the NTCommand description for "Commands and Valid Parameters", that page shows an Instrument parameter for the PLACE command and says it is Required - which makes sense. But if you include an instrument parameter in the code, between account and action, the DLL throws an error.

                      Please look at the line of code I presented and tell me where in the parameter list an instrument should go.

                      Comment


                        #12
                        If NTBuyMarket() works but NTCommand() does not, then I would ammend the functions to print out the data to see what is different. NTBuyMarket() is just a wrapper for NTCommand(). Printing out the actualy data will help zone in on the what is different/missing.

                        Thanks for the tip on the Help Guide. Will check it out.
                        RayNinjaTrader Customer Service

                        Comment


                          #13
                          Thanks Ray. NTCommand is using GetSymbolName to pass the instrument automatically from the Chart to COMMAND, so that is working fine. After a little trial and error I have determined that if I pass "Sim101" as the account, the order is not placed and it sits in the log as Processing...

                          If I change "Sim101" to "", it works fine and places the order in the default account.

                          Shouldn't "Sim101" work?

                          Comment


                            #14
                            That is interesting glenng. I would assume that "Sim101" should work. Are there any error messages sitting in the log that might give a hint to why it doesn't like that account name?
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Sim101 requires market data. Please make sure you are connected and have a market data stream coming in for the market you want to trade.
                              RayNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              587 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              341 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              103 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              555 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              552 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X