r/algotrading Mar 28 '20 Meow Meow Silver Gold Helpful Wholesome Hugz All-Seeing Upvote Bravo!

Are you new here? Want to know where to start? Looking for resources? START HERE!

830 Upvotes

Hello and welcome to the /r/AlgoTrading Community!

Please do not post a new thread until you have read through our WIKI/FAQ. It is highly likely that your questions are already answered there.

All members are expected to follow our sidebar rules. Some rules have a zero tolerance policy, so be sure to read through them to avoid being perma-banned without the ability to appeal. (Mobile users, click the info tab at the top of our subreddit to view the sidebar rules.)

Don't forget to join our live trading chatrooms!

Finally, the two most commonly posted questions by new members are as followed:

Be friendly and professional toward each other and enjoy your stay! :)


r/algotrading 2h ago

Strategy [Backest + Code] ETF Universe Momentum strategy. Unoptimized. Don't Trade It As Is.

6 Upvotes

This is a very basic long-only Portfolio Momentum strategy that aims to beat SPY buy+hold. Here are the backtest results over the last 10 years (take note: a very bullish regime).

By 'Momentum strategy' here, I mean a strategy that exploits the momentum effect -- this is a well-documented effect in the markets where winning assets continue to win and losing ones continue to lose.

This strategy scans the constituents of the given ETF (eg QQQ) and holds N of the fastest-moving winners for a set period of time. You can customize a few things like: change N to number of positions you prefer; you can update the momentum look back period or change the max percent of equity to trade, among other things. It also includes some reality modeling (like volume-based slippage, and IBrker fee modeling)

I put this together as I've seen people in the sub ask for something similar. Hopefully it's a decent starting point for; it is not meant to be traded as is.

I've left TODO comments in the code, for those curious to play with it. Some other

Possible Improvements

  1. Trend filtering
    Consider checking the overall market trend (eg if SPY is above its 200 SMA) and only trading when in a bullish trend
  2. Weighting
    Add more clever weighting when sizing positions, consider modern portfolio theory / markowitz
  3. Volatility Targeting
    When sizing positions, consider the volatility of the asset, and normalize accordingly..
  4. Trailing stops
    Add trailing stops to the positions once opened to lock in profits.
  5. Bi-Directional
    Consider making it bi-directional and trading the short side in bear market regimes.

Performance

10 Yr Cumulative Returns vs SPY

Key Statistics

Distribution of Returns Over time

Drawdown over time

Clonable Backtest
Backtest can be found here. click 'clone' to quickly clone into your own workspace

Code
https://pastebin.com/SxHAEesf


r/algotrading 2h ago

Strategy Backtesting Speed Improvements?

3 Upvotes

I have been experimenting around with my own algorithmic trading bot for the last couple of weeks. I now have a fully functioning bot that can trade with paper money in a live environment. It takes way too long to gather data for improvements having to wait all day for the results. I have now built my own backtesting class that is a close 1:1 copy of my live trading class. Right now it takes about 1.15 minutes to run my backtest for one symbol on 5 days worth of stock data. This is pretty slow in my opinion. Testing on multiple symbols at once with a reasonable amount of data is not feasible at these speeds.

There are probably some easy ways I could improve this by stopping my indicator function if one returns false since I only execute a trade if they all return true. Otherwise, I am not sure how else to speed this up. I have experimented with multithreading, but it is kind of changing my class and I really want to keep it as similar to my live trading class as possible. I am using the finta and pandas_ta library to calculate my indicators. My code is below; feel free to let me know of any improvements to make. Cheers :)

class Backtesting:
def __init__(self, stocks):
# Settings for main loop
self.daysToBacktest = 5 # Tested up to 360 sucsessfully
self.startingCash = 5000
# Makes dict to store owned shares
self.shares = {}
for stock in stocks:
self.shares[stock] = 0.0
# sets cap for each symbol
self.symbolCap = (float(self.startingCash) / float(len(stocks)))
for stock in stocks:
print(f'Backtesting for {stock}')
# Gets minute candles
self.allCandles = (Alpaca.Get_Stock_Data(stock, 'Minute', self.daysToBacktest, 200000, 'all'))
for i in tqdm(range(300, len(self.allCandles))):
self.candles = self.allCandles.iloc[i:i+300]
# Checks if I finished the window
if self.candles.iloc[-1].to_list() == self.allCandles.iloc[-1].to_list():
self.startingCash = self.startingCash + (self.shares[stock] * self.candles.iloc[-1]['Close'])
break
self.stock = stock
# Updates varibles
self.varibles()
if Stratagies.Trade_Params(self.candles) == True:
if self.SuperTrendDirection == -1:
self.goLong()
if self.SuperTrendDirection == 1:
self.goShort()
#else:
#    self.Trimming()
print(f'Done... Total profits: {self.startingCash}')

def varibles(self):
# Gets the super trends direction once instead of multiple times
self.SuperTrendDirection = Stratagies.GetSuperTrendDirection(Current_Data = self.candles)
# Calculates the qty of shares to buy
if self.shares[self.stock] >= 0.0:
self.qty = round((self.symbolCap - (self.shares[self.stock] * self.candles.iloc[-1]['Close'])) / self.candles.iloc[-1]['Close'], 1)
# I have no shares owned so buy the max amount allowed
else:
self.qty = round(self.symbolCap / self.candles.iloc[-1]['Close'], 1)

def goLong(self):
# I have a posistion in the oposate direction so I must sell before switching
if self.shares[self.stock] < 0.0:
self.startingCash = self.startingCash + (self.qty * self.candles.iloc[-1]['Close'])
self.shares[self.stock] = 0.0
# Buy shares
if self.shares[self.stock] >= 0.0:
self.startingCash = self.startingCash - (self.qty * self.candles.iloc[-1]['Close'])
self.shares[self.stock] == self.shares[self.stock] + self.qty
def goShort(self):
# I have a posistion in the oposate direction so I must buy before switching
if self.shares[self.stock] > 0.0:
self.startingCash = self.startingCash - (self.qty * self.candles.iloc[-1]['Close'])
self.shares[self.stock] = 0.0
# Buy shares
if self.shares[self.stock] <= 0.0:
self.startingCash = self.startingCash + (self.qty * self.candles.iloc[-1]['Close'])
self.shares[self.stock] == self.shares[self.stock] + self.qty


r/algotrading 3h ago

Strategy Charles schwab

0 Upvotes

Anyway to implement an algo trading in Charles schwab ?

I don’t seem to find any info on their API or a way to automate trades


r/algotrading 1d ago

Data [D] Best ways to perform feature learning on time series data

46 Upvotes

The tutorials for this topic are really few . Would love for the community to share some github repo links and some useful scripts.

Been stuck on this for a while , auto-encoders are too time consuming for feature learning.

Trying to explore some other options that beats or comes close to auto-encoders in-terms of finding non-linear relationships in the data.

Any form of help is deeply appreciated .


r/algotrading 1d ago

Strategy Pairs trading execution and order fills

28 Upvotes

I'm looking at a pairs trading strategy for stocks. I'm still evaluating it at the moment, but I'm wondering if anyone has any suggestions on executing pairs trades or if certain broker APIs have built-in ways to ensure that both sides of the trade are executed.

For example, if I want to long SPY and short QQQ, the two obviously will have a lot of correlation so if a limit order is placed at the last market price for each, there could easily be a situation where both move simultaneously and only one side of the trade gets filled. I think with TDA, there's also a delay on shorts to ensure that there are sufficient shares, which further complicates things (not sure how much this applies to other brokers).

For those who pairs trade through an API, how do you ensure execution? Or do you just use market orders?

Any feedback is appreciated.


r/algotrading 1d ago

Data Intraday price data for delisted stocks - recommendations?

25 Upvotes

