Hi Team
I used to run my strategy without any problem earlier. But now I am not able to run it as it tells me to change lot of things in code. I tried changing it. But it still throws out lot of error.
Please help me.
*I noticed certain symols are not matching like M&M. L& Finance, Bajaj-Auto, Mc-Dowell-N etc
*Previously I used to select NIFTY DAILY as data. But now it is not there. How can I change my code to run it in daily time frame.
Following is my code. Please help me out.
from blueshift.pipeline import Pipeline
"""
Title: Buy and hold strategy
Description: This is a long only strategy which rebalces the portfolio weights every month
Style tags: Systematic Fundamental
Asset class: Equities, Futures, ETFs, Currencies and Commodities
Dataset: NSE Daily or NSE Minute
"""
from blueshift.api import symbol, get_datetime, order_target_percent, schedule_function, date_rules, time_rules, attach_pipeline, pipeline_output, set_commission, set_slippage, get_open_orders, cancel_order, order, record, order_target
import pandas as pd
import numpy as np
from blueshift.pipeline import Pipeline, CustomFactor
from blueshift.pipeline.data import EquityPricing
from blueshift.pipeline.factors import SimpleMovingAverage
from blueshift.errors import HistoryWindowStartsBeforeData
from blueshift.pipeline import Pipeline, CustomFilter
from blueshift.finance import slippage
from blueshift.finance import commission
from blueshift.pipeline.factors import DailyReturns
"""
A function to define things to do at the start of the strategy
"""
def initialize(context):
context.long_securities = [symbol('TITAN'), symbol('SUNPHARMA'), symbol
('MARUTI'), symbol('TATAMOTORS'), symbol('BPCL'), symbol(
'BAJFINANCE'), symbol('HDFCBANK'), symbol('ASIANPAINT'), symbol(
'TCS'), symbol('RELIANCE'), symbol('INFY'), symbol('KOTAKBANK'),
symbol('WIPRO'), symbol('HEROMOTOCO'), symbol('AXISBANK'), symbol(
'POWERGRID'), symbol('UPL'), symbol('TITAN'), symbol('MARUTI'),
symbol('INDUSINDBK'), symbol('ICICIBANK'), symbol('GAIL'), symbol(
'TECHM'), symbol('BHARTIARTL'), symbol('VEDL'), symbol('NTPC'),
symbol('HCLTECH'), symbol('HINDUNILVR'), symbol('ONGC'), symbol(
'TATASTEEL'), symbol('LT'), symbol('JSWSTEEL'), symbol('GRASIM'),
symbol('SBIN'), symbol('DRREDDY'), symbol('BRITANNIA'), symbol('ZEEL'), symbol('ADANIPORTS'),
symbol('CIPLA'), symbol('HDFC'), symbol('SIEMENS'), symbol(
'MOTHERSUMI'), symbol('AMBUJACEM'), symbol('DABUR'), symbol('DLF'),
symbol('OFSS'), symbol('COLPAL'), symbol('ACC'), symbol('LUPIN'),
symbol('BHEL'), symbol('CADILAHC'), symbol('PIDILITIND'), symbol(
'CONCOR'), symbol('MARICO'), symbol('SAIL'), symbol('HINDPETRO'),
symbol('PEL'), symbol('GODREJCP'), symbol('DIVISLAB'), symbol('ABB'
), symbol('ASHOKLEY'), symbol('AUROPHARMA'), symbol('HINDZINC'),
symbol('HAVELLS'), symbol('PETRONET'), symbol('IDEA'), symbol('BIOCON'), symbol('BANKBARODA'), symbol(
'SRTRANSFIN'), symbol('RCOM'), symbol('UNITECH'), symbol(
'RELCAPITAL'), symbol('RELINFRA'), symbol('SUZLON'), symbol(
'JPASSOCIAT'), symbol('IBREALEST'), symbol('NMDC'), symbol(
'BAJAJHLDNG'), symbol('UBL'), symbol('YESBANK'), symbol('NHPC'),
symbol('COALINDIA'), symbol('DHFL'), symbol(
'IBULHSGFIN'), symbol('INDIGO'), symbol('ICICIPRULI'), symbol(
'DMART'), symbol('GICRE'), symbol('SBILIFE'), symbol('ICICIGI'),
symbol('BANDHANBNK'), symbol('HDFCAMC'), symbol('HDFCLIFE'), symbol
('NIACL'), symbol('ADANIENT'), symbol('ADANITRANS'), symbol(
'APOLLOHOSP'), symbol('ADANIGREEN'), symbol('TATACONSUM'), symbol(
'JUBLFOOD'), symbol('IOC'), symbol('LTI'), symbol('SBICARD'),
symbol('INDUSTOWER'), symbol('GLAND'), symbol('ALKEM'), symbol('PNB')]
context.newlist = context.long_securities
context.oldlist = context.long_securities
#context.set_slippage(FixedBasisPointsSlippage(basis_points=0))
schedule_function(strategy, date_rules.month_start(days_offset=0),
time_rules.market_open(hours=0, minutes=15))
attach_pipeline(make_pipeline(context), name='my_pipeline')
def filter_universe(universe):
class FilteredUniverse(CustomFilter):
inputs = ()
window_length = 1
def compute(self, today, assets, out):
in_universe = [(asset in universe) for asset in assets]
out[:] = in_universe
return FilteredUniverse()
def technical_factor(lookback, indicator_fn, indicator_lookback):
class TechnicalFactor(CustomFactor):
inputs = [EquityPricing.close]
def compute(self, today, assets, out, close_price):
signals = np.apply_along_axis(indicator_fn, 0, close_price,
indicator_lookback)
out[:] = signals
return TechnicalFactor(window_length=lookback)
def up_days(px, lookback):
return np.sum(np.where(np.diff(px) > 0, 1, 0))
def std_dev(px, lookback):
return np.std(np.diff(px[:-22]) / px[:-23])
def sma(px, lookback):
return np.mean(px)
def maxreturn(px, lookback):
return np.max(np.diff(px[:-22]) / px[:-23])
def minreturn(px, lookback):
return np.min(np.diff(px[:-22]) / px[:-23])
def make_pipeline(context):
pipe = Pipeline()
plusdays252 = technical_factor(250, up_days, 0)
plusdays22 = technical_factor(20, up_days, 0)
plusdays = plusdays252 - plusdays22
maxret = technical_factor(252, maxreturn, 0)
minret = technical_factor(252, minreturn, 0)
volatility = maxret - minret
sma5 = technical_factor(5, sma, 0)
pipe.add(plusdays, 'plus')
pipe.add(volatility, 'stdev')
pipe.add(sma5, 'sma')
nifty200 = filter_universe(context.long_securit
The stock symbols you mentioned are all available, but now we do not allow any special (in pythonic sense) characters in the symbol name, so replace them by underscore ('_'). So "M&M" becomes "M_M", "L&TFH" becomes "L_TFH", "BAJAJ-AUTO" is "BAJAJ_AUTO" and "MCDOWELL-N" is "MCDOWELL_N". If you are facing issues because of this, that should fix it. Else, please share the strategy code as an attachment to blueshift-support@quantinsti.com so that we receive it with proper formatting.
We do not support daily frequency anymore. Generally running your strategy in minute frequency will be more accurate and depending on how you write the code, may take slightly more time. Unfortunately, we still have not found a reliable source before 2017, so the minute data for NSE does not extend beyond that. Any specific reason you are looking for daily frequency?