Comprehensive Simulation Loop

If you find yourself asking, "When do scripts get executed?" the following rather complicated section shows exactly the algorithm used by Trading Blox Builder during a simulation.  This is what we refer to as the "Simulation Loop".

 

When running a test, Trading Blox Builder  will call scripts according to the algorithm defined below.

 

One thing to remember is that Trading Blox Builder helps keep you out of trouble by only allowing access to data you would really have for making trading decisions. For example, before the markets open when entering orders, the instrument' s current data is for the previous trading day. This is what happens in real life, you don't have access to today's data until the end of the day.

 

This has implications for the current dates of the instrument and test objects.

 

Each day before trading begins the test object date is set to the current date while the instrument's date is still set to the previous trading day's date. For example, on a Monday the test date might be 2006-04-10 while the instrument's date might be 2006-04-07 (the previous Friday).

 

After all the orders for the day have been entered and just before Trading Blox Builder  starts to process orders to see if they have been filled, Trading Blox Builder moves the instrument's date to match the current date if there is data in the instrument for this day.

 

The test runs from the first real trading day after the start date of the test (test.currentDay = 1) to the end date of the test, for all weekdays.

 

Scripts specific to instruments like entry and exit are not run for an instrument on holidays or other days without data

 

Scripts that are not instrument specific, or require input from all instruments, run on all weekdays

 

Lines listed in red describe actions that Trading Blox Builder performs that either affect or rely on actions performed by scripts.

 

Multiple scripts of the same type, in different blox, will run in Alphabetical Order based on the block name (case sensitive) always. So if there are 10 blox each with a Before Trading Day script, the scripts will run in alphabetical order according to the block name.

 

Simulation Loop:

 
for ( each block in all systems )
  call Before Simulation script
next ( block )
 
for ( each test (parameter step) in the simulation )
 
  setup parameters and reset variables to default
 
  for ( each block in all systems )
      call Before Test script
  next ( block )
   
  for (each day in the test )
 
          Set test.currentdate = test date and test.currenttime = first testing time
          Set instrument.date and instrument.time = the date/time of the _
                                                      bar prior to the test date/time
   
      call Before Trading Day script for the Global Suite System, if available.
 
      for ( each system )
   
          for ( each instrument in the portfolio that is primed )
              call Rank Instruments script
           
          Sort the instruments by long and short ranking
           
          for (each instrument in the portfolio that is primed)
              call Filter Portfolio script
   
          for ( each block in system )
              call Before Trading Day script
              for ( each instrument in the portfolio that is primed )
                  call Before Instrument Day
   
      next ( system )
 
      Intra-day loop start
 
      for ( each system )
  call Before Bar script
      next ( system )
 
      for ( each system place all the orders with the broker )
          for ( each instrument in the portfolio that is primed and _
                      has trade data on the trading date/time )
              for ( each entry/exit block in system )
                  call Exit Orders script only if there is a position
                  call Entry Orders script every time
                      call Unit Size script when order is created by broker object
                      call Can Add Unit script to check if trade is allowed
      next ( system )
 
      for ( each system )
  call Before Order Execution script
        next ( system )
      call Before Order Execution script for the Global Suite System, if available.
   
      Set instrument.date = test date and instrument.time = test.time _
                                                  for all instruments in system portfolio
      After this point there is full access to instrument data for test date/time
 
      for ( each system process the orders and fill based on actual market activity )
          for (each instrument in the portfolio that is primed and _
                                            has trade data on the trading date/time )
                call Update Indicators scripts
 
          for (each on-open exit order that has been created by the broker object)
              if order is filled based on price bar data    
                  call Can Fill Order to see if the order can be filled
                      if filled call Exit Order Filled
 
          Insert Actual Broker Positions for "Open" execution type if required
 
          for (each on-open entry order that has been created by the broker object. )
              if order is filled based on price bar data
                  call Can Fill Order to see if the order can be filled
                      if filled call Entry Order Filled
 
          for (each Entry Block in the system )
              for (each instrument in the portfolio that is primed and _
                            has trade data on the trading date)
                  call After Instrument Open script if present
 
          for (each stop or limit exit order that has been created by the broker object)
              if order is filled based on price bar data _
                  call Can Fill Order to see if the order can be filled _
                      if filled call Exit Order Filled
 
          Insert Actual Broker Positions for "Bar" execution type if required
 
          for (each stop or limit entry order that has been created by the broker object. )
              if order is filled based on price bar data
                  call Can Fill Order to see if the order can be filled _
                      if filled call Entry Order Filled
 
          for (each on-close exit order that has been created by the broker object)
              if order is filled based on price bar data    
                  call Can Fill Order to see if the order can be filled _
                      if filled call Exit Order Filled
 
          Insert Actual Broker Positions for "Close" execution type if required
 
          for (each on-close entry order that has been created by the broker object. )
              if order is filled based on price bar data
                  call Can Fill Order to see if the order can be filled _
                      if filled call Entry Order Filled
 
          Update Equity and Risk Statistics for system
 
      next ( system fill process )
 
      for ( each system )
  call After Bar script
      next ( system )
 
      Intra-day loop end -- increment date/time by test.timeIncrement and _
                                        loop until day is finished
 
      for ( each system do after daily trading )
 
          for ( each instrument in the portfolio with an open position )
              for ( each block in system )
                  call Adjust Stops
   
          call Initialize Risk Management
   
          for ( each instrument in the portfolio with an open position )
              call Compute Instrument Risk
   
          call Compute Risk Adjustments
   
          for ( each instrument in the portfolio with an open position )
              call Adjust Instrument Risk
   
          Update Equity and Risk Statistics for system again
   
          for ( each block in the system )
              for ( each instrument in the portfolio that is primed )
                  call After Instrument Day
              call After Trading Day
   
      next ( system )
 
      call After Trading Day for the Global Suite System
 
      Update Equity and Risk Statistics for test
      Compute end-of-day, month, and year statistics as _
                          appropriate for all systems and test
   
  next ( day in test )
   
  Closeout open positions on the close of the _
                  Last day if not generating orders
   
  for ( each block in all systems )
      call After Test script
   
  if generating orders
      Generate Orders
       
next ( test in simulation )
 
for ( each block in all systems )
  call After Simulation script

 

 


Edit Time: 9/25/2017 09:09:58 AM


Topic ID#: 214

 

Created with Help & Manual 7 and styled with Premium Pack Version 2.80 © by EC Software