Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simple coding problem

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

    Simple coding problem

    Hi
    I have a simple question about coding
    I want to enter the buy trade when candle number one is green, that's why I wrote this simple piece of code

    if ((Close[1] > Open[1])
    && (Position.MarketPosition == MarketPosition.Flat))
    {
    EnterLong(Convert.ToInt32(Contract), "");
    }​

    But when I look at the chart, sometimes it is entered buy trade when candle number one is red.
    Am I missing something or is there a problem with My Ninja Trader?​
    If im wrong please correct me

    Thanks

    #2
    What exactly do you mean by the 'number one candle'?

    Comment


      #3
      Hello FarzadHA,

      We would need more details to be able to assist here. Are you seeing a problem with the condition in some way? Have you tried adding a print inside your condition to see when it becomes true?

      A print like the following would help to see why the condition was true:

      Print(Time[0] + " " + Close[1] + " > " + Open[1]);

      This would tell you on which bar the condition was true and the values the script had seen.

      Comment


        #4
        Hi bltdavid
        I mean bar number 1 , previous bar before the current bar

        Comment


          #5
          Originally posted by FarzadHA View Post
          Hi
          I have a simple question about coding
          I want to enter the buy trade when candle number one is green, that's why I wrote this simple piece of code

          if ((Close[1] > Open[1])
          && (Position.MarketPosition == MarketPosition.Flat))
          {
          EnterLong(Convert.ToInt32(Contract), "");
          }​

          But when I look at the chart, sometimes it is entered buy trade when candle number one is red.
          Am I missing something or is there a problem with My Ninja Trader?​
          If im wrong please correct me

          Thanks
          If you are updating on bar close, change the one's to zeros.
          RJay
          NinjaTrader Ecosystem Vendor - Innovative Trading Solutions

          Comment


            #6
            Hi Jesse
            I made this on the Strategy Builder and i implanted the code you mentioned
            I will send 3 screenshot of my codes , chart and ninja script output , i hope it can clear my situation
            I dont know much about coding but i think script output numbers are fine but in chart i believe those 3 order that i marked should not be taken
            Is there anything wrong with my ninja or i made a mistake in my codes?
            Attached Files

            Comment


              #7
              Hello FarzadHA,

              In the Strategy Builder you can check CurrentBar is 0 and then save the Close and Open to variables to use anytime later.

              In an unlocked script you can use CurrentBar as the barsAgo index to get the values from the first bar.

              if (Close[CurrentBar] > Open[CurrentBar])
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Thanks ChelseaB , Problem solved

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Today, 05:17 AM
                0 responses
                23 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                120 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                63 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
                45 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X