![]() |
This website will be down for maintenance from Friday May 24th at 6PM MDT until Sunday May 26th at 12PM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Apr 2009
Posts: 114
Thanks: 0
Thanked 0 times in 0 posts
|
I have a strategy that trades multiple Instruments and I am backtesting it for ~10 years using daily bars. Since this strategy trades many Instruments and could eaily over draw the account of say a starting balance of $50,000 I added some code to back test the strategy to keep track of the account and not place trades if the account balence gets close to zero. The code is as follows and seem to be accurate when looking at historical backtest Summary and Trades/Execution tabs (the strategy only goes long)
Code:
protectedoverridevoid OnStartUp() {
if (Historical) {
this.myAccountSize = 50000;
Print(string.Format("OnStartUp: {0},{1}", Time[0].ToShortDateString(), this.myAccountSize));
}
}
protectedoverridevoid OnExecution(IExecution iexec) {
// if backtesting keep track of account size by hand,
// otherwise use GetAccountValue(AccountItem.CashValue)
if (Historical) {
lock (this.lockObj) {
int Qty = iexec.Quantity;
double Price = iexec.Price;
string BuyOrSell = "Sell";
if (iexec.MarketPosition == MarketPosition.Long) {
Price *= -1;
BuyOrSell = "Buy";
}
this.myAccountSize += (Qty * Price);
Print(string.Format("{0},{1},{2},{3},{4}"
, Time[0].ToShortDateString(), Instrument.FullName, BuyOrSell, (Qty * Price), this.myAccountSize));
}
}
}
Thanks, Erik |
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
|
Erik, this looks like a valid approach to keep track of your account value and you will be able to use GetAccountValue() to get the account balance during real-time trading.
Austin
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Apr 2009
Posts: 114
Thanks: 0
Thanked 0 times in 0 posts
|
So I am going to add this logic to my strategy for live trading and then I think my code should work for live trading as well as backtesting and optimization. Does this look right?
Thanks, Erik Code:
protectedoverridevoid OnBarUpdate() {
for (int i = 0; i < CurrentBars.Length; i++) if (CurrentBars[i] <= BarsRequired) return;
if (!Historical) this.myAccountSize = GetAccountValue(AccountItem.CashValue);
TryExit();
if (!PassedFilters()) return;
TryEnter();
}
|
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
|
Erik, I'm not sure what those additional methods are (TryExit(), PassedFilters(), etc), but the code looks like it should work fine.
Austin
NinjaTrader Customer Service |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| PaperTrading question. | ErikHR | Automated Trading | 5 | 02-17-2011 10:47 AM |
| Differences in data: GAIN live, GAIN historical, NT historical | terenyi | Connecting | 2 | 01-12-2010 06:15 PM |
| Need suggestion for inexpensive datafeed with NT for papertrading | upupnorth | Charting | 1 | 08-30-2009 01:34 PM |
| PaperTrading at IB TWS | jriverac | Miscellaneous Support | 1 | 02-09-2009 06:29 AM |
| Unwanted Position Reversal on Exit with Market Order (IB Papertrading) | EvolveK | Automated Trading | 3 | 09-27-2007 01:36 PM |