1

I have a written a script that uses the code below and I would like to optimize rsi_high and rsi_low to get the best sharpe_ratio:

#
import numpy
import talib as ta

global rsi_high, rsi_low

rsi_high = 63
rsi_low = 41


def myTradingSystem(DATE, OPEN, HIGH, LOW, CLOSE, VOL, exposure, equity, settings):
    ''' This system uses trend following techniques to allocate capital into the desired equities'''

    nMarkets = CLOSE.shape[1] # SHAPE OF NUMPY ARRAY

    result, rsi_pos = numpy.apply_along_axis(rsicalc, axis=0, arr=CLOSE)

    pos = numpy.asarray(rsi_pos, dtype=numpy.float64)

    return pos, settings

def rsicalc(num):
    # print rsi_high
    try:
        rsival = ta.RSI(numpy.array(num,dtype='f8'),timeperiod=14)
        if rsival[14] > rsi_high: pos_rsi = 1
        elif rsival[14] < rsi_low: pos_rsi = -1
        else: pos_rsi = 0
    except:
        rsival = 0
        pos_rsi = 0
    return rsival, pos_rsi


def mySettings():
    ''' Define your trading system settings here '''

    settings = {}


    # Futures Contracts
    settings['markets'] = ['CASH','F_AD', 'F_BO', 'F_BP', 'F_C', 'F_CC', 'F_CD',
                           'F_CL', 'F_CT', 'F_DX', 'F_EC', 'F_ED', 'F_ES', 'F_FC', 'F_FV', 'F_GC',
                           'F_HG', 'F_HO', 'F_JY', 'F_KC', 'F_LB', 'F_LC', 'F_LN', 'F_MD', 'F_MP',
                           'F_NG', 'F_NQ', 'F_NR', 'F_O', 'F_OJ', 'F_PA', 'F_PL', 'F_RB', 'F_RU',
                           'F_S', 'F_SB', 'F_SF', 'F_SI', 'F_SM', 'F_TU', 'F_TY', 'F_US', 'F_W',
                           'F_XX', 'F_YM']

    settings['slippage'] = 0.05
    settings['budget'] = 1000000
    settings['beginInSample'] = '19900101'
    settings['endInSample'] = '19931231'
    settings['lookback'] = 504

    return settings

# Evaluate trading system defined in current file.
if __name__ == '__main__':
    import quantiacsToolbox
    results = quantiacsToolbox.runts(__file__, plotEquity=False)
    sharpe_ratio = results['stats']['sharpe']

I suspect that using something like scipy minimize function would do the trick, but I am having trouble understanding how to package my script so that it can be in a usable form.

I have tried putting everything in a function and then running all the code through a number of loops, each time incrementing values but there must be a more elegant way of doing this.

Apologies for posting all my code but I thought it would help if the responder wanted to reproduce my setup and for anyone who is new to quantiacs to see a real example who is faced with the same issue.

Thanks for your help in advance!

Sukhi Kaur
  • 84
  • 6
  • First of all, in order to use optimizer, you should define your function which calculates sharpe ratio of the resulting portfolio. And that function should take your rsi_high and rsi_low as input variables. Then, scipy optimize can change those values in order to produce maximum sharpe. – Olzhas Arystanov Apr 18 '18 at 17:21
  • Thank you for the explanation. The challenge that I have is that I need to manipulate the code above so that it can be presented as a function with two input parameters . I refer to the quantiacstoolbox.runts() which requires me to call the python file as part of __main__. Could you explain 1. how i convert the code above into a function 2. use the scipy optimise function. – Sukhi Kaur Apr 18 '18 at 17:37
  • The problem is that your code listed above basically just makes some preparations to pass data further to quantiacsToolbox. And everything related to the calculation of Sharpe Ratio goes there, behind the scenes. In order to deal with it, you might need to look at quantiacsToolbox code that is calculating Sharpe Ratio and try to port it into your function with your input variables. – Olzhas Arystanov Apr 18 '18 at 17:44

0 Answers0