How to Write a Limit Order Strategy with Backtrader? - python

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.

Related

Is there a way to retrieve the hypothetical growth of a stock from Yahoo Finance API?

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.

How to get general spot instances pricing history

I would like to get spot instances pricing history for different regions and instances.
I found how to get my own requests pricing history
and found how to get the current spot instances pricing in the spot instance advisor
But can't find how to get the general history for all instance types and regions. How can I do that? preferably something that is ready for download, or in Python code.
You can use describe_spot_price_history():
Describes the Spot price history.
When you specify a start and end time, this operation returns the prices of the instance types within the time range that you specified and the time when the price changed. The price is valid within the time period that you specified; the response merely indicates the last time that the price changed.
Please note that since March 2018, Spot Prices are relatively stable. Previously, when capacity was required, AWS increase the spot price. Now, however, the Spot Price tends to stay the same but capacity is still recovered when needed. It means that higher bids do not impact the spot price.
For details, see: New Amazon EC2 Spot pricing model: Simplified purchasing without bidding and fewer interruptions | AWS Compute Blog
As a result, the Spot Price History is not particularly interesting any more. The Spot Instance Advisor is just as good a source of information to determine the likelihood of having spot instances taken away.

Stock price and volume derivative at time t

According to Forecasting of Jump Arrivals in Stock Prices: New Attention-based Network Architecture using Limit Order Book Data at page 9, I would be interested in derivating the the price and volume, but I am struggling how to do it. So far I tried nothing, because I just don't know where to start and how to do it.
How can I derive the price and the volume with python?

Items movement daily collection database design system issue

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.

How to solve mysql daily analytics that happens when date changes

I have two separate programs; one counts the daily view stats and another calculates earning based on the stats.
Counter runs first and followed by Earning Calculator a few seconds later.
Earning Calculator works by getting stats from counter table using date(created_at) > date(now()).
The problem I'm facing is that let's say at 23:59:59 Counter added 100 views stats and by the time the Earning Calculator ran it's already the next day.
Since I'm using date(created_at) > date(now()), I will miss out the last 100 views added by the Counter.
One way to solve my problem is to summarise the previous daily report at 00:00:10 every day. But I do not like this.
Is there any other ways to solve this issue?
Thanks.
You have to put a date on your data and instead of using now() use it.

Categories

Resources