Nano Course On Python For Trading
==========================
Module 3

In this post, I will attempt to teach you how to write an algorithm in python to automatically find support and resistance. We shall use the data we downloaded in module 2.

+

To visualize price action, I have used the daily candlestick chart. To quantify the meaning of support and resistance, I have taken below definitions:
In a series of 5 continuous candles, if the first three candles make increasing high and then 2 decreasing highs, then the

+
high of the third candle is considered resistance. Similarly, if the first 3 candles make lower lows and the next 2 make high lows then the low of the third candle is support.

+
The above explanation is for the sake of quantifying support and resistance at a very basic level. These are called fractals. Now let's get started with the code.

mpl_finance is the new module here. We will use its candlestick_ohlc function.

+
Now, if you remember, you downloaded data for nifty 50 stocks in module 2. We shall use that data. Let's work on ITC data this time.
We will use data from 1 Jan 2021 to 30 June 2021.

+
Let's plot the candlestick chart for this data frame. We are going to use the candlestick_ohlc function from the mpl_finance module. I have defined a function plot_chart() which you can see in the google colab link in the last thread

+
We will iterate over this dataframe and see if the low of the current candle is a support or high is a resistance. For that, we have created an array named levels.

Again remember the definition of fractals, we shall leave the first two candles and the last 2 candles as we

+
need two candles to the right of a candle in order to consider it a fractal. Similarly, we will leave the last two candles. We will iterate over the data frame from index 2 till it is 2 less than its size. We also need to define two functions "isSupport" and "isResistance"
+
determines and returns true/false if a candle low is a support or its high is resistance. In this image below 1-2-3-4-5 is a fractal as explained above, low of the candle on 10 Jan is support and high on 20 Jan is resistance. Repeating this process for the entire data frame.

+
To plot the levels we add it in our plot function (named it plot_all() ) and execute it.

+
But, alas it looks so ugly currently, I don't wanna look at it. There is a way that we can reduce the number of levels marked. What we will do that will first find the average length of a candle and reject all other price levels which are closed then twice this average

+
let's find the average length of a candle i.e. average of high - low

s = 2 * np.mean(df['High'] - df['Low'])

Using this number, we will reject the current price level if there is already another price level within the range +- s. For this, we defined a function isFarFromLevel
Finally, we have reduced the number of price levels and the chart looks like below:

+
If you want to work on the code and tinker with the code, go to Google Colab https://t.co/G8DuZvWO0g

I have shared the entire code on colab and added references for you to look into.

D: these won't help you become profitable but this will teach you how quant traders think &

+
automate their day-to-day tasks and quantify certain ideas. If you have any doubt reach out to me in DM.

+
In Module 4, we will learn about cloud computing and the use of cloud in trading. Until then, happy learning.

More from Finance

Ok here is the explanation. Grab a cup of coffee and read on. If you have not read/noticed this, you will see intraday options movement in a new light.


Say we have two options, one 50 delta ATM options and another 30 delta OTM option. Normally for a 100 point move, the ATM option will move 50 points and the OTM option will move 30 points. But in a high volatile environment, the OTM option will also move nearly 50 points

To understand why this happens, first understand why an ATM option is 50 delta. An ATM option has the probability of 50% of expiring as ITM. The price just has to close a rupee above the strike for the CE to be ITM and vice versa for PEs

Now think of a highly volatile day like today. If someone is asked where the BNF will close for the day or expiry, no one can answer. BNF can close freakin anywhere, That makes every option of an equal probability of being ITM. So all options have a 50% probability of being ITM

Hence, when a huge volatile move starts, all OTM options behave like ATM options. This phenomenon was first observed in the Black Monday crash of 1987 at Wall Street, which also gave rise to the volatility skew/smirk

You May Also Like

๐™Ž๐™๐™–๐™ง๐™ž๐™ฃ๐™œ ๐™ข๐™ฎ ๐™ฌ๐™ž๐™จ๐™™๐™ค๐™ข ๐‘พ๐’๐’'๐’• ๐’ƒ๐’† ๐’”๐’–๐’“๐’‘๐’“๐’Š๐’”๐’†๐’… ๐’Š๐’‡ ๐’•๐’๐’Ž๐’๐’“๐’“๐’๐’˜ ๐’– ๐’“๐’†๐’‚๐’… ๐’•๐’‰๐’† ๐’”๐’‚๐’Ž๐’† ๐’”๐’•๐’–๐’‡๐’‡ ๐’Š๐’ 50๐’Œ ๐’˜๐’๐’“๐’Œ๐’”๐’‰๐’๐’‘ ๐’๐’“ ๐’”๐’๐’Ž๐’†๐’๐’๐’† ๐’Ž๐’‚๐’๐’‚๐’ˆ๐’Š๐’๐’ˆ ๐’š๐’๐’–๐’“ ๐’Ž๐’๐’๐’†๐’š ๐’˜๐’Š๐’•๐’‰ ๐’”๐’‚๐’Ž๐’† ๐’๐’๐’ˆ๐’Š๐’„
Simple and effective way 2 make Money


Idea 1:- Use pivot level like 14800 in case of nifty and sell 14800straddle monthly expiry (365+335) exit if nifty closes on daily basis below S1 or above R1

After closing below S1 if it closes above S1 next day or any day enter the same position again vice versa for R1

Idea2:- Use R1 and S1 corresponding strikes multiple
Incase of R1 15337 take 15300ce
N in case of S1 14221 use 14200pe
Sell both and hold till expiry or exit if nifty closes below S1 or above R1 around closing
If the same bounces above S1 and falls below R1 re-enfer same strikes

Use same criteria for nifty, usdinr and banknifty

(This is must)Use this margin rule for 1lot banknifty pair keep 4Lax margin
For nifty one lot keep 3Lax
For usdinr 100lots keep 4Lax

I bet you if you do this on consistent basis your ROI will be more than 70% on yearly basis.

Couldn't explain easier than this

Criticisms are most welcomed.
1/ Hereโ€™s a list of conversational frameworks Iโ€™ve picked up that have been helpful.

Please add your own.

2/ The Magic Question: "What would need to be true for you


3/ On evaluating where someoneโ€™s head is at regarding a topic they are being wishy-washy about or delaying.

โ€œGun to the headโ€”what would you decide now?โ€

โ€œFast forward 6 months after your sabbatical--how would you decide: what criteria is most important to you?โ€

4/ Other Qโ€™s re: decisions:

โ€œPutting aside a list of pros/cons, whatโ€™s the *one* reason youโ€™re doing this?โ€ โ€œWhy is that the most important reason?โ€

โ€œWhatโ€™s end-game here?โ€

โ€œWhat does success look like in a world where you pick that path?โ€

5/ When listening, after empathizing, and wanting to help them make their own decisions without imposing your world view:

โ€œWhat would the best version of yourself doโ€?