I have a data frame and I want to write a function to optimize for profit. The data is organized by entry date. Here are the rules I want to use:
look at a trade by trade number
if there are no overlapping trades going on, complete the trade and take profit
if there is an overlapping trade do not take the trade
end result would be the same dataframe with a column for took trade or not, and if took trade, then the profit. I've attached a picture of the dataframe []
This is the base function I want to build off of but I'm having trouble getting started.
Related
I am new to Backtrader and I can't figure out how to write the following strategy:
Every morning it places a limit buy order at 80% of Open price. If the order is executed during the day (i.e. Low price < the limit price for that day), then sell the stock at Close.
I am using Yahoo's OHLC daily data.
Can any one show me how to write the Strategy part of the code? I posted a similar question on BT's official forum but couldn't get an answer.
Thanks.
I'm interested pulling the hypothetical growth, including reinvested dividends, from the yahoo finance api. I wrote the following code that pulls the hypothetical growth in NOT including the dividends:
import yfinance as yf
data = yf.download("MSFT", '2015-01-01', '2021-09-10')['Adj Close']
ROI = (data[-1] - data[0]) / data[0]
print ("ROI:", ROI)
Is there data that factors in reinvested dividends available on their API somewhere, or do I need to go through the painstaking process of figuring out what the paid dividends were for each quarter, and factoring that in one quarter at a time inside a loop?
First, Your phrase "hypothetical growth" is confusing since your using "AdjClose" data. This is not hypothetical.
Second, You're mistaken. Your understanding of what "AdjClose" represents is wrong. This value exists so that it does include changes to the stock such as dividends and stock splits.
For details refer to yahoo's knowledge base
Yahoo's definition for Adj. Close
However, if you're interested in comparing the changes(GROWTH) between Closing prices and "AdjClose", there is an easy math solution.
simply divide all Closing Prices by the first Closing Price in the series. This will give you the relative change and the normalized growth rate.
perform the same method to the "AdjClose" series. Now you'll see that these ratios are larger, which of course include the changes due to dividends and stock splits. These two ratios can now be directly compared.
If you wish to see the total changes in value, these are also simple calculations. But there are several results to consider and compute: a. simple stock price changes, b. accumulated prices + dividends without reinvestment, c. reinvesting accumulated prices + dividends + stock splits.
However providing you with specific code would require that you narrow your question.
I am checking my data for stationarity. I have a time series with daily data between 1986 and 2019. I would like to know what is the right way to choose the window for the rolling mean and std. I was thinking 252 - the number of business days a year, but I am not sure if this is too big a period.
There are various factors to be considered before choosing the correct time time for rolling mean (moving average). First you should have a clear aim (forecasting, smoothing, etc). Mostly people use it for forecasting in stock prices (thus i assume you have daily data for a stock/commodity/trade-able product and you would like to predict its prices using rolling mean.
Check out the seasonality component and how much fluctuation is happening in the data and if by observation you can see that if has a cycle of say 2 months (60 days, or approx 50 working days) then your period of rolling mean should be lesser than it.
In short there is not sure shot formula for finding the period of most suitable rolling mean to be used, and there is subjectivity involved in it.
I am doing a very simple database in mysql to track movement of items. The current paper form looks like this:
Date totalFromPreviousDay NewToday LeftToday RemainAtEndOfDay
1.1.2017 5 5 2 8 (5+5-2)
2.1.2017 8 3 0 11 ( 8+ 3 -0)
3.1.2017 11 0 5 6 (11+0-5)
And so forth. In my table, I want to make totalFromPreviousDay and RemainAtEndOfDay calculated fields which I show in my front end only. That is mainly cos we tend to erase on the paper due to errors. I want them to be reflected based on changes to the other two fields. As such, I did my table like this:
id
date
NewToday
LeftToday
Now the problem I am facing is, I want to select any date and be able to say "there were 5 items at the start of the day or from previous day, then 5 were added, 0 left and the day ended with 10 items"
So far, I can't really think of a way going about it. Theoretically, I want to try something like this: if the requested day is Feb. 1, 2017, start at 0 cos that's the day we started collecting data. If not, loop thru the records at 0 and doing the math until the requested date is found.
But that is obviously inefficient cos i have to start form first date until the last every time.
Is my approach ok or I should include the columns in the table? If the first, what would be the way to do it in python/mysql?
I think you have to step back a little bit and define the business needs first (it is worthwhile to talk somebody, who worked with stocks before) because these determine your table structure.
A system always tracks the current level of stocks and the movement. It is a business decision how often you save your historical stock level and this influences how you store the data.
You may save the current stock level along with all transactions. In this case you would store the stock level in the transactions table. You do not even have to sum up a transactions per day because the last transaction per day will have the daily closing stock level anyway.
You may choose to save the historic stock levels regularly (on a daily / weekly / monthly, etc. basis). In this case you will have a separate historic stock levels table with stock id, stock name (name may change over the time, so may be a good idea to save it), date and the level. If you would like to know the historic stock level for any point of time that falls between your saved points, then you need to take the latest saved stock level before the period you are looking for, and sum up all transactions to the saved period.
I will have hourly temperature data for each city in US which needs some kind of statistical and plotting post-processing using grandfathered tools in Python. Right now the list has only few cities but it will grown to include more cities.
Each city with hourly temperature data is thus a 2 column vector. The first column being hourly date-time and the second column as temperature.
What is the suggested open source database tool that I can use as I am trying to avoid keeping this temperature data in csv file.
Edit: I also get 10 day hourly temperature forecast each day for each city. I want to store the city historical forecast I receive from vendor every day also.