![]() |
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
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Feb 2012
Posts: 178
Thanks: 42
Thanked 13 times in 11 posts
|
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. |
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,420
Thanks: 252
Thanked 981 times in 963 posts
|
Please check into our GetProfitLoss() in NinjaScript for this task - http://www.ninjatrader.com/support/h...profitloss.htm
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Member
Join Date: Sep 2010
Posts: 41
Thanks: 19
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Dec 2009
Location: Netherlands
Posts: 179
Thanks: 15
Thanked 72 times in 51 posts
|
Store GetProfitLoss() into a variable if in position, and readout this variable after trade is closed ?
|
|
|
|
|
The following user says thank you to marcow for this post: |
|
|
|
#5 | |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,420
Thanks: 252
Thanked 981 times in 963 posts
|
Quote:
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
The following user says thank you to NinjaTrader_Bertrand for this post: |
|
|
|
#6 | |
|
Member
Join Date: Sep 2010
Posts: 41
Thanks: 19
Thanked 0 times in 0 posts
|
Quote:
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 |
|
|
|
|
|
|
#7 |
|
Senior Member
Join Date: Dec 2009
Location: Netherlands
Posts: 179
Thanks: 15
Thanked 72 times in 51 posts
|
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.
|
|
|
|
|
The following user says thank you to marcow for this post: |
|
|
|
#8 | |
|
Member
Join Date: Sep 2010
Posts: 41
Thanks: 19
Thanked 0 times in 0 posts
|
Quote:
Many thanks for that hint! Let me check how that works. I will get back to you for reporting results. Best, Rob |
|
|
|
|
|
|
#9 |
|
Senior Member
Join Date: Dec 2009
Location: Netherlands
Posts: 179
Thanks: 15
Thanked 72 times in 51 posts
|
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;
}
}
Marco |
|
|
|
|
The following user says thank you to marcow for this post: |
|
|
|
#10 | |
|
Member
Join Date: Sep 2010
Posts: 41
Thanks: 19
Thanked 0 times in 0 posts
|
Quote:
Many thanks! I will check this out and let you know the results. Best, Rob |
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |