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-22-2010, 04:06 AM   #1
AnotherTrader
Senior Member
 
Join Date: Aug 2009
Posts: 266
Thanks: 41
Thanked 8 times in 8 posts
Send a message via ICQ to AnotherTrader
Default Working with "Trade Class" on a day-by-day basis

Hi

In an intraday strategy, I would like to include a condition that will stop the placing of further trades if I’ve already had 2 consecutive losses that day. Also, I would like to code this in a manner that will allow me to backtest the effect of this condition.

My code(below) is based on the example in NT 6 documentation covering “Trade Class”.

However, in order to be able to backtest the condition, I have changed ” .RealtimeTrades. ” everywhere to “ .AllTrades. ”.

The difficulty this gives me is that, instead of stopping the placing of new trades in a given day after a second consecutive loss in that day, the condition once triggered also prevents ANY future trades being taken on succeeding days, too.

How can I change the coding to get a condition that re-evaluates each day, and - if triggered - will only prevent trading on that day (rather than on all future days, too)?
Code:
// Section to keep track of whether there have been 2 successive losses in a row, and to stop trading if there have
if (Performance.AllTrades.Count > 1) 
{ 
// Get the last completed trades 
Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1]; 
Trade secondlastTrade = Performance.AllTrades[Performance.AllTrades.Count - 2]; 
 
// Calculate the PnL for the last two completed trades 
double lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;
double secondlastProfit = secondlastTrade.ProfitCurrency * secondlastTrade.Quantity;
 
if(lastProfit < 0 && secondlastProfit < 0)
{
keepTrading = 0;
}
}
Many thanks in advance for your help.
Last edited by AnotherTrader; 01-22-2010 at 04:11 AM.
AnotherTrader is offline  
Reply With Quote
Old 01-22-2010, 05:59 AM   #2
AnotherTrader
Senior Member
 
Join Date: Aug 2009
Posts: 266
Thanks: 41
Thanked 8 times in 8 posts
Send a message via ICQ to AnotherTrader
Default

OK, I think I managed to get there ...
I introduced a new integer “tradesBeforeToday”, and before each day’s session open I set its value as follows:
tradesBeforeToday = Performance.AllTrades.Count;

Then I modified my previous code to the following ...

Code:
Section to keep track of whether there have been 2 successive losses in a row, and to stop trading if there have
if (Performance.AllTrades.Count > tradesBeforeToday + 1
{ 
// Get the last completed trades 
Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1]; 
Trade secondlastTrade = Performance.AllTrades[Performance.AllTrades.Count - 2]; 
 
// Calculate the PnL for the last two completed trades 
double lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;
double secondlastProfit = secondlastTrade.ProfitCurrency * secondlastTrade.Quantity;
 
if(lastProfit < 0 && secondlastProfit < 0)
{
keepTrading = 0;
}
}
I‘m sure a coding “purist“ (rather than a “Heath Robinson” coder, like myself) will point out that I’ve violated some principle of coding elegance ... but it seems to work, so I’m happy ...
AnotherTrader is offline  
Reply With Quote
Old 01-22-2010, 07:45 AM   #3
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

Glad you got it resolved. That would be one acceptable way to proceed with in your code.
NinjaTrader_Josh 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
"Immediatly submit live working historical orders" trader20 Automated Trading 11 10-05-2009 09:28 AM
"Import stock symbols" not working heech Automated Trading 1 05-12-2009 02:24 AM
summary of "strategy realized" is not equal "account performance, total net profit"" Fragolino Miscellaneous Support 1 02-19-2009 04:12 AM
Minute chart for "Day session" only? PKFFW Charting 2 06-17-2008 01:50 AM
Plotting "n" day charts - not working correctly stevestrading Suggestions And Feedback 1 09-08-2007 03:32 AM


All times are GMT -6. The time now is 10:08 PM.