Trading Automation with DecisionRules Workflows

Develop a 200-Day SMA Trading Strategy Using DecisionRules and Polygon—No Coding Required

Michal Roman
Fullstack Developer
?
Use Cases
Check icon A checkmark inside a circle signifying "yes" Minus icon A minus inside a circle signifying "no" PROS Icon A plus symbol representing positive aspects or benefits. CONS Icon A minus symbol representing negative aspects or drawbacks.

For those interested in automated trading strategies, building a 200-day Simple Moving Average (SMA) system can now be done without any programming expertise. Leveraging the new Workflow engine from DecisionRules alongside real-time financial data from Polygon, you can seamlessly create, automate, and optimize your trading strategy. This approach empowers users to track market trends and implement responsive strategies through an intuitive, no-code interface, enhancing both efficiency and accessibility. Discover how to simplify complex trading strategies and elevate your investment approach with cutting-edge, code-free solutions.

This is where DecisionRules.io comes in as a no-code platform that allows you to visually design, test, and implement trading strategies using real-time market data. By integrating with APIs like Polygon.io, DecisionRules.io enables you to automate stock analysis and decision-making based on simple financial logic, without needing to know how to code.

In this post, we'll walk through how you can leverage DecisionRules.io to build a trading strategy that uses the 200-day moving average to determine when to buy or sell stocks - all without the need for coding.

About Polygon

Polygon.io is a powerful data platform that provides real-time and historical market data, enabling developers and businesses to access comprehensive stock, forex, and cryptocurrency information. Known for its speed, reliability, and extensive data coverage, Polygon.io is an essential tool for building and automating financial applications. Its robust API allows users to fetch detailed market data, analyze trends, and make informed decisions—making it an ideal choice for integrating with DecisionRules.io to enhance automated trading strategies.

Overview of the Tutorial

In this guide, we will walk you through the steps required to build a trading strategy that uses the 200-day moving average to determine when to buy or sell stocks - all without the need for coding.

Step 1: Set Up Accounts

1.1 Sign up for DecisionRules

DecisionRules allows you to create workflows and decision tables that can be used to automate various business logic, including trading strategies.

  • Go to DecisionRules and sign up for a free account.
  • Once signed in, navigate to the Workflows section where we will build our trading logic.

1.2 Sign up for Polygon.io

Polygon.io provides real-time and historical market data, which we’ll use to fetch stock prices for our strategy.

  • Go to Polygon and create an account.
  • Obtain your API Key from the dashboard, which will be needed to call their API and retrieve stock data.

Step 2: Create a Blank Workflow in DecisionRules

2.1 Create a New Workflow

  • After logging in to DecisionRules.io, in the sidebar menu click on Create, go to the Workflow tab.
  • Select Blank Workflow and name it something like "Trading Strategy."
  • In the workflow, we’ll add steps to fetch stock price data from Polygon.io and apply the trading logic based on the 200-day moving average.

Step 3. Set up input and output model

Before building the workflow, you'll need to define the input and output models. These models will structure the data you feed into and get from your trading strategy.

3.1 Input Model

The input model defines the data you’ll provide for the workflow. In this case, you need to specify the following values:

  • ticker: The stock symbol (e.g., AAPL, TSLA) you want to track.
  • moving_average_value: The number of days for calculating the moving average (e.g., 200 for the 200-day moving average).
  • moving_average_type: The type of moving average to use (e.g., simple moving average, exponential moving average).

3.2 Output Model

The output model will structure the decision made by your trading strategy:

  • position: The trading action (e.g., Buy, Sell) based on the price comparison.
  • offset: The difference between the current stock price and the moving average, giving you insight into how far off the price is from the moving average.

Step 4. Workflow design

To implement the strategy in DecisionRules.io using Polygon.io, you will need a few specific nodes to structure the logic and API interactions. Here's a breakdown of the necessary nodes:

4.1. Global Variable Node (globalVariable)

This node will store your API key and any additional variables (like ticker symbol, etc.) needed throughout the workflow.

  • Purpose: Store API credentials and reusable values.
  • Variables:
    • api_key: Your Polygon.io API key.

