Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Working with "Trade Class" on a day-by-day basis

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

    Working with "Trade Class" on a day-by-day basis

    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:
    [FONT=Courier New][COLOR=green][COLOR=green][FONT=Courier New]// Section to keep track of whether there have been 2 successive losses in a row, and to stop trading if there have[/FONT][/COLOR]
    [COLOR=blue][FONT=Courier New]if[/FONT][/COLOR][COLOR=black][FONT=Courier New] (Performance.AllTrades.Count > [/FONT][/COLOR][COLOR=purple][FONT=Courier New]1[/FONT][/COLOR][COLOR=black][FONT=Courier New]) [/FONT][/COLOR]
    [COLOR=black][FONT=Courier New]{ [/FONT][/COLOR]
    [COLOR=green][FONT=Courier New]// Get the last completed trades [/FONT][/COLOR]
    [COLOR=black][FONT=Courier New]Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - [/FONT][/COLOR][COLOR=purple][FONT=Courier New]1[/FONT][/COLOR][COLOR=black][FONT=Courier New]]; [/FONT][/COLOR]
    [COLOR=black][FONT=Courier New]Trade secondlastTrade = Performance.AllTrades[Performance.AllTrades.Count - [/FONT][/COLOR][COLOR=purple][FONT=Courier New]2[/FONT][/COLOR][COLOR=black][FONT=Courier New]]; [/FONT][/COLOR]
     
    [COLOR=green][FONT=Courier New]// Calculate the PnL for the last two completed trades [/FONT][/COLOR]
    [COLOR=blue][FONT=Courier New]double[/FONT][/COLOR][COLOR=black][FONT=Courier New] lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;[/FONT][/COLOR]
    [COLOR=blue][FONT=Courier New]double[/FONT][/COLOR][COLOR=black][FONT=Courier New] secondlastProfit = secondlastTrade.ProfitCurrency * secondlastTrade.Quantity;[/FONT][/COLOR]
     
    [COLOR=blue][FONT=Courier New]if[/FONT][/COLOR][COLOR=black][FONT=Courier New](lastProfit < [/FONT][/COLOR][COLOR=purple][FONT=Courier New]0[/FONT][/COLOR][COLOR=black][FONT=Courier New] && secondlastProfit < [/FONT][/COLOR][COLOR=purple][FONT=Courier New]0[/FONT][/COLOR][COLOR=black][FONT=Courier New])[/FONT][/COLOR]
    [COLOR=black][FONT=Courier New]{[/FONT][/COLOR]
    [COLOR=black][FONT=Courier New]keepTrading = [/FONT][/COLOR][COLOR=purple][FONT=Courier New]0[/FONT][/COLOR][COLOR=black][FONT=Courier New];[/FONT][/COLOR]
    [COLOR=black][FONT=Courier New]}[/FONT][/COLOR]
    [COLOR=black][FONT=Courier New]}[/FONT][/COLOR]
    [/COLOR][/FONT]
    Many thanks in advance for your help.
    Last edited by AnotherTrader; 01-22-2010, 05:11 AM.

    #2
    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:
    [FONT=Courier New][COLOR=green][COLOR=green][FONT=Courier New]Section to keep track of whether there have been 2 successive losses in a row, and to stop trading if there have[/FONT][/COLOR]
    [COLOR=blue][FONT=Courier New]if[/FONT][/COLOR][COLOR=black][FONT=Courier New] (Performance.AllTrades.Count > tradesBeforeToday + [/FONT][/COLOR][COLOR=purple][FONT=Courier New]1[/FONT][/COLOR]
    [COLOR=black][FONT=Courier New]{ [/FONT][/COLOR]
    [COLOR=green][FONT=Courier New]// Get the last completed trades [/FONT][/COLOR]
    [COLOR=black][FONT=Courier New]Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - [/FONT][/COLOR][COLOR=purple][FONT=Courier New]1[/FONT][/COLOR][COLOR=black][FONT=Courier New]]; [/FONT][/COLOR]
    [COLOR=black][FONT=Courier New]Trade secondlastTrade = Performance.AllTrades[Performance.AllTrades.Count - [/FONT][/COLOR][COLOR=purple][FONT=Courier New]2[/FONT][/COLOR][COLOR=black][FONT=Courier New]]; [/FONT][/COLOR]
     
    [COLOR=green][FONT=Courier New]// Calculate the PnL for the last two completed trades [/FONT][/COLOR]
    [COLOR=blue][FONT=Courier New]double[/FONT][/COLOR][COLOR=black][FONT=Courier New] lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;[/FONT][/COLOR]
    [COLOR=blue][FONT=Courier New]double[/FONT][/COLOR][COLOR=black][FONT=Courier New] secondlastProfit = secondlastTrade.ProfitCurrency * secondlastTrade.Quantity;[/FONT][/COLOR]
     
    [COLOR=blue][FONT=Courier New]if[/FONT][/COLOR][COLOR=black][FONT=Courier New](lastProfit < [/FONT][/COLOR][COLOR=purple][FONT=Courier New]0[/FONT][/COLOR][COLOR=black][FONT=Courier New] && secondlastProfit < [/FONT][/COLOR][COLOR=purple][FONT=Courier New]0[/FONT][/COLOR][COLOR=black][FONT=Courier New])[/FONT][/COLOR]
    [COLOR=black][FONT=Courier New]{[/FONT][/COLOR]
    [COLOR=black][FONT=Courier New]keepTrading = [/FONT][/COLOR][COLOR=purple][FONT=Courier New]0[/FONT][/COLOR][COLOR=black][FONT=Courier New];[/FONT][/COLOR]
    [COLOR=black][FONT=Courier New]}[/FONT][/COLOR]
    [COLOR=black][FONT=Courier New]}[/FONT][/COLOR]
    [/COLOR][/FONT]
    I‘m sure a coding “purist“ (rather than a “Heath Robinson” coder, like myself) will point out that I’ve violated some principle of coding elegance ... but it seems to work, so I’m happy ...

    Comment


      #3
      Glad you got it resolved. That would be one acceptable way to proceed with in your code.
      Josh P.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by r68cervera, Today, 05:29 AM
      0 responses
      2 views
      0 likes
      Last Post r68cervera  
      Started by geddyisodin, Today, 05:20 AM
      0 responses
      3 views
      0 likes
      Last Post geddyisodin  
      Started by JonesJoker, 04-22-2024, 12:23 PM
      6 responses
      35 views
      0 likes
      Last Post JonesJoker  
      Started by GussJ, 03-04-2020, 03:11 PM
      12 responses
      3,239 views
      0 likes
      Last Post Leafcutter  
      Started by AveryFlynn, Today, 04:57 AM
      0 responses
      6 views
      0 likes
      Last Post AveryFlynn  
      Working...
      X