View Full Version : Ending Ninjascript strategy
jlm0@infionline.net
10-23-2007, 03:12 PM
How can I check in a Ninjascript strategy, to see if a certain profit level has been reached, and then have the strategy stop?
NinjaTrader_Ray
10-23-2007, 04:32 PM
We have seriously enhanced the area of accessing performance data in a strategy with our upcoming NT 6.5. What you want to do can be accomodated in this release. We expect beta next week sometime.
jlm0@infionline.net
10-24-2007, 09:36 AM
Thanks Ray,
Can I test Beta version, and be notifyed when available?
Jim
NinjaTrader_Ray
10-24-2007, 12:06 PM
Yes, we will provide public access to the beta software when available.
jlm0@infionline.net
10-30-2007, 09:17 AM
is this beta version on target to be released this week?
NinjaTrader_Ray
10-30-2007, 09:38 AM
Yes, that is what we are shooting for.
dendy
11-04-2007, 06:37 AM
This will be a serious enhancement. Can you tell us how to access it? Let's say we want to measure the current P/L in ticks, the max adverse excursion in ticks and the max favorable excursion in ticks and set each equal to a user variable. What would be the code to call the P/L this bar, MAE and the MFE?
Thanks,
dendy.
NinjaTrader_Ray
11-04-2007, 10:22 AM
When the beta is out, it will come with documentation that will answer your questions.
dendy
11-05-2007, 06:20 AM
Thanks Ray,
Will the Beta version be announced just like one of the regular versions (on program start-up)? Or will it be announced only on the website somewhere.
Thanks,
dendy.
NinjaTrader_Ray
11-05-2007, 06:55 AM
It will be announced in the 6.5 Beta section of this forum to start.
dendy
11-05-2007, 07:04 AM
I see.
Thanks,
dendy.
jlm0@infionline.net
11-07-2007, 07:29 AM
I have 6.5 installed and am back to my original question:
How can I check in a Ninjascript strategy, to see if a certain profit level has been reached, and then have the strategy stop?
NinjaTrader_Ray
11-07-2007, 08:03 AM
Please see the Help Guide section Strategy Performance Methods. We will post reference samples in this area in the coming weeks.
jlm0@infionline.net
11-07-2007, 08:56 AM
Will this coding do what I want?
Stop the strategy from running once I have achieved a net realized profit of at leat $100?
// If the profit on real-time trades is > $100 stop trading
if (Performance.RealtimeTrades.TradesPerformance.Curr ency.CumProfit > 100)
return;
NinjaTrader_Ray
11-07-2007, 09:07 AM
To be technically accurate:
- If you include that code in any public event driven method (such as OnBarUpdate()) it will return from that method. Thus, if its the first line in that method, it will no longer process your code underneath that condition check.
- So if you only have strategy order logic in OnBarUpdate(), you could add that line of code and prevent any further processing of this method. It will not stop your strategy from running but it will do what you want.
- If you want to terminate the strategy (remove it from a chart its running etc..) you can set the strategy "Running" property to "false".
// If the profit on real-time trades is > $100 stop trading
if (Performance.RealtimeTrades.TradesPerformance.Curr ency.CumProfit > 100)
Running = false;
jlm0@infionline.net
11-07-2007, 09:24 AM
Thanks Ray,
How do I do the Strategy Running false thing, once I have achieved the profit target?
jlm0@infionline.net
11-07-2007, 09:37 AM
Sorry Ray,
I see now where you showed me after my code.
Thanks,
Jim
jlm0@infionline.net
11-08-2007, 06:06 AM
Hi Ray,
I have the code entered as below and it doesn't work. I get past $100.00 in profit, and the strategy keeps running/keeps sending orders, etc.
// If the profit on real-time trades is > $100 stop trading
if (Performance.RealtimeTrades.TradesPerformance.Curr ency.CumProfit > 100)
Running = false;
NinjaTrader_Ray
11-08-2007, 07:00 AM
Try "return" after the check for $100.
jlm0@infionline.net
11-08-2007, 07:14 AM
Are you saying instead of Running = false, use return?
NinjaTrader_Ray
11-08-2007, 07:24 AM
Yes that is correct.
jlm0@infionline.net
11-08-2007, 08:25 AM
Why won't this exit a long position that reaches > $100 profit, and stop the strategy from running.
It isn't doing either thing.
// If the profit on real-time trades is > $100 stop trading
if (Position.MarketPosition == MarketPosition.Long && Performance.RealtimeTrades.TradesPerformance.Curre ncy.CumProfit > 100)
ExitLong("lg");
if (Position.MarketPosition == MarketPosition.Short && Performance.RealtimeTrades.TradesPerformance.Curre ncy.CumProfit > 100)
ExitShort("sht");
if (Performance.RealtimeTrades.TradesPerformance.Curr ency.CumProfit > 100)
return;
NinjaTrader_Ray
11-08-2007, 08:31 AM
You will need to debug your code.
For example,
if (Position.MarketPosition == MarketPosition.Long && Performance.RealtimeTrades.TradesPerformance.Curre ncy.CumProfit > 100)
ExitLong("lg");
- Can you confirm this "if" branch is entered?
- What's the value of "CumProfit" property etc... does it ever go above 100?
jlm0@infionline.net
11-08-2007, 08:35 AM
I have
CalculateOnBarClose = false;
And I see the unrealized profit go above $100, so it seems it should exit and stop the strategy from running.
NinjaTrader_Ray
11-08-2007, 08:41 AM
CumProfit is realized PnL not unrealized. For unrealized, see Position.GetProfitLoss().
http://www.ninjatrader-support.com/HelpGuideV6/GetProfitLoss.html
jlm0@infionline.net
11-08-2007, 08:50 AM
The results of closed trades do not seem to be moving into the realized column in Version 6.5?
jlm0@infionline.net
11-08-2007, 09:09 AM
CumProfit is realized PnL not unrealized--
In order to stop running a strategy once accumulated profit has reached a certain target level, it would seem that unrealized p&L would need to be adding to or subtracting from the realized P&L while the strategy is in an open trade.
Is that what is supposed to be happening in the realized p&l column?
Thanks,
Jim
NinjaTrader_Ray
11-08-2007, 09:14 AM
An open trade is not "Realized" PnL since the trade is not closed. Only closed trades make up "Realized" PnL.
jlm0@infionline.net
11-08-2007, 09:23 AM
So is there coding that would allow me to check unrealized P&L plus realized p&L to see if a certain CumProfit has been attained in order to close open positions and stop the strategy from running?
NinjaTrader_Ray
11-08-2007, 09:26 AM
Add the CumProfit to the Position.GetProfitLoss().
jlm0@infionline.net
11-08-2007, 09:47 AM
$GBPUSD
Sell
1
2.1087
########
c7209a0160674c04b07d0644603eb631
Exit
-
aa201c62c41e436590e7813bc6f157f1
Sell
$GBPUSD
Buy
1
2.1094
########
bcfa91a0c2794828a16e06beed5d5574
Entry
1L
5340d8adaf064e36a7112469ec24e4a3
Buy
$GBPUSD
Sell
1
2.1088
########
7ca85de54daf4c3d9963866d930d12f9
Exit
-
1bca3245936144a5a0fd1c1cf2166e29
Sell
$GBPUSD
Buy
1
2.1093
########
cfb44e9c36c74371a294ef7e74e6bc83
Entry
1L
e49a4b4ac47541a78cc6cd96cb9c3925
Buy
$GBPUSD
Sell
1
2.1088
########
739bb21aaa484bb39bf07fc5924ba06d
Exit
-
ae795fc661644722bce641ad09cefe3d
Sell
$GBPUSD
Buy
1
2.1094
########
e7ee78df7a8c40368bacb0eac38a19dd
Entry
1L
096eeb2c5a684e6da8540010eff83c7e
Buy
Strategy
Instrument
Data Series
Parameters
Position
Avg Price
Unrealized
Realized
Account
Connection
Start/Stop
test
$GBPUSD
20 Seconds
1 (MyInput0)
-
0
$0.00
$0.00
Sim101
Amp Forex
Stop
I ran the same 20 second test. 3 trades
Realized should be $180.00 loss, shows zero.
NinjaTrader_Ray
11-08-2007, 09:59 AM
Thanks, I can reproduce now. Issue with currencies. Thanks for your co-operation and reporting this.
jlm0@infionline.net
11-08-2007, 10:00 AM
Add the CumProfit to the Position.GetProfitLoss().
I need help with how to code this.
Thanks,
Jim
jlm0@infionline.net
11-08-2007, 10:11 AM
Would this be right?
if (Position.MarketPosition == MarketPosition.Long && Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) + (Performance.RealtimeTrades.TradesPerformance.Curr ency.CumProfit) > 100)
return;
jlm0@infionline.net
11-08-2007, 10:14 AM
Sorry, I meant this--
if (Position.MarketPosition == MarketPosition.Long && Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) + (Performance.RealtimeTrades.TradesPerformance.Curr ency.CumProfit) > 100)
ExitLong("lg");
return;
NinjaTrader_Ray
11-08-2007, 10:16 AM
Close...
if (Position.MarketPosition == MarketPosition.Long && Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) + Performance.RealtimeTrades.TradesPerformance.Curre ncy.CumProfit > 100)
return;
If you are trading FX, you may consider swapping in GetCurrentBid() or GetCurrentAsk() instead of Close[0]. This is since Close[0] in FX through GAIN or MBT is always the bid price since there is no true "last" price in FX.
jlm0@infionline.net
11-08-2007, 10:47 AM
You don't show ExitLong()
Will the return; cause the exit and then stop the strategy?
NinjaTrader_Ray
11-08-2007, 10:49 AM
return will return from the method, if you want to do something else, then you will need to add some more lines of code after condition is true.
For example, this is pseudo code:
if (myCondition == true)
{
// Do some stuff here
}
jlm0@infionline.net
11-09-2007, 06:38 AM
The coding below gives the following result trading the $USDCAD in simulation:
After having a buy at .9399, there was a sell at .9409 for a $100.00 gain, which shows under the Executions Tab, but the strategy is still running, and there are no results showing under the Strategies Tab for Realized P&L, and nothing showing under the Account tab for Realized P&L.
Money is still accumulating in the Unreaqlized P&L column.
Also, the buy shows on the chart, but the sell exit does not.
if (Position.MarketPosition == MarketPosition.Long && Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) + Performance.RealtimeTrades.TradesPerformance.Curre ncy.CumProfit > 100)
ExitLong("lg");
return;
jlm0@infionline.net
11-12-2007, 02:00 PM
Everything appears to be working fine, now, with Version 3.5.3 on stopping a strategy from running once a given amount of profit has been attained.
Thanks for solving this,
Jim
NinjaTrader_Ray
11-12-2007, 02:21 PM
Excellent thanks for reporting back.
kennyguo
11-14-2007, 04:33 AM
oh Add the CumProfit to the Position.GetProfitLoss().
I need help with how to code this.
Thanks,:)
NinjaTrader_Ray
11-14-2007, 07:17 AM
See post #36 for a sample.
jlm0@infionline.net
11-14-2007, 09:42 AM
I spoke too soon on this.
It is not ending the running of the strategy.
It is exiting, but not removing orders or ending the strategy.
here's the code:
// If the profit on real-time trades is > $100 stop trading
if (Position.MarketPosition == MarketPosition.Flat && Position.GetProfitLoss(GetCurrentBid(), PerformanceUnit.Currency) + Performance.RealtimeTrades.TradesPerformance.Curre ncy.CumProfit > 200)
return;
if (Position.MarketPosition == MarketPosition.Flat && Position.GetProfitLoss(GetCurrentAsk(), PerformanceUnit.Currency) + Performance.RealtimeTrades.TradesPerformance.Curre ncy.CumProfit > 200)
return;
jlm0@infionline.net
11-14-2007, 09:43 AM
Should have said:
// If the profit on real-time trades is > $200 stop trading
NinjaTrader_Ray
11-14-2007, 09:46 AM
Jim,
You will have to debug this yourself to see how and why your strategy behaves as it does. Based on the code above, its working as expected which is to return out of the OnBarUpdate() method, if an order is working, it will utlimately cancel itself out.
jlm0@infionline.net
11-14-2007, 09:55 AM
Would coding like this be correct, and possibly solve this?
if (Position.MarketPosition == MarketPosition.Long && Position.GetProfitLoss(GetCurrentBid(), PerformanceUnit.Currency) + Performance.RealtimeTrades.TradesPerformance.Curre ncy.CumProfit > 200)
{
ExitLong();
return;
}
NinjaTrader_Ray
11-14-2007, 12:09 PM
If you have an open long position, then it would close it.
jlm0@infionline.net
11-14-2007, 12:15 PM
Would the "return;" stop the strategy from running any further?
NinjaTrader_Ray
11-14-2007, 12:28 PM
It will stop any further code below "return" to process.
And it would only get done once (assuming the ExitLong happens before the the next bar starts; or if the exit don't happen right away it could get done on multiple bars until the position actually exited.)
You would probably still want the code from post #44 (placed after the code from post #47) to cause the strategy to continue to exit without doing anything on subsequent bars.