Long Short Strategy

Hi,



I coded a long short strategy. I am not getting any syntax error. But It is not taking any trades. Can you help me out?



 

Zipline

from zipline.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_target,order,get_order

)

from zipline.errors import HistoryWindowStartsBeforeData

from zipline.finance import commission,slippage

from zipline.finance.slippage import FixedBasisPointsSlippage

from zipline.pipeline import Pipeline, CustomFactor

from zipline.pipeline.data import EquityPricing

from zipline.pipeline.factors import SimpleMovingAverage

from zipline.errors import HistoryWindowStartsBeforeData

from zipline.pipeline import Pipeline, CustomFilter

from zipline.finance.slippage import FixedSlippage

from zipline.finance import commission

from zipline.finance.slippage import FixedBasisPointsSlippage

from zipline.pipeline.factors import DailyReturns

Data manipulation

import pandas as pd

import numpy as np

import talib as ta

import bisect

def initialize(context):

context.universe = [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('YESBANK'),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('COALINDIA'),symbol('JSWSTEEL'),symbol('GRASIM'),symbol('SBIN'),

symbol('DRREDDY'),symbol('BRITANNIA'),symbol('M&M'),symbol('ZEEL'),symbol('BAJAJ-AUTO'),

symbol('ADANIPORTS'),symbol('CIPLA'),symbol('HDFC'),symbol('INFRATEL'),symbol('IBULHSGFIN'),

symbol('SIEMENS'),symbol('UBL'),

symbol('MOTHERSUMI'),symbol('ICICIPRULI'),symbol('AMBUJACEM'),

symbol('DABUR'),symbol('L&TFH'),

symbol('DLF'),symbol('OFSS'),symbol('INDIGO'),symbol('COLPAL'),

symbol('NMDC'),symbol('ACC'),symbol('NHPC'),symbol('LUPIN'),

symbol('BHEL'),symbol('CADILAHC'),symbol('PIDILITIND'),symbol('CONCOR'),symbol('MARICO'),

symbol('SAIL'),symbol('HINDPETRO'),symbol('PEL'),symbol('GODREJCP'),

symbol('DIVISLAB'),symbol('ASHOKLEY'),symbol('AUROPHARMA'),symbol('HINDZINC'),

symbol('HAVELLS'),symbol('PETRONET'),symbol('IDEA'),

symbol('MCDOWELL-N'),symbol('BIOCON'),symbol('BANKBARODA'),

symbol('SRTRANSFIN')

]

context.set_slippage(FixedBasisPointsSlippage(basis_points = 3))



schedule_function(strategy,

date_rules.every_day(),

time_rules.market_open(hours=0, minutes=30))



schedule_function(placetrade, date_rules.every_day(),

time_rules.market_close(hours=0, minutes=10))



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 make_pipeline(context):

Initialize the pipeline

pipe = Pipeline()

Construct Factors

plusdays = technical_factor(30, up_days, 0)

pipe.add(plusdays, 'plus')

nifty200 = filter_universe(context.universe)

pipe.set_screen(nifty200)

return pipe



def strategy(context,data):

Access results using the name passed to attach_pipeline.

n=5

pipeline_results = pipeline_output('my_pipeline')

context.longselected_stocks = pipeline_results.sort('plus', ascending=False).iloc[:n]

context.shortselected_stocks = pipeline_results.sort('plus', ascending=True).iloc[:n]

weight = 1/10

for security in context.longselected_stocks.index:

#print(context.longselected_stocks)

#print(context.shortselected_stocks)

order_target_percent(security, weight)

for security in context.shortselected_stocks.index:

order_target_percent(security, -weight)



def placetrade(context,data):

for security in context.portfolio.positions:

order_target(security,0)

I applied it in NSE minute data.

Unfortuantely, our community board editor seems to lose white space formatting in simply copy paste of code. Can you please copy your code first to a Notepad (or similar plaint text editor) and then paste it back here. That should preserve the indentation.

I am not able to past here. I opened a new post sir.