![]() |
|
|||||||
| General Programming General NinjaScript programming questions. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Junior Member
Join Date: Nov 2007
Posts: 10
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
|
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.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Junior Member
Join Date: Nov 2007
Posts: 10
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#4 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
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
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Junior Member
Join Date: Nov 2007
Posts: 10
Thanks: 0
Thanked 0 times in 0 posts
|
Josh,
Yeah great idea, I should have thought of that! Thanks a lot for the help, pagey |
|
|
|
|
|
#6 |
|
Senior Member
Join Date: Mar 2007
Location: , Florida, USA
Posts: 663
Thanks: 36
Thanked 7 times in 6 posts
|
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.
}
In the mean time, good luck, and good trading. |
|
|
|
|
|
#7 |
|
Junior Member
Join Date: Nov 2007
Posts: 10
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |