def algoRunNext(self): if(self.hasNext()): currentSeriesValue = self.timeDF.iloc[self.index]; self.index += 1; start, end = self._getStartEndTimeFromCurrentSeries(currentSeriesValue); algo = TradingAlgorithm(initialize=self.initialize, handle_data=self.handle_data, identifiers=self.stockName) data = loader.load_bars_from_yahoo(stocks=self.stockName, start=start, end=end); return algo.run(data); else: raise ValueError;
import pandas as pd from zipline import TradingAlgorithm from zipline.api import order, sid from zipline.data.loader import load_bars_from_yahoo # creating time interval start = pd.Timestamp('2008-01-01', tz='UTC') end = pd.Timestamp('2013-01-01', tz='UTC') # loading the data input_data = load_bars_from_yahoo( stocks=['AAPL', 'MSFT'], start=start, end=end, ) #checking if I can merge this def initialize(context): context.has_ordered = False def handle_data(context, data): if not context.has_ordered: for stock in data: order(sid(stock), 100) context.has_ordered = True algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data)
ax2 = plt.subplot(212, sharex=ax1) results.AAPL.plot(ax=ax2) ax2.set_ylabel('AAPL price (USD)') # Show the plot. plt.gcf().set_size_inches(18, 8) plt.show() # Note: this if-block should be removed if running # this algorithm on quantopian.com if __name__ == '__main__': from datetime import datetime import pytz from zipline.algorithm import TradingAlgorithm from zipline.data.loader import load_bars_from_yahoo import pandas as pd # Set the simulation start and end dates start = pd.Timestamp('2008-01-01', tz='UTC') end = pd.Timestamp('2013-01-01', tz='UTC') # Load price data from yahoo. data = load_bars_from_yahoo(stocks=['AAPL'], indexes={}, start=start, end=end) # Create and run the algorithm. algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data) results = algo.run(data) analyze(results=results)
stocks = ['AAPL', 'MSFT'] def initialize(context): context.has_ordered = False context.stocks = stocks def handle_data(context, data): if not context.has_ordered: for stock in context.stocks: order(symbol(stock), 100) context.has_ordered = True if __name__ == '__main__': # creating time interval start = pd.Timestamp('2008-01-01', tz='UTC') end = pd.Timestamp('2013-01-01', tz='UTC') # loading the data input_data = load_bars_from_yahoo( stocks=stocks, start=start, end=end, ) algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data) results = algo.run(input_data)
import pytz from datetime import datetime from zipline.api import order, symbol, record, order_target from zipline.algorithm import TradingAlgorithm from zipline.data.loader import load_bars_from_yahoo import pyexcel # Load data manually from Yahoo! finance start = datetime(2011, 1, 1, 0, 0, 0, 0, pytz.utc).date() end = datetime(2012, 1, 1, 0, 0, 0, 0, pytz.utc).date() data = load_bars_from_yahoo(stocks=['SPY'], start=start, end=end) #code def initialize(context): context.security = symbol('SPY') #code def handle_data(context, data): MA1 = data[context.security].mavg(50) MA2 = data[context.security].mavg(100) date = str(data[context.security].datetime)[:10] current_price = data[context.security].price current_positions = context.portfolio.positions[symbol('SPY')].amount cash = context.portfolio.cash value = context.portfolio.portfolio_value current_pnl = context.portfolio.pnl if (MA1 > MA2) and current_positions == 0:
def initialize(context): context.has_ordered = False context.stocks = stocks def handle_data(context, data): if not context.has_ordered: for stock in context.stocks: order(symbol(stock), 100) context.has_ordered = True if __name__ == '__main__': # creating time interval start = pd.Timestamp('2008-01-01', tz='UTC') end = pd.Timestamp('2013-01-01', tz='UTC') # loading the data input_data = load_bars_from_yahoo( stocks=stocks, start=start, end=end, ) algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data, identifiers=stocks) results = algo.run(input_data)
# 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 pandas as pd from zipline import TradingAlgorithm from zipline.api import order, sid from zipline.data.loader import load_bars_from_yahoo # creating time interval start = pd.Timestamp('2008-01-01', tz='UTC') end = pd.Timestamp('2013-01-01', tz='UTC') # loading the data input_data = load_bars_from_yahoo( stocks=['AAPL', 'MSFT'], start=start, end=end, ) def initialize(context): context.has_ordered = False def handle_data(context, data): if not context.has_ordered: for stock in data: order(sid(stock), 100) context.has_ordered = True