execution price for market order
-
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 TestStrategydf1 = 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()
-
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.