简体   繁体   English

如何使用一个符号的 SMA 信号来交易另一个符号

[英]How to use SMA signal from one symbol to trade another symbol

My idea is to track crude oil commodity prices and trade an ETF based on the movements of Oil.我的想法是跟踪原油商品价格并根据石油的走势交易 ETF。

Logic is:逻辑是:

  • If price crosses SMA100 upwards trending for Oil, buy ETF如果油价突破 SMA100 向上趋势,则买入 ETF
  • If price crosses SMA100 downwards trending for oil, sell ETF如果价格越过 SMA100 下跌趋势石油,卖出 ETF

I use a generic SMA strategy but I cannot make it use the data from oil.我使用通用 SMA 策略,但我不能让它使用石油数据。 Based on the plot it uses SMA 100 for the ETF.基于 plot,它使用 SMA 100 作为 ETF。


import backtrader as bt
import datetime

start = datetime.datetime(2018,1,1)

end = datetime.datetime(2021,5,1)


cerebro = bt.Cerebro()
cerebro.broker.set_cash(1000000)

etf = bt.feeds.YahooFinanceData(dataname='SUSW.L', fromdate=start,
                                  todate=end)

oil = bt.feeds.YahooFinanceData(dataname='CL=F', fromdate=start,
                                  todate=end)

cerebro.adddata(etf, name='etf')
cerebro.adddata(oil, name='oil')

I use data1 to refer to the Oil datafeed.我使用 data1 来指代石油数据馈送。 Not sure what else I can change in the code - I am fairly new to backtrader and look through documentation.不知道我还可以在代码中更改什么 - 我对反向交易者很陌生,并查看了文档。

class SmaCross(bt.Strategy):
    def log(self, txt, dt=None):
        ''' Logging function for this strategy'''
        dt = dt or self.datas[0].datetime.date(0)
        print('%s, %s' % (dt.isoformat(), txt))
    
    params = (
            ('sma_fast', 50),
            ('sma_slow', 100),
            ('macro', 100)    
        )
    
    def __init__(self):
        
        #sma1 = bt.ind.SMA(period= self.params.sma_fast)  # fast moving average
        #sma2 = bt.ind.SMA(period= self.params.sma_slow)  # slow moving average
        
        sma_macro = bt.ind.SMA(period= self.params.macro)
               
        self.macro = self.data1.close
        self.crossover_macro = bt.ind.CrossOver(self.macro, sma_macro)   
        
    def notify_order(self, order):
        if order.status in [order.Submitted, order.Accepted]:
            return
        
        if order.status in [order.Completed]:
            if order.isbuy():
                self.log('BUY EXECUTED {}'.format(order.executed.price))
            elif order.issell():
                self.log('SELL EXECUTED {}'.format(order.executed.price))
       
    def next(self):

        if not self.position:  # not in the market
            if self.crossover_macro < 0:
                self.buy() 
                
        elif self.crossover_macro > 0:
            self.close() 

cerebro.addstrategy(SmaCross, oil = 100)
cerebro.addsizer(bt.sizers.PercentSizer, percents=20)
cerebro.run()
cerebro.plot()

I think I found something in the documentation to help me with this issue: https://www.backtrader.com/blog/posts/2015-09-03-multidata-strategy/multidata-strategy/我想我在文档中找到了一些东西来帮助我解决这个问题: https://www.backtrader.com/blog/posts/2015-09-03-multidata-strategy/multidata-strategy/

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

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