Python Packages for Finance
Data Cleaning
Clean any data which is not within market time:
df = df[(df.datetime.dt.time > datetime.time(9, 15)) & (df.datetime.dt.time < datetime.time(15, 31))]
df['calender_date'] = df.datetime.dt.date
day_wise_data = df.groupby(df['calender_date'])
combined_data = [] for calender_date, day_data in day_wise_data: # print(calender_date, val) day_data.set_index(‘datetime’, inplace=True) day_data = day_data.resample(‘60T’, origin=’start’).agg({‘open’:’first’, ‘high’: ‘max’, ‘low’:’min’, ‘close’:’last’}) day_data.reset_index(inplace=True) combined_data.append(day_data)
resampled_df = pd.concat(combined_data, ignore_index=True)
print(resampled_df)
Can also try `df.resample('1H', origin=origin_start)`
### Concepts
* Price Action
* Support Resistance
* Trend lines
* Trading Uday Zone, see how the market is moving today and draw two lines and based on that, decide when to enter
### Bullish vs Bearish
* Bull attacks with horns from bottom to up, hence market going to upward
* Bear attacks from top to bottom with hands, hence market is going in downward direction

### Candle Stick Patterns


#### Dark cover cloud
The "dark cloud cover" is a candlestick pattern used in technical analysis to predict potential reversals in an upward trend

The bearish candlestick should penetrate at least halfway into the body of the previous bullish candlestick. This shows that the bears are gaining strength and pushing the price lower.
The dark cloud cover pattern suggests a potential reversal in the uptrend because it indicates that bears are starting to exert control over the market. It represents a shift in sentiment from bullishness to bearishness, and traders often interpret it as a signal to consider selling or taking profits.
#### Classic example of Reversal
<img width="383" alt="image" src="https://github.com/remidinishanth/distributed_systems/assets/19663316/a5370cc6-5f84-4159-b6ea-c68aed0159ff">
### Long vs Short
[long-and-short-trading-term-definitions-1031122_FINAL-1501219fbdac4b0e90bca691294125ae.webm](https://github.com/remidinishanth/distributed_systems/assets/19663316/c419f002-0d55-4165-9b69-b2b2bb746a01)
Thank of Long as buying something, Short as selling something



Long Put is somewhat confusing because the price of the underlying asset actually is going down, hence you buy a put, but it is called Long Put

### Call and Put Option
<img width="616" alt="image" src="https://github.com/remidinishanth/distributed_systems/assets/19663316/cd928e2d-5440-4b4d-a016-74efca7c1bca">
<img width="922" alt="image" src="https://github.com/remidinishanth/distributed_systems/assets/19663316/ea9eef67-2aed-41c6-90a9-db96852f4135">
* The intrinsic value of a call option = Spot price - Strike price
* The intrinsic value of a put option = Strike price - Spot price
#### In the money:
If the intrinsic value of an options contract is a positive number, then the option is considered to be ‘in the money’.
For instance, assume that the spot price of a stock is currently at Rs. 1,600 and the strike price of a call option of that said stock is Rs. 1,500. The intrinsic value of the said call option would be Rs. 100 (Rs. 1,600 - Rs. 1,500). Since the intrinsic value is positive, the call option of the stock is considered to be ‘in the money’.
<img width="687" alt="image" src="https://github.com/remidinishanth/distributed_systems/assets/19663316/994d7b40-21c2-4095-a116-72078a549057">
#### Pay off
Long Call option - Unlimited Profit, Buying Call option

Short call option - Limited Profit, Selling call option

* Buying a Call option

* Buying a Put option

#### Call option value (vs) Stock price

### Spreads
When you do this ensure
* All strikes belong to the same underlying
* Belong to the same expiry series
* Each leg involves the same number of options
#### Bull call spread



KEY TAKEAWAYS
* A bull call spread is an options strategy used when a trader is betting that a stock will have a limited increase in its price.
* The strategy uses two call options to create a range consisting of a lower strike price and an upper strike price.
* The bullish call spread can limit the losses of owning stock but also caps the gains.
Ref: https://zerodha.com/varsity/chapter/bull-call-spread/
For Backtesting etc
* https://github.com/jesse-ai/jesse
Refer check function in Strategy
<img width="845" alt="image" src="https://github.com/remidinishanth/distributed_systems/assets/19663316/b5dc409d-a87d-4833-8208-c3c429872bb2">
> `should_long()` and `should_short()` are for entering trades only. This means they would get called on every new candle only if no position is open, and no order is active.
> If you're looking to close trades dynamically, update_position() is what you're looking for.
> `go_short()` Same as `go_long()` but uses `self.sell` for entry instead of `self.buy`
<img width="1695" alt="image" src="https://github.com/remidinishanth/distributed_systems/assets/19663316/00498ccc-1f85-4210-af5d-e66b11eac0ea">
- First you need to define your strategy, Refer `def _execute(self)` in `Strategy`
- Then based on your route, it does trading
- 
Not a great design, in go_long we are hard coding, when all to enter and exit to take profits

* `_execute_long` is just looking into `self.buy`, based on the price, it is looking at whether to place (`Stop`, `Limit` or `Market`) order.
* `_detect_and_handle_entry_and_exit_modifications` has logic to handle `self.buy`, `self.stop_loss`, and `self.take_profit` modifications in the program, it will cancel any non-executed orders and will place new ones as per the modifications.
- Everything in that works using `if not np.array_equal(self.take_profit, self._take_profit)` whether things modified from earlier used values.
- Broker is handling reducing position as we start taking profit
```python
self._take_profit_orders.append(
self.broker.reduce_position_at(
o[0],
o[1],
order_roles.CLOSE_POSITION
)
)
- Using filters for entry and exit price, Having so many conditional statements in should_long()/should_short() is not good practice.
-
https://www.backtrader.com/ and https://github.com/kernc/backtesting.py
-
Mayank’s architecture https://twitter.com/mayank_408/status/1682270404470079488?s=20
- TimeScaledDB is recommended for storing tick data
- Communication - GRPC calls between service to service. REST is only for Broker API.
Commments:
- There should be some kind of feedback loop from RMS to OMS and the strategy logic part.
Strategies
Simple moving averages (SMA) based strategies
The basic idea of SMA usage for buy and sell signal generation is already decades old. SMAs are a major tool in the so-called technical analysis of stock prices. A signal is derived, for example, when an SMA defined on a shorter time window— say 42 days—crosses an SMA defined on a longer time window—say 252 days.
Momentum strategies
These are strategies that are based on the hypothesis that recent performance will persist for some additional time. For example, a stock that is downward trending is assumed to do so for longer, which is why such a stock is to be shorted.
Mean-reversion strategies
The reasoning behind mean-reversion strategies is that stock prices or prices of other financial instruments tend to revert to some mean level or to some trend level when they have deviated too much from such levels.” category: “fin” —
Ref
- https://v2api.aliceblueonline.com/introduction
- https://github.com/TulipCharts/tulipindicators and https://pypi.org/project/newtulipy/ and https://tulipindicators.org/
- https://technical-analysis-library-in-python.readthedocs.io/en/latest/ta.html
- https://github.com/twopirllc/pandas-ta#quick-start
-
https://pandas.pydata.org/Pandas_Cheat_Sheet.pdf
- Awesome resource https://napkinfinance.com/napkins/napkins/
Also
- https://tradingtick.com/
- https://in.tradingview.com/chart/YQrjA7DA/
Converting minute candles to hourly candles
import pandas as pd
import datetime
df = pd.read_csv('test_data.csv')
df['datetime'] = pd.to_datetime(df['datetime'])
# Clean any data which is not within market time
df = df[(df.datetime.dt.time > datetime.time(9, 15)) & (df.datetime.dt.time < datetime.time(15, 31))]
df['calender_date'] = df.datetime.dt.date
day_wise_data = df.groupby(df['calender_date'])
combined_data = []
for calender_date, day_data in day_wise_data:
# print(calender_date, val)
day_data.set_index('datetime', inplace=True)
day_data = day_data.resample('60T', origin='start').agg({'open':'first', 'high': 'max', 'low':'min', 'close':'last'})
day_data.reset_index(inplace=True)
combined_data.append(day_data)
resampled_df = pd.concat(combined_data, ignore_index=True)
print(resampled_df)
Can also try df.resample('1H', origin=origin_start)
Concepts
- Price Action
- Support Resistance
- Trend lines
- Trading Uday Zone, see how the market is moving today and draw two lines and based on that, decide when to enter
Bullish vs Bearish
- Bull attacks with horns from bottom to up, hence market going to upward
- Bear attacks from top to bottom with hands, hence market is going in downward direction
Candle Stick Patterns
Dark cover cloud
The “dark cloud cover” is a candlestick pattern used in technical analysis to predict potential reversals in an upward trend
The bearish candlestick should penetrate at least halfway into the body of the previous bullish candlestick. This shows that the bears are gaining strength and pushing the price lower.
The dark cloud cover pattern suggests a potential reversal in the uptrend because it indicates that bears are starting to exert control over the market. It represents a shift in sentiment from bullishness to bearishness, and traders often interpret it as a signal to consider selling or taking profits.
Classic example of Reversal
Long vs Short
long-and-short-trading-term-definitions-1031122_FINAL-1501219fbdac4b0e90bca691294125ae.webm
Thank of Long as buying something, Short as selling something
Long Put is somewhat confusing because the price of the underlying asset actually is going down, hence you buy a put, but it is called Long Put
Call and Put Option
- The intrinsic value of a call option = Spot price - Strike price
- The intrinsic value of a put option = Strike price - Spot price
In the money:
If the intrinsic value of an options contract is a positive number, then the option is considered to be ‘in the money’.
For instance, assume that the spot price of a stock is currently at Rs. 1,600 and the strike price of a call option of that said stock is Rs. 1,500. The intrinsic value of the said call option would be Rs. 100 (Rs. 1,600 - Rs. 1,500). Since the intrinsic value is positive, the call option of the stock is considered to be ‘in the money’.
Pay off
Long Call option - Unlimited Profit, Buying Call option
Short call option - Limited Profit, Selling call option
- Buying a Call option
- Buying a Put option
Call option value (vs) Stock price
Spreads
When you do this ensure
- All strikes belong to the same underlying
- Belong to the same expiry series
- Each leg involves the same number of options
Bull call spread
KEY TAKEAWAYS
- A bull call spread is an options strategy used when a trader is betting that a stock will have a limited increase in its price.
- The strategy uses two call options to create a range consisting of a lower strike price and an upper strike price.
- The bullish call spread can limit the losses of owning stock but also caps the gains.
Ref: https://zerodha.com/varsity/chapter/bull-call-spread/
For Backtesting etc
- https://github.com/jesse-ai/jesse
Refer check function in Strategy
should_long()
andshould_short()
are for entering trades only. This means they would get called on every new candle only if no position is open, and no order is active.
If you’re looking to close trades dynamically, update_position() is what you’re looking for.
go_short()
Same asgo_long()
but usesself.sell
for entry instead ofself.buy
-
First you need to define your strategy, Refer
def _execute(self)
inStrategy
- Then based on your route, it does trading
Not a great design, in go_long we are hard coding, when all to enter and exit to take profits
_execute_long
is just looking intoself.buy
, based on the price, it is looking at whether to place (Stop
,Limit
orMarket
) order._detect_and_handle_entry_and_exit_modifications
has logic to handleself.buy
,self.stop_loss
, andself.take_profit
modifications in the program, it will cancel any non-executed orders and will place new ones as per the modifications.- Everything in that works using
if not np.array_equal(self.take_profit, self._take_profit)
whether things modified from earlier used values. - Broker is handling reducing position as we start taking profit
- Everything in that works using
self._take_profit_orders.append(
self.broker.reduce_position_at(
o[0],
o[1],
order_roles.CLOSE_POSITION
)
)
- Using filters for entry and exit price, Having so many conditional statements in should_long()/should_short() is not good practice.
-
https://www.backtrader.com/ and https://github.com/kernc/backtesting.py
-
Mayank’s architecture https://twitter.com/mayank_408/status/1682270404470079488?s=20
- TimeScaledDB is recommended for storing tick data
- Communication - GRPC calls between service to service. REST is only for Broker API.
Commments:
- There should be some kind of feedback loop from RMS to OMS and the strategy logic part.
Strategies
Simple moving averages (SMA) based strategies
The basic idea of SMA usage for buy and sell signal generation is already decades old. SMAs are a major tool in the so-called technical analysis of stock prices. A signal is derived, for example, when an SMA defined on a shorter time window— say 42 days—crosses an SMA defined on a longer time window—say 252 days.
Momentum strategies
These are strategies that are based on the hypothesis that recent performance will persist for some additional time. For example, a stock that is downward trending is assumed to do so for longer, which is why such a stock is to be shorted.
Mean-reversion strategies
The reasoning behind mean-reversion strategies is that stock prices or prices of other financial instruments tend to revert to some mean level or to some trend level when they have deviated too much from such levels.