NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 05-10-2012, 04:35 PM   #1
arbuthnot
Senior Member
 
Join Date: Feb 2012
Posts: 178
Thanks: 42
Thanked 13 times in 11 posts
Default Profit/Loss since trade entered

Hi - I'd be very grateful if you could indicate how I can find a way to calculate the profit or loss realized, at every range bar occurring during a trade in progress, since the current trade was entered.

(I'd like to be able to manipulate this mathematically in Ninjascript so I can construct a trailing stop that varies according to profit/loss levels, i.e. one that is not static.)

Thanks in advance for your help.
arbuthnot is offline  
Reply With Quote
Old 05-11-2012, 01:15 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,420
Thanks: 252
Thanked 981 times in 963 posts
Default

Please check into our GetProfitLoss() in NinjaScript for this task - http://www.ninjatrader.com/support/h...profitloss.htm
NinjaTrader_Bertrand is offline  
Reply With Quote
The following 2 users say thank you to NinjaTrader_Bertrand for this post:
Old 07-21-2012, 01:48 PM   #3
Robertp75
Member
 
Join Date: Sep 2010
Posts: 41
Thanks: 19
Thanked 0 times in 0 posts
Default

Dear Bertrand,

Is there any possibility to calculate the REALIZED profit and loss of a single trade immediately after the trade was closed? (e.g. by refering to the execution.ExecutionId or execution.Order.OrderId). I assume GetProfitLoss() is not the right function as this function displays the UNREALIZED profit and loss of a position according to the ninjascript tutorial.

Many thanks.

Robert
Robertp75 is offline  
Reply With Quote
Old 07-21-2012, 08:15 PM   #4
marcow
Senior Member
 
Join Date: Dec 2009
Location: Netherlands
Posts: 179
Thanks: 15
Thanked 72 times in 51 posts
Default

Store GetProfitLoss() into a variable if in position, and readout this variable after trade is closed ?
marcow is offline  
Reply With Quote
The following user says thank you to marcow for this post:
Old 07-23-2012, 02:21 AM   #5
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,420
Thanks: 252
Thanked 981 times in 963 posts
Default

Quote:
Originally Posted by Robertp75 View Post
Dear Bertrand,

Is there any possibility to calculate the REALIZED profit and loss of a single trade immediately after the trade was closed? (e.g. by refering to the execution.ExecutionId or execution.Order.OrderId). I assume GetProfitLoss() is not the right function as this function displays the UNREALIZED profit and loss of a position according to the ninjascript tutorial.

Many thanks.

Robert
You can directly work with the NinjaScript Performance class for this task - http://www.ninjatrader.com/support/h.../nt7/trade.htm
NinjaTrader_Bertrand is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Bertrand for this post:
Old 08-05-2012, 01:03 PM   #6
Robertp75
Member
 
Join Date: Sep 2010
Posts: 41
Thanks: 19
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by marcow View Post
Store GetProfitLoss() into a variable if in position, and readout this variable after trade is closed ?
Hi marcow,

Many thanks for your reply!
According to the conversion at this link:
http://www.ninjatrader.com/support/f...ad.php?t=49774

I cannot believe that your advise works as NinjaTrader_Joydeep wrotes:
"Position.GetProfitLoss will return the unrealized PnL of the strategy position.

If your strategy is flat then it will return 0 (zero). If your strategy is Long or Short then it will return the unrealized PnL.
http://www.ninjatrader.com/support/h...profitloss.htm

If you are getting any other result then please upload a NinjaScript code replicating it."

Cheers!

Rob
Robertp75 is offline  
Reply With Quote
Old 08-05-2012, 03:00 PM   #7
marcow
Senior Member
 
Join Date: Dec 2009
Location: Netherlands
Posts: 179
Thanks: 15
Thanked 72 times in 51 posts
Default

Code:
 
#region Variables
// Wizard generated variables
private double ProfitLoss_LastTrade = 0; 
// User defined variables (add any user defined variables below)
#endregion
///<summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
///</summary>
protectedoverridevoid Initialize()
{
CalculateOnBarClose = true;
}
///<summary>
/// Called on each bar update event (incoming tick)
///</summary>
protectedoverridevoid OnBarUpdate()
{
 
if ( Position.MarketPosition != MarketPosition.Flat ) 
ProfitLoss_LastTrade = Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) ;
 
 
}

ProfitLoss_LastTrade will now hold the latest unrealizedPnL of each closed position.
Assuming that the last unrealizedPnL of an open position becomes realizedPnL once that position is closed.


Marco
Last edited by marcow; 08-05-2012 at 03:16 PM.
marcow is offline  
Reply With Quote
The following user says thank you to marcow for this post:
Old 08-05-2012, 03:32 PM   #8
Robertp75
Member
 
