Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with my first strategy

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

    Help with my first strategy

    I’m trying to code my first strategy and I’m stuck on a couple of issues.

    Issue #1 – all of my signals line up and it gets in a short, hard target hits then it takes another one max loss. So my questions is, how do I code it to only take a another short after a long signal has printed.

    Issue #2 – when it enters a trade I want to put a limit order in on a SMA. But the problem I’m having is that it moves the order with the SMA. How can this be fixed?

    #2
    Hello EvilHong,

    Welcome to the NinjaTrader forums here. For item #1 this could be set by the sequences you define, commonly using either bool flags or UserVariables. That link will take you to a sample that uses uservariables to create sequences.

    For item #2 if you want to fix the price of your order to the indicator value at the time of entry, you could work with BarsSinceEntry() in the index value or "bars ago" value for the indicator.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      another question

      I have another question

      I’m trying to write a simpler code than before just to get it to work but I’m running into the same problem. When 2 SMA’s cross I want to set the target on the 40 SMA but it get’s out at the same price I just bought at.


      Any ideas?

      protectedoverridevoid OnOrderUpdate(IOrder order){
      DrawText(order.Token,
      "--",0,order.AvgFillPrice,Color.Red);
      }
      protectedoverridevoid OnBarUpdate()
      {
      // Condition set 1
      EntryHandling = EntryHandling.UniqueEntries;
      if (Position.MarketPosition != MarketPosition.Flat) return;
      if (CrossBelow(SMA(Fast), SMA(Slow), 1))
      {

      SetProfitTarget(SMA(
      40)[0]);
      EnterLong(DefaultQuantity,
      "");

      }
      Attached Files

      Comment


        #4
        It's likely a marketable limit order.

        To protect long position with SetProfitTarget, the strategy will submit a Sell Limit order.

        If SMA(40) is below the last traded price, then this order is considered marketable and you are filled right away.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Thanks Ryan,

          is there any way to fix that?

          Comment


            #6
            Yes, you could make checks against all these values before setting the value of your target. What is happening essentially is you're using the wrong order type -- using a limit order when a stop may be more appropriate.

            There's unfortunately not one answer here as it depends what you want to do. First step is to break down what outcome you want to see happen here. Like in cases where your strategy would submit this marketable limit order - would you want a stop loss order at your SMA(40) value? Would you want your target order at static value?
            Ryan M.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            116 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            61 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            40 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            43 views
            0 likes
            Last Post TheRealMorford  
            Started by Mindset, 02-28-2026, 06:16 AM
            0 responses
            82 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Working...
            X