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 > Application Technical Support > Automated Trading

Automated Trading Support for automated trading systems using NinjaScript. Support for our ATI (Automated Trading Interface) used to link an external application such as TradeStation and eSignal to NinjaTrader.

Reply
 
Thread Tools Display Modes
Old 01-26-2009, 05:15 PM   #1
Trade1953
Member
 
Join Date: Feb 2008
Location: Wellsville, Ut
Posts: 81
Thanks: 0
Thanked 0 times in 0 posts
Default Realized PnL with ATM in Script

Hi,
I've used the Sample Price Modification strategy code to have an autostrategy stop trading if it accumulated X amount of Real PnL. ie, quit trading if Real PnL > $100.

Now I have an autostrategy calling an ATM and the Real PnL stop trading limit doesn't work. It looks like the Real PnL is attributed to the ATM it calls instead of the strategy itself, so the strategy doesn't see any Real PnL and just keeps on rolling.

Is there a way to still have the autostrategy quit trading after X PnL with an ATM being called? I guess it would have to be able to add up the ATM Real PnL and recognize that somehow ???? If so, how would you do that?
Thanks,
Joe
Trade1953 is offline  
Reply With Quote
Old 01-26-2009, 05:35 PM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,404
Thanks: 252
Thanked 973 times in 956 posts
Default

Hi Joe, you could try incorporating GetAtmStrategyRealizedProfitLoss() in your code and use this to check - http://www.ninjatrader-support.com/H...rofitLoss.html
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 01-26-2009, 09:38 PM   #3
Trade1953
Member
 
Join Date: Feb 2008
Location: Wellsville, Ut
Posts: 81
Thanks: 0
Thanked 0 times in 0 posts
Default

Thank Bertrand,
Looks like the example shows a Print function. I'll play with it and see if I can make it turn into a value I can use in the strat so it won't trade if the value is > some amount. The explanation in the Help Guide says what it does but not how to use it. I guess it's for more experienced programmers.
Joe
Trade1953 is offline  
Reply With Quote
Old 01-27-2009, 02:51 PM   #4
Trade1953
Member
 
Join Date: Feb 2008
Location: Wellsville, Ut
Posts: 81
Thanks: 0
Thanked 0 times in 0 posts
Default

Hey Bertrand,
Sorry to bother you, but I can't seem to get it to work. I used examples of a similar nature but to no avail. Would you mind looking at this code and see if there's something obviously wrong or missing?

Under Variables
(realPnL defined)
private double realPnL = 90.000;
private string atmStrategyId = string.Empty;
private string orderId = string.Empty;

Under OnBarUpdate
(I defined PnL to compare against realPnL. Is the GetAtm statement correct? 2 Point is name of the ATM strategy which I assume is the ATM strategyID.)

double PnL = GetAtmStrategyRealizedProfitLoss("2 Point");

// 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)
&& PnL <= realPnL
&& Position.MarketPosition == MarketPosition.Flat
&& orderId.Length == 0 && atmStrategyId.Length == 0
&& highestHigh > 0 && Close[0] > highestHigh)

{
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(Action.Buy, OrderType.Limit, highestHigh, 0,
TimeInForce.Day,orderId , "2 Point",
atmStrategyId);
}

It's calling the ATM just fine and placing orders. I just don't want it to place any orders if the total realized PnL for the ATM on the day is greater than $90.
Thanks,
Joe
Trade1953 is offline  
Reply With Quote
Old 01-27-2009, 02:57 PM   #5
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

double PnL = GetAtmStrategyRealizedProfitLoss("2 Point");
"2 Point" is not your strategyId. Please use your atmStrategyId.

Position.MarketPosition == MarketPosition.Flat
Does not work for ATM orders, designed for NinjaScript orders. Instead use this: http://www.ninjatrader-support.com/H...yPosition.html
NinjaTrader_Josh is offline  
Reply With Quote
Old 01-27-2009, 07:36 PM   #6
cirion
Member
 
Join Date: May 2008
Posts: 68
Thanks: 0
Thanked 0 times in 0 posts
Default

When i do this in my code all it returns is 0 no matter what the profit or loss for the days trading is.

Print("PnL is " + GetAtmStrategyRealizedProfitLoss("AtmStrategyId"). ToString());

What am i doing wrong?
cirion is offline  
Reply With Quote
Old 01-27-2009, 10:27 PM   #7
Trade1953
Member
 
Join Date: Feb 2008
Location: Wellsville, Ut
Posts: 81
Thanks: 0
Thanked 0 times in 0 posts
Default

Josh,
Where do I find the atmStrategyID? All I can find in Help is that one is generated. It doesn't say how to find it, or when it's generated. In the ATM Sample Strat code I copied, down where it calls the ATM after the conditions are met it says
atmStrategyId = GetAtmStrategyUniqueId();

Do I simply put the Word atmStrategyId between the quotes? Or is there somewhere else I have to look for the atmStrategyId?

