Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    execution price for market order

    General Discussion
    2
    2
    346
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      PythonNoob last edited by

      I have gone through the documentation and it mentioned when order type is "market" then the executed price should be the open of next period but i dont understand how it ends up with below
      e.g. there is an order 2021-01-12, BUY CREATE but ends up the executed price is 128.57 , which is not the open of 2021-01-13. (which yahoo it should be 128.75999450683594)

      and sorry for the second question I saw being asked many times yet I dont see any clear explanation. is there anyway i can run python line by line like F8 in excel VBA? i know there is a debug function (ALT SHIFT E) but when i try to run the line one line it ends up error on lines like defining a class

      Main

      import numpy as np
      import pandas as pd
      import datetime
      import time
      import yfinance as yf
      import backtrader as bt
      from Strategies import TestStrategy

      df1 = yf.download("AAPL",start='2021-01-03',end='2021-02-05')
      print(df1)

      df1.to_csv("YahooFileAAPL.csv")
      data = bt.feeds.YahooFinanceCSVData(dataname='YahooFileAAPL.csv')

      cerebro = bt.Cerebro()
      cerebro.broker.set_cash(500000)
      cerebro.adddata(data)
      cerebro.addstrategy(TestStrategy)

      print('Starting Portfolio Value: %d' % cerebro.broker.getvalue())
      cerebro.run()
      print('Final Portfolio Value: %d' % cerebro.broker.getvalue())

      Create a Stratey

      class TestStrategy(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))
      
      def __init__(self):
          # Keep a reference to the "close" line in the data[0] dataseries
          self.dataclose = self.datas[0].close
          self.order = None
      
      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, Price: %.2f, Cost: %.2f, Comm %.2f' %
                      (order.executed.price,
                       order.executed.value,
                       order.executed.comm))
      
              elif order.issell():
                  self.log("SELL EXECTUED{}".format(order.executed.price))
      
              self.bar_executed = len(self)
      
          self.order = None
      
      
      def next(self):
          # Simply log the closing price of the series from the reference
          self.log('Close, %.2f' % self.dataclose[0])
          # print(str(len(self)) +" "+ str(bool(self.position)))
          # print(type(self.position))
      
      
          # print(len(self))
          # print(self.order)
          # print(self.position)
          if self.order:
              return
      
          if not self.position:
              if self.dataclose[0] < self.dataclose[-1]:
                  # current close less than previous close
      
                  if self.dataclose[-1] < self.dataclose[-2]:
                      # previous close less than the previous close
      
                      # BUY, BUY, BUY!!! (with all possible default parameters)
                      self.log('BUY CREATE, %.2f' % self.dataclose[0])
                      self.order = self.buy()
          else:
              if len(self) >= (self.bar_executed + 5):
                  self.log("SELL CREATED {}".format(self.dataclose[0]))
                  self.order = self.sell()
      
      R 1 Reply Last reply Reply Quote 0
      • R
        rajanprabu @PythonNoob last edited by

        @pythonnoob

        You are reading from Yahoo finance which has a field Adj. Price this is used to adjust the open, high , low, close prices. Thats why you are seeing different price.

        1 Reply Last reply Reply Quote 2
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors