NinjaTrader Support Forum  
X

Attention!

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


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 02-26-2011, 01:28 AM   #1
ErikHR
Senior Member
 
Join Date: Apr 2009
Posts: 114
Thanks: 0
Thanked 0 times in 0 posts
Default Historical vs PaperTrading/Live?

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));
}
}
}
I have the code in these event handlers wrapp with "if (Historical)". The idea is that once I start paper trading (Using IQFeed) and then trading live (using MB Trading) that I can just use "GetAccountValue(AccountItem.CashValue)" in my code instead of "this.MyAccountSize". Am I doing this right or will this not work?

Thanks,
Erik
ErikHR is offline  
Reply With Quote
Old 02-26-2011, 01:01 PM   #2
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
Default

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.
NinjaTrader_Austin is offline  
Reply With Quote
Old 02-27-2011, 12:16 PM   #3
ErikHR
Senior Member
 
Join Date: Apr 2009
Posts: 114
Thanks: 0
Thanked 0 times in 0 posts
Default

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();
}
ErikHR is offline  
Reply With Quote
Old 02-27-2011, 02:47 PM   #4
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
Default

Erik, I'm not sure what those additional methods are (TryExit(), PassedFilters(), etc), but the code looks like it should work fine.
NinjaTrader_Austin 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
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


All times are GMT -6. The time now is 01:19 AM.