![]() |
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
|
|||||||
| 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. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
|
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;
}
}
Last edited by AnotherTrader; 01-22-2010 at 04:11 AM.
|
|
|
|
|
|
#2 |
|
Senior Member
|
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;
}
}
|
|
|
|
|
|
#3 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
Glad you got it resolved. That would be one acceptable way to proceed with in your code.
Josh
NinjaTrader Customer Service |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |