Click here to Login




Backtest Trading Strategies Programmatically and on Schedule

Updated on 2020-01-08





Using the QuantShare API, it is possible to perform many tasks programmatically. One of the things you can do is backtest and get results of a specific strategy. This can be accomplished using the Script Editor tool.

To open the script editor, select "Tools -> Script Editor" then create a new script using "File -> New".

To access and run backtests, you need to use the "Simulator" object. This object allows you to retrieve the list of trading systems, create trading systems, update them, backtest them and optimize them.

In the script editor, type "Simulator." (without the double quotes), after typing the dot, a list of available methods will be displayed.

Examples to help you get started

To list all your trading systems, you can for example use:

string[] ts = Simulator.GetTradingSystems("");
// Category is empty here to get all trading systems



To create a new trading system based on an existing one (copy) then update the start date:

QSTradingSystem ts = Simulator.CreateTradingSystem("", "MyStrategy");

ts.CopySettingsFromTradingSystem("", "ExistingStrategyName"); // Replace "ExistingStrategyName" with the name of an existing strategy you have

ts.StartDate = new DateTime(2000, 10, 1); // Year, Month, Day

ts.Save();



To backtest a specific strategy:

SimulatorTask task = Simulator.Backtest("", "MyStrategy", true);
// The last parameter indicates whether to show a report or not (Run the backtest silently)


Note that this function returns a "SimulatorTask" variable.
We can use this variable to check whether the simulation ended and to access the report programmatically.

The following example will run the backtest silently then display a message with the simulation annual return:

SimulatorTask task = Simulator.Backtest("", "MyStrategy", false);

while(!task.IsCompleted){
App.Main.Sleep(100);
}

// Returns one report after a backtest (we access it using [0] next to Reports variable) and multiple reports after an optimization

double annualReturn = task.Reports[0].AnnualReturn;

MessageBox.Show("The strategy annual return is: " + annualReturn);



Backtest on Schedule

You can schedule backtests to run at specific times or after an event (such as a new file added or updated in a directory) using the "Task Manager" tool of QuantShare.

First, you need to create a script to run the backtest.

- After that, open the task manager using "Tools -> Task Manager".
- In the new "Task Manager" control, click on "Click here to add a task".
- Specify the task trigger settings then click on "Add" button at the bottom/left corner to add the script to be executed when the task is triggered.
- Name your task then click on "Add" at the bottom/right corner.

The task will be added in the task scheduler of the Windows operating system and when executed will execute the script.

You can even have the script open automatically QuantShare if it is closed.
To enable this, check the "Execute even if application is not loaded" field in the "Add Task" form.












no comments (Log in)

QuantShare Blog
QuantShare
Search Posts




QuantShare
Recent Posts

Create Graphs using the Grid Tool
Posted 1480 days ago

Profile Graphs
Posted 1585 days ago

QuantShare
Previous Posts

Lock, Link and Undock your Charts
Posted 2395 days ago

How to Use the Percentage Scale
Posted 2944 days ago


More Posts

Back







QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright © 2024 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items



Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.