python_大智慧SAR指标编写

20 篇文章 13 订阅
6 篇文章 0 订阅
import pandas as pd
import numpy as np
import datetime
import time
#获取数据
df=pd.read_csv('C:/Users/HXWD/Desktop/000001.csv',encoding='gbk')
df.columns=['date','code','name','close','high','low','open','preclose',
'change','change_per','volume','amt']
df=df[['date','open','high','low','close','volume','amt']]
df.head()
def get_SAR(df,N=10,step=2,maxp=20):
    sr_value=[]
    sr_up=[]
    ep_up=[]
    af_up=[]
    sr_down=[]
    ep_down=[]
    af_down=[]
    for i in range(len(df)):
            
        if i >=N:
            if len(sr_up)==0 and len(sr_down)==0:
                if df.ix[i,'close']>df.ix[0,'close']:
                     #标记为上涨趋势
                    sr0=df['low'][0:i].min()
                    af0=0.02
                    ep0=df.ix[i,'high']
                    sr_up.append(sr0)
                    ep_up.append(ep0)
                    af_up.append(af0)
                    sr_value.append(sr0)
                if df.ix[i,'close']<=df.ix[0,'close']:
                    #标记为上涨趋势
                    sr0=df['high'][0:i].max()
                    af0=0.02
                    ep0=df.ix[i,'high']
                    sr_down.append(sr0)
                    ep_down.append(ep0)
                    af_down.append(af0)
                    sr_value.append(sr0)
            if len(sr_up)>0:
                if df.ix[i-1,'low']>sr_up[-1]:
                   sr0=sr_up[-1]
                   ep0=df['high'][-len(sr_up):].max()
                   if df.ix[i,'high']>df['high'][-(len(sr_up)-1):].max() :
                      af0=af_up[-1]+0.02
                   if df.ix[i,'high']<=df['high'][-(len(sr_up)-1):].max() :
                      af0=af_up[-1]
                   
                   sr=sr0+af0*(ep0-sr0)
                   sr_up.append(sr)
                   ep_up.append(ep0)
                   af_up.append(af0)
                   sr_value.append(sr)
                   print('上涨sr0={},ep0={},af0={},sr={}'.format(sr0,ep0,af0,sr))
                if df.ix[i-1,'low']<=sr_up[-1]:
                   ep0=df['high'][-len(sr_up):].max()
                   sr0=ep0
                   af0=0.02
                   sr_down.append(sr0)
                   ep_down.append(ep0)
                   af_down.append(af0)
                   sr_value.append(sr0)
                   sr_up=[]
                   ep_up=[]
                   af_up=[]
            if len(sr_down)>0:
                if df.ix[i-1,'high']<sr_down[-1]:
                    sr0=sr_down[-1]
                    ep0=df['low'][-len(sr_down):].max()
                    if df.ix[i,'low']<df['low'][-(len(sr_down)-1):].max() :
                      af0=af_down[-1]+0.02
                    if df.ix[i,'low']>=df['low'][-(len(sr_down)-1):].max() :
                      af0=af_down[-1]
                   
                    sr=sr0+af0*(ep0-sr0)
                    sr_down.append(sr)
                    ep_down.append(ep0)
                    af_down.append(af0)
                    sr_value.append(sr)
                    print('下跌sr0={},ep0={},af0={},sr={}'.format(sr0,ep0,af0,sr))
                if df.ix[i-1,'high']>=sr_down[-1]:
                   ep0=df['low'][-len(sr_up):].max()
                   sr0=ep0
                   af0=0.02
                   sr_up.append(sr0)
                   ep_up.append(ep0)
                   af_up.append(af0)
                   sr_value.append(sr0)
                   sr_down=[]
                   ep_down=[]
                   af_down=[]
    return sr_value
sar=get_SAR(df)
#测试下来看,似乎中间逻辑有问题,有时间进一步修改
注:需要单独编写文华财经,金字塔,大智慧,通达信,TB,Python平台的指标、策略等,请联系扣扣1733505732,兼职时间:工作日下午18:00-22:00,节假日。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云金杞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值