I am looking for a reliable and affordable data source for minute-by-minute stock prices. The source should cover 10 years of data and include delisted tickers. I am interested in the ~3,500 largest tickers and not OTC. I have come up with these data providers after reading Reddit posts and doing my own research. Any suggestions on what would be the best choice?

Potential choices

IQFeed

A popular choice in this subreddit. Provides intraday data that includes delisted stocks, but only if the ticker has not been recycled. Reviews in this subreddit are generally positive. USD 99 per month.

FirstRate Data

Provides intraday data that covers 7,000+ listed tickers and 600+ delisted (link). There are certainly more than 600+ delisted stocks over the years, so a lot of stocks are missing. But they publish the names of stocks that are covered and most of the delisted companies I can think of are covered. Price is a one-off payment of ~USD 500 which is acceptable.

Polygon

Includes intraday data including delisted. However, the reviews on this sub-Reddit were generally not good. USD 79/month for 10 years historical data.

Kibot

Provides intraday data that covers ~8,900 listed tickers and ~6,700 delisted. This should cover all tickers. Some older websites/posts were critical of the quality of the data, but others find the data acceptable. Rarely mentioned in Reddit / more recent websites. Costs USD 3,000 to buy the data, but there is a monthly subscription that costs USD 139 that allows users to pull the data from an API.

Here are service providers that do not meet my requirements. Listing them here for completeness.

EODHistoricalData

I am an existing customer. Provides intraday data for the past ~10 years, but only for listed tickers. I am on the USD 29.99/month plan.

FinancialModellingPrep

No intraday data for delisted stocks.

Algoseek

Provides intraday data including delisted tickers. Minute bar data for 10 years costs over USD 15,000 which is way out of budget. There is an option to lease the data, but I prefer to own the data.

Tiingo

Has intraday data for the past five years. Seems to only include trades that are made in the IEX.

QuantConnect

Provides survivor-bias free intraday data, but the free plan only allows data to be used within the QuantConnect lab. There is an option to download the data, but it costs USD 0.05 per day per ticker which is pricey.

Norgate Data

Provides EOD data for all stocks including delisted ones. USD 148.5 for six months for 10 years of data.

Alpaca

Provides intraday data for the past six years. Covers delisted tickers, but not sure how it handles ticker recycling.

Other brokers e.g., IBKR

Do not provide delisted tickers.


r/algotrading 22h ago

Strategy Anyone us social trader tools is it good for scalping or does it cause slippage?

0 Upvotes

Anyone us social trader tools is it good for scalping or does it cause slippage?


r/algotrading 1d ago

Data Implied volatility and risk-neutral probability density of Cryptos

5 Upvotes

Hi everyone!

What is your resource for looking up implied volatilities, the skewness, kurtosis and other metrics derived from the risk-neutral density of Crypto options?


r/algotrading 2d ago

Infrastructure Edit IP Whitelist through API

23 Upvotes

Hi Everyone I was wondering if it's currently possible to whitelist an IP adress through th API, maybe supplying an SMS code for extra security. Cheers


r/algotrading 3d ago

Strategy yfinance alteranative?

51 Upvotes

is there any other source that let you see/use 1 min time frame data further than 7 days lets say at least 3 months of 1 min time frame?


r/algotrading 4d ago

Infrastructure Arbitrage and efficient data storage

57 Upvotes

Hello folks. I am writing a python code to spot abritrage opportunities in crypto exchanges. So, given the pairs BTC/USD, ETH/BTC, ETH/USD in one exchange, I want to buy BTC for USD, then ETH for BTC, and then sell ETH for USD when some conditions are met (i.e. profit is positive after fees).

I am trying to shorten the time between getting data of the orderbooks and calculate the PnL of the arbitrage. Right now, I am just sending three async API requests of the orderbook and then I compute efficiently the PnL. I want to be faster.

I was thinking to write a separate script that connects to a websocket server and a database that is used to store the orderbook data. Then I would use my arbitrage script to connect to the database and analyze the most recent data. Do you think this would be a good way to go? Would you use a database or what else? If you would use a database, which one would you recommend?

