Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. ForexBeginner
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    F
    • Profile
    • Following 0
    • Followers 0
    • Topics 4
    • Posts 7
    • Best 1
    • Groups 0

    ForexBeginner

    @ForexBeginner

    1
    Reputation
    5
    Profile views
    7
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    ForexBeginner Unfollow Follow

    Best posts made by ForexBeginner

    • How to avoid overbuying/overselling

      Hello everyone,

      I am a beginner to backtrader. It seems that when I use self.buy or self.sell, I expect the trader to not use more cash than initially set with broker.setcash(x). However, this does not seem to happen. How can I make sure that no more than the set cash is used?

      posted in General Code/Help
      F
      ForexBeginner

    Latest posts made by ForexBeginner

    • RE: Multiple notify_order logs

      @ab_trader Aaah, that makes sense. Thanks a lot!

      posted in General Code/Help
      F
      ForexBeginner
    • Multiple notify_order logs

      Hello everyone,

      I created a simple strategy that does a prediction for the close price of the next day and invests accordingly. If an increase is predicted, the broker will invest 20% of the starting capital. If a decrease is predicted, it will short 20% of the investment. This is for foreign exchange prices. The code is as follows.

      def notify_order(self, order):
              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))
      
              self.order = None
      
          def next(self):
              prediction = #prediction code returning 0 (decrease) or 1 (increase)
      
              self.log('Close, %.5f' % self.dataclose[0])
      
              STAKE_PERCENTAGE = 0.2
              STAKE = math.floor(self.starting_cash / self.dataclose[0] * STAKE_PERCENTAGE)
      
              if prediction == 1:
                  if (self.position.size < 0):
                      self.close()
      
                  if (self.position.size + STAKE < self.broker.getvalue() / self.dataclose[0]):
                      self.log('BUY CREATE, %.5f, size %.2f' % (self.dataclose[0], STAKE))
                      self.order = self.buy(size=STAKE)
              elif prediction == 0:
                  if (self.position.size > 0):
                      self.close()
      
                  if (self.position.size + STAKE < self.broker.getvalue() / self.dataclose[0]):
                      self.log('SELL CREATE, %.5f, size %.2f' % (self.dataclose[0], STAKE))
                      self.order = self.sell(size=STAKE)
      

      My problem is that in the logs I sometimes see more than 1 notify_order log. An example is the following log:

      2018-08-13, BUY CREATE, 1.56699, size 12763.00
      2018-08-14, BUY EXECUTED 1.56703
      2018-08-14, BUY EXECUTED 1.56703
      

      Why is this happening? Am I doing something wrong here? I get some pretty high returns (up to 60% ROI in a year) and very few losses for some currencies.

      Any other general advice is appreciated.

      posted in General Code/Help
      F
      ForexBeginner
    • RE: Getting a calculated value from strategy

      Ah, I missed that in the documentation. Thanks a lot, my problem is solved now!

      posted in General Code/Help
      F
      ForexBeginner
    • Getting a calculated value from strategy

      I have a number that is calculated in the strategy when it is initialized. I would like to retrieve this variable, but it does not seem possible to get this from cerebro because it instantiates the strategy itself. What is a way around this?

      posted in General Code/Help
      F
      ForexBeginner
    • RE: How to avoid overbuying/overselling

      Thank you for the quick reply!

      I am quite new to trading, does this mean that the broker.getvalue() value might overestimate the real value of the portfolio, because loaned money is added to the total value?

      posted in General Code/Help
      F
      ForexBeginner
    • How to avoid overbuying/overselling

      Hello everyone,

      I am a beginner to backtrader. It seems that when I use self.buy or self.sell, I expect the trader to not use more cash than initially set with broker.setcash(x). However, this does not seem to happen. How can I make sure that no more than the set cash is used?

      posted in General Code/Help
      F
      ForexBeginner
    • Beginner questions

      Hello everyone,

      I am new to trading and I am trying to create a simple strategy that uses a simple machine learning algorithm to trade forex currency pairs. Simply put, the model predicts whether the closing price in the next interval will be higher or lower and use self.buy or self.sell accordingly. This, however, leads to ridiculous ROIs such as -1000% or +600% on some currency pairs.

      I don't think I understand self.buy, self.sell and sell.close correctly. From the documentation, when self.buying, you will attain a long position. When self.selling, you attain a short position. My code is therefore as follows:

      if prediction == 1:
          self.log('BUY CREATE, %.5f' % self.dataclose[0])
          self.order = self.buy()
      elif prediction == 0:
          self.log('SELL CREATE, %.5f' % self.dataclose[0])
          self.order = self.sell()
      

      Is this the wrong way of looking at it? When a currency has been bought, does self.sell sell the currency first and if there is none left, it will buy a short position? Should I check for self.position when ordering a self.buy or self.sell command? Should I close the position before executing self.sell?

      My knowledge or forex trading is also very basic so I might have some wrong assumptions somewhere.

      Thanks!

      posted in General Code/Help
      F
      ForexBeginner