How to Create An Algorithm to Back Test Your Trading Strategy in ThinkorSwim
Entertainment
Introduction
ThinkorSwim is a powerful tool that allows traders to back test their trading strategies using its thinkScript language. By utilizing thinkScript, traders can automate their back testing process and quickly find profitable strategies. In this article, we will walk through the process of creating a strategy using thinkScript in ThinkorSwim.
Step 1: Initial Setup
To start, open ThinkorSwim and remove all studies from the chart. We will start with a blank chart.
Next, go to Studies and select Edit Studies. Click on Strategies and then select Create to create a new strategy.
In the Strategy Editor, give your strategy a name. For this example, let's name it "Moving Average Crossover".
Step 2: Building the Strategy
In the Strategy Editor, you will see a "Add order" command. Leave a few lines above it to add your code.
To create the code for your strategy, you will need to initialize variables. In thinkScript, variables are used to store data. For this example, let's create two variables: short_ma_length
and long_ma_length
.
input short_ma_length = 9;
input long_ma_length = 20;
Next, you will need to define your moving averages using the variables you created above. In this example, we will be using simple moving averages (SMA).
def short_ma = simpleMovingAverage(close, short_ma_length);
def long_ma = simpleMovingAverage(close, long_ma_length);
Now that we have our moving averages defined, we can build our strategy. In this example, we will buy when the short moving average crosses above the long moving average, and sell when the short moving average crosses below the long moving average.
def buy = short_ma > long_ma;
def sell = short_ma < long_ma;
Step 3: Executing the Strategy
Once we have our strategy defined, we need to add the code to execute the strategy. This is where the Add order command comes in.
To buy, we use the command Buy to open
and set it equal to our buy
variable. This tells ThinkorSwim to open a position when the buy
condition is true.
AddOrder(OrderType.BUY_TO_OPEN, buy, 100);
To sell, we use the command Sell to close
and set it equal to our sell
variable. This tells ThinkorSwim to close the position when the sell
condition is true.
AddOrder(OrderType.SELL_TO_CLOSE, sell, 100);
Step 4: Running the Strategy
Once you have finished creating your strategy, click Apply and OK. Now you can see the trades on the chart.
You can add additional studies, such as Floating P&L, to see the profit or loss of the trades according to your strategy.
Summary
In summary, ThinkorSwim's thinkScript language allows traders to create and back test their trading strategies. By utilizing variables, moving averages, and specific conditions, traders can automate their trading strategies and quickly find profitable opportunities.
Keywords: ThinkorSwim, thinkScript, back test, trading strategy, moving average crossover, simple moving average, buy, sell, execute, profit or loss, automated trading.
FAQ:
Q: Can I use thinkScript to back test other technical indicators? A: Yes, thinkScript allows you to incorporate various technical indicators, such as RSI or MACD, into your back testing strategy.
Q: Is thinkScript case sensitive? A: Generally, thinkScript is not case sensitive, but it is good practice to use consistent capitalization to avoid confusion.
Q: Can I adjust the number of shares bought or sold in the strategy? A: Yes, you can change the number of shares traded in the strategy by modifying the "AddOrder" command with your desired quantity.
Q: Can I back test my strategy on different time frames? A: Yes, thinkScript automatically adjusts the strategy to the selected time frame, allowing you to back test on various intervals, such as daily or weekly.