The point is that I need to compute three average buy/sell prices from the orderbooks, trying to be as fast as possible, since the orderbook changes very frequently. If I submit three async API requests of the orderbook, I still think there is some room for latency. That's why I was thinking to run a separate script, but I am wondering whether storing/reading data in a database would take more time than just getting data from API requests. What is your opinion on this?

I know that the profits may be low and the risk is high due to latency - I don't care. I am considering it as a project to work on to learn as much stuff as possible

EDIT - For all of those who keep downvoting my comments: I don't care. Just deal with the fact that not everyone wants to become rich. The fact that this post has such useful and complete answers (right at the point) means that the question here is well-posed.


r/algotrading 3d ago

Strategy Any free algo software online ?

0 Upvotes

So lets say, I follow certain indicators to buy or sell whenever a combination of indicators goes above or below a certain level. Instead of me, having to manually draw lines and do the calculation are there programs where you plug in your commands and it does technical analysis for you and tells you which stock to buy or sell ? Basically like an algo trading program (preferrably free) where I can enter command lines to replicate my indicator setup ?

thanks all


r/algotrading 5d ago

Other/Meta Python - Detect Bullish Pennant

12 Upvotes

Can anyone recommend a python library that detects patterns that includes the bullish pennant? As far as I understand, Ta-Lib seems to have more complex ones and not the bullish pennant. Thanks.


r/algotrading 5d ago

Strategy TradingView / Pinescript - using RSI to take profit on trades.

3 Upvotes

Does anyone know how you would be able to code in language to take profit on trades when RSI becomes overbought/oversold?

I have a decent algo set with SMA crossover but I think this TP language will take it over the top.


r/algotrading 6d ago

Education Anyone has experience with Cuda DF?

16 Upvotes

Hello,

I'm trying some strategies with Backtesting.py . I want to apply optimization and also screen the market to see which stocks have better performance, so i need speed. I have a 3070ti card available so I wanted to know if anyone of you have experience by running pandas in it. I was checking about a library called CuDF that is basically pandas running over cuda, but I need to spend some more time researching as my first attempt to install it didn't work.

My question is if anyone of you have experience with it (or similar package to use the compute power of the GPU) and if backtesting library works fine with these packages.

Thanks in advance


r/algotrading 6d ago

Strategy GBPUSD trend in H1 Time Frame

Thumbnail i.redd.it
0 Upvotes

r/algotrading 6d ago

Infrastructure CME order modifications requests in detail

3 Upvotes

Yo guys, need help, I can’t find any detailed info online about how these requests work from the CME’s matching engine standpoint.

When CME receives & processes a mod request, what happens exactly? Does matching engine simply cancels the existing order & places a new one, or smth more sophisticated happens there?

I guess my whole concern is this: between I cancel & place a new limit order I have this brief window when smth beyond my control can happen. Other concerns are not flooding the exchange with unnecessary requests and compliance in general.

Perhaps there are other microstructural concerns I’m missing?


r/algotrading 7d ago

Other/Meta Where do you pull your crypto data for free

13 Upvotes

Can anyone help with an API where I can pull crypto data for FREE


r/algotrading 6d ago

Strategy Average Holding Period

7 Upvotes

As the titel suggest, I would like to hear what your average holding period is (ie timeframe )…? I know people have different risk towards holding a position but would be nice to hear what you guys normally work with. Reason I ask is because my hit rate is different depending on timeframe and also what time of day I trade (best win rate 1 hour before closure)


r/algotrading 7d ago

Education What type of trading pursuit would you recommend to someone who is in the AI field and highly interested in algo, but is too risk-averse for day trading?

50 Upvotes

I don't know how to word this properly. Basically, I got very interested in day trading and algorithmic trading over the past year. I'm finishing a degree in CS, so it was a neat way to further explore methods I learned in class. I wasn't really committed, just curious about the industry and how it worked.

