-
Notifications
You must be signed in to change notification settings - Fork 63
/
rsiExample.py
67 lines (47 loc) · 2.21 KB
/
rsiExample.py
1
import numpy as npfrom scipy.signal import lfilterdef mySettings(): settings = {} settings['markets'] = ['CASH', 'F_AD', 'F_AE', 'F_AH', 'F_AX', 'F_BC', 'F_BG', 'F_BO', 'F_BP', 'F_C', 'F_CA', 'F_CC', 'F_CD', 'F_CF', 'F_CL', 'F_CT', 'F_DL', 'F_DM', 'F_DT', 'F_DX', 'F_DZ', 'F_EB', 'F_EC', 'F_ED', 'F_ES', 'F_F', 'F_FB', 'F_FC', 'F_FL', 'F_FM', 'F_FP', 'F_FV', 'F_FY', 'F_GC', 'F_GD', 'F_GS', 'F_GX', 'F_HG', 'F_HO', 'F_HP', 'F_JY', 'F_KC', 'F_LB', 'F_LC', 'F_LN', 'F_LQ', 'F_LR', 'F_LU', 'F_LX', 'F_MD', 'F_MP', 'F_ND', 'F_NG', 'F_NQ', 'F_NR', 'F_NY', 'F_O', 'F_OJ', 'F_PA', 'F_PL', 'F_PQ', 'F_RB', 'F_RF', 'F_RP', 'F_RR', 'F_RU', 'F_RY', 'F_S', 'F_SB', 'F_SF', 'F_SH', 'F_SI', 'F_SM', 'F_SS', 'F_SX', 'F_TR', 'F_TU', 'F_TY', 'F_UB', 'F_US', 'F_UZ', 'F_VF', 'F_VT', 'F_VW', 'F_VX', 'F_W', 'F_XX', 'F_YM', 'F_ZQ'] settings['slippage'] = 0.05 settings['budget'] = 1000000 settings['beginInSample'] = '20050101' settings['endInSample'] = '20121231' settings['lookback'] = 504 return settingsdef myTradingSystem(DATE, OPEN, HIGH, LOW, CLOSE, settings): period1 = 504 rsi1 = RSI(CLOSE, period1) p = rsi1 - 50 return p, settingsdef RSI(CLOSE,period): closeMom = CLOSE[1:, :] - CLOSE[:-1, :] upPosition = np.where(closeMom >= 0) downPosition = np.where(closeMom < 0) upMoves = closeMom.copy() upMoves[downPosition] = 0 downMoves = np.abs(closeMom.copy()) downMoves[upPosition] = 0 out = 100 - 100 / (1 + (np.mean(upMoves[-(period+1):, :], axis=0) / np.mean(downMoves[-(period+1):, :], axis=0))) return outdef nDayEMA(field, period): ep = 2.0/(period+1) aa = np.array([ep]) bb = np.array([1, -(1-ep)]) zInit = np.array(field[1, :]*(1-ep))[np.newaxis] out = lfilter(aa, bb, field[1:, :], zi=zInit, axis=0) out = out[0] out[:period-1, :] = np.NaN return out# Evaluate trading system defined in current file.if __name__ == '__main__': import quantiacsToolbox results = quantiacsToolbox.runts(__file__)