The back-testing in SAMOA is performed in stages:
- The daily and/or tick data are combined into one table. From that table, a stream of Events is produced. Each event carries the data that triggered it.
- Whenever an event is triggered, it’s data are fed as global variables to the JScript, and the corresponding event function is called.
- The JScript function interacts with the Book object to add trades, marks and what not. It’s up to the user to decide how to program this JScript.
The function that carries this workflow is ssRunStrategyS (or ssRunPortfolioS in the case of a portfolio of strategies)
select @simulationOutput = ssRunStrategyS( @xmldailyEvents, @tickEventsQuery, @tickFrom, @tickTo, @strategy, @yieldAll )
This function takes a strategy JScript (parameter “strategy”) and executes that script through the list of events. The input data is provided with the first 2 parameters:
- Low frequency events (parameter “xmldailyEvents”)
- High frequency events (parameter “tickEventsQuery”)
These parameters can be provided in the following combinations:
| xmlDailyEvents | tickEventsQuery | Result |
| YES | NO | The strategy only has low frequency events (for instance, a strategy that trades once per day, or once per month) |
| NO | YES | The strategy only has high frequency events (for instance a moving average of minute by minute data) |
| YES | YES | The strategy has low frequency events and high frequency events (for instance, say that the strategy uses a model that gets calibrated once at the beginning of the day, and it trades with that calibration throughout the day on each tick data). |
| NO | NO | Returns an empty output. |
The strategy script (parameter “strategy”) can perform actions for each or some events. The function ssRunStrategyS collects these actions and returns the table “simulationOutput”. This table is simply a log of events and actions. Some actions can be the inputs for booking system (trades, marks, etc). However, “simulationOuput” can contain any other information the user desires to output. The columns of “simulationOutput” are the following:
[TimeS] datetime [Event] xml [Outputs] xml
Parameter: xmldailyEvents
This is an xml variable containing the list of low frequency events (as rows). Typically the SQL clause “FOR XML AUTO” is used to convert a table with daily rows into an xml variable that is later passed as this parameter. For instance:
declare @xmldailyEvents xml = (select TS.* from table1 TS FOR XML AUTO, root('ROOT'), xmlschema('type'), TYPE);
Parameter: tickEventsQuery
This is a string with the actual SQL query used to retrieve the high frequency events. For instance:
declare @tickEventsQuery nvarchar(max) = 'select TimeS, [Last] from Trades_Final where Instrument=''AUD'' ORDER BY TimeS';
Parameter: tickFrom / tickTo
These two parameters provide the opening and closing time filter for the high frequency events provided in “tickEventsQuery”. Even high frequency event that is outside this range will get discarded. These parameters are optional, and can be provided in any combination:
| tickFrom | tickTo | Description |
| ‘time’ | ‘time’ | Events before tickFrom or after tickTo will get discarded. |
| ‘time’ | NULL | Only events before tickFrom will get discarded. |
| NULL | ‘time’ | Only events after tickTo will get discarded. |
| NULL | NULL | No events will get discarded. |
For instance:
declare @tickFrom time(0) = '13:00'; declare @tickTo time(0) = '16:00';
Parameter: strategy
This is a script written in JScript that defines the strategy. The user can program anything in this script. However, one of the most useful techniques is to attach JScript functions to events. An events can be one of the following:
| Event Name | Description |
| OpenBook | Fired once at the beginning of the simulation (typically used to initialize the book with capital) |
| OpenYear | Fired at the beginning of each year. |
| OpenMonth | Fired at the beginning of each month. |
| OpenDay | Fired at the beginning of each day (typically used to calculate daily parameters) |
| Trade | Fired with every trade. |
| CloseDay | Fired at the end of each day (typically used to mark the book) |
| CloseMonth | Fired at the end of each month. |
| CloseYear | Fired at the end of each year (typically used to change the capital base of the strategy by the accumulated P&L for the year) |
| CloseBook | Fired once at the end of the strategy (typically used to liquidate the positions since we’ve reached the end of trading). |
For every “Open” event there is a “Close” event, and vice versa (that’s the reason for the coloring in the table above).
Any JScript function with the name “event_X()” (where X is the name of the event) will only get executed when the event is triggered.
The columns of the low and high frequency rows (coming from the parameters xmldailyEvents and tickEventsQuery) are accessible through the JScript object “vars”, and the trading book is available through the JScript object “book”. Furthermore, to manipulate the book and guarantee that those actions get logged in “simulationOuput”, the user can access the JScript object “context” that exposes the following methods:
context.AddMark(string instrument, decimal p) context.AddTrade(DateTime? settlement, string instrument, decimal q, decimal p, string comment) context.AddCapital(decimal amount, string comment) context.AddIncome(decimal amount, string comment) context.AddBookTag(string tagName, string tagV)
Parameter: yieldAll
This is a debugging parameter. The default is 0, which will produce a “simulationOutput” containing only the rows that have values in the column [outputs]. When you pass a 1, “simulationOutut” will contain every single row. For performance reasons, a 1 should only be used when debugging the strategy.
An Example
Let’s assume that we want to run a strategy that will trade TY futures, during the week of 2008-01-14 to 2008-01-18.
- The front contract is the TYH08.
SELECT dbo.ssDateTimeToString('2008-01-14', 'yyyy-MM-dd dddd') as 'Day', dbo.ssToFrontContract( 'TY', '2008-01-14', null) as 'Front'
union all
SELECT dbo.ssDateTimeToString('2008-01-18', 'yyyy-MM-dd dddd') as 'Day', dbo.ssToFrontContract( 'TY', '2008-01-18', null) as 'Front'
- We can query the closing prices for that week:
select DateS, [Close] from DailyFut where Market='TY' and Expiry='H08' AND DateS >='2008-01-14' AND DateS<='2008-01-18' ORDER BY DateS
- These closing prices are the “Low frequency” events, which will be used to mark the book to market. Therefore, let’s convert the table to xml, and store it in the variable @xmldailyEvents for later use.
declare @xmldailyEvents xml = (
select DateS, [Close] from DailyFut TS where Market='TY' and Expiry='H08' AND DateS >='2008-01-14' AND DateS<='2008-01-18' ORDER BY DateS
FOR XML AUTO, root('ROOT'), xmlschema('type'), TYPE
);
- The “High frequency” events will be the minute prices (the following query returns 5,479 rows of tick data):
select TimeS, [Last] from Trades_Final where Instrument='TY' and TimeS>='2008-01-14' and TimeS<'2008-01-19' ORDER BY TimeS
- This sample strategy is very simple: It calculates the signal = (close – min)/(max – min) in the period between 9am and 1pm, and it takes a long position if the signal > 0.5, and a short if the signal < 0.5.
- Let’s program the strategy, and save the script in the @script variable:
declare @script nvarchar(max) = '...script...'
- Where ‘…script…’ is the following:
- Now we are ready to run the strategy, and save the simulation output in the table variable @so:
declare @so TABLE (
[TimeS] datetime,
[Event] xml,
[Outputs] xml
);
declare @xmldailyEvents xml = (
select DateS, [Close] from DailyFut TS where Market='TY' and Expiry='H08' AND DateS >='2008-01-14' AND DateS<='2008-01-18' order by DateS
FOR XML AUTO, root('ROOT'), xmlschema('type'), TYPE
);
----SQLTable: Run_Strategy----
insert into @so
select * from dbo.ssRunStrategyS(
@xmldailyEvents,
'select TimeS, [Last] from Trades_Final where Instrument=''TY'' and TimeS>=''2008-01-14'' and TimeS<''2008-01-19'' ORDER BY TimeS',
'9:00','13:00',
@script,
0
);
Select * from @so Order by TimeS;
- From @so it’s simple to extract the xml inputs for the Booking System:
select
ROW_NUMBER() OVER (ORDER BY TimeS, BookCallsT.Seq) as Seq,
TimeS,
convert(nvarchar(max), BookCallsT.xout) as 'XmlCall'
from @o1
CROSS APPLY
(
select *
from dbo.ssUnlistXmlList(Outputs)
) BookCallsT;
- Which produces the following P&L
- If we had executed the function ssRunStrategyS(…) with the parameter yieldAll set to 1 (for debugging), we would have gotten not only the 9 events in which we took actions (and therefore with values in the “Outputs” column), but also all all the others:
select * from dbo.ssRunStrategyS(
@xmldailyEvents,
'select TimeS, [Last] from Trades_Final where Instrument=''TY'' and TimeS>=''2008-01-14'' and TimeS<''2008-01-19'' ORDER BY TimeS',
'9:00','13:00',
@script,
1
);