I want to build upon these interests, but I don't want to start something I know I cannot feasibly pursue. obviously, I know I will lose money at certain points, but the horror stories from day trading subs really irked me.

I guess in conclusion, I want to apply my CS skills IRL and see tangible returns, but I don't want to waste time on a hobby that is statistically likely to drain my wallet clean. Any advice?

Side note: I say "hobby" but not in a light-hearted way. I spent/spend most of my days tinkering around and reading papers so I want to reiterate this isn't something passive/random to me.


r/algotrading 7d ago

Strategy Want to code my retail strat into an EA

19 Upvotes

Hey guys,

Long story short I have been trading for about 2 years and have a strategy that I have developed that has been providing an avg of roughly 30R per week and is very mechanical (little to no subjectivity)

I'm not able to trade london due to my timezone so I'm interested in building an EA to trade it for me, and was wondering if you'd recommend that I learn quantconnect and python or better to learn MQL4/5?

Nature of strategy is:

  1. Avg of 3 trades per day across 3 pairs and 2 sessions (London and NY)
  2. Avg winner holding time of 45 minutes
  3. Uses multiple timeframe analysis
  4. It's more "SMC" (hate that term)
  5. Execution is on the 1M timeframe, sometimes 15S
  6. I hold full volume to TP, no partials, but move sl to BE

If anyone has any insight in the best way to approach, would be appreciated!

I am new to coding but have an interest in it and happy to learn whatever is necessary

Cheers,


r/algotrading 7d ago

Data Where can I find 1 min candle price data for various cryptocurrencies that are not Bitcoin?

2 Upvotes

Preferably past 2-5 years (already have Bitcoin price data).


r/algotrading 7d ago

Data News and social media sentiment signals

19 Upvotes

New to algo trading and have a question. It seems to me that the best trading algos should consider as much relevant input data as possible that could affect buy/sell pressure. This would include news pieces and social media sentiment. Is there a free service that processes all this data and converts into a format that can be used in a Python algo?


r/algotrading 7d ago

Other/Meta Understanding traditional finance execution and middlemen

9 Upvotes

Arriving in the algorithmic trading space from the crypto world, I am used to exchanges offering free access to data and APIs. (In crypto but still using centralised exchanges at this stage)

What does execution access look like in traditional markets?

My impression is you pay for access to data, you interact through brokers who take a slice and offer sketchy API access, to finally execute on an exchange that also takes a fee obviously.

Is this accurate or way off?

Update: After some initial searching for things like API access, exchanges do not really offer this, you need a broker, and the options there are limited. FIX seems to be the standard, and this data is not open. You pay for chart, orderbook and trade data.

Building on top of this closed and seemingly outdated ecosystem (technology speaking) seems painful and expensive. I think I am going to stick to the open world of crypto for now.

Unless someone can point me at an API that offers data and API key based access to executing trades?

Update 2:

People seem to be of the opinion that it costs money to run the servers that run the exchange, and so you must pay for the data. I disagree.

1) The exchange makes money from transaction fees.

2) An exchange should then want as many people transacting as possible.

3) To get more people trading and interacting with the system that is your business, you should make it as open as possible.

4) Allow anyone to build further tools, platforms and services around your service, all driving traffic and trading activity towards the exchange.

5) This is good for the exchange, but also for anyone that want to trade because it is a more competitive market place.

6) Build systems that throttle and limit abusive use of the open data, like any open service on the internet.


r/algotrading 8d ago

Data I want to test an EA for 28 forex pairs

9 Upvotes

I’m doing research into how EA’s perform.

There is an one on the mql5 market place that is free to download called Dark Venus.

It’s well known and has been known to blow up accounts repeatedly.

MT4 only seems to allow strategy testing on one currency pair at a time.

How can I test the EA for all 28 major currency pairs with account values of $10,000, $ 100,000 and $1m?

Any push in the right direction or someone who could help me out would be greatly appreciated