CONNEXION
  • RetourJeux
    • Sorties
    • Hit Parade
    • Les + populaires
    • Les + attendus
    • Soluces
    • Tous les Jeux
    • Gaming
  • RetourActu Gaming
    • News
    • Astuces
    • Tests
    • Previews
    • Toute l'actu gaming
  • RetourBons plans
    • Bons plans
    • Bons plans Smartphone
    • Bons plans Hardware
    • Bons plans Image et Son
    • Bons plans Amazon
    • Bons plans Cdiscount
    • Bons plans Decathlon
    • Bons plans Fnac
    • Tous les Bons plans
  • RetourJVTech
    • Actus High-Tech
    • Intelligence Artificielle
    • Smartphones
    • Mobilité urbaine
    • Hardware
    • Image et son
    • Tutoriels
    • Tests produits High-Tech
    • Guides d'achat High-Tech
    • JVTech
  • RetourCulture
    • Actus Culture
    • Culture
  • RetourVidéos
    • A la une
    • Gaming Live
    • Vidéos Tests
    • Vidéos Previews
    • Gameplay
    • Trailers
    • Chroniques
    • Replay Web TV
    • Toutes les vidéos
  • RetourForums
    • Hardware PC
    • PS5
    • Switch 2
    • Xbox Series
    • Switch
    • Pokemon pocket
    • FC 25 Ultimate Team
    • League of Legends
    • Tous les Forums
  • PC
  • PS5
  • Xbox Series
  • Switch 2
  • PS4
  • One
  • Switch
  • iOS
  • Android
  • MMO
  • RPG
  • FPS
En ce moment Genshin Impact Valhalla Breath of the wild Animal Crossing GTA 5 Red dead 2
Liste des sujets

Problème avec la librairie backtrader de python

GhostPriders2
GhostPriders2
Niveau 6
08 septembre 2021 à 20:18:16

Bonsoir, je suis encore en train d'apprendre à utiliser la libraire Backtrader et mon code ne possède pas de réelle problème en soit mais ne m'affiche pas les résultats espérés.
J'ai mis une stratégie banale qui se base sur le croisement d'un SMA (période = 20) avec un autre SMA (période = 200).
Je vous explique le but: lorsque le SMA de 20 croise le SMA de 200 en venant du haut (crossdown), un ordre de vente doit être exécuté, et inversement, lorsque le SMA de 20 croise le SMA de 200 en venant du bas (crossup), un ordre d'achat doit être exécuté.
Et mon problème et que les achats et ventes ne sont pas exécutés selon cette stratégie, car en regardant le moment du crossup ou du crossdown quasi aucun ordre n'est exécuté au bon moment.
Voilà mon code:

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import datetime  # For datetime objects
import os.path  # To manage paths
import sys  # To find out the script name (in argv[0])


# Import the backtrader platform
import backtrader as bt


