NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 11-05-2007, 08:05 AM   #1
jammy page
Junior Member
 
Join Date: Nov 2007
Posts: 10
Thanks: 0
Thanked 0 times in 0 posts
Default 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
jammy page is offline  
Reply With Quote
Old 11-05-2007, 09:08 AM   #2
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
Default

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.
NinjaTrader_Ray is offline  
Reply With Quote
Old 11-05-2007, 02:32 PM   #3
jammy page
Junior Member
 
Join Date: Nov 2007
Posts: 10
Thanks: 0
Thanked 0 times in 0 posts
Default

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
jammy page is offline  
Reply With Quote
Old 11-05-2007, 11:58 PM   #4
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

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.
NinjaTrader_Josh is offline  
Reply With Quote
Old 11-06-2007, 12:13 AM   #5
jammy page
Junior Member
 
Join Date: Nov 2007
Posts: 10
Thanks: 0
Thanked 0 times in 0 posts
Default

Josh,

Yeah great idea, I should have thought of that!

Thanks a lot for the help,

pagey
jammy page is offline  
Reply With Quote
Old 11-06-2007, 01:03 AM   #6
KBJ
Senior Member
 
Join Date: Mar 2007
Location: , Florida, USA
Posts: 663
Thanks: 36
Thanked 7 times in 6 posts
Default

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.
KBJ is offline  
Reply With Quote
Old 11-06-2007, 07:31 AM   #7
jammy page
Junior Member
 
Join Date: Nov 2007
Posts: 10
Thanks: 0
Thanked 0 times in 0 posts
Default

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
jammy page is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling an ATM Strategy Within NinjaScript Jim-Boulder Strategy Development 5 08-26-2010 02:54 AM
Ending Ninjascript strategy jlm0@infionline.net General Programming 50 11-14-2007 10:59 PM
NinjaScript Strategy Wizard crazyhorse2393 Automated Trading 3 10-29-2007 06:47 AM
ATM reverse strategy ali_chambers ATM Strategies (Discretionary Trading) 5 10-08-2007 09:15 AM
Reverse Strategy / Position Torso Automated Trading 2 02-17-2006 04:21 AM


All times are GMT -6. The time now is 04:43 AM.