0

希望我能得到一些帮助,将这个 pinescript 转换为反向交易者策略。我的目标是创建一个简单的 EMA 交叉策略,如果 EMA 为正,我们买入并持有,直到 EMA 变为负值。

下面是我的 PineScript 代码。

strategy(title = "test", overlay=true, initial_capital = 10000, pyramiding = 0, commission_type = strategy.commission.percent, commission_value = 0.0, calc_on_order_fills = false, calc_on_every_tick = false, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, currency = currency.USD)

fastMA = ema(close, 22)
slowMA = ema(close, 23)

plot(fastMA, color = red, linewidth = 1, title = "Fast MA")
plot(slowMA, color = blue, linewidth = 1, title = "Slow MA")

buyCondition = crossover(fastMA, slowMA)
closeCondition = crossunder(fastMA, slowMA)

if buyCondition 
    strategy.entry("MA Cross - Long", strategy.long)

strategy.close("MA Cross - Long", closeCondition)

以下是我的 BackTrader 策略

class LongCloseLarpStrat(bt.Strategy):
    params = (('fastMA',22),('slowMA', 23))
        
        
    def __init__(self):
        self.livetrades = {}
        self.counter = 0
        
        self.fastMA = bt.indicators.ExponentialMovingAverage(period=self.p.fastMA)
        self.slowMA = bt.indicators.ExponentialMovingAverage(period=self.p.slowMA)
        self.crossover = bt.indicators.CrossOver(self.fastMA, self.slowMA) 
          
            
    def next(self):
        if self.position.size:
            if self.crossover == -1:
                self.sell()
        elif not self.position.size:
            if self.crossover == 1:
                self.buy()

交易数量、利润和没有交易匹配。

提前致谢, 埃尔南多

4

0 回答 0