This is my first posting. I'm gonna make my skill more up and then I'll be glad if I can be competitive with you
This is my first posting. I'm gonna make my skill more up and then I'll be glad if I can be competitive with you
Add this to record_vars and take a took:
record(leverage=context.account.leverage)
Use order_target_percent instead of order_percent to bring leverage down to 1
import quantopian.algorithm as algo
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import Q1500US
import quantopian.algorithm as algo
def initialize(context):
context.x = symbol('JNJ')
context.y = symbol('HON')
context.term = 120
context.threshold = 1
algo.schedule_function(
rebalance,
algo.date_rules.every_day(),
algo.time_rules.market_close(),
)
algo.schedule_function(
record_vars,
algo.date_rules.every_day(),
algo.time_rules.market_close(),
)
def rebalance(context, data):
pricex = data.history(context.x, fields="price", bar_count=context.term, frequency="1d")
pricey = data.history(context.y, fields="price", bar_count=context.term, frequency="1d")
pricex_mean = pricex.mean()
pricey_mean = pricey.mean()
beta = pricex_mean / pricey_mean
context.spread = pricex[-1] - pricey[-1] * beta
if context.spread > context.threshold:
if data.can_trade(context.x) and data.can_trade(context.y):
order_target_percent(context.x, -0.5)
order_target_percent(context.y, 0.5)
elif context.spread < -context.threshold:
if data.can_trade(context.x) and data.can_trade(context.y):
order_target_percent(context.x, 0.5)
order_target_percent(context.y, -0.5)
else:
pass
def record_vars(context, data):
record(spread=context.spread)
record(leverage=context.account.leverage)
def handle_data(context, data):
pass