Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Why SVM does't work here?

I want to develop an SVM strategy, however it doesn't work.
It's said that" 65 Error Runtime exception: ValueError: This solver needs samples of at least 2 classes in the data, but the data contains only one class: False"
But I double checked that line context.Y has two classes!
Who can tell me why!

import numpy as np
import pandas as pd
import talib
from sklearn import svm
from sklearn.ensemble import RandomForestClassifier

def initialize(context):
context.stock = symbol('SPY')
context.date = None
context.classifier = svm.LinearSVC()

def handle_data(context, data):
todays_date = get_datetime().date()
if todays_date == context.date:
return
context.date = todays_date

high = history(1000,'1d','high')  
low = history(1000,'1d','high')  
close = history(1000,'1d','close_price')  
prices = history(1000,'1d','price')  

atrlst = talib.ATR(high[context.stock],  
                low[context.stock],  
                close[context.stock],  
                timeperiod=14)  
atr = atrlst[-502:-2]  
atr_new = atrlst[-1]  

macdlst = prices.apply(MACD, fastperiod=12, slowperiod=26, signalperiod=9)  
macdlst = np.reshape(np.array(macdlst),len(np.array(macdlst)))  
macd = macdlst[-502:-2]  
macd_new = macdlst[-1]  

rsilst = prices.apply(talib.RSI, timeperiod=14)  
rsilst = np.reshape(np.array(rsilst),len(np.array(rsilst)))  
rsi = rsilst[-502:-2]  
rsi_new = rsilst[-1]  

slowklst, slowdlst = talib.STOCH(high[context.stock],  
                               low[context.stock],  
                               close[context.stock],  
                               fastk_period=5,  
                               slowk_period=3,  
                               slowk_matype=0,  
                               slowd_period=3,  
                               slowd_matype=0)  
slowk = slowklst[-502:-2]  
slowd = slowdlst[-502:-2]  
slowk_new = slowklst[-1]  
slowd_new = slowdlst[-1]  

dataX = np.vstack((atr,macd,rsi,slowk,slowd)).T  
context.X = dataX  

prices_array = np.reshape(np.array(prices),len(np.array(prices)))  
dataY = (np.diff(prices_array)>0)[-501:-1]  
context.Y = dataY  

context.xnew = np.array([atr_new,macd_new,rsi_new,slowk_new,slowd_new])  
log.info(context.Y)  
#Why always Error here!?  
context.classifier.fit(context.X,context.Y)  
signal = context.classifier.predict(context.xnew)  
log.info(signal)  
if signal:  
    order_target_percent(context.stock,1)  
else:  
    order_target_percent(context.stock,0)  

def MACD(prices, fastperiod=12, slowperiod=26, signalperiod=9):
macd, signal, hist = talib.MACD(prices,
fastperiod=fastperiod,
slowperiod=slowperiod,
signalperiod=signalperiod)
return macd-signal

2 responses

Did you really mean to set low to a history dataframe of high prices?

I got

55 dataX = np.vstack((atr,macd,rsi,slowk,slowd)).T

55 Error Runtime exception: ValueError: all the input array dimensions except for the concatenation axis must match exactly