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

need to reverse position in NinjaScript strategy

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

    need to reverse position in NinjaScript strategy

    Hi, I'm new to Ninja and have been making steadily slow progress at scripting my first simple strategy, but now I've run into an issue that I would appreciate some thoughts on.

    I'm updating on bar close, and if an MA crossover has occurred then I want to go long or short (and will have to reverse position if not flat). The key is that, in case of reversal, I want reverse the position in the first bar after the crossover (not for example go flat in the bar after the crossover and then in the next bar put on a position in the other direction).

    So, as I'm updating on bar close, I think I've realized that I need to give a command to reverse the position.

    Does someone have an example of implementing a reverse position command in a NinjaScript strategy? I'm aware of the OIF builder, but the documentation on it is sparse and I'm unsure of the following things for example:

    -can I put a reverse command in a NinjaScript or is it only for DLL files, etc.?
    -in the OIF reverse command, account and instrument are mandatory, but I would prefer to not have to specify these as is done with for example enterlong()

    Thanks in advance for any thoughts...

    Jammy

    #2
    For clarification....

    - I am assuming you are referring to a strategy developed in NinjaScript
    - If you are long and your strategy crosses and you wish to reverse you only need to call the EnterShort() method which will take care of closing the long position and entering a short position

    Please see the SampleMACrossOver strategy for how this works.
    RayNinjaTrader Customer Service

    Comment


      #3
      Ray,

      Many thanks for the swift response. This is great if it will close out the existing position and enter the new position in the opposite direction all within one bar. I didn't see anything about this in the documentation for enterlong/entershort, etc, but I guess I should have figured it out.

      Now I just need to figure out how to set a new profit target for the new position based on the day's total realized PnL (I understand that cumprofit returns the realized profits for the strategy as opposed to the day). I gather that the 6.5 beta will have more features along these lines, so I'm looking forward to that.

      Thanks again,

      Jammy

      Comment


        #4
        Hi jammy page,

        Right. If you have an open position already submitting an Enter in the opposite direction will handle the reversal all in one step for you automatically.

        You can probably just save out the CumProfit value at the first bar of the day and then you can carry that value throughout the day. When you want to know the realized profit for the current day simply call CumProfit again and subtract it from the stored value.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Josh,

          Yeah great idea, I should have thought of that!

          Thanks a lot for the help,

          pagey

          Comment


            #6
            It occured to me that it might be useful to make the comparison based on the realized profits for the last however many hours or minutes.

            So, if you want to base your comparison off of realized profit for, say, the last 3 hours, try this...


            Code:
            private DataSeries realized_profits;    // Save profit realized for each bar here.
            
            protected override void Initialize()
            {
               realized_profits =  new DataSeries(this);  // Allocate memory for dataseries.
            }
            
            
            
            protected override void OnBarUpdate()
            {
               if (CurrentBar == 0)                 // Enough bars yet?
               {                                    // No.
                  realized_profits.Set( 0 );        // Initialize first bar value.
                  return;                           // Done.
               }
            
               realized_profits.Set( Performance.AllTrades.Performance.Currency.CumProfit - realized_profits[1] );
                                                    // Save profit realized since prior bar.
            
               TimeSpan three_hours = TimeSpan( 0, 3, 0, 0, 0 ); // Create value that represents 3-hours.
                                                                 // params: (days, hours, mins, secs, millisecs)
            
               double profit_for_last_three_hours = SUM( CurrentBar - Bars.GetBar( DateTime.Now - three_hours ));
                                                    // Add up profits over last 3-hours.
            }
            Caveat: I haven't tested this code. This is just an idea I decided to sketch out quickly. I think it should work; if not please let me know and I'll correct it.

            In the mean time, good luck, and good trading.

            Comment


              #7
              KBJ,

              Thank you very much for the useful code and the explanatory notes. That's really very nice of you.

              I will dig into the code and it will be very useful in various strategies. Thank you very much, it is very instructive.

              Good trading to you,

              Jammy

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by nandhumca, Today, 03:41 PM
              0 responses
              4 views
              0 likes
              Last Post nandhumca  
              Started by The_Sec, Today, 03:37 PM
              0 responses
              3 views
              0 likes
              Last Post The_Sec
              by The_Sec
               
              Started by GwFutures1988, Today, 02:48 PM
              1 response
              5 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Started by ScottWalsh, 04-16-2024, 04:29 PM
              6 responses
              33 views
              0 likes
              Last Post ScottWalsh  
              Started by frankthearm, Today, 09:08 AM
              10 responses
              36 views
              0 likes
              Last Post frankthearm  
              Working...
              X