Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Sell at opening with profit

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

    Sell at opening with profit

    I am pretty newbie, trying to develope my first script, and find problems trying to write:
    " exit long at the firsts open price with profit"
    Any clue?
    Thanks a lot!

    #2
    You can do this by running a check against the PnL of your current position. If it is positive then you can do a ExitLong() or ExitLongLimit().

    To check your unrealized PnL you can use this: GetProfitLoss()
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by Josh View Post
      You can do this by running a check against the PnL of your current position. If it is positive then you can do a ExitLong() or ExitLongLimit().

      To check your unrealized PnL you can use this: GetProfitLoss()
      Thank you very much for your help.
      I did this:
      Code:
      [SIZE=2][FONT=Courier New][COLOR=#008000]// Condition set 2[/COLOR][/FONT][/SIZE]
      [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (Position.GetProfitLoss(Open[[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]], PerformanceUnit.Percent) > [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
      [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
      [SIZE=2][FONT=Courier New]ExitLong([/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2], [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
      [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
      But I am afraid all long positions are exited at the Open, no matter if I get a profit or loss exit...

      Thanks!

      Comment


        #4
        I would use Close[0] instead of Open[0]. This is the case because when the new bar starts, the Close[0] price would reflect the latest change in price. Especially when running with CalculateOnBarClose = false.

        You may need to debug your code a little. Try some prints to see what value you are getting out of GetProfitLoss. This will help you better determine the behavior you are experiencing.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by Josh View Post
          I would use Close[0] instead of Open[0]. This is the case because when the new bar starts, the Close[0] price would reflect the latest change in price. Especially when running with CalculateOnBarClose = false.

          You may need to debug your code a little. Try some prints to see what value you are getting out of GetProfitLoss. This will help you better determine the behavior you are experiencing.
          Thank you very much for your help.
          I started again from zero, and tried to understand how the systrem works, and followed your suggestion of printing the outputs.
          I am using 1 day bars, and try this simple order:
          If today's open value is above yesterday close, then buy.
          I wrote:
          Code:
          if (Open[0] > Close[1])
                      {
            EnterLong(DefaultQuantity, "");
          }
          As you can imagine, it didn't work: the check is ok, but the EnterLong order is excecuted not today after checking the opening but tomorrow.
          Aha! it must be the CalculateOnBarClose, I thought...
          Ok, then I set: CalculateOnBarClose = false
          But the behavior is exactly the same.
          At this point I exactly don't understand the following:
          1. Is there any way to simulate within the bar, instead of "in following bar"?
          2. What exactly "CalculateOnBarClose" means?
          I read the Ninjatrader Help Guide but couldn't get it clear: probably there are some language missunderstandings from my part, sorry.
          Thank you very much for your patience.

          jm

          PS: by the way, I could't find the way to print the bar date (or time) in the output, in order to easilly check the calculations...
          Last edited by eswap0; 06-04-2008, 11:30 AM.

          Comment


            #6
            Josh will follow up on this later today.
            RayNinjaTrader Customer Service

            Comment


              #7
              Hi eswap0,

              The first thing you would need to do is debug your code as per these tips: http://www.ninjatrader-support.com/v...ead.php?t=3418


              I also suspect you may be running into this issue: http://www.ninjatrader-support.com/v...ead.php?t=3170

              Please check the error log in the Control Center for anything fishy.

              In regards to CalculateOnBarClose, historical data will always be assessed as if CalculateOnBarClose = true. This is the nature of backtesting because you cannot look into the future thus you cannot place trades on the same bar since you already know the closing price of that bar. You can only place a trade on the next bar. In realtime it is a different story.

              Please read this article from the Help Guide: http://www.ninjatrader-support.com/H...sBacktest.html

              To print time you can use Time[0].
              Code:
              Print(Time[0] + " Enter Long Here");
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Thank you very much for your help!
                Now everything is clear.
                I assume then that I will never be able to backtest a rule like "buy if today's open is higher than yesterday's close), as the order should be executed in the same day's bar.
                Thanks so much!
                ----------------------------------------
                Edited: I think this is what I need:
                Last edited by eswap0; 06-05-2008, 01:40 PM.

                Comment


                  #9
                  Yes, the example you found will "alleviate" what you need to some degree. It will still not be perfect since you are not going down to the tick level.

                  This is generally why people will go into forward testing their strategy with Market Replays or just simulating on live data after they do some preliminary backtests. These forward tests provide more conclusive results that are more accurate than backtests, but take more time to conduct.
                  Josh P.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by GLFX005, Today, 03:23 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post GLFX005
                  by GLFX005
                   
                  Started by XXtrader, Yesterday, 11:30 PM
                  2 responses
                  11 views
                  0 likes
                  Last Post XXtrader  
                  Started by Waxavi, Today, 02:10 AM
                  0 responses
                  6 views
                  0 likes
                  Last Post Waxavi
                  by Waxavi
                   
                  Started by TradeForge, Today, 02:09 AM
                  0 responses
                  12 views
                  0 likes
                  Last Post TradeForge  
                  Started by Waxavi, Today, 02:00 AM
                  0 responses
                  2 views
                  0 likes
                  Last Post Waxavi
                  by Waxavi
                   
                  Working...
                  X