### Run Zipline Trading Algorithm Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla.ipynb This Python code executes a Zipline trading algorithm using the `run_algorithm` function. It specifies the start and end dates, the `initialize` and `handle_data` functions, and the initial capital base. The output `perf` contains performance data. ```python perf = zipline.run_algorithm(start=datetime(2017, 1, 5, 0, 0, 0, 0, pytz.UTC), end=datetime(2018, 3, 1, 0, 0, 0, 0, pytz.UTC), initialize=initialize, capital_base=100000, handle_data=handle_data) ``` -------------------------------- ### List Zipline Data Bundles Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla.ipynb This command lists all the data bundles that are currently available on the machine. It's a fundamental step to understand what data is accessible for Zipline backtesting. ```shell !zipline bundles ``` -------------------------------- ### Zipline Algorithm Initialization and Data Handling Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla.ipynb This Python code defines the `initialize` and `handle_data` functions for a Zipline trading algorithm. The `initialize` function sets up initial context, including the list of stocks and commission/slippage settings. The `handle_data` function executes trades once at the beginning of the algorithm's run. ```python import pandas as pd import matplotlib.pyplot as plt from matplotlib import style from zipline.finance import commission, slippage import pytz pytz.all_timezones from zipline.api import order, record, symbol, set_benchmark import zipline import matplotlib.pyplot as plt from datetime import datetime stocks = ['AAPL', 'MSFT'] def initialize(context): context.has_ordered = False context.stocks = stocks # Explicitly set the commission/slippage to the "old" value until we can # rebuild example data. # github.com/quantopian/zipline/blob/master/tests/resources/ # rebuild_example_data#L105 context.set_commission(commission.PerShare(cost=.0075, min_trade_cost=1.0)) context.set_slippage(slippage.VolumeShareSlippage()) def handle_data(context, data): if not context.has_ordered: for stock in context.stocks: order(symbol(stock), 100) context.has_ordered = True def _test_args(): """Extra arguments to use when zipline's automated tests run this example. """ return { 'start': pd.Timestamp('2008', tz='utc'), 'end': pd.Timestamp('2013', tz='utc'), } ``` -------------------------------- ### Zipline Extension Module Setup (Python) Source: https://context7.com/tqtensor/vn_quantopian/llms.txt Configures Zipline to recognize a custom trading calendar and data bundle ('HOSE') by automatically registering it on import. This involves defining the start and end sessions and specifying the data source using `csvdir_equities`. Requires Pandas. ```python # In extension.py - loaded automatically by Zipline import pandas as pd from zipline.data.bundles import register from zipline.data.bundles.csvdir import csvdir_equities # Auto-register on import start_session = pd.Timestamp('2016-01-04', tz='utc') end_session = pd.Timestamp('2099-01-01', tz='utc') register( 'hose', csvdir_equities( ['daily'], '/home/user/documents/project/csvdir', ), calendar_name='HOSE', start_session=start_session, end_session=end_session ) # To use: Set ZIPLINE_EXTENSION environment variable # export ZIPLINE_EXTENSION=/path/to/extension.py # Then run: zipline run -f algorithm.py --bundle hose ``` -------------------------------- ### Load and Retrieve Assets from Zipline Bundle Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla.ipynb This Python code loads a specific Zipline data bundle ('quantopian-quandl') and then retrieves all available assets within that bundle using the asset finder. This is useful for inspecting the universe of tradable assets. ```python from zipline.data import bundles bundle = bundles.load('quantopian-quandl') bundle.asset_finder.retrieve_all(bundle.asset_finder.sids) ``` -------------------------------- ### Visualize Zipline Algorithm Performance Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla.ipynb This Python snippet visualizes the performance of a Zipline trading algorithm. It sets the plot style to 'ggplot', calculates the cumulative returns of the portfolio, and displays the plot with a legend. This helps in evaluating the strategy's effectiveness. ```python style.use('ggplot') perf.portfolio_value.pct_change().fillna(0).add(1).cumprod().sub(1).plot(label='portfolio') plt.legend(loc=2) plt.show() ``` -------------------------------- ### Clean and Re-ingest Zipline Data Bundle Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla.ipynb This command sequence cleans a specified data bundle ('quantopian-quandl') before a certain date and then re-ingests it. This is useful for ensuring data freshness or correcting ingestion issues. ```shell !zipline clean --bundle quantopian-quandl --before 2099-01-01 !zipline ingest -b quantopian-quandl ``` -------------------------------- ### Register Custom Zipline Data Bundle Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla_algo.ipynb Registers a custom data bundle named 'hose' for Zipline, using CSV files from a specified directory. It defines the start and end sessions for the data and specifies the trading calendar to be used. ```python try: zipline.data.bundles.unregister('hose') except Exception as e: print(e) print("# Register the HOSE bundle") start_session = pd.Timestamp('2016-01-04', tz='utc') end_session = pd.Timestamp('2019-04-22', tz='utc') register( 'hose', csvdir_equities( ['daily'], '/home/doidoi/Documents/cryptocean/csvdir', ), calendar_name='HOSE', # HOSE Vietnam start_session=start_session, end_session=end_session ) ``` -------------------------------- ### Suppress Warnings in Python Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla.ipynb This Python snippet suppresses warning messages, which can be useful for cleaning up the output of scripts or notebooks. It imports the `warnings` module and sets the filter to ignore all warnings. ```python import warnings warnings.filterwarnings("ignore") ``` -------------------------------- ### Run Zipline Backtest Algorithm Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla_algo.ipynb Executes the Zipline trading algorithm. It specifies the backtest period, the `initialize` and `handle_data` functions, the initial capital, the trading calendar, and the data frequency. The 'hose' bundle is used for data. ```python perf = zipline.run_algorithm(start=datetime(2016, 4, 1, 0, 0, 0, 0, pytz.timezone('Asia/Ho_Chi_Minh')), end=datetime(2019, 4, 22, 0, 0, 0, 0, pytz.timezone('Asia/Ho_Chi_Minh')), initialize=initialize, handle_data=handle_data, capital_base=1e9, trading_calendar=get_calendar('HOSE'), data_frequency='daily', default_extension=True, bundle='hose') ``` -------------------------------- ### Initialize Zipline Algorithm and Schedule Rebalancing Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla_algo.ipynb Initializes the Zipline algorithm by attaching the defined pipeline and scheduling the `rebalance` function to run every day. This sets up the trading strategy's execution frequency. ```python def initialize(context): attach_pipeline(make_pipeline(), name='my_pipeline') # Rebalance each day. In daily mode, this is equivalent to putting # `rebalance` in our handle_data, but in minute mode, it's equivalent to # running at the start of the day each day. schedule_function(rebalance, date_rules.every_day()) ``` -------------------------------- ### Import Libraries for Zipline Algorithm Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla_algo.ipynb Imports necessary libraries for Zipline, including data handling (pandas, pytz), plotting (matplotlib), and Zipline's core components for algorithm execution, pipelines, and data bundles. ```python import warnings from datetime import datetime import matplotlib.pyplot as plt import pandas as pd import pytz import zipline from matplotlib import style from six import viewkeys from trading_calendars import get_calendar from zipline.api import ( attach_pipeline, date_rules, order_target_percent, pipeline_output, record, schedule_function, ) from zipline.data.bundles import register from zipline.data.bundles.csvdir import csvdir_equities from zipline.pipeline import Pipeline from zipline.pipeline.factors import RSI ``` -------------------------------- ### Zipline Data Bundle Commands Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla_algo.ipynb Shell commands to manage Zipline data bundles. '!zipline bundles' lists available bundles, and '!zipline clean' removes old data, while '!zipline ingest' downloads and prepares data for a specified bundle. ```bash #!zipline bundles ``` ```bash # zipline.data.bundles.bundles ``` ```bash !zipline clean --bundle hose --before 2099-01-01 !zipline ingest -b hose ``` -------------------------------- ### Handle Data for Zipline Algorithm Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla_algo.ipynb Defines the `handle_data` function for the Zipline algorithm, which is responsible for retrieving pipeline output for the current day and making it available to the context. This function is called for each trading day. ```python def handle_data(context, data): context.pipeline_data = pipeline_output('my_pipeline') ``` -------------------------------- ### Zipline Rebalancing Logic Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla_algo.ipynb Implements the rebalancing logic for the trading algorithm. It uses pipeline output to determine which assets to go long and short, places orders to achieve a target percentage in each, and manages existing positions, removing assets that are no longer in the target universe. ```python def rebalance(context, data): # Pipeline data will be a dataframe with boolean columns named 'longs' and # 'shorts'. pipeline_data = context.pipeline_data all_assets = pipeline_data.index longs = all_assets[pipeline_data.longs] shorts = all_assets[pipeline_data.shorts] record(universe_size=len(all_assets)) print(context.blotter.current_dt) print("longs: " + str(longs)) print("shorts: " + str(shorts)) print("portfolio value: " + str(context.portfolio.portfolio_value)) # Build a equal-weight, long-short portfolio. one_fourth = 1.0 / 4.0 for asset in longs: order_target_percent(asset, one_fourth) for asset in shorts: order_target_percent(asset, -one_fourth) # Remove any assets that should no longer be in our portfolio. portfolio_assets = longs | shorts positions = context.portfolio.positions for asset in viewkeys(positions) - set(portfolio_assets): # This will fail if the asset was removed from our portfolio because it # was delisted. if data.can_trade(asset): order_target_percent(asset, 0) ``` -------------------------------- ### Visualize Zipline Algorithm Performance Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla_algo.ipynb Visualizes the performance of the Zipline algorithm by plotting the cumulative percentage change of the portfolio value. It customizes the plot appearance, formats the y-axis to show percentages, and saves the plot to a file. ```python style.use('seaborn-dark') dpi = 300 plt.figure(figsize=(1920/dpi, 1080/dpi), dpi=dpi) ax = perf.portfolio_value.pct_change().fillna(0).add(1).cumprod().sub(1).plot(label='portfolio-pecentage-change', color='green') vals = ax.get_yticks() ax.set_yticklabels(['{:,.0%}'.format(x) for x in vals]) plt.legend(loc=0) plt.savefig('zipline_vanilla_algo.png', bbox_inches='tight') plt.show() ``` -------------------------------- ### Basic Buy-and-Hold Algorithm with Zipline (Python) Source: https://context7.com/tqtensor/vn_quantopian/llms.txt A simple Zipline trading algorithm that implements a buy-and-hold strategy for a predefined list of Vietnamese stocks (VCB, ACB, VIC, FPT). It initializes the context with these stocks, sets commission and slippage models, and places an order to buy 100 shares of each on the first data point. The algorithm also records the portfolio value over time. ```python import zipline from zipline.api import order, record, symbol from zipline.finance import commission, slippage import pandas as pd import pytz from datetime import datetime def initialize(context): """Initialize the algorithm with Vietnamese stocks""" context.has_ordered = False context.stocks = ['VCB', 'ACB', 'VIC', 'FPT'] # Vietnamese tickers # Set commission and slippage models context.set_commission(commission.PerShare(cost=.0075, min_trade_cost=1.0)) context.set_slippage(slippage.VolumeShareSlippage()) def handle_data(context, data): """Execute trades on market data updates""" if not context.has_ordered: for stock in context.stocks: order(symbol(stock), 100) # Buy 100 shares of each context.has_ordered = True # Record portfolio value for analysis record(portfolio_value=context.portfolio.portfolio_value) # Run backtest on Vietnamese market perf = zipline.run_algorithm( start=datetime(2017, 1, 5, 0, 0, 0, 0, pytz.UTC), end=datetime(2018, 3, 1, 0, 0, 0, 0, pytz.UTC), initialize=initialize, capital_base=100000, handle_data=handle_data ) # Expected output: DataFrame with columns like: # - portfolio_value # - returns # - pnl # - transactions ``` -------------------------------- ### Zipline Data Ingestion Commands (Bash) Source: https://context7.com/tqtensor/vn_quantopian/llms.txt Command-line instructions to manage and ingest data bundles within Zipline. It includes commands to clean existing data for a specific bundle ('hose') and to ingest new data. The `zipline bundles` command lists all registered data bundles. ```bash # Clean existing bundle data zipline clean --bundle hose --before 2099-01-01 # Ingest the HOSE bundle zipline ingest -b hose # List all available bundles zipline bundles ``` -------------------------------- ### Performance Visualization with Matplotlib (Python) Source: https://context7.com/tqtensor/vn_quantopian/llms.txt Generates a performance chart of cumulative percentage returns from Zipline backtest results using Matplotlib. It calculates and plots portfolio returns, formatting the y-axis for percentages. Requires Matplotlib and Pandas. ```python import matplotlib.pyplot as plt from matplotlib import style # Load backtest results (perf DataFrame from run_algorithm) style.use('seaborn-dark') dpi = 300 plt.figure(figsize=(1920/dpi, 1080/dpi), dpi=dpi) # Calculate cumulative returns cumulative_returns = ( perf.portfolio_value .pct_change() .fillna(0) .add(1) .cumprod() .sub(1) ) # Plot with percentage formatting ax = cumulative_returns.plot( label='Portfolio Returns', color='green' ) vals = ax.get_yticks() ax.set_yticklabels(['{:,.0%}'.format(x) for x in vals]) plt.legend(loc=0) plt.xlabel('Date') plt.ylabel('Cumulative Return') plt.title('Vietnamese Stock Portfolio Performance') plt.savefig('zipline_vanilla_algo.png', bbox_inches='tight') plt.show() # Expected output: PNG chart showing cumulative percentage returns over time ``` -------------------------------- ### Define Zipline Trading Pipeline Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla_algo.ipynb Defines a Zipline pipeline that calculates the Relative Strength Index (RSI) and identifies top (longs) and bottom (shorts) assets based on RSI values. This pipeline serves as the data source for the trading algorithm. ```python def make_pipeline(): rsi = RSI() return Pipeline( columns={ 'longs': rsi.top(3), 'shorts': rsi.bottom(3), }, ) ``` -------------------------------- ### Zipline Data Bundle Registration for HOSE (Python) Source: https://context7.com/tqtensor/vn_quantopian/llms.txt Registers a custom data bundle named 'hose' for Zipline, pointing to a directory containing daily stock data in CSV format. It specifies the calendar to use ('HOSE') and a date range for the data. This allows Zipline to ingest and process Vietnamese stock market data. ```python from zipline.data.bundles import register from zipline.data.bundles.csvdir import csvdir_equities import pandas as pd # Register the HOSE bundle with date range start_session = pd.Timestamp('2016-01-04', tz='utc') end_session = pd.Timestamp('2099-01-01', tz='utc') register( 'hose', csvdir_equities( ['daily'], '/home/user/documents/project/csvdir', # Path to CSV data ), calendar_name='HOSE', start_session=start_session, end_session=end_session ) # Expected CSV format in csvdir/daily/{ticker}.csv: # date,close,open,high,low,volume # 22/04/2019,67.9,67.1,68.1,67,313210 # 19/04/2019,68,67.9,68.5,67.9,232550 ``` -------------------------------- ### Pipeline-Based RSI Strategy for Vietnamese Stocks (Python) Source: https://context7.com/tqtensor/vn_quantopian/llms.txt Implements a mean-reversion strategy using the RSI indicator with Zipline's Pipeline API. It identifies oversold and overbought stocks to create long and short positions, rebalancing the portfolio daily. Requires Zipline and associated libraries. ```python from zipline.api import ( attach_pipeline, date_rules, order_target_percent, pipeline_output, record, schedule_function, ) from zipline.pipeline import Pipeline from zipline.pipeline.factors import RSI from six import viewkeys from trading_calendars import get_calendar import zipline import pytz from datetime import datetime def make_pipeline(): """Create pipeline with RSI factor for Vietnamese stocks""" rsi = RSI() return Pipeline( columns={ 'longs': rsi.top(3), # Top 3 oversold stocks 'shorts': rsi.bottom(3), # Top 3 overbought stocks }, ) def rebalance(context, data): """Rebalance portfolio based on pipeline output""" pipeline_data = context.pipeline_data all_assets = pipeline_data.index longs = all_assets[pipeline_data.longs] shorts = all_assets[pipeline_data.shorts] print(f"Date: {context.blotter.current_dt}") print(f"Longs: {longs}") print(f"Shorts: {shorts}") print(f"Portfolio Value: {context.portfolio.portfolio_value}") # Equal weight long-short portfolio one_fourth = 1.0 / 4.0 for asset in longs: order_target_percent(asset, one_fourth) for asset in shorts: order_target_percent(asset, -one_fourth) # Exit positions no longer in strategy portfolio_assets = longs | shorts positions = context.portfolio.positions for asset in viewkeys(positions) - set(portfolio_assets): if data.can_trade(asset): order_target_percent(asset, 0) def initialize(context): """Setup pipeline and schedule rebalancing""" attach_pipeline(make_pipeline(), name='my_pipeline') schedule_function(rebalance, date_rules.every_day()) def handle_data(context, data): """Update pipeline data each trading day""" context.pipeline_data = pipeline_output('my_pipeline') # Run RSI strategy on Vietnamese stocks perf = zipline.run_algorithm( start=datetime(2016, 4, 1, 0, 0, 0, 0, pytz.timezone('Asia/Ho_Chi_Minh')), end=datetime(2019, 4, 22, 0, 0, 0, 0, pytz.timezone('Asia/Ho_Chi_Minh')), initialize=initialize, handle_data=handle_data, capital_base=1e9, # 1 billion VND trading_calendar=get_calendar('HOSE'), data_frequency='daily', default_extension=True, bundle='hose' ) # Analyze results print(f"Total Return: {perf.portfolio_value.iloc[-1] / perf.portfolio_value.iloc[0] - 1:.2%}") print(f"Sharpe Ratio: {perf.sharpe:.2f}") ``` -------------------------------- ### HOSE Exchange Calendar Implementation (Python) Source: https://context7.com/tqtensor/vn_quantopian/llms.txt Defines a custom trading calendar for the Ho Chi Minh City Stock Exchange (HOSE) compatible with Zipline. It specifies trading hours, timezone (Asia/Ho_Chi_Minh), and fetches precomputed holidays from a Google Sheet. This enables Zipline to correctly interpret trading days and hours for the Vietnamese market. ```python from datetime import time import pandas as pd from pytz import timezone from zipline.utils.calendars import TradingCalendar # HOSE calendar with precomputed holidays from Google Sheets class HOSEExchangeCalendar(TradingCalendar): """ Exchange calendar for HOSE (Ho Chi Minh City Stock Exchange) Trading hours: 9:00-15:00 Asia/Ho_Chi_Minh """ name = 'HOSE' tz = timezone('Asia/Ho_Chi_Minh') open_times = ( (None, time(9, 1)), ) close_times = ( (None, time(15, 0)), ) @property def precomputed_holidays(self): # Fetches from Google Spreadsheet import requests from io import BytesIO url = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vTH623s5LSTmljwPPqSlYniruc8u3OF16et08zYVsgPEno1cLAkCG0wsfhxsaCjTyl6suKOywDcWTVN/pub?gid=0&single=true&output=csv' r = requests.get(url) dateparse = lambda x: pd.datetime.strptime(x, '%m/%d/%Y') df = pd.read_csv(BytesIO(r.content), parse_dates=['date'], date_parser=dateparse) return pd.to_datetime(df.date) ``` -------------------------------- ### Filter Warnings in Python Source: https://github.com/tqtensor/vn_quantopian/blob/master/starter/zipline_vanilla_algo.ipynb Suppresses warning messages, which can be useful for cleaning up output during script execution, especially in data-intensive or iterative processes. ```python warnings.filterwarnings("ignore") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.