For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Trying to check 3 consecutive candles in the same direction



  • Hello,

    i'm trying to check for for consecutive candles in the same direction in my CSV, here is how im doign it:

    Strategy file:
    #------------------------------------------------------------------------------------------------------------------------------------------------
    import backtrader as bt

    class ThreeCandles(bt.Strategy):

    #def log(self, txt, dt=None):
    #    dt= dt or self.datas[0].datetime.date(0)
     #   print("{}, {}".format(dt.isoformat(),txt))
    
    def __init__(self):
        self.dataclose = self.datas[0].close
        self.dataopen = self.datas[0].open
    
    def next(self):
        #self.log('Close, {}'.format(self.dataclose[0]))
    
        if self.dataclose[0] > self.dataopen[0]:
    
            if self.dataclose[-1] > self.dataopen[-1]:
    
                if self.dataclose[-2] > self.dataopen[-2]:
    
                    print("Três linhas de crescimento consecutivas !")
        
        elif self.dataclose[0] < self.dataopen[0]:
    
            if self.dataclose[-1] < self.dataopen[-1]:
    
                if self.dataclose[-2] < self.dataopen[-2]:
    
                    print("Três linhas de decrescimeto consecutivas !")
    

    #-------------------------------------------------------------------------------------------------------------------------------------------
    Main file:
    #------------------------------------------------------------------------------------------------------------------------------------------------
    from future import (absolute_import, division, print_function, unicode_literals)

    import datetime
    import os.path
    import sys
    import strategies

    if name == 'main':
    cerebro = strategies.bt.Cerebro()

    cerebro.addstrategy(strategies.ThreeCandles)
    
    modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
    datapath = os.path.join(modpath, '/home/daniel/Documentos/FOREX/data/values.txt') 
    data = strategies.bt.feeds.YahooFinanceCSVData(dataname=datapath, fromdate=datetime.datetime(2002, 1, 1), todate=datetime.datetime(2002,12,31), reverse=False)
    
    cerebro.adddata(data)
    
    cerebro.broker.setcash(sys.argv[1])
    
    print("Begin with com: {0}".format(cerebro.broker.get_value()))
    
    cerebro.run()
    
    print("Finish with com: {0}".format(cerebro.broker.get_value()))
    

    when i run it im getting this error:

    Begin with com: 1000
    Traceback (most recent call last):
    File "main.py", line 24, in <module>
    cerebro.run()
    File "/usr/local/lib/python3.5/dist-packages/backtrader/cerebro.py", line 1127, in run
    runstrat = self.runstrategies(iterstrat)
    File "/usr/local/lib/python3.5/dist-packages/backtrader/cerebro.py", line 1187, in runstrategies
    self._broker.start()
    File "/usr/local/lib/python3.5/dist-packages/backtrader/broker.py", line 64, in start
    self.init()
    File "/usr/local/lib/python3.5/dist-packages/backtrader/brokers/bbroker.py", line 287, in init
    self._fundshares = self.p.cash / self._fundval
    TypeError: unsupported operand type(s) for /: 'str' and 'float'

    what am i doing wrong? could it be something wrong with my csv file?


  • administrators

    @dvenzi said in Trying to check 3 consecutive candles in the same direction:

    self._fundshares = self.p.cash / self._fundval
    TypeError: unsupported operand type(s) for /: 'str' and 'float'

    Your code is far from legible (please see the top of the forum for how to format code blocks)

    But the above error should be the key: you have managed to get this operation: str / float, which means that you have set the cash using a string.



  • Hello,

    sorry for the ilegible code =s .
    You are correct, i was setting the cash as a string. Already fixed it.

    Thanks !