Running a Backetest in CloudQuant Mariner
Overview
In this lesson, you will learn about running a simulation in CloudQuant.
Creating a Strategy
A strategy is a subclass of Strategy that defines calculations and actions that are taken in response to events. These events can be market trades, order imbalances, or timed events. In the example below, %%writefile BasicCounter.py creates this file with the code located directly below it. The following is a very simple strategy that just counts the number of trades of a particular stock:
%%writefile BasicCounter.py
#Code below will be saved into 'BasicCounter.py', this line included
from cloudquant.interfaces import Strategy
class BasicCounter(Strategy):
def on_start(self, md, order, service, account):
self.count = 0
def on_trade(self, event, md, order, service, account):
if event.symbol == self.symbol:
self.count += 1
def on_finish(self, market, order, service, account):
print "COUNT: ", self.count
return self.count
Validating a Strategy
The function client.validate_client_strategy(filename, depends_on, data_files) validates the client strategy for common sources of errors.
Checks include the following: - There should be only one class defined within 'filename'. - The one class should be derived from Strategy, in cloudquant.interfaces. - The strategy script should conform to valid Python syntax. - All Strategy and Order method calls should provide the proper number of arguments.
If any of these checks fail, a StrategyError exception will be raised. The following code validates our BasicCounter script.
client.validate_client_strategy('BasicCounter.py', None, None)
Uploading a Strategy
To connect to CloudQuant, you must get an authentication token for your user and pass it into the Client constructor.
from cloudquant.client import Client
client = Client(master='https://tath.cloudquant.com', token='YOUR_TOKEN')
Next, we want to upload our strategy.
client.strategy_upload('BasicCounter.py')
Running a Simulation
Once we've uploaded our trading strategy, we can create and submit tests using that strategy. In the example below, the BasicCounter strategy will execute on January 2, 2015 from 9:30am to 4pm on the AAPL, IBM, and LCNB stock symbols.
submission_id = client.submit(strategy='BasicCounter',
start_date='2015-01-02',
end_date='2015-01-02',
symbols=['AAPL', 'IBM', 'LCNB'],
start_time='09:30:00',
end_time='16:00:00')