Does anyone know how to code this?
Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
Ending Ninjascript strategy
Collapse
X
-
Ending Ninjascript strategy
I still have not been able to come up with a way to exit a strategy, and have the strategy cancel open orders, and stop running, after reaching a certain level of profit or loss, based on both unrealized and realized PnL.
Does anyone know how to code this?Tags: None
-
Jim,
I've already given you the code in a PM. Please use the code provided.
Code:if ((Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit + Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) >= 200) || (Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit + Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) <= -200)) ExitLong();
Josh P.NinjaTrader Customer Service
-
Right. The line I pasted is meant to be used as a supplement along with the reference sample I posted. The line above adds calculations for unrealized PnL while the reference sample only shows realized PnL.
You can review the reference sample for a technique you can employ to halt the strategy here.
Josh P.NinjaTrader Customer Service
Comment
-
ending ninjascript
We're making progress Josh.
The coding below did cause an exit of a long position in the $AUDUSD, with $200 in profit, but it doesn't cancel an order that was in place for reversing and going short, upon certain conditions being met.
In effect, it still hasn't stopped the strategy.
Is there any way to have it not only exit, upon reaching the target profit or loss, but cancel any orders that had been put in place, before reaching the target, and then not place any more orders, because the strategy has been stopped from running beyond the return statement.
Thanks,
Jim
//* Prevents further trading if the current session's realized profit exceeds $200.
if ((Performance.RealtimeTrades.TradesPerformance.Cur rency.CumProfit + Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) >= 200)
|| (Performance.RealtimeTrades.TradesPerformance.Curr ency.CumProfit + Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) <= -200))
{
ExitLong("lg");
ExitShort("sht");
return;
}
// Conditions below this point for going long or short based on the strategy parameters, which are based on a breakout above of below a certain beginning bars high and low.
Based on intrabar, and not close of bar.
Comment
-
ending ninjascript
More info.
Please read comments in below post about cancelling orders.
The reversal order to go short did eventually cancel once changing to the next hourly bar, even though the strategy says "false" on CalculateonBarClose.
I would like for it to cancel with out waiting on the next bar to start.
This is based on an hourly chart.
Jim
Comment
-
-
ending ninjascript
Hi Ray,
When I am in a long position, there will be a sell stop order to close the position at the low of the bar from which I went long, and there is also a sell short order to reverse and go short at that same low.
This morning, the strategy obtained $200 profit at 7:34am est, and it issued a sell market order to exit, based on reaching that profit, and it also cancelled the sell stop order, that was at the low of the entry order bar, at the same time that it exited.
However, the sell short order was still working until the opening of the next bar at 8:00am est.
It was then cancelled, and no more orders are being placed, which is what I want, if I reach the target profit or loss amount of $200.
I just would like to be able to have that reversal order, for possibly going in the other direction, to be cancelled, as soon as I exit, versus waiting for the close of the bar.
I am using
CalculateOnBarClose = false;
Jim
Comment
-
-
Lets try this:
Where you "return;" in your code after the PnL condition is met, subsitute "return;" for --> "Running = false;" excluding the quotations of course.
This will really terminate the strategy itself. The termination process should cancel out your pending entry stop order.RayNinjaTrader Customer Service
Comment
-
-
ending ninjascript
When making this substitution
Running = false
What happens is that the strategy will not show on the chart, and does not show under the strategies tab for position, AvgPrice, Unrealized, or Realized.
My strategy still sends orders and takes a position, but it acts as if the strategy is not running, right from the start.
Jim
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by judysamnt7, 03-13-2023, 09:11 AM
|
4 responses
57 views
0 likes
|
Last Post
by DynamicTest
Today, 08:25 PM
|
||
Started by ScottWalsh, Today, 06:52 PM
|
4 responses
36 views
0 likes
|
Last Post
by ScottWalsh
Today, 07:40 PM
|
||
Started by olisav57, Today, 07:39 PM
|
0 responses
7 views
0 likes
|
Last Post
by olisav57
Today, 07:39 PM
|
||
Started by trilliantrader, Today, 03:01 PM
|
2 responses
20 views
0 likes
|
Last Post
by helpwanted
Today, 07:25 PM
|
||
Started by cre8able, Today, 07:24 PM
|
0 responses
9 views
0 likes
|
Last Post
by cre8able
Today, 07:24 PM
|
Comment