(returned 1216 rows)
[...] Back-testing Strategies [...]
[...] Back-testing Strategies [...]
Direct Download Movies…
[...]while the sites we link to below are completely unrelated to ours, we think they are worth a read, so have a look[...]…
I was lokiong everywhere and this popped up like nothing!
oZ8hQI ftaybzahahcu
N20rLV , [url=http://cyhwaxcorskl.com/]cyhwaxcorskl[/url], [link=http://etagewwzoyyv.com/]etagewwzoyyv[/link], http://umqmcyyokwjf.com/
suwTVE vdjcwcpiutbb
pGTsLR , [url=http://bmcafgsejumd.com/]bmcafgsejumd[/url], [link=http://fkgkuoyqqkfv.com/]fkgkuoyqqkfv[/link], http://kyjpqklykxdk.com/
penny…
examine further down, are some entirely not related websites to mine, having said that, there’re most reputable article sources that we utilize…
2012 Fashion ideas5…
Coming home from very lonely places, all of us go a little mad: whether from great personal success, or just an all-night drive, we are the sole survivors of a world no one else has ever seen….
People must understand what they do. It’s necessary for doing all right.
2012 Fashion ideas h…
Coming home from very lonely places, all of us go a little mad: whether from great personal success, or just an all-night drive, we are the sole survivors of a world no one else has ever seen….
Sites You Should Check Out…
http://www.lifereader.co.uk/psychics/clairvoyant_psychic_readings...
Sites You Should Check Out…
http://www.driversday.getlisted.co.nz/where-can-i-drive-a-race-car...
Lavoro per il domani – uno sguardo di Yesturdays ad alcuni esempi…
È stato indicato questo esempio, via Megan Messer sopra Twitter e credilo per essere piuttosto informativo ed ugualmente il punto…
Sites You Should Check Out…
http://www.pitstop.getlisted.co.nz/how-much-is-a-wof...
Yeast Infection Treatment…
[...]following are a handful of hyper-links to online sites which we link to for the fact we believe they are worthwhile visiting[...]…
Recent Blogroll Additions……
[...]usually posts some very interesting stuff like this. If you’re new to this site[...]……
Angry Birds Online…
[...]the following are several url links to online sites that we connect to as we think they’re worth checking out[...]…
Entry Level Mechanical Engineering Jobs…
Entry Level Mechanical Engineering Jobs…
Sites You Should Check Out…
http://www.xpanda.getlisted.co.nz/screen-doors-nz...
Lavoro per il domani – uno sguardo di Yesturdays ad alcuni esempi…
È stato indicato questo esempio, via David McCormick sopra Twitter e credilo per essere estremamente informativo ed ugualmente il punto…
Sites You Should Check Out…
http://www.jagkitchens.getlisted.co.nz/nz-kitchen...
Sites You Should Check Out…
http://www.cedarlite.getlisted.co.nz/doors-auckland...
Sites You Should Check Out…
http://www.crest.getlisted.co.nz/new-zealand-business-sale...
Sites You Should Check Out…
http://www.driversday.getlisted.co.nz/where-can-i-drive-a-race-car...
Superb website…
[...]always a big fan of linking to bloggers that I love but don’t get a lot of link love from[...]……
Sites You Should Check Out…
http://www.dwhomes.getlisted.co.nz...
Enjoyed every bit of it…
I ought to say, as a good deal as I enjoyed reading what you had to say, I couldnt assist but shed interest following a although….
tramadol while pregnant…
google is great ..i got actually what I was looking for. Cheers….
Sites You Should Check Out…
http://www.hyspecs.getlisted.co.nz/pumps...
… [Trackback]…
[...] Informations on that Topic: nestorsulikowski.com/index.php/2009/06/back-testing-strategies/ [...]…
Sites You Should Check Out…
http://www.xpanda.getlisted.co.nz/screen-doors-nz...
Yesturdays Arbeit für Morgen – ein Blick auf einige Beispiele…
Wurde dieses Beispiel, über gezeigt David halign an Twitter und glauben Sie ihm vor um zu sein ein wenig informativ und auch der Punkt…
Just keep looking…
this blog appears not to be so well maintained these days but I like the post…
Check These Out…
[...]check below, are some totally unrelated websites to ours, however, they are most trustworthy sources that we use[...]…
Sites You Should Check Out…
http://www.mymmainfo.com...
Solar Lights…
[...]listed here are a handful of listings to web-sites we connect to as we feel these are well worth checking out[...]…
Sites You Should Check Out…
http://www.hyspecs.getlisted.co.nz/pumps...
Sites You Should Check Out…
http://www.xpanda.getlisted.co.nz/screen-doors-nz...
Best articles…
[..] More info about this article creator Click Here [..]…
Het werk van Yesturdays voor morgen – een blik op sommige voorbeelden…
Werd getoond dit voorbeeld, via jon Messer Tjilpen en veronderstel het om te zijn uiterst informatief en ook het punt…
Articles…
[..] More info Here [..]…
More info…
[..] More info here [..]…
… [Trackback]…
[...] Find More Informations here: nestorsulikowski.com/index.php/2009/06/back-testing-strategies/ [...]…
Yesturdays work for tomorrow – a look at some examples…
Noticed this example, via arnold halign on Live Journal and found it to be somewhat informative and too the point…
Sites You Should Check Out…
http://www.lifereader.com/psychics/clairvoyant_phone_readings...
Lucas…
i really want to add your site link at my blog, because your site help me a lot…please give me an anchor text if you want to add your link at my blogroll……
Great information…
This is certainly brilliant. Tip looked at this ingredients and we are stunned. We are most certainly curious about this type of pieces. That is why we appreciate his tip, and cost your time in this. Please keep modifying. They are very outstanding mar…
I always wanted to write in my site something like that but I guess you’r faster…
I was looking for crucial information on this subject. The information was important as I am about to launch my own portal. Thanks for providing a missing link in my business….
erotyczne…
Ostre ruchanie ogolonej cipy…
Great information…
This can be first-class. The checked out good results ease therefore we are baffled. We’re interested in this type of everything. An individual appreciate a rising gather, and value doing inside this. Please keep editing. They may be precisely valuabl…
important links…
Guys check out these important links…
………..
We guess in the meantime i am going to settle for bookmarking and also incorporating your current Rss to be able to my personal Search engines bank account. I anticipate fresh updates and may talk about this specific website together with my own Facebo…
Sites You Should Check Out…
http://www.rotarydirect.getlisted.co.nz/rx7-specs...
[...]Sites of interest we have a link to[...]……
[...]usually posts some very interesting stuff like this. If you’re new to this site[...]……
Great information…
This can be really good. Another looked at all the group material so we are bowled over. We are attracted to this kind of problems. Solitary appreciate more struggle, and profit doing while in this. Please keep enhancing. They’re very much practical m…
Great information…
This can be first-class. Tip checked out this disease material therefore we are impressed. We’re curious about this type of stories. Us appreciate your selected endeavor, and significance the effort in this. Please keep control. These are jolly robust…
You should check this out…
[...] Wonderful story, reckoned we could combine a few unrelated data, nevertheless really worth taking a look, whoa did one learn about Mid East has got more problerms as well [...]……
Trabalho para o amanhã – um olhar de Yesturdays em alguns exemplos…
Foi mostrado este exemplo, através de David Messer sobre Jornal vivo e acredite-o para ser extremamente informativo e demasiado o ponto…
[...] that is the end of this article. Here you’ll find some sites that we think you’ll appreciate, just click the links over[...]……
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose [...]……
No Deposit Poker…
hello!,I like your writing very much! share we keep up a correspondence extra approximately your article on AOL? I need an expert in this area to resolve my problem. May be that’s you! Having a look ahead to look you….
………..
We guess for the time being i will accept book-marking and incorporating the RSS feed to be able to our Yahoo and google accounts. I enjoy brand new improvements and will talk about this particular site together with our Fb party….
………..
My spouse and i suppose for the present time i’m going to settle for book-marking and also introducing your RSS feed to my personal Google account. We look ahead to brand new updates and will share this website along with our Myspace class….
[...] that is the end of this article. Here you’ll find some sites that we think you’ll appreciate, just click the links over[...]……
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose [...]……
[...]we like to honor many other internet sites on the web, even if they aren’t linked to us, by linking to them. Under are some webpages worth checking out[...]……
[...]Here are some of the sites we recommend for our visitors[...]……
Online Article……
[...]The information mentioned in the article are some of the best available [...]……
Great information…
This can be superior. A miami investigator looked at this coming testimony so we are confounded. We are interested in this sort of troubles. Human beings appreciate all the gather, and evaluate your precious time inside this. Please keep control. They …
Great information…
This is really good. Us checked out the offer pleasure therefore we are astonished. We are precisely interested in this kind of anything. Our team appreciate your sexual tip, and estimate your precious time with this. Please keep cutting. These are esp…
[...]always a big fan of linking to bloggers that I love but don’t get a lot of link love from[...]……
[...]just beneath, are numerous totally not related sites to ours, however, they are surely worth going over[...]……