""" SMA Crossover Strategy Buy when short SMA crosses above long SMA (golden cross). Sell when short SMA crosses below long SMA (death cross). Timeframe: 1h Pairs: BTC/USDT, ETH/USDT, SOL/USDT, BNB/USDT """ import numpy as np import pandas as pd from datetime import datetime, timedelta, timezone from pandas import DataFrame from typing import Optional, Union from freqtrade.strategy import ( IStrategy, Trade, Order, PairLocks, BooleanParameter, CategoricalParameter, DecimalParameter, IntParameter, RealParameter, ) import talib.abstract as ta from technical import qtpylib class SmaCrossStrategy(IStrategy): """ Simple SMA Crossover strategy. Buy: SMA short crosses above SMA long. Sell: SMA short crosses below SMA long. """ INTERFACE_VERSION = 3 can_short: bool = False # ROI table: exit at these profit levels after N minutes minimal_roi = { "0": 0.05, # 5% profit at any time "60": 0.03, # 3% after 60 minutes "120": 0.01, # 1% after 120 minutes "240": 0.0, # break-even after 240 minutes } stoploss = -0.07 # 7% stop loss # Trailing stop trailing_stop = True trailing_stop_positive = 0.01 trailing_stop_positive_offset = 0.03 trailing_only_offset_is_reached = True timeframe = "1h" process_only_new_candles = True use_exit_signal = True exit_profit_only = False ignore_roi_if_entry_signal = False # Hyperoptable parameters buy_sma_short = IntParameter(5, 30, default=10, space="buy", optimize=True) buy_sma_long = IntParameter(20, 100, default=50, space="buy", optimize=True) # Number of candles needed before producing valid signals startup_candle_count: int = 100 order_types = { "entry": "limit", "exit": "limit", "stoploss": "market", "stoploss_on_exchange": False, } order_time_in_force = {"entry": "GTC", "exit": "GTC"} plot_config = { "main_plot": { "sma_short": {"color": "blue"}, "sma_long": {"color": "orange"}, }, "subplots": { "Volume": { "volume": {"color": "green"}, }, }, } def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """Calculate SMA indicators.""" # Calculate SMAs for the full range to support hyperopt for period in range(5, 101): dataframe[f"sma_{period}"] = ta.SMA(dataframe, timeperiod=period) return dataframe def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """Buy when short SMA crosses above long SMA.""" sma_short = dataframe[f"sma_{self.buy_sma_short.value}"] sma_long = dataframe[f"sma_{self.buy_sma_long.value}"] dataframe.loc[ ( (qtpylib.crossed_above(sma_short, sma_long)) & (dataframe["volume"] > 0) ), "enter_long", ] = 1 return dataframe def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """Sell when short SMA crosses below long SMA.""" sma_short = dataframe[f"sma_{self.buy_sma_short.value}"] sma_long = dataframe[f"sma_{self.buy_sma_long.value}"] dataframe.loc[ ( (qtpylib.crossed_below(sma_short, sma_long)) & (dataframe["volume"] > 0) ), "exit_long", ] = 1 return dataframe