【问题标题】:Problems creating an MACD Strategy that will execute based to rules创建将根据规则执行的 MACD 策略的问题
【发布时间】:2020-03-12 19:21:58
【问题描述】:

我在格式化 python 3 中的 MACD 策略代码时遇到了一些问题,当当前柱线 > 2 且 MACD DIFF 过零时,该策略会创建买入信号。我已经定义了指标并尝试在下面编写代码。非常感谢任何帮助

指标定义:

OHLC_DF= OHLC_DF.sort_index()
OHLC_DF['Ema_short'] = OHLC_DF['Close'].ewm(span=6,min_periods=0,adjust=False,ignore_na=False).mean()
OHLC_DF['Ema_long'] = OHLC_DF['Close'].ewm(span=23,min_periods=0,adjust=False,ignore_na=False).mean()
OHLC_DF['MY_MACD'] = OHLC_DF['Ema_short'] - OHLC_DF['Ema_long']

#MACD AVERAGE
OHLC_DF['MACD_AVG'] = OHLC_DF['MY_MACD'].ewm(span=9,min_periods=0,adjust=False,ignore_na=False).mean()

#MACDDiff = MyMACD - MACDAvg;
OHLC_DF['MACD_DIFF'] = OHLC_DF['MY_MACD'] - OHLC_DF['MACD_AVG']  

英文交易规则和尝试代码:

入场交易规则

CB > 2 ##check used to avoid spurious cross confirmation at CB = 2 (at CB = 1, 
  MyMACD and MACDAvg will be the same) }
If CurrentBar > 2 and MACDDiff crosses over 0 then 
    Buy ( !( "Long" ) ) next bar at market; 100% portfolio
/////////////////////////////////////////////////////////////////////////
##Stop out Rules
IF position(long)  and profit >$27:
            Execute 0.025 Trailing_stop (for all positions)
Else            Execute  0.048 Stop_loss 

END
///////////////////////////////////////////////////////////////

【问题讨论】:

    标签: python-3.x quantitative-finance algorithmic-trading trading


    【解决方案1】:

    从您的示例中不清楚您的问题到底是什么。在您的伪代码中,您记下以下内容:

    If CurrentBar > 2
    
    Buy ( !( "Long" ) ) next bar at market
    
    If position(long) and profit >$27
    
    Execute 0.025 Trailing_stop (for all positions)
    

    这些将引用您代码中其他地方的对象和/或来自某些交易平台的外部对象。然而,根据设置,如果没有现有的订单逻辑来管理是否已建立多头头寸,您的购买逻辑似乎有缺陷。否则,代码将不断尝试取消报价,而不管购买的可用资金如何。

    另外,我不确定这条线的用途。我假设CB 指的是CurrentBar,简单地表示<= 2 的条件会更有意义

    CB > 2 ##check used to avoid spurious cross confirmation at CB = 2 (at CB = 1, 
      MyMACD and MACDAvg will be the same) }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-20
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多