Skip to content

Creating Your First Strategy on Blueshift®

A simple strategy

The strategy we are going to implement is a simple buy and hold. We buy a specific stock, investing 50% of our portfolio value (rest remains in cash). We continuously monitor the position value to keep this percentage constant at 50%. If the stock rallies, we sell some of it. If the stock price goes down, we buy more of it to achieve the 50% target. We start with an initial amount of capital and are interested to check how this simple scheme (with a twist of mean-reversion) holds up, starting from January 2019, till date.

Creating a new strategy

Once you log in, you will land up in the main strategy list page at /research. Click on the create folder button. On the dialog box, create a new folder named example. Click to go inside this folder. Then hit the create strategy button. Name the strategy as buy_and_hold, select Buy and Hold (NYSE) for the template (doesn't matter, we will erase all template codes later). You will land up in the strategy page as shown below.

strategy

The black editor area is our code editor.

The strategy code

Let's now delete the entire pre-filled template code and copy-paste the following snippet in the code editor.

from blueshift.api import symbol, order_target_percent

def initialize(context):
    """
        called once at the start of the run.
    """
    # convert the symbol string to asset and store a reference to it
    context.stock = symbol('AAPL')

def handle_data(context,data):
    """
        called every 'bar' (minute or daily depending on the data 
        frequency).
    """
    # order to target 50% of portfolio value in the asset
    order_target_percent(context.stock, 0.5)

What is happening here? We first begin with importing some functions we are going to use from the core engine. We use two API functions - symbol and order_target_percent. The first one takes in a string and returns an asset object understood by the engine. The second one is an automatic order sizing function - takes in an asset and a target percentage number, and computes the number of units we need to buy and sell, so that we are at our target investment of 50% of the portfolio value.

We save the asset reference within a function called initialize. It is a special function. Once you run the strategy, the platform will automatically look for this function in your strategy code and run it only once - at the very beginning. The actual trading happens within another such special function called handle_data. This function is called by the platform at every time bar (every minute or daily, depending on the input data frequency).

Running backtests

Fill in your backtest parameters in the input fields (dataset, start date, end date and initial capital). We must choose a dataset that contains the assets we have in our code, in this case let's go with the US Equities dataset (where AAPL trades). Choose the start date as 1st January 2019, and then pick the last available date for end date. Leave the capital at default 10,000 dollars. Now hit the run button on the right (or the shortcut Ctrl+U). This will start the quick backtest, streaming the results along with some basic performance metrics. You can continue to work on the code while it runs.

results

You can also run a full-fledged backtest by hitting the run button. Full backtest runs in the background, and you will see a notification ( notification on top right.) when it is ready. Click (or follow the bread-crumb) to go to the backtest page (you can also click the results button on the backtest page) once the status is complete. A full backtest report has a much richer set of details of the simulation, including transactions history.

Going live

Move on to the live trading section!