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 charlesugo_1, 05-26-2026, 05:03 PM
        0 responses
        64 views
        0 likes
        Last Post charlesugo_1  
        Started by DannyP96, 05-18-2026, 02:38 PM
        1 response
        149 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 05-11-2026, 05:56 AM
        0 responses
        162 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 05-10-2026, 08:12 PM
        0 responses
        99 views
        0 likes
        Last Post CarlTrading  
        Started by Hwop38, 05-04-2026, 07:02 PM
        0 responses
        286 views
        0 likes
        Last Post Hwop38
        by Hwop38
         
        Working...
        X