Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Translating a v6.0 strategy to 7

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

    Translating a v6.0 strategy to 7

    Ive just installed 7, and attempting to modify an old strategy to run in 7.
    Ive added
    Code:
    Unmanaged = true
    to the Initialize section.

    First step was to try and translate the entry orders as per the example,
    From this:
    Code:
    EnterLongLimit(this.bid - this.offsetTicks * TickSize)
    to this:
    Code:
    entryOrder = SubmitOrder(0,OrderAction.Buy,OrderType.Limit,1,this.bid - this.offsetTicks * TickSize,0,"","Long Limit");
    However, when I attempted to Compile I get a CS0103 error, the name 'entryOrder' does not exist in the current context. Have I made a simple error in this line of code, or is translating this a little more complex than I think....

    Thanks

    #2
    It seems you're missing the 'private IOrder entryOrder' in your variables section of the script.

    Comment


      #3
      That fixed that problem, thanks.

      Created a new one though......

      I tried to run the strategy to see if the entries work as expected, but when I go to run the strategy the window where I usually adjust the setup and parameters is just blank....

      Comment


        #4
        maninjapan,

        Can you please try restart NinjaTrader and let us know your results. If you're seeing the same thing, let us know where you're running the strategy from and if there are any error messages in the log tab of the control center.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Yeah, Im getting an error in the log. 'Set Profit Target' method cant be called on unmanaged strategies.

          Comment


            #6
            Actually are there any strategies that use the unmanaged setting that I can follow as an example? Had a look, but wasnt able to find any....

            Comment


              #7
              There's unfortunately no complete unmanged sample available, the Set methods would not work here as there's no signal tracking by NT done to avoid having to deal with the Internal Order Handling Rules - you can SubmitOrder, ChangeOrder or CancelOrder in this environment.

              Comment


                #8
                ok, well I seemed to have got through that part without too much trouble, which brings me to the next problem. It keeps sending orders in. Here is the code as it currently stands, do I just need to add an extra condition to the first if statement?
                // Market Position information
                int quantity = Position.Quantity;
                MarketPosition marketPosition = Position.MarketPosition;
                this.MyPrint(marketPosition + " " + quantity.ToString());

                if (marketPosition == MarketPosition.Flat)
                {
                #region No Position


                int now = ToTime(DateTime.Now);


                int diff = now - this.lastTime;

                if (diff >= this.waitSecond || diff < 0)
                {

                this.bid = GetCurrentBid();
                this.ask = GetCurrentAsk();


                if (this.bid == this.ask) return;


                this.lastTime = now;
                }

                this.MyPrint("bid = " + this.bid + ", ask = " + this.ask);

                // Buy

                entryOrder = SubmitOrder(0,OrderAction.Buy,OrderType.Limit,1,th is.bid - this.offsetTicks * TickSize,0,"","Long Limit");

                // Sell

                entryOrder = SubmitOrder(0,OrderAction.SellShort,OrderType.Limi t,1,this.ask + this.offsetTicks * TickSize,0,"","Short Limit");

                #endregion

                Comment


                  #9
                  There's no signal tracking as such the familiar EntriesPerDirection / EntryHandling properties are not applicable here - you should only submit an entry if the associated IOrder object is null, the 'empty' it later as your order reaches a terminal state.



                  Code:
                   
                  protected override void OnBarUpdate()
                  {
                       // Entry condition
                       if (Close[0] > SMA(20)[0] && entryOrder == null)
                            entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Limit, 1, GetCurrentBid(), 0, "", "Long Limit");
                  }

                  Comment


                    #10
                    ok, so by changing the initial if statement to the following, I was able to get it to stay with one order per side. Now the next problem. It should reposition the orders every N seconds to the bid/ask +/- X ticks. Im guessing I need to use change order.... Ive added an if statement down the bottom, albeit quite randomly, it doesnt recalculate though. And I dont know how to call both the buy and sell order.

                    Code:
                    if (entryOrder == null)
                       {
                        #region No Position
                      
                        
                        int now = ToTime(DateTime.Now);
                        
                        
                        int diff = now - this.lastTime;
                        
                        if (diff >= this.waitSecond || diff < 0)
                        {    
                         
                         this.bid = GetCurrentBid();
                         this.ask = GetCurrentAsk();
                         
                       
                         if (this.bid == this.ask) return;
                         
                       
                         this.lastTime = now;
                        }
                        
                        this.MyPrint("bid = " + this.bid + ", ask = " + this.ask);
                        
                        // Buy
                      
                        entryOrder = SubmitOrder(0,OrderAction.Buy,OrderType.Limit,1,this.bid - this.offsetTicks * TickSize,0,"","Long Limit");
                       
                        // Sell
                      
                        entryOrder = SubmitOrder(0,OrderAction.SellShort,OrderType.Limit,1,this.ask + this.offsetTicks * TickSize,0,"","Short Limit");
                        
                        if (now >= this.lastTime + waitSecond)
                        {
                         ChangeOrder(entryOrder,1,this.bid - this.offsetTicks * TickSize,0);
                         
                        }
                        
                        #endregion

                    Comment


                      #11
                      maninjapan,

                      The issue is because you are using the same IOrder object for both orders. You have two separate distinct orders in play here and you should create two separate IOrder objects to accommodate both orders. Otherwise when you assign entryOrder a second time you lose the ability to keep track of the first order you just sent.

                      For your if-statement on time. I suggest you print each individual component and run that evaluation by hand to see if it is even going into that if-statement branch.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks, got the orders part fixed up (at least the initial buy/sell limit orders. Its still seems to be ignoring that if statement to change the orders though. This is turning out to be a lot more difficult than I envisioned, was such a simple little system in version 6....
                        atempted to put the changeorder above the initial order and incorporate an if else statement. Still got the original orders, but no changes. Will have a look at printing everything

                        Comment


                          #13
                          Yes, please add prints to the changeOrder section to see if your conditions to changeOrder triggers as you would expect.

                          Comment


                            #14
                            ok, well I figured out that problem. I needed to move it outside the main if statement.
                            I can get the orders to enter and change as expected now.
                            Next step is to get the exit orders working.
                            Ive tried the following, but the second 'else' statement is giving me errors.
                            I've just tried wrapping the entry positions in another if statement but not quite working as I expected
                            Code:
                            if (Position.AvgPrice == 0);
                            {  //entry rules here
                            }
                            else
                            {//exit rules here
                            }
                            Here is the full code so far. All the brackets match, But Im getting an error 'Invalid expression term 'else'.

                            Code:
                            if (Position.AvgPrice == 0);
                               {
                                #region No Position
                                if (entryBuyOrder == null )
                                {
                                 
                              
                                
                                 int now = ToTime(DateTime.Now);
                                
                                
                                 int diff = now - this.lastTime;
                                
                                 if (diff >= this.waitSecond || diff < 0)
                                 {    
                                 
                                  this.bid = GetCurrentBid();
                                  this.ask = GetCurrentAsk();
                                 
                               
                                  if (this.bid == this.ask) return;
                                 
                               
                                  this.lastTime = now;
                                  this.Print("bid = " + this.bid + ", ask = " + this.ask);
                                 }
                                
                                
                                 // Buy
                              
                                 entryBuyOrder = SubmitOrder(0,OrderAction.Buy,OrderType.Limit,1,this.bid - this.offsetTicks * TickSize,0,"","Long Limit");
                                
                                 // Sell
                              
                                 entrySellOrder = SubmitOrder(0,OrderAction.SellShort,OrderType.Limit,1,this.ask + this.offsetTicks * TickSize,0,"","Short Limit"); 
                                }
                                
                                 else
                                
                                {
                                
                                
                                 int now = ToTime(DateTime.Now);
                                
                                
                                 int diff = now - this.lastTime;
                                
                                 if (diff >= this.waitSecond || diff < 0)
                                 {    
                                 
                                  this.bid = GetCurrentBid();
                                  this.ask = GetCurrentAsk();
                                 
                               
                                  if (this.bid == this.ask) return;
                                 
                               
                                  this.lastTime = now;
                                  this.Print(Position.AvgPrice);
                                  ChangeOrder(entryBuyOrder,1,this.bid - this.offsetTicks * TickSize,0);
                                  ChangeOrder(entrySellOrder,1,this.ask + this.offsetTicks * TickSize,0);
                                 }
                                
                                
                                }
                               
                               
                               #endregion
                               }
                               
                               else
                               {
                                //exit rules here
                               }

                            Comment


                              #15
                              Hello maninjapan,

                              You have an unneccessary semicolon at the end of your first if block.

                              if (Position.AvgPrice == 0);
                              Ryan M.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              649 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              370 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              576 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X