Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Realized PnL with ATM in Script
Collapse
X
-
OK, I THINK I might have found the problem, but not sure. This code is in my strat which was copied from the ATM Sample.
} // If the strategy has terminated reset the strategy id
else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
atmStrategyId = string.Empty;
Since the GetAtmStrategyPnL uses the atmStrategyId, could this be why it's not adding up the PnL? It LOOKS like this resets it back to zero every time a trade is done. When is says "If the strategy has terminated", is it talking about the ATM strategy and not the NinjaScript strategy terminating?
Is this a simple matter of taking this reset code out of the strategy?
Joe
Comment
-
That helps a LOT!!! Thank You!!
I didn't know we could use a += so I guess that's what makes them add together.
The cummulative trades, cummulative profit works great in a plain old Ninjascript strategy, but not with one that creates an ATM.
Now if I can just get it to work......
I appreciate it Josh!
Comment
-
Josh (and NT support) you are so much help i thank you for sticking with us through our noobness. NT is a great product on its own but having support like you provide puts it above the rest imo. Thanks
Will give this a go tonight but i believe it will solve my issue.
Comment
-
Josh,
Great news is that it works! I also add my THANKS to cirion's.
HOWEVER.........
It doesn't seem to be adding (subtracting) a loss to the realized PnL. It looks like it only sees the profitable trades to aggregate. I just ran the strat on yesterdays data.
Daily Profit Goal was set to $200.
First Trade: - $202.50
Second Trade: $95.30
Third Trade: $95.30
Fourth Trade: $95.30
Then it stopped trading. Total Realized PnL was $83.40. Even if it doesn't see commissions it should have kept trading since the PnL limit was $200 and it would have only been $100 to the good.
The total for the profitable trades was $285.90, which is over $200, so it quit.
Any ideas?? I double checked the Time and it was definitely in bounds and conditions met.
Joe
PS - when this thing works as advertised I'll post a sample strat that others can use.
Comment
-
OK. I'm posting the part I think you will want to look at.
The code you had me put is is just under OnBarUpdate.
#region Variables
// Wizard generated variables
// User defined variables (add any user defined variables below)
private double realPnL = 90.000;
private string atmStrategyId = string.Empty;
private string orderId = string.Empty;
private double profit= 0;
#endregion
protected override void Initialize()
{
CalculateOnBarClose = true;
}
protected override void OnBarUpdate()
{
// Checks for total realized PnL
profit += GetAtmStrategyRealizedProfitLoss(atmStrategyId);
**** ALL BAR CALCULATION STUFF LEFT OUT****
// calculate the highest high for this range
double highestHigh = MAX(High, startBarsAgo - endBarsAgo)[endBarsAgo];
// calculate the lowest low for this range
double lowestLow = MIN(Low, startBarsAgo - endBarsAgo)[endBarsAgo];
if (Historical)
return;
// Check for valid condition and create an ATM Strategy
// Condition set 3
if (ToTime(Time[0]) >= ToTime(7, 50, 0)
&& ToTime(Time[0]) <= ToTime(14, 0, 0)
&& orderId.Length == 0 && atmStrategyId.Length == 0
&& profit <= realPnL
&& highestHigh > 0 && Close[0] > highestHigh)
{
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(Action.Buy, OrderType.Limit, highestHigh, 0,
TimeInForce.Day,orderId , "2 Point",
atmStrategyId);
}
// Condition set 4
if (ToTime(Time[0]) >= ToTime(7, 50, 0)
&& ToTime(Time[0]) <= ToTime(14, 0, 0)
&& Position.MarketPosition == MarketPosition.Flat
&& orderId.Length == 0 && atmStrategyId.Length == 0
&& profit <= realPnL
&& lowestLow > 0 && Close[0] < lowestLow)
{
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(Action.Sell, OrderType.Limit, lowestLow, 0,
TimeInForce.Day,orderId , "2 Point",
atmStrategyId);
}
// Check for a pending entry order
if (orderId.Length > 0)
{
string[] status = GetAtmStrategyEntryOrderStatus(orderId);
// If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
if (status.GetLength(0) > 0)
{
// If the order state is terminal, reset the order id value
if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
orderId = string.Empty;
}
} // If the strategy has terminated reset the strategy id
else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
atmStrategyId = string.Empty;
Thanks Josh, you have the patience of a saint.
Joe
Comment
-
You should not put it up on the top there.
Please just try aggregating it as your position is closed.
Code:... else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat) { profit += GetAtmStrategyRealizedProfitLoss(atmStrategyId); atmStrategyId = string.Empty; }
Josh P.NinjaTrader Customer Service
Comment
-
Exit based on PNL
Hello,
I want to know if its possible and how it would be good.
FLAT position and remove pending orders on a day of trading based on profit trading PNL(+200) or PNL(-1000) incluiding comissionsfor stop trading.
Thanks.
DelekLast edited by delek77; 06-14-2015, 09:21 PM.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Tin34, Today, 03:30 AM
|
2 responses
19 views
0 likes
|
Last Post
by Tin34
Today, 04:11 AM
|
||
Started by sastrades, Yesterday, 09:59 AM
|
2 responses
32 views
0 likes
|
Last Post Today, 04:06 AM | ||
Started by ETFVoyageur, Today, 12:52 AM
|
1 response
16 views
0 likes
|
Last Post Today, 03:36 AM | ||
Started by lollers, Today, 03:26 AM
|
0 responses
12 views
0 likes
|
Last Post
by lollers
Today, 03:26 AM
|
||
Started by aliyahany, Today, 03:16 AM
|
0 responses
4 views
0 likes
|
Last Post
by aliyahany
Today, 03:16 AM
|
Comment