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 07-21-2009, 03:30 AM   #1
MasterSepp
Senior Member
 
Join Date: Apr 2009
Posts: 195
Thanks: 0
Thanked 0 times in 0 posts
Default Profit target depending on last bar

Hi there,

Got a little problem while programming my strategy, concerning the Profit targets. I want to make them dependent of the last bar. So, let's say, the profit target of a position should always be on the Open of the last bar. I have really no idea, how I should get this.
Can anyone help me?

Thanks in advance

Sepp
MasterSepp is offline  
Reply With Quote
Old 07-21-2009, 04:14 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,377
Thanks: 252
Thanked 966 times in 949 posts
Default

Sepp, you would need to adjust your Target value on each bar then, this sample should help - http://www.ninjatrader-support2.com/...ead.php?t=3222
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 07-21-2009, 07:07 AM   #3
MasterSepp
Senior Member
 
Join Date: Apr 2009
Posts: 195
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks for the link, but it didn't help me as much as I hoped... My problem is, that for example I go long, when the bar was red (and vice versa). Now, I want to take the profit at the Open of the bar before. I tried it with SetProfitTarget(CalculationMode.Price, Open[1]) but it doesn't work
Can you help me again?

Thanks in advance

Sepp
MasterSepp is offline  
Reply With Quote
Old 07-21-2009, 07:24 AM   #4
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

Sepp,

Please provide the complete code snippet you are using. Thank you.
NinjaTrader_Josh is offline  
Reply With Quote
Old 07-21-2009, 07:38 AM   #5
MasterSepp
Senior Member
 
Join Date: Apr 2009
Posts: 195
Thanks: 0
Thanked 0 times in 0 posts
Default

Here it is:

Quote:

protected
overridevoid Initialize()
{
SetProfitTarget(
"", CalculationMode.Price, Open[1]);
CalculateOnBarClose =
false;
}
///<summary>
/// Called on each bar update event (incoming tick)
///</summary>
protectedoverridevoid OnBarUpdate()
{
// Condition set 1
if (Close[0] > Open[0])
{
EnterShort(DefaultQuantity,
"");
}
// Condition set 2
if (Close[0] < Open[0])
{
EnterLong(DefaultQuantity,
"");
}
// Condition set 3
if (BarsSinceEntry() > 0)
{
ExitLong(
"", "");
ExitShort(
"", "");
}
}
Just saw that the problem is in my opinion, that it exits only on the open of the next bar and not as soon as the profit is reached.

Regards,

Sepp
MasterSepp is offline  
Reply With Quote
Old 07-21-2009, 07:48 AM   #6
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

Sepp,

You can't put it in Initialize(). Open[1] is not recognized there. You need to put it in OnBarUpdate().
NinjaTrader_Josh is offline  
Reply With Quote
Old 07-21-2009, 01:21 PM   #7
MasterSepp
Senior Member
 
Join Date: Apr 2009
Posts: 195
Thanks: 0
Thanked 0 times in 0 posts
Default

Thank you very much, now it works better but still not as I want. The main problem now is, that once the Profit target is triggered, he sells only on the next bar.
I've put the SetProfitTarget now in a if (Market.Position == Long/Short) {SetProfitTarget(...)}, because he didn't do anything when I just wrote it into the code alone. Is there another way to set the target as soon as the position is opened?

Regards,

Sepp
MasterSepp is offline  
Reply With Quote
Old 07-21-2009, 01:29 PM   #8
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

Sepp,

Not following you. When profit target is reached it will execute immediately. If it is not reached then nothing will happen.

You should be calling your Set() method before you call your Enter() method.
NinjaTrader_Josh is offline  
Reply With Quote
Old 07-22-2009, 08:51 AM   #9
MasterSepp
Senior Member
 
Join Date: Apr 2009
Posts: 195
Thanks: 0
Thanked 0 times in 0 posts
Default

I still don't get it... Tried yesterday the whole evening... Here my code again:

Again my strategy: if a bar closes green, I want to sell short on the open of the next bar. The profit target for this example is the open of the last bar (the green bar e.g.) + 2% of the Close of the same bar. If the target shoulnd't be reached until the current bar closes, the position shall be closed.
Can you help me?

