四十、区块量化 LON策略

发布时间 2023-07-11 09:05:10作者: 一生所悟
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import talib
import cross_order as order
import time
import pandas as pd


def LON(df, n=10):
"""
钱龙长线指标是一种描述当前趋势的指标,相比均线只使用收盘价而言,钱龙长线指标在描述趋势时,还考虑到了最高价、最低价、成交量对趋势的影响。
最常用的使用方法是,LON指标上穿0线时做多;LON指标下穿0线时做空。
@param df: 数据源
@param n: 时间
@return:
"""
lc = df['close'].shift(1)
a = (df['high'].rolling(2).max() - df['low'].rolling(2).min()) * 100
vid = df['volume'].rolling(2).sum() / a
rc = (df['close'] - lc) * vid
long = rc.cumsum()
dif = long.ewm(com=9, adjust=False).mean()
dea = long.ewm(com=19, adjust=False).mean()
lon = dif - dea
return pd.DataFrame({'lon': lon, 'lonma': lon.rolling(n).mean()}, index=df.index)


def main():
for symbol in order.symbol_pool:
print("任务开始时间:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
# 设置杠杆倍数
order.set_leverage(symbol=symbol, leverage='18')
# 获取标的的最新价
df = order.get_candlesticks(symbol=symbol, interval='4H', limit='200')
close = df['close'].values
lon = LON(df, 10)
# 做多止盈价格=委托价+atr
slpx = close[-1] * 1.008
# 做空止盈价格=委托价-atr
tppx = close[-1] / 1.004
print(
'重要参数:\n币种:{}\n当前价格:{:.2f}\nlon:{:.2f}\nlonma:{:.2f}\n止盈价格:{:.2f}\n止损价格:{:.2f}'
.format(symbol, close[-1], lon['lon'].iloc[-1], lon['lonma'].iloc[-1], slpx, tppx))
if lon['lon'].iloc[-1] < 0:
if lon['lon'].iloc[-2] > lon['lonma'].iloc[-2] and lon['lon'].iloc[-1] < lon['lonma'].iloc[-1]:
print('LON指标下穿0轴时做空,市价单卖出')
order.down_cross_order(symbol=symbol, message='LON指标下穿0轴时做空,市价单卖出')
elif lon['lon'].iloc[-1] > 0:
if lon['lon'].iloc[-2] < lon['lonma'].iloc[-2] and lon['lon'].iloc[-1] > lon['lonma'].iloc[-1]:
print('LON指标上穿0轴时做多,市价单买入')
order.up_cross_order(symbol=symbol, message='LON指标上穿0轴时做多,市价单买入')

time.sleep(5)
print("任务结束时间:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
print("-----------------------------------v----------------------------------------")


if __name__ == '__main__':
print("-----------------------------------^----------------------------------------")
main()