Skip to content

Building a Strategy

Jason O edited this page Feb 3, 2023 · 4 revisions

Strategies

With StockBench, we give you the freedom to create and test different strategies.

In this tutorial, we'll show you how to create a strategy from scratch and provide some pointers to help you create your own.

Imports

Before we build a strategy, you'll need these imports.

import time
from StockBench.simulator import Simulator
import StockBench.constants as const

Python Dictionaries

The strategy is defined inside of a Python dictionary. my_strategy = {}

Picking a Start Date

StockBench provides support for selecting a start and end date so you can simulate over any size window where data is available. Let's suppose that we want our simulation window to be 1 year. The simulation uses UNIX timestamps. We use the time.time() fragment to get the today's date in UNIX. StockBench's const file provides some helpful conversions that you can use. In this example, we use the const.SECONDS_1_YEAR fragment to get the UNIX date a year ago.

my_strategy = {
    'start': int(time.time()) - const.SECONDS_1_YEAR
}

Picking an End Date

Similar to the start date, the end date is also a UNIX timestamp. In this example, we're going to to set the end date of the simulation to today's date converted to UNIX, once again using time.time().

my_strategy = {
    'start': int(time.time()) - const.SECONDS_1_YEAR,
    'end': int(time.time())
}

Buying vs. Selling

Now that we have our window setup, we want to add some rules. Rules fall into 2 categories: buy or sell.

my_strategy = {
    'start': int(time.time()) - const.SECONDS_1_YEAR,
    'end': int(time.time()),
    'buy': {},
    'sell':{}
}

A Basic Rule

In this example, we're going to add a simple buy rule, 'price': '>100'. This translates to, 'buy if the price is greater than 100'. The syntax should be pretty easy to follow.

my_strategy = {
    'start': int(time.time()) - const.SECONDS_1_YEAR,
    'end': int(time.time()),
    'buy': {
         'price': '>100'
    },
    'sell':{}
}

Another Basic Rule

Now, we need to have a sell rule, or else our simulation is just going to buy once and never sell - useless. We'll add a simple stop loss to the sell rules section, 'stop_loss': '50'. This translates to, 'sell if the value of the position has decreased by $50 or more'.

my_strategy = {
    'start': int(time.time()) - const.SECONDS_1_YEAR,
    'end': int(time.time()),
    'buy': {
         'price': '>100'
    },
    'sell':{
        'stop_loss': '50'
    }
}

OR Rules

Before you go and start writing strategies, it's important to understand how they get executed. Let's say we have a buy section that looked like this:

'buy': {
   'price': '>100',
   'RSI': '<30'
}

The rules in either section (buy/sell) are executed as OR conditionals. This means that if either price > 100 OR RSI < 30 is true, the simulator will buy. No matter how many rules you have, they will be executed as OR conditionals. StockBench supports Multiple rules so go crazy! Don't worry, we'll explain AND conditions in the next section.

Note: The same rules apply to the sell section of the strategy!

AND Rules

If you're an experienced trader, you probably have a more complex trading strategy that involves multiple rules that need to be true before you buy. In this section, we introduce the AND comparison:

'buy': {
   'and1': {
        'price' : '>100',
        'RSI': '<30'
    }
}

Now, the simulator will only buy if price > 100 AND RSI < 30. Notice in this example how the AND block is titled 'and1'. This is because Python dictionaries need to have unique keys. It is perfectly fine to have multiple AND rules, they just need to have unique identifiers. If you're using an IDE, it should catch that error before you even run it. The solution is simple, if you have multiple AND rules, just add a unique number to the end of each 'and'.

Note: The same rules apply to the sell section of the strategy!

Finale

Putting it all together might look something like:

strategy = {
    'start': int(time.time()) - const.SECONDS_5_YEAR,
    'end': int(time.time()),
    'buy': {
        'and1': {
            'RSI': '<40',
            'SMA20': '>256',
            }
    },
    'sell': {
        'stop_loss': '50',
        'RSI': '>60
    }
}

Let's begin the translation with the buy section.

If RSI < 40 AND SMA20 > 256, then buy.

If loss > 50 OR RSI > 60, then sell.

Just like that, you have a strategy ready to go. See the example on the homepage for setting up and running your simulation.

Next Step: Rules