Thanks in advance again

Sepp
MasterSepp is offline  
Reply With Quote
Old 07-22-2009, 08:52 AM   #10
MasterSepp
Senior Member
 
Join Date: Apr 2009
Posts: 195
Thanks: 0
Thanked 0 times in 0 posts
Default

Here's the code, cause he was too long for the other post:

Quote:
protectedoverridevoid OnBarUpdate()
{
if (Position.MarketPosition == MarketPosition.Long)
{
SetProfitTarget(
"", CalculationMode.Price, Open[1]+0.02*Close[1]);
}
if (Position.MarketPosition == MarketPosition.Short)
{
SetProfitTarget(
"", CalculationMode.Price, Open[1]-0.02*Close[1]);
}

if (Close[0] > Open[0])
{
EnterShort(DefaultQuantity,
"");
}

if (Close[0] < Open[0])
{
EnterLong(DefaultQuantity,
"");
}

if (BarsSinceEntry() > 0)
{
ExitLong(
"", "");
ExitShort(
"", "");
}
MasterSepp is offline  
Reply With Quote
Old 07-22-2009, 09:04 AM   #11
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,377
Thanks: 252
Thanked 966 times in 949 posts
Default

MasterSepp, please try printing the values you set as targets, so they actually return what you expect. You also want to reset those values when you're flat, as mentioned in this sample - http://www.ninjatrader-support2.com/...ead.php?t=3222
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 07-23-2009, 07:57 AM   #12
MasterSepp
Senior Member
 
Join Date: Apr 2009
Posts: 195
Thanks: 0
Thanked 0 times in 0 posts
Default

First, I don't really understand, what you mean with "printing the values ... so they return what you expect"?
Second, I tried like in the PriceModification-Sample, but when I enter anything other than a number in the variables (for example Open[1]), he can't compile it correctly.
Can you help me again please?

Thanks in advance

Sepp
MasterSepp is offline  
Reply With Quote
Old 07-23-2009, 08:11 AM   #13
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,377
Thanks: 252
Thanked 966 times in 949 posts
Default

Sepp, I refererred to printing the value for the stop loss to loss to the output window with the Print command - http://www.ninjatrader-support2.com/...ead.php?t=3418

This way you can easily check the calculated values on each bar and if they turn out to be what you expect.

What compile error do you receive in the Price modification sample?
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 07-23-2009, 09:54 AM   #14
MasterSepp
Senior Member
 
Join Date: Apr 2009
Posts: 195
Thanks: 0
Thanked 0 times in 0 posts
Default

I don't think, that I have to use the print command, because I think to know what the system does My 2 main problems are just that the position is closed when it's hold longer than one bar. Second problem: the "complicated" profit target. My NT is in German, so I can tell you the exact problem, but the Codes are CS0120 and CS0266. Here the excerpt of the code:

#region Variables
privateint profittargetprice = Open[1];
#endregion
///<summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
///</summary>
protectedoverridevoid Initialize()
{
SetProfitTarget(CalculationMode.Price,profittarget price);

CalculateOnBarClose =
true;
}
MasterSepp is offline  
Reply With Quote
Old 07-23-2009, 10:05 AM   #15
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,377
Thanks: 252
Thanked 966 times in 949 posts
Default

On the compile error, please try SetProfitTarget
Code:
(CalculationMode.Price,profittargetprice);
For a long trade in a rising market, this would get filled immediately I think, as you place a marketable limit order with this as the last price is most likely higher than the open then.
NinjaTrader_Bertrand 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
Profit Target tooearly Automated Trading 1 02-05-2009 05:49 AM
Profit target in a strategy ju1234 Strategy Development 9 12-15-2008 07:22 AM
Profit target Charlie ATM Strategies (Discretionary Trading) 4 11-19-2008 10:32 AM
Profit Target? Ninja B Automated Trading 4 08-04-2008 12:09 PM
Profit target maninjapan Strategy Development 11 05-19-2008 07:41 AM


All times are GMT -6. The time now is 08:00 AM.