mhallsmoore / qstrader

QuantStart.com - QSTrader backtesting simulation engine.
https://www.quantstart.com/qstrader/
MIT License
2.88k stars 856 forks source link

Could not subscribe ticker SPY as no data CSV found for pricing. #271

Closed studyquant closed 7 months ago

studyquant commented 5 years ago

I am using python 3.7

from collections import deque import datetime

import numpy as np

from qstrader import settings from qstrader.strategy.base import AbstractStrategy from qstrader.event import SignalEvent, EventType from qstrader.compat import queue from qstrader.trading_session import TradingSession

class MovingAverageCrossStrategy(AbstractStrategy): """ Requires: ticker - The ticker symbol being used for moving averages events_queue - A handle to the system events queue short_window - Lookback period for short moving average long_window - Lookback period for long moving average """ def init( self, ticker, events_queue, short_window=100, long_window=300, base_quantity=100 ): self.ticker = ticker self.events_queue = events_queue self.short_window = short_window self.long_window = long_window self.base_quantity = base_quantity self.bars = 0 self.invested = False self.sw_bars = deque(maxlen=self.short_window) self.lw_bars = deque(maxlen=self.long_window)

def calculate_signals(self, event):
    if (
        event.type == EventType.BAR and
        event.ticker == self.ticker
    ):
        # Add latest adjusted closing price to the
        # short and long window bars
        self.lw_bars.append(event.adj_close_price)
        if self.bars > self.long_window - self.short_window:
            self.sw_bars.append(event.adj_close_price)

        # Enough bars are present for trading
        if self.bars > self.long_window:
            # Calculate the simple moving averages
            short_sma = np.mean(self.sw_bars)
            long_sma = np.mean(self.lw_bars)
            # Trading signals based on moving average cross
            if short_sma > long_sma and not self.invested:
                print("LONG %s: %s" % (self.ticker, event.time))
                signal = SignalEvent(
                    self.ticker, "BOT",
                    suggested_quantity=self.base_quantity
                )
                self.events_queue.put(signal)
                self.invested = True
            elif short_sma < long_sma and self.invested:
                print("SHORT %s: %s" % (self.ticker, event.time))
                signal = SignalEvent(
                    self.ticker, "SLD",
                    suggested_quantity=self.base_quantity
                )
                self.events_queue.put(signal)
                self.invested = False
        self.bars += 1

def run(config, testing, tickers, filename):

Backtest information

title = ['Moving Average Crossover Example on AAPL: 100x300']
initial_equity = 10000.0
start_date = datetime.datetime(2000, 1, 1)
end_date = datetime.datetime(2014, 1, 1)

# Use the MAC Strategy
events_queue = queue.Queue()
strategy = MovingAverageCrossStrategy(
    tickers[0], events_queue,
    short_window=100,
    long_window=300
)

# Set up the backtest
backtest = TradingSession(
    config, strategy, tickers,
    initial_equity, start_date, end_date,
    events_queue, title=title,
    benchmark=tickers[0],
)
results = backtest.start_trading(testing=testing)
return results

if name == "main":

Configuration data

testing = False
config = settings.from_file(
    settings.DEFAULT_CONFIG_FILENAME, testing
)
tickers = ["SPY"]
filename = None
run(config, testing, tickers, filename)

_cffi_ext.c D:\Anaconda3.7\lib\site-packages\zmq\backend\cffi__pycache_cffi_ext.c(213): fatal error C1083: Cannot open include file: 'sys/un.h': No such file or directory _cffi_ext.c D:\Anaconda3.7\lib\site-packages\zmq\backend\cffi__pycache_cffi_ext.c(213): fatal error C1083: Cannot open include file: 'sys/un.h': No such file or directory _cffi_ext.c D:\Anaconda3.7\lib\site-packages\zmq\backend\cffi\pycache_cffi_ext.c(213): fatal error C1083: Cannot open include file: 'sys/un.h': No such file or directory _cffi_ext.c D:\Anaconda3.7\lib\site-packages\zmq\backend\cffi\pycache___cffi_ext.c(213): fatal error C1083: Cannot open include file: 'sys/un.h': No such file or directory Traceback (most recent call last): File "E:/区块链工作/区块链工作总结整理/回测系统开发/qstrader-master/examples/moving_average_cross_backtest.py", line 108, in run(config, testing, tickers, filename) File "E:/区块链工作/区块链工作总结整理/回测系统开发/qstrader-master/examples/moving_average_cross_backtest.py", line 94, in run benchmark=tickers[0], File "D:\Anaconda3.7\lib\site-packages\qstrader\trading_session.py", line 52, in init self._config_session() File "D:\Anaconda3.7\lib\site-packages\qstrader\trading_session.py", line 68, in _config_session end_date=self.end_date File "D:\Anaconda3.7\lib\site-packages\qstrader\price_handler\yahoo_daily_csv_bar.py", line 38, in init self.bar_stream = self._merge_sort_ticker_data() File "D:\Anaconda3.7\lib\site-packages\qstrader\price_handler\yahoo_daily_csv_bar.py", line 68, in _merge_sort_ticker_data df = pd.concat(self.tickers_data.values()).sort_index() File "D:\Anaconda3.7\lib\site-packages\pandas\core\reshape\concat.py", line 225, in concat copy=copy, sort=sort) File "D:\Anaconda3.7\lib\site-packages\pandas\core\reshape\concat.py", line 259, in init__ raise ValueError('No objects to concatenate') ValueError: No objects to concatenate Could not subscribe ticker SPY as no data CSV found for pricing.

Do not know how to solve it

JamesKBowler commented 5 years ago