# Create a Stratey
class TestStrategy(bt.Strategy):
    params = (
        ('maperiod', 20),
        ('printlog', True),
    )

    def log(self, txt, dt=None, doprint=False):
        ''' Logging function fot this strategy'''
        if self.params.printlog or doprint:
            dt = dt or self.datas[0].datetime.date(0)
            print('%s, %s' % (dt.isoformat(), txt))

    def __init__(self):
        # Keep a reference to the "close" line in the data[0] dataseries
        self.dataclose = self.datas[0].close

        # To keep track of pending orders and buy price/commission
        self.order = None
        self.buyprice = None
        self.buycomm = None

        # Add a MovingAverageSimple indicator
        self.sma = bt.indicators.SimpleMovingAverage(
            self.datas[0], period=self.params.maperiod)
        self.sma200 = bt.indicators.SimpleMovingAverage(self.datas[0], period=200)
        self.price = self.datas[0]
        self.crossup = bt.indicators.CrossUp(self.sma, self.sma200)
        self.crossdown = bt.indicators.CrossDown(self.sma, self.sma200)

    def notify_order(self, order):
        if order.status in [order.Submitted, order.Accepted]:
            # Buy/Sell order submitted/accepted to/by broker - Nothing to do
            return

        # Check if an order has been completed
        # Attention: broker could reject order if not enough cash
        if order.status in [order.Completed]:
            if order.isbuy():
                self.log(
                    'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                    (order.executed.price,
                     order.executed.value,
                     order.executed.comm))

                self.buyprice = order.executed.price
                self.buycomm = order.executed.comm
            else:  # Sell
                self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                         (order.executed.price,
                          order.executed.value,
                          order.executed.comm))

            self.bar_executed = len(self)

        elif order.status in [order.Canceled, order.Margin, order.Rejected]:
            self.log('Order Canceled/Margin/Rejected')

        # Write down: no pending order
        self.order = None

    def notify_trade(self, trade):
        if not trade.isclosed:
            return

        self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %
                 (trade.pnl, trade.pnlcomm))

    def next(self):


        # Simply log the closing price of the series from the reference
        self.log('Close, %.2f' % self.dataclose[0])
        # Check if an order is pending ... if yes, we cannot send a 2nd one
        if self.order:
            return

        # Check if we are in the market
        if not self.position:

            # Not yet ... we MIGHT BUY if ...
            if self.crossup:

                balance = float(self.broker.getcash())
                tp = 0.02 * balance / (0.002 * self.price)
                sl = 0.01 * balance / (0.002 * self.price)
                long_sl_price = self.price * (1 - sl)
                long_tp_price = self.price * (1 + tp)
                # BUY, BUY, BUY!!! (with all possible default parameters)
                self.log('BUY CREATE, %.2f' % self.dataclose[0])

                # Keep track of the created order to avoid a 2nd order
                self.order = self.buy_bracket(limitprice=long_tp_price, stopprice=long_sl_price, exectype=bt.Order.Market, price=self.price)


            elif self.crossdown:

                balance = float(self.broker.getcash())
                tp = 0.02 * balance / (0.002 * self.price)
                sl = 0.01 * balance / (0.002 * self.price)
                short_sl_price = self.price * (1 + sl)
                short_tp_price = self.price * (1 - tp)
                self.order = self.sell_bracket(limitprice=short_tp_price, stopprice=short_sl_price, exectype=bt.Order.Market, price=self.price)

            else:
                self.order = None

        else:
            if self.order:
                return



    def stop(self):
        self.log('(MA Period %2d) Ending Value %.2f' %
                 (self.params.maperiod, self.broker.getvalue()), doprint=True)


if __name__ == '__main__':
    # Create a cerebro entity
    cerebro = bt.Cerebro()

    # Add a strategy
    strats = cerebro.optstrategy(
        TestStrategy,
        maperiod=range(10, 31))

    # Datas are in a subfolder of the samples. Need to find where the script is
    # because it could have been called from anywhere
    modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
    datapath = os.path.join(modpath, '../../datas/orcl-1995-2014.txt')

    # Create a Data Feed

    cerebro = bt.Cerebro()

    cerebro.addstrategy(TestStrategy)

    data = bt.feeds.GenericCSVData(
        dataname='BTCUSDT_15m.csv',
        fromdate=datetime.datetime(2021, 7, 1),
        todate=datetime.datetime(2021, 8, 1),
        datetime=0,
        open=1,
        high=2,
        low=3,
        close=4,
        volume=5,
        openinterest=-1
    )

    # Add the Data Feed to Cerebro
    cerebro.adddata(data)

    # Set our desired cash start
    cerebro.broker.setcash(1000.0)

    # Add a FixedSize sizer according to the stake
    cerebro.addsizer(bt.sizers.FixedSize, stake=0.002)

    # Set the commission
    cerebro.broker.setcommission(commission=0.0)

    # Run over everything
    cerebro.run(maxcpus=1)

    cerebro.plot(style='candlestick')

Et voici aussi une image du graph avec entouré en vert là où un achat aurait dû être créé et en rouge inversement : https://image.noelshack.com/fichiers/2021/36/3/1631124858-backtrader.png

Pour ceux qui sont familiers avec cette librairie, merci d'avance pour vos réponses. :hap:

Pseudo supprimé
Pseudo supprimé 09 septembre 2021 à 16:58:49

Apprends à coder ?

GhostPriders2
GhostPriders2
Niveau 6
09 septembre 2021 à 23:35:24

Le 09 septembre 2021 à 16:58:49 :
Apprends à coder ?

Non jure :ouch:
Si je viens sur un forum c'est pour de l'aide ou critiques constructives, pas pour qu'on vienne me dire de la merde alors passe ton chemin

Bref, sinon à la base je suis revenu sur le topic pour dire que j'ai résolu mon problème.
Si ça peut aider, le problème venait de la source de mes données qui étaient de binance, j'ai pris celles de Yahoo à la place et maintenant mes signaux sont syncro avec mes ordres.
Voilà bonne journée

Sous forums
  • Aide à l'achat Mac
  • Création de Jeux
  • Linux
  • Création de sites web
  • Programmation
  • Internet
  • Steam Deck
  • Macintosh
  • Hardware
La vidéo du moment