Introduction to SMA (S) Strategy
The SMA (S) strategy, developed by MachinaLabs Ltd., is a simple yet effective trading algorithm designed to identify potential buy and sell signals in financial markets. SMA stands for Simple Moving Average, a widely used technical indicator in the field of trading analysis.
How it Works:
The strategy calculates two Simple Moving Averages (SMAs) based on user-defined parameters, SMA Fast and SMA Slow. When the faster SMA crosses above the slower SMA, it generates a “cross-over” signal, indicating a potential buying opportunity. Conversely, when the faster SMA crosses below the slower SMA, it generates a “cross-under” signal, indicating a potential selling opportunity.
Trading Logic:
When a cross-over signal occurs, the strategy executes a “Buy” order for the selected market.
When a cross-under signal occurs, the strategy executes a “Sell” order for the selected market.
If there is no cross-over or cross-under signal, the strategy holds its position in the market.
Customizability:
The SMA (S) strategy offers flexibility through user-configurable parameters. Traders can adjust the time periods for the fast and slow SMAs, allowing for customization based on market conditions and trading preferences.
Visualization:
To aid in decision-making, the strategy visualizes the SMAs on the chart, highlighting the cross-over and cross-under events. This feature provides a clear visual representation of the strategy’s signals.
Usage:
The SMA (S) strategy is available on the MachinaTrader platform, empowering traders with a straightforward yet powerful tool for trading various financial instruments. It serves as a valuable addition to any trader’s toolkit, providing insights into potential entry and exit points in the markets.
Note:
As with any trading strategy, users are encouraged to backtest the SMA (S) strategy on historical data and exercise caution when applying it to live trading. Past performance does not guarantee future results, and risk management is essential in trading.
# Strategy Name: SMA (S) # Author: MachinaLabs Ltd. # Update Date: 2022-06-19 # Importing required libraries import sys, talib, requests, json, numpy, base64, json, enum, datetime, array, math, mtCommon # Define strategy parameters and constants STRAT_NAME = 'SMA (S)' VERSION = '1.0' STRAT_TYPE = mtCommon.SimpleStrategy NUM_CANDLES = 25 SMA_FAST = 'SMA Fast' SMA_SLOW = 'SMA Slow' X_OVER = 'X-Over' X_UNDER = 'X-Under' # ------------------------------------------------------------------- # Global methods - Not specific to any market, the config is for internal use only # ------------------------------------------------------------------- def onSendParams(): # Build up parameters to send to MT _mt.addNumericParameter(SMA_FAST, 12, True) # This parameter will override the NUM_CANDLES value if it is higher _mt.addNumericParameter(SMA_SLOW, 18, True) # This parameter will override the NUM_CANDLES value if it is higher # ------------------------------------------------------------------- # Per market methods - Execution triggered per market (as per machina configuration) # ------------------------------------------------------------------- def onInit(): _mt.logInfoP('onInit called [' + STRAT_NAME + ', v' + VERSION + ']') def onTick(currentDate, candles, config): # Create an instance of the Calcs class to perform calculations calcs = Calcs(candles) # Check if candles data is available if len(candles.C) == 0: _mt.logError('No candles returned for ' + config.MARKET) return # Implement the trading logic based on the calculated indicators if calcs.hasCrossedOver(): _mt.buy(config.MARKET) elif calcs.hasCrossedUnder(): _mt.sell(config.MARKET) else: _mt.hold(config.MARKET) def onSendIndicatorModels(candles): # Create an instance of the Calcs class to perform calculations calcs = Calcs(candles) # Add simple moving average indicators to the chart _mt.addSimpleIndicator(SMA_FAST, calcs.smaFast, 'orange', mtCommon.Line, mtCommon.Solid, 3) _mt.addSimpleIndicator(SMA_SLOW, calcs.smaSlow, 'purple', mtCommon.Line, mtCommon.Solid, 3) # Draw the cross overs if they are switched on xovers = _mt.createIndicatorModel('Cross Overs') xovers.addShapeIndicator(X_OVER, calcs.crossOver, 'rgba(45, 188, 32, 0.5)', mtCommon.TriangleUp, mtCommon.AboveBar, mtCommon.Small) xovers.addShapeIndicator(X_UNDER, calcs.crossUnder, 'rgba(188, 32, 32, 0.5)', mtCommon.TriangleDown, mtCommon.BelowBar, mtCommon.Small) _mt.addIndicatorModel(xovers) class Calcs: def __init__(self, candles): self.onCalculate(candles) def onCalculate(self, candles): self.IDX = len(candles.T) - 1 # Calculate Simple Moving Averages (SMAs) based on user-defined parameters self.smaFast = talib.func.SMA(candles.C, _mt.getParameter(SMA_FAST)) self.smaSlow = talib.func.SMA(candles.C, _mt.getParameter(SMA_SLOW)) # Check for cross over and cross under events between SMAs self.crossOver = _mt.crossOver(self.smaFast, self.smaSlow) self.crossUnder = _mt.crossUnder(self.smaFast, self.smaSlow) def hasCrossedOver(self): # Return the most recent cross over event return self.crossOver[len(self.crossOver) - 1] def hasCrossedUnder(self): # Return the most recent cross under event return self.crossUnder[len(self.crossUnder) - 1]