This allows you to manage these values centrally without hardcoding them in multiple places.

4.2. REST API Node 1: Get Actual Price (get_last_price)

This REST API node will call the Polygon API to fetch the current or previous day’s price of the stock.

  • Method: GET
  • URL: https://api.polygon.io/v2/aggs/ticker/{input.ticker}/prev?adjusted=true&apiKey={globalVariable.api_key}
  • Variables:
    • Value {input.ticker} will be used from our  input model.
    • Value {globalVaruable.api_key} with the value stored in the global variable node.

Expected Response: The API will return a JSON object with the stock’s closing price.

More info in polygon documentation HERE

4.3. REST API Node 2: Get 200-Day Moving Average (get_moving_averages)

This node will fetch the 200-day historical price data to calculate the 200-day simple moving average.

  • Method: GET
  • URL: https://api.polygon.io/v1/indicators/{input.moving_average_type}/{input.ticker}?timespan=day&adjusted=true&window={input.moving_average_value}&series_type=close&order=desc&limit=1&apiKey={globalVariable.api_key}
  • Variables:
    • {input.ticker} is the value from the input model.
    • {globalVariable.api_key} is dynamically pulled from the global variable.
    • {input.moving_average_type} is the value from the input model.
    • {input.moving_average_value} is the value from the input model.

Expected Response: A JSON object with the historical closing prices for the last 200 days.

More info in polygon documentation HERE

4.4. Assign Node: Extract Required Data

This node is responsible for parsing and extracting the needed values from the responses of both REST API nodes.

  • Purpose:
    • From the first API call, extract the last_closed_price.
    • From the second API call, extract the moving_average_value.
    • From both prices we count the offset how far is the value from moving average

In the Assign Node, you can directly use DecisionRules functions to extract and manipulate the API response data efficiently. Here’s how to implement the extraction and calculation:

  1. Extract last_closed_price using the PICK function:
    • Expression: last_closed_price <- PICK({get_last_price.data.results}, "[0].c")
    • This extracts the closing price (c) from the first result in the array returned by the get_last_price API.
  2. Extract 200_day_moving_average from the moving averages API response:
    • Expression: 200_day_moving_average <- PICK({get_moving_averages.data}, "results.values[0].value")
    • This picks the moving average value from the results array in the response from the get_moving_averages API.
  3. Calculate the offset and round it to two decimal places:
    • Expression: output.offset <- ROUND(({last_closed_price} - {200_day_moving_average})/{200_day_moving_average}, 2)
    • This calculates the offset as the percentage difference between the last_closed_price and 200_day_moving_average and rounds it to two decimal places.

4.5. Switch Node: Price Comparison (Decision Node)

This node will decide whether the current price is above or below the 200-day moving average.

  • Switch Logic:
    • Condition 1: If last_closed_price > 200_day_moving_average, trigger a Buy signal.
    • Condition 2: If last_closed_price < 200_day_moving_average, trigger a Sell signal.

4.6. Connect Assign Nodes for Each Outcome of the Switch Node

After the Switch node decides whether the stock’s price is above or below the 200-day moving average, you’ll need to assign specific outcomes for each decision. This is where the Assign nodes come in, which will map the trading position (Buy/Sell) to the output model.

Final Output

Let’s review the final output of the workflow. Using the 200-day moving average, the system evaluates the stock’s current price and has determined that a bull order is the best course of action with the last closed price 16% above the moving average value. This output reflects the automated decision-making that can be achieved through the no-code workflow. You can download the full rule here:

Conclusion

This tutorial shows you how to  create a fully functional trading strategy without writing a single line of code. DecisionRules.io provides a user-friendly, visual interface where the logic is handled through nodes, and integration with external APIs (like Polygon.io) enables you to automate stock trading decisions based on real-time and historical data. This workflow design helps you build, test, and adjust trading strategies quickly and efficiently, even if you're unfamiliar with programming. This example demonstrates a simple use of the workflow, but you can extend it to create much more complex strategies as needed.

Enjoyed the read? Let’s take the next step together.