Skip to content

Commit

Permalink
Issue 129 fix backtest tutorial doc (#133)
Browse files Browse the repository at this point in the history
* Update doc to match the demo script
  • Loading branch information
marekbais authored Nov 6, 2023
1 parent 5c0dd3d commit d8030d1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
3 changes: 2 additions & 1 deletion demo_scripts/strategies/simple_ma_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import matplotlib.pyplot as plt

from qf_lib.backtesting.events.time_event.regular_time_event.calculate_and_place_orders_event import \
Expand Down Expand Up @@ -76,7 +77,7 @@ def main():
ticker = DummyTicker("AAA")

# configuration
session_builder = container.resolve(BacktestTradingSessionBuilder) # type: BacktestTradingSessionBuilder
session_builder = container.resolve(BacktestTradingSessionBuilder)
session_builder.set_frequency(Frequency.DAILY)
session_builder.set_backtest_name(backtest_name)
session_builder.set_data_provider(daily_data_provider)
Expand Down
22 changes: 19 additions & 3 deletions docs/source/tutorials/first_strategy_backtest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ is used by the Backtest Trading Session to execute our trading strategy, so it's
.. code-block::
class SimpleMAStrategy(AbstractStrategy):
"""
strategy, which computes every day, before the market open time, two simple moving averages
(long - 20 days, short - 5 days) and creates a buy order in case if the short moving average
is greater or equal to the long moving average.
"""
def __init__(self, ts: BacktestTradingSession, ticker: Ticker):
super().__init__(ts)
self.broker = ts.broker
Expand All @@ -143,14 +148,15 @@ is used by the Backtest Trading Session to execute our trading strategy, so it's
long_ma_len = 20
short_ma_len = 5
# Use data handler to download last 20 daily close prices and use them to compute the moving averages
long_ma_series = self.data_handler.historical_price(self.ticker, PriceField.Close, long_ma_len)
long_ma_price = long_ma_series.mean()
short_ma_series = long_ma_series.tail(short_ma_len)
short_ma_price = short_ma_series.mean()
if short_ma_price >= long_ma_price:
# Place a buy Market Order, adjusting the position to 100% of the portfolio
# Place a buy Market Order, adjusting the position to a value equal to 100% of the portfolio
orders = self.order_factory.target_percent_orders({self.ticker: 1.0},
MarketOrder(), TimeInForce.DAY)
else:
Expand Down Expand Up @@ -197,10 +203,11 @@ the `start_trading()` on the Backtest Trading Session!
import matplotlib.pyplot as plt
from qf_lib.backtesting.events.time_event.regular_time_event.calculate_and_place_orders_event import \
CalculateAndPlaceOrdersRegularEvent
from qf_lib.backtesting.strategies.abstract_strategy import AbstractStrategy
from qf_lib.backtesting.strategies.signal_generators import OnBeforeMarketOpenSignalGeneration
plt.ion() # required for dynamic chart
plt.ion() # required for dynamic chart, good to keep this at the beginning of imports
from demo_scripts.common.utils.dummy_ticker import DummyTicker
from demo_scripts.demo_configuration.demo_data_provider import daily_data_provider
Expand All @@ -216,6 +223,11 @@ the `start_trading()` on the Backtest Trading Session!
class SimpleMAStrategy(AbstractStrategy):
"""
strategy, which computes every day, before the market open time, two simple moving averages
(long - 20 days, short - 5 days) and creates a buy order in case if the short moving average
is greater or equal to the long moving average.
"""
def __init__(self, ts: BacktestTradingSession, ticker: Ticker):
super().__init__(ts)
self.broker = ts.broker
Expand All @@ -228,13 +240,15 @@ the `start_trading()` on the Backtest Trading Session!
long_ma_len = 20
short_ma_len = 5
# Use data handler to download last 20 daily close prices and use them to compute the moving averages
long_ma_series = self.data_handler.historical_price(self.ticker, PriceField.Close, long_ma_len)
long_ma_price = long_ma_series.mean()
short_ma_series = long_ma_series.tail(short_ma_len)
short_ma_price = short_ma_series.mean()
if short_ma_price >= long_ma_price:
# Place a buy Market Order, adjusting the position to a value equal to 100% of the portfolio
orders = self.order_factory.target_percent_orders({self.ticker: 1.0},
MarketOrder(), TimeInForce.DAY)
else:
Expand All @@ -247,11 +261,13 @@ the `start_trading()` on the Backtest Trading Session!
def main():
# settings
backtest_name = 'Simple MA Strategy Demo'
start_date = str_to_date("2010-01-01")
end_date = str_to_date("2015-03-01")
ticker = DummyTicker("AAA")
# configuration
session_builder = container.resolve(BacktestTradingSessionBuilder)
session_builder.set_frequency(Frequency.DAILY)
session_builder.set_backtest_name(backtest_name)
Expand Down

0 comments on commit d8030d1

Please sign in to comment.