IF this is the case and I can simply put the word atmStrategyId between the quotes, do you see anything else obvious that would prevent it from checking the realized PnL before calling the ATM and submitting another order?

I can remove the marketposition = flat since I don't think it's really needed but it's good to have your reference link in case I do.
Thanks!!!!!
Joe
Trade1953 is offline  
Reply With Quote
Old 01-28-2009, 07:44 AM   #8
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

In your code:
"AtmStrategyCreate(Action.Buy, OrderType.Limit, highestHigh, 0,
TimeInForce.Day,orderId , "2 Point",
atmStrategyId);"

You've made your own atmStrategyId. Yes, you just put atmStrategyId into there. It is the variable you made to hold your ID. You do not put it between quotes. You just put it inside the parenthesis with no quotes.
NinjaTrader_Josh is offline  
Reply With Quote
Old 01-28-2009, 10:23 AM   #9
Trade1953
Member
 
Join Date: Feb 2008
Location: Wellsville, Ut
Posts: 81
Thanks: 0
Thanked 0 times in 0 posts
Default

Thank You Josh!
And thanks for your explanation. It also cleared up something else. I guess I can make atmStrategyId anything I want it to say.
It's starting to come together for me now.
Again, many thanks!!! I'll give this a shot.
Joe
Trade1953 is offline  
Reply With Quote
Old 01-28-2009, 10:34 AM   #10
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,404
Thanks: 252
Thanked 973 times in 956 posts
Default

Hi Trade1953, you are correct. Or just use

Code:
GetAtmStrategyUniqueId()
http://www.ninjatrader-support.com/H...yUniqueId.html
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 01-28-2009, 06:04 PM   #11
cirion
Member
 
Join Date: May 2008
Posts: 68
Thanks: 0
Thanked 0 times in 0 posts
Default

What would cause it to return 0 no matter what trades the atm takes?

Print("PnL is " + GetAtmStrategyRealizedProfitLoss(AtmStrategyId). ToString());

What am i doing wrong?
cirion is offline  
Reply With Quote
Old 01-28-2009, 06:09 PM   #12
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,404
Thanks: 252
Thanked 973 times in 956 posts
Default

Hi cirion, are sure you reference the correct AtmStrategyId? Also, you will not access to this historical values in the PnL with this call, you will need to save those yourself in a cumulative variable.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 01-28-2009, 06:20 PM   #13
cirion
Member
 
Join Date: May 2008
Posts: 68
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_Bertrand View Post
Hi cirion, are sure you reference the correct AtmStrategyId? Also, you will not access to this historical values in the PnL with this call, you will need to save those yourself in a cumulative variable.
Here is my sections of code that apply to my issue. (please tell me if i have missed anything out.)
Code:
if (orderId.Length == 0 && atmStrategyId.Length == 0 && buypostion == true)
            {
                atmStrategyId = GetAtmStrategyUniqueId();
                orderId = GetAtmStrategyUniqueId();
                AtmStrategyCreate(Action.Buy, OrderType.Limit, Close[0], 0, TimeInForce.Day, orderId, "AutoATM", atmStrategyId);
                exitbar = Close[0];
            }
Code:
if (X == X)
            {
              Print("The current ATM Strategy realized PnL is: " + GetAtmStrategyRealizedProfitLoss(atmStrategyId));
            }
It always returns 0 even after a day of trading
cirion is offline  
Reply With Quote
Old 01-29-2009, 07:20 AM   #14
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

Realized profit won't show up until the trade has closed.
NinjaTrader_Josh is offline  
Reply With Quote
Old 01-29-2009, 09:54 AM   #15
Trade1953
Member
 
Join Date: Feb 2008
Location: Wellsville, Ut
Posts: 81
Thanks: 0
Thanked 0 times in 0 posts
Default

Josh,
I have pretty much the same issue as cirion does. My strat trades many times a day yet even after the profit threshold has been reached it just keeps trading. It's almost like it sees every trade as starting with a 0 profit. (I don't have a buy position = true requirement).

Would you happen to have a working Sample Strategy that calls an ATM and has the "stop trading if X Realized PnL" working?

"A picture is worth a thousand words".
Thanks!
Joe
PS- I hope they are paying you well for dealing with frustrated beginners like me!!
Trade1953 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
Unrealized and Realized PnL in points/pips and % in stead of only $ whitegun Suggestions And Feedback 22 11-14-2008 05:54 PM
problems wit realized PnL dedomraz Miscellaneous Support 9 09-23-2008 10:11 AM
ATM from script MelbourneRich Automated Trading 1 09-19-2008 09:38 AM
ATM Strategy with Ninja Script suitguy1 Automated Trading 1 07-22-2008 01:34 PM
Why did you remove "Realized + Unrealized PnL" from the Columns list? shawnj Market Analyzer 7 05-28-2008 11:34 AM


All times are GMT -6. The time now is 01:23 PM.