简体   繁体   English

Python优化移动平均值

[英]Python Optimization Moving Average

I want to create moving average strategy with rolling funtion and scipy optimization but my code do not optimize rolling. 我想创建具有滚动功能和结构优化的移动平均策略,但是我的代码无法优化滚动。 It gives result which I entered as first x0 values. 它给出了我作为第一个x0值输入的结果。 I searched for it in google there are ways to create all rolling possibilities but it takes lot of time. 我在google中搜索了它,有很多方法可以创建所有滚动的可能性,但是要花很多时间。 Is there any way to optimize efficiently. 有什么办法可以有效地进行优化。 Here is my code, thanks in advance; 这是我的代码,谢谢。

import pandas as pd
import os
import numpy as np
from datetime import datetime
import scipy.optimize as opt
#read file
data = pd.read_csv(r'C:\Users\Kaan\USDTRY-2018_06_01-2018_09_07.csv', encoding='utf-8', header=None, index_col=0)
data.columns = ['buy','sell',1,2]
data1 = data[['buy','sell']].head(100000)


# Optimization -------------------------------////////////////////------------------------------------

def objective(x):
    x1 = x[0]
    x2 = x[1]
    x3 = x[2]
    x4 = x[3]
    data3 = pd.DataFrame(data=data1)  
    data3['sma1']=data3['buy'].rolling(int(x3)).mean()
    data3['sma2']=data3['buy'].rolling(int(x4)).mean()
    data3['sma1-sma2'] = np.round(data3['sma1']-data3['sma2'],5) 
    data3['pos'] = np.where(data3['sma1-sma2'] >= x1, 1, 0)
    data3['pos'] = np.where((data3['sma1-sma2'] < -x1) ,-1, data3['pos'])
    data3['pos'] = np.where(abs(data3['sma1-sma2']) > x2, 0, data3['pos'])
    data3['return'] = np.round(np.log(data3['buy'] / data3['buy'].shift(1)),5)
    data3['st'] = data3['pos'].shift(1)*data3['return']
    return -1*data3['st'].cumsum().apply(np.exp).tail(1)[0]
def constraints1(x):
    return x[3] * x[2] - 0
def constraints2(x):
    return x[3] - x[2] - 0
b = (0.0,1000000.0)
bonds = (b,b,b,b)
x0=[0.00205062, 0.19746918, 893, 1990]
print(objective(x0))
con1 = {'type':'ineq','fun':constraints1}
con2 = {'type':'ineq','fun':constraints2}
cons = [con1,con2]
sol = opt.minimize(objective, x0, bounds=bonds, constraints=cons)
print(sol)  

As an aside, it is normal in quant strategies to use EWMA as it preserves memory. 顺便说一句,在量化策略中使用EWMA是很正常的 ,因为它可以保留内存。 If you want a super fast EWMA solution then see my other answer on calculating fast EWMA 如果您需要超快速EWMA解决方案,请参阅我的其他关于计算快速EWMA的答案

As per the numba examples this should significantly increase your code speed 根据numba 示例,这应该显着提高代码速度

import numpy as np
from numba import guvectorize

@guvectorize(['void(float64[:], intp[:], float64[:])'], '(n),()->(n)')
def move_mean(a, window_arr, out):
    window_width = window_arr[0]
    asum = 0.0
    count = 0
    for i in range(window_width):
        asum += a[i]
        count += 1
        out[i] = asum / count
    for i in range(window_width, len(a)):
        asum += a[i] - a[i - window_width]
        out[i] = asum / count

Another alternative is to replace guvectorize with jit 另一种方法是替换guvectorizejit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM