简体   繁体   English

用于算法交易的 Python 脚本使用

[英]Python script for algo trading using

Looking for suggestions for Python program for trading Forex.寻找有关 Python 外汇交易程序的建议。 Basically I want to use short sma and long sma crosses to trigger buy long or short, but only just after it crosses not when it is already above to go long or below to go short.基本上我想使用短 sma 和长 sma 交叉来触发买入多头或空头,但只有在它已经超过 go 多头或低于 go 空头时才交叉。 The second part is to use a short sma crossing a medium sma to exit the position.第二部分是使用短 sma 穿过中 sma 退出 position。 The problem I am running into is the short and medium sma's keep triggering buy and sells, when they are above or below the long sma.我遇到的问题是,当短期和中期 SMA 高于或低于长期 SMA 时,它们会不断触发买入和卖出。 I do not want to enter any trades using the sma short and medium crosses only to exit positions, that I have entered previously on the short/long sma cross.我不想使用 sma 短线和中线交叉进入任何交易,只是为了退出头寸,我之前在短期/多头 SMA 交叉中输入过这些头寸。

This script seems to be getting close to what I am looking for.这个脚本似乎越来越接近我正在寻找的东西。 It has the short/long signal when exact time they cross.当它们交叉的确切时间时,它具有短/长信号。 It does not have the sma short and medium cross to exit a position and no failsafe so it does not trigger any buys or sells to enter a trade when they cross otherwise.它没有 sma 短线和中线交叉以退出 position 并且没有故障保护,因此当它们交叉时不会触发任何买入或卖出进入交易。 I only want the short/medium cross to exit trades, after I enter on the long/short sma cross.在我进入多头/空头 SMA 交叉后,我只希望空头/中头交叉退出交易。

df['position'] = df['SMA_15'] > df['SMA_45']
df['pre_position'] = df['position'].shift(1)
df.dropna(inplace=True) # dropping the NaN values
df['crossover'] = np.where(df['position'] == df['pre_position'], False, True)

I could show you a solution that only grabs the first buy signal from each little string of buys and sells repeated, but I believe you might like a library called tulipy .我可以向您展示一个解决方案,该解决方案仅从重复的每一小串买卖中获取第一个买入信号,但我相信您可能会喜欢一个名为tulipy的库。 This allows you to easily make technical indicators (For more functions, here's the docs https://tulipindicators.org/ ).这使您可以轻松制作技术指标(有关更多功能,请参阅文档https://tulipindicators.org/ )。 If you want that other solution, feel free to comment that and I'll add it here.如果您想要其他解决方案,请随时发表评论,我会在此处添加。

pip install tulipy To install the library pip install tulipy安装库

Utilize the crossover function.利用交叉 function。 We also have to keep the lists the same length here.我们还必须在此处保持列表的长度相同。

import pandas as pd
import numpy as np
import tulipy

def makeListsSameLength(someList, matchThisLengthList):
    #make someList the same length as matchThisLengthList by adding None's to the front
    for i in range(abs(len(matchThisLengthList) - len(someList))): 
        someList = np.insert(someList, 0, None, axis=0) #Push a None to the front of the list
    return someList

df['SMA_15'] = makeListsSameLength(tulipy.sma(df['close'].values, 15), df)
df['SMA_45'] = makeListsSameLength(tulipy.sma(df['close'].values, 45), df)
df['crossover'] = makeListsSameLength(tulipy.crossover(df['SMA_15'].values, df['SMA_45'].values), df)

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

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