Prob an unsupported pandas version with deprecated functions

On Thu, 20 Dec 2018, 06:26 StudyQuant <notifications@github.com wrote:

I am using python 3.7

from collections import deque import datetime

import numpy as np

from qstrader import settings from qstrader.strategy.base import AbstractStrategy from qstrader.event import SignalEvent, EventType from qstrader.compat import queue from qstrader.trading_session import TradingSession

class MovingAverageCrossStrategy(AbstractStrategy): """ Requires: ticker - The ticker symbol being used for moving averages events_queue - A handle to the system events queue short_window - Lookback period for short moving average long_window - Lookback period for long moving average """ def init( self, ticker, events_queue, short_window=100, long_window=300, base_quantity=100 ): self.ticker = ticker self.events_queue = events_queue self.short_window = short_window self.long_window = long_window self.base_quantity = base_quantity self.bars = 0 self.invested = False self.sw_bars = deque(maxlen=self.short_window) self.lw_bars = deque(maxlen=self.long_window)

def calculate_signals(self, event):

if (

    event.type == EventType.BAR and

    event.ticker == self.ticker

):

    # Add latest adjusted closing price to the

    # short and long window bars

    self.lw_bars.append(event.adj_close_price)

    if self.bars > self.long_window - self.short_window:

        self.sw_bars.append(event.adj_close_price)

    # Enough bars are present for trading

    if self.bars > self.long_window:

        # Calculate the simple moving averages

        short_sma = np.mean(self.sw_bars)

        long_sma = np.mean(self.lw_bars)

        # Trading signals based on moving average cross

        if short_sma > long_sma and not self.invested:

            print("LONG %s: %s" % (self.ticker, event.time))

            signal = SignalEvent(

                self.ticker, "BOT",

                suggested_quantity=self.base_quantity

            )

            self.events_queue.put(signal)

            self.invested = True

        elif short_sma < long_sma and self.invested:

            print("SHORT %s: %s" % (self.ticker, event.time))

            signal = SignalEvent(

                self.ticker, "SLD",

                suggested_quantity=self.base_quantity

            )

            self.events_queue.put(signal)

            self.invested = False

    self.bars += 1

def run(config, testing, tickers, filename):

Backtest information

title = ['Moving Average Crossover Example on AAPL: 100x300'] initial_equity = 10000.0 start_date = datetime.datetime(2000, 1, 1) end_date = datetime.datetime(2014, 1, 1)

Use the MAC Strategy

events_queue = queue.Queue()

strategy = MovingAverageCrossStrategy(

tickers[0], events_queue,

short_window=100,

long_window=300

)

Set up the backtest

backtest = TradingSession(

config, strategy, tickers,

initial_equity, start_date, end_date,

events_queue, title=title,

benchmark=tickers[0],

)

results = backtest.start_trading(testing=testing)

return results

if name == "main":

Configuration data

testing = False config = settings.from_file( settings.DEFAULT_CONFIG_FILENAME, testing ) tickers = ["SPY"] filename = None run(config, testing, tickers, filename)

cffi_ext.c D:\Anaconda3.7\lib\site-packages\zmq\backend\cffi_pycache_cffi_ext.c(213): fatal error C1083: Cannot open include file: 'sys/un.h': No such file or directory

cffi_ext.c D:\Anaconda3.7\lib\site-packages\zmq\backend\cffi_pycache_cffi_ext.c(213): fatal error C1083: Cannot open include file: 'sys/un.h': No such file or directory

cffi_ext.c D:\Anaconda3.7\lib\site-packages\zmq\backend\cffi_pycache_cffi_ext.c(213): fatal error C1083: Cannot open include file: 'sys/un.h': No such file or directory

cffi_ext.c D:\Anaconda3.7\lib\site-packages\zmq\backend\cffi_pycache_cffi_ext.c(213): fatal error C1083: Cannot open include file: 'sys/un.h': No such file or directory Traceback (most recent call last): File "E:/区块链工作/区块链工作总结整理/回测系统开发/qstrader-master/examples/moving_average_cross_backtest.py", line 108, in run(config, testing, tickers, filename) File "E:/区块链工作/区块链工作总结整理/回测系统开发/qstrader-master/examples/moving_average_cross_backtest.py", line 94, in run benchmark=tickers[0], File "D:\Anaconda3.7\lib\site-packages\qstrader\trading_session.py", line 52, in init self._config_session() File "D:\Anaconda3.7\lib\site-packages\qstrader\trading_session.py", line 68, in _config_session end_date=self.end_date File "D:\Anaconda3.7\lib\site-packages\qstrader\price_handler\yahoo_daily_csv_bar.py", line 38, in init self.bar_stream = self._merge_sort_ticker_data() File "D:\Anaconda3.7\lib\site-packages\qstrader\price_handler\yahoo_daily_csv_bar.py", line 68, in _merge_sort_ticker_data df = pd.concat(self.tickers_data.values()).sort_index() File "D:\Anaconda3.7\lib\site-packages\pandas\core\reshape\concat.py", line 225, in concat copy=copy, sort=sort) File "D:\Anaconda3.7\lib\site-packages\pandas\core\reshape\concat.py", line 259, in init raise ValueError('No objects to concatenate') ValueError: No objects to concatenate Could not subscribe ticker SPY as no data CSV found for pricing.

Do not know how to solve it

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mhallsmoore/qstrader/issues/271, or mute the thread https://github.com/notifications/unsubscribe-auth/AXGWkHoF7FLC0fT0fxbjCxRdbMCgUzp_ks5u6y2JgaJpZM4Zble4 .