之前發現美股的SPY期權居然有周1,周3過期的期權,這樣加上週5,一週有3天過期的期權了。之前嘗試賣過幾次末日期權,勝率還蠻高。來去quantopian上統計下真的歷史勝率有多少?
"""
This is a template algorithm on Quantopian for you to adapt and fill in.
"""
import quantopian.algorithm as algo
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS
def initialize(context):
"""
Called once at the start of the algorithm.
"""
context.enter_price = 0
context.b_rise = False
# Rebalance every day, 1 hour after market open.
algo.schedule_function(
rebalance,
algo.date_rules.every_day(),
algo.time_rules.market_open(minutes=20),
)
# Record tracking variables at the end of each day.
algo.schedule_function(
record_vars,
algo.date_rules.every_day(),
algo.time_rules.market_close(),
)
def before_trading_start(context, data):
"""
Called every day before market open.
"""
pass
def rebalance(context, data):
"""
Execute orders according to our schedule_function() timing.
"""
weekday = get_datetime("US/Eastern").date().weekday() + 1
if weekday == 1 or weekday == 3 or weekday == 5:
log.info(weekday)
history_data = data.history(symbol('SPY'), ['close','high','low','open'], 2, '1m')
context.enter_price = history_data['close'][-1]
history_data_day = data.history(symbol('SPY'), ['close','high','low','open'], 2, '1d')
log.info("enter_price:" + str(context.enter_price))
if history_data_day['close'][-1] > history_data_day['close'][0]:
context.b_rise = True
else:
context.b_rise = False
pass
def record_vars(context, data):
"""
Plot variables at the end of each day.
"""
weekday = get_datetime("US/Eastern").date().weekday() + 1
if weekday == 1 or weekday == 3 or weekday == 5:
history_data = data.history(symbol('SPY'), ['close','high','low','open'], 2, '1d')
high_price = history_data['high'][-1]
low_price = history_data['low'][-1]
log.info("high_price:" + str(high_price))
log.info("low_price:" + str(low_price))
if context.b_rise:
if low_price >= context.enter_price * 0.993:
log.info("Success Put Ex")
else:
log.info("Fail Put Ex")
else:
if high_price <= context.enter_price * 1.006:
log.info("Success Call Ex")
else:
log.info("Fail Call Ex")
pass
def handle_data(context, data):
"""
Called every minute.
"""
pass
邏輯是開盤20分鐘後,如果SPY是漲的,就賣執行價爲當前價格 * 0.993的PUT,如果是跌的,就賣執行價爲價格* 1.006的Call。如果賣的是PUT,收盤前最低價擊穿執行價就止損,如果賣的是CALL,收盤前最高價擊穿執行價就止損。 統計Log,發現跟無腦賣的勝率差不多,都是75%左右。
估計盈虧比是1:3.所以這個策略沒法盈利。