Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Code example needed

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

    Code example needed

    My strategy scales in if current unrealized pnl is negative. I am trying to figure out how to add an additional position to my next entree. So if I'm Long with position size 1 then my second entree is size 2, third is 3, and so on. Code example would be greatly appreciated.

    #2
    relogical, as a courtesy to get started please find a simple example attached, it works with the Position.Quantity and MarketPosition to check what size you currently have and thus which should be used on next scale in. Please ensure to run with the EntryHandling set to .UniqueEntries from the UI, so multiple signals in the same direction could be executed.
    Attached Files
    BertrandNinjaTrader Customer Service

    Comment


      #3
      The only problem with this example is if I want it to make 100 entrees then I would have to ctreate each scale in condition 100 times. How would I do this a different way?

      Comment


        #4
        relogical, right then a more efficient approach would be beneficial - an idea how you could approach this would be in this modified example. We'll hope this will get you going on further custom works in this area.

        For demo purposes, it will take up to 10 entries and then hold overnight only exiting on Friday's at 4PM.
        Attached Files
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Great example! Thank you. Can yo also show me how to space each entree so that each one is at least 20 ticks away from each other? Another words, after it makes Entry 1 do not make Entry 2 if it's less then 20 ticks away.

          Comment


            #6
            relogical, I would just include a tracking variable then you would set to the Close[0] price for example you last had a signal at. As part of the reentry condition when long check that current price needs to be smaller than this variable - your 20 ticks. Once another scale has been done, this tracking variable would again be updated, so you always have the last signal price as reference to check against. If you're flat and have the first entry again of the sequence you would reset it so another cycle could begin.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Could you give me an example code? I am just learning C.

              Comment


                #8
                relogical, here's how this could be addressed in your script's OnBarUpdate() - refPrice is just a double variable to help us with the tracking of the last trade's level.

                Code:
                if (Position.MarketPosition == MarketPosition.Flat && CrossAbove(Close, KeltnerChannel(1.5, 10).Upper, 1))
                {
                	entryCount = 0;
                	entryCount++;
                              EnterLong(1, "Entry"+ entryCount.ToString());
                	refPrice = Close[0];
                }
                
                if (entryCount < maxEntries && Position.MarketPosition == MarketPosition.Long && Close[0] < refPrice - 20 * TickSize)
                {
                	if (CrossAbove(Close, KeltnerChannel(1.5, 10).Lower, 1))
                	{
                	         newSize = entryCount + 1;
                	         entryCount++;
                	         EnterLong(newSize, "Entry" + entryCount.ToString());
                	         refPrice = Close[0];
                              }
                }
                So you see whenever we trade, we update the tracking variable with the Close[0] value (you can store any you like though) and have a filter condition in place for further entries if long that would check that the current close is 20 tick lower than the last reference point. If positions of the sequence are closed out and strategy acting from flat state again > we update the tracking then as well...
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Thank you for the example. I tried this but strategy still enters without observing my 20 tick rule.

                  Comment


                    #10
                    relogical, I would then suggest further debugging the strategy you created from the snippet provided to better understand which part of your conditions evaluates differently as you would expect to allow the entry still to happen. Prints statements would be very helpful in the process -

                    BertrandNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by jclose, Today, 09:37 PM
                    0 responses
                    4 views
                    0 likes
                    Last Post jclose
                    by jclose
                     
                    Started by WeyldFalcon, 08-07-2020, 06:13 AM
                    10 responses
                    1,413 views
                    0 likes
                    Last Post Traderontheroad  
                    Started by firefoxforum12, Today, 08:53 PM
                    0 responses
                    10 views
                    0 likes
                    Last Post firefoxforum12  
                    Started by stafe, Today, 08:34 PM
                    0 responses
                    10 views
                    0 likes
                    Last Post stafe
                    by stafe
                     
                    Started by sastrades, 01-31-2024, 10:19 PM
                    11 responses
                    169 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Working...
                    X