Hi All,
First time user here so be gentle :) Managed to get my backtest to buy stocks, but having issues with getting it to do exactly what I want.
I'm trying to back test a simple strategy based on a custom data input. Basically I have a CSV that says - buy these stocks at the exact time of the alert. No other data is provided.
The custom data looks like this (I control the format of the data). Its only buy signals (eg 5) and doesn't have any other data (eg sell or hold).
date,symbol,datetime,buy_signal
23/06/2020,TSLA,23/06/2020 21:20,5
23/06/2020,MSFT,23/06/2020 21:54,5
Basically I want to buy x amount of stock - at the exact minute that my alert is triggered. And then sell based on whatever strategy I come up with (eg trailing stop loss of 10%)
Things I'm stuck on.
- Pipeline only seems to run once a day. Need it to buy at the exact time in the CSV.
- It remembers the buy_signal from previous days - so wants to buy the same stock again based on yesterdays signal. The pipeline seems to have multiple entries of the same data. Eg TSLA every day after the signal.
- Need to implement a sell / take profit strategy. Maybe something like 10% trailing stop loss.
Heres the code thus far
import quantopian.algorithm as algo
from quantopian.pipeline import Pipeline
from quantopian.pipeline.filters.morningstar import Q1500US
from quantopian.pipeline.data.user_xxxxx import my_data_3
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS
def initialize(context):
"""
Called once at the start of the algorithm.
"""
# Rebalance every day, 1 hour after market open.
algo.schedule_function(
rebalance,
algo.date_rules.every_day(),
algo.time_rules.market_open(hours=1),
)
# Record tracking variables at the end of each day.
algo.schedule_function(
record_vars,
algo.date_rules.every_day(),
algo.time_rules.market_close(),
)
# Create our dynamic stock selector.
algo.attach_pipeline(make_pipeline(), 'pipeline')
"""
A function to create our dynamic stock selector (pipeline). Documentation
on pipeline can be found here:
https://www.quantopian.com/help#pipeline-title
"""
def make_pipeline():
trade_time = my_data_3.datetime.latest
buy_factor = my_data_3.buy_signal.latest
universe = Q1500US() & buy_factor.notnull()
pipe = Pipeline(
columns={
'buy_factor': buy_factor,
'longs': (buy_factor >=4),
'trade_time': trade_time
},
screen=universe
)
return pipe
def before_trading_start(context, data):
"""
Called every day before market open.
"""
context.output = algo.pipeline_output('pipeline')
# These are the securities that we are interested in trading each day.
context.security_list = context.output.index
def rebalance(context, data):
long_secs = context.output[context.output['longs']].index
for security in long_secs:
if data.can_trade(security):
order_target_value(security, 1000)
pass
def record_vars(context, data):
long_count = 0
short_count = 0
for position in context.portfolio.positions.values():
if position.amount > 0:
long_count+=1
elif position.amount < 0:
short_count += 1
record(num_long = long_count, leverage = context.account.leverage)
pass
def handle_data(context, data):
"""
Called every minute.
"""
pass