Join Date: Sep 2010
Posts: 41
Thanks: 19
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by marcow View Post
Code:
 
#region Variables
// Wizard generated variables
private double ProfitLoss_LastTrade = 0; 
// User defined variables (add any user defined variables below)
#endregion
///<summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
///</summary>
protectedoverridevoid Initialize()
{
CalculateOnBarClose = true;
}
///<summary>
/// Called on each bar update event (incoming tick)
///</summary>
protectedoverridevoid OnBarUpdate()
{
 
if ( Position.MarketPosition != MarketPosition.Flat ) 
ProfitLoss_LastTrade = Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) ;
 
 
}
ProfitLoss_LastTrade will now hold the latest unrealizedPnL of each closed position.
Assuming that the last unrealizedPnL of an open position becomes realizedPnL once that position is closed.


Marco
Hi Marco,

Many thanks for that hint!
Let me check how that works.
I will get back to you for reporting results.

Best,

Rob
Robertp75 is offline  
Reply With Quote
Old 08-06-2012, 11:41 AM   #9
marcow
Senior Member
 
Join Date: Dec 2009
Location: Netherlands
Posts: 179
Thanks: 15
Thanked 72 times in 51 posts
Default

hello Rob,

after looking through the thread you posted the link for : http://www.ninjatrader.com/support/f...ad.php?t=49774

I realized Joydeep M. came with a very good solution in post#14, which is way better then the one I posted above. It involves using the trades colection class.
please find below a strategy which demonstrates the use of this trade collection class.

Code:
 
#region Variables
// Wizard generated variables
private double ProfitLoss_LastTrade = 0;   // holds profit/loss of last trade
private int temp = 0;                                // temp value 
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
 
// simple EMA crossovers to generate some trades 
if( CrossAbove(EMA(8), EMA(14), 1) )
EnterLong(DefaultQuantity, "");
 
if( CrossBelow(EMA(8), EMA(14), 1) )
EnterShort(DefaultQuantity, "");
 
 
 
if(       Performance.AllTrades.Count >= 1       // wait for first trade
    && Performance.AllTrades.Count > temp   // only execute if there was a new trade 
) 
 {   Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
     ProfitLoss_LastTrade = lastTrade.ProfitCurrency; 
 
     PrintWithTimeStamp("The last trade profit is " + ProfitLoss_LastTrade);
     temp = Performance.AllTrades.Count;
}
 
}
please let me know if this works for you or if you have any questions,

Marco
marcow is offline  
Reply With Quote
The following user says thank you to marcow for this post:
Old 08-06-2012, 05:58 PM   #10
Robertp75
Member
 
Join Date: Sep 2010
Posts: 41
Thanks: 19
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by marcow View Post
hello Rob,

after looking through the thread you posted the link for : http://www.ninjatrader.com/support/f...ad.php?t=49774

I realized Joydeep M. came with a very good solution in post#14, which is way better then the one I posted above. It involves using the trades colection class.
please find below a strategy which demonstrates the use of this trade collection class.

Code:
 
#region Variables
// Wizard generated variables
private double ProfitLoss_LastTrade = 0;   // holds profit/loss of last trade
private int temp = 0;                                // temp value 
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
 
// simple EMA crossovers to generate some trades 
if( CrossAbove(EMA(8), EMA(14), 1) )
EnterLong(DefaultQuantity, "");
 
if( CrossBelow(EMA(8), EMA(14), 1) )
EnterShort(DefaultQuantity, "");
 
 
 
if(       Performance.AllTrades.Count >= 1       // wait for first trade
    && Performance.AllTrades.Count > temp   // only execute if there was a new trade 
) 
 {   Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
     ProfitLoss_LastTrade = lastTrade.ProfitCurrency; 
 
     PrintWithTimeStamp("The last trade profit is " + ProfitLoss_LastTrade);
     temp = Performance.AllTrades.Count;
}
 
}
please let me know if this works for you or if you have any questions,

Marco
Hi Marco,

Many thanks! I will check this out and let you know the results.

Best,

Rob
Robertp75 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
Cumulative Profit/Loss DenMan Strategy Development 0 10-22-2011 03:50 PM
How could i know the profit or loss ? marynja Strategy Development 1 03-16-2011 05:36 AM
SampleIntrabarBacktest - Premataure Exit at close of the day the trade was entered tonyh Strategy Development 7 03-15-2010 02:33 PM
Getting last trade profit or loss xmms1 General Programming 1 01-12-2010 10:40 AM
Using Unrealized Profit for Stop Loss and Profit Target dancorcal Strategy Development 1 12-17-2009 10:12 AM


All times are GMT -6. The time now is 10:05 AM.