Can someone take a look at my algorithm to see whether its accurate?
I am facing ValueError in line 84.
My algorithm logic is as follow:
1) Compute CRS lines based on yesterday's OHLC
2) You are only allow to go long if your current price is above both CRS lines and vice versa
3) Current price must be in between SMA line 7 and 18
4) To initiate long position : CCI must be less than <-100 ; For short position : CCI must be > 100
5) If all the criteria above are satisfied, work to buy at 1 tick higher of previous closing (LONG) or 1 tick lower of previous closing (SHORT)
6) Profit target : 200 ticks ; Stop Loss : 200 ticks
Any guidance would be appreciated. Thank You
import numpy as np
import scipy as sp
import talib
from quantopian.algorithm import order_optimal_portfolio
import quantopian.optimize as opt
def initialize(context):
#obtain continous futures for CL
context.crude_oil = continuous_future('CL', offset=0, roll='volume', adjustment='mul')
context.long_order = False
context.short_order = False
# close all positions 5 minutes before market close
schedule_function(
close_all_positions,
date_rules.every_day(),
time_rules.market_close(minutes = 5),
)
#this function will run every minute
def handle_data(context, data):
#get previous day crude data
hist_price_1d = data.history(context.crude_oil, ['close','high','low'], 1, '1d')
# CRS line construction
s3_line = (hist_price_1d.high[-1] + hist_price_1d.low[-1]) / 2
r0_line = (hist_price_1d.high[-1] + hist_price_1d.low[-1] + hist_price_1d.close[-1]) / 3 * 2 - (s3_line/2)
#obtain 90 mins historical data of crude
min_price = data.history(context.crude_oil, ['close','high','low'], 90, '1m')
# 18MA on 5 mins chart = 90 x 1min data
long_ma = min_price.close.mean()
# 7MA on 5 mins chart = 35 x 1 min data
short_ma = min_price.close[-35:].mean()
# obtain the current price of crude oil
CL_price = data.current(context.crude_oil, 'price')
# obtain the active contract for order
CL_contract = data.current(context.crude_oil, 'contract')
# CCI Construction
period = 5
H = min_price.high[-5:]
L = min_price.low[-5:]
C = min_price.close[-5:]
cci = talib.CCI(H, L, C, period)[-1]
# if there is open long position
if context.portfolio.positions[CL_contract].amount == 1:
order(CL_contract, -1, style = StopOrder(min_price.close[-6] - 0.19)) #Stop Loss
order(CL_contract, -1, style = LimitOrder(min_price.close[-6] + 0.21)) # Profit Target
elif context.portfolio.positions[CL_contract].amount == -1:
order(CL_contract, 1, style = StopOrder(min_price.close[-6] + 0.19)) #Stop Loss
order(CL_contract, 1, style = StopOrder(min_price.close[-6] - 0.21)) #Profit Target
# If no long order & previous 5min bar closing is above CR line
elif not context.long_order and (min_price.close[-5] > s3_line and r0_line):
if (CL_price < long_ma and CL_price > short_ma) or (CL_price > long_ma and CL_price < short_ma):
if cci < -100:
order(CL_contract, 1, style = LimitOrder(C + 0.01))
log.info('Long in %s'% context.crude_oil.symbol)
context.long_order = True
# If no short order & previous 5min bar closing is above CR line
elif not context.short_order and (min_price.close[-5] < s3_line and r0_line):
if (CL_price < long_ma and CL_price > short_ma) or (CL_price > long_ma and CL_price < short_ma):
if cci > 100:
order(CL_contract, -1, style = LimitOrder(C - 0.01))
log.info('Short in %s'% context.crude_oil.symbol)
context.short_order = True
return context.portfolio.positions[CL_contract].last_sale_price
def close_all_positions(context, data):
# Close all positions 5 mins before market close, if there is
if context.portfolio.positions[CL_contract].amount == 1:
order(CL_contract, -1, style = MarketOrder())
if context.portfolio.positions[CL_contract].amount == -1:
order(CL_contract, 1, style = MarketOrder())