Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Historical vs PaperTrading/Live?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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:
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]protected[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] OnStartUp() {[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (Historical) {[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].myAccountSize = 50000;[/SIZE]
    [SIZE=2]Print([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Format([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"OnStartUp: {0},{1}"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], Time[0].ToShortDateString(), [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].myAccountSize));[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE]
     
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]protected[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] OnExecution([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]IExecution[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] iexec) {[/SIZE]
    [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// if backtesting keep track of account size by hand, [/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// otherwise use GetAccountValue(AccountItem.CashValue)[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (Historical) {[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]lock[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].lockObj) {[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Qty = iexec.Quantity;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Price = iexec.Price;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] BuyOrSell = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Sell"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (iexec.MarketPosition == [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MarketPosition[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Long) {[/SIZE]
    [SIZE=2]   Price *= -1;[/SIZE]
    [SIZE=2]   BuyOrSell = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Buy"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    [SIZE=2] }[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].myAccountSize += (Qty * Price);[/SIZE]
    [SIZE=2] Print([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Format([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"{0},{1},{2},{3},{4}"[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2] , Time[0].ToShortDateString(), Instrument.FullName, BuyOrSell, (Qty * Price), [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].myAccountSize));[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE]
    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

    #2
    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.
    AustinNinjaTrader Customer Service

    Comment


      #3
      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:
      [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]protected[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] OnBarUpdate() {[/SIZE][SIZE=2][COLOR=#008000]
      [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i = 0; i < CurrentBars.Length; i++) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (CurrentBars[i] <= BarsRequired) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
      [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][B]if[/B][/COLOR][/SIZE][/COLOR][/SIZE][B][SIZE=2] (!Historical) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].myAccountSize = GetAccountValue([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]AccountItem[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].CashValue);[/SIZE][/B]
      [SIZE=2] TryExit();[/SIZE]
      [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (!PassedFilters()) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
      [SIZE=2] TryEnter();[/SIZE]
      [SIZE=2]}[/SIZE]

      Comment


        #4
        Erik, I'm not sure what those additional methods are (TryExit(), PassedFilters(), etc), but the code looks like it should work fine.
        AustinNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        656 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        371 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        109 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        574 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        579 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X