Free Option buying strategy with python code! — backtested on Indian market data | FakeTrades
FakeTrades.in
← all strategies

Free Option buying strategy with python code!

Analysed 03 Aug 2026, 03:30 AM IST
⏳ Backtest pending — a data-backed verdict will be attached.

Detected components (auto-read from transcript)

Options (buying)FuturesIntraday ATR

Claims it makes (quotes pulled from the transcript)

  • “hi everyone in this video I'm going to explain you a option buying strategy by using ranko chart uh since it's a buying strategy so it's a highrisk high reward ”
  • “2 in the after next 5 Second now what this will do this will make sure that if there is a price difference between my limit price and the current price so we wi”

Verdict

Not auto-backtested — honestly, we can't. AI-decoded: Renko-chart-based ATM option buying strategy (calls/puts) with partial exits, trailing stop-loss, and awesome-oscillator confirmation on BANKNIFTY intraday.

We give real option backtests only for fixed-entry option-selling structures (weekly credit/ratio spreads) priced on real cached NIFTY premiums. This one is directional option BUYING (needs intraday/tick option data), which needs intraday/tick option data and a chart-signal engine we don't have — so we show no number rather than a misleading proxy. Flagged for a hand-built review.

See strategies that scored 4★+ →
Know someone trading this?
Full transcript (17317 words)
hi everyone in this video I'm going to explain you a option buying strategy by using ranko chart uh since it's a buying strategy so it's a highrisk high reward strategy uh the risk to reward ratio is 1 is to one but you have the flexibility to change it as per your risk appetite uh I have implemented this entire strategy by using fire API but uh you can go for some many other broker to 80% of the code Remains the Same you just need to change the broker related code and and you can use it on whichever broker you're comfortable with now this entire code that I have used in this video if you want to access this I want you to go to the comment section you'll find our WhatsApp number WhatsApp link just click on the link or just DM us on WhatsApp let us know that you want the code for Reno option buying strategy and we will give the code for free to you okay uh this is how the entire code looks like right and here you can see there are parameters like how much profit point you want how much stop loss you want how what should be the break size so these are all the things uh which you have to change uh as per your understanding and as per your risk appetite and deploy the strategy now once you get the code I don't want you to directly deploy it in the live market first run it in the paper trading environment run it for few days uh play with the parameters once you're confident with the strategy uh then you can deploy it in the live market now the capital required for this is also low because it's an option buying strategy you can start with uh 20 to 30,000 and you can see how the strategy works now I'm not guaranteeing anything I don't say I'm not saying that the strategy will work but this is a good uh place where you can learn how to implement a end to end option buying strategy from start to end right so now this video this entire video is divided into four part uh in the first part I'm going to explain you what are Reno how do they work in the second part I will explain you the about the strategy so in the strategy I'm using uh multiple indicators like da and Channel and awesome oscillator how I'm using it and how everything works uh is what I'm going to explain you in the second part in the third part I'm going to explain you how to use this code because a lot of student they come to me they take the code and they still don't know how to use it so I I will spend like 5 to 10 minutes just to explain you how this entire code works and in the last part I'm going to explain you the entire code line by line okay in detail I'm going to explain you so if you are someone who wants to design such type of strategy end to end do watch this video till the end you'll learn a lot you'll understand how entire trading system is designed okay so let's start with the first part that is how Reno Works let's try to understand what Reno charts are and how do they work so this is the candle chart you can see in front of you this is Bank Nifty index and right now the time frame is 1 minute okay so I just have to click on the candle and change this candle to Reno and this is how the Reno chart will look like now in Reno chart the most important parameter is a brick size how do you change the brick size if you do a right click if you click on setting uh you will see the box size assignment method so right now I've selected it to traditional there are two option one is traditional and one is ATR when you click on traditional you can give your own box size and the box size can be anything and when you click on ATR it will use the current ATR whatever the current ATR is with 14 period and it will use that number as the box size okay so this is how you change your setting to uh from Reno from candle to Reno and this is how you can change the box size uh let's try to understand now so I'll just change this to traditional and I right now the box size is 25 and I've clicked on okay and this is how the chart looks like let me explain you how uh a candle chart is converted to a eneno chart now remember that if I change the time frame right now I'm on 1 minute time frame if I change this to uh let's say 5 minute time frame the chart will also change accordingly if I change to 30 minute again the chart will change okay this is because when you are on a 1 minute time frame like 1 minute candle time frame it is using the closing price of the candle to calculate the Rango and when you move your time frame from 1 minute to 5 minute it will use the closing price of the 5 minute so it will look at that closing price and accordingly it will calculate the Reno breaks okay so let's try to understand how they are calculated now the biggest difference between candle and Reno is the time frame okay okay in Candle chart you will see a candle at every time stamp so if it's a 1 minute time uh if if the interval is 1 minute so at every minute you'll see a candle but in Rano you will only see a brick so in in Rano we call it a brick you can also call it candle brick whatever but it's the right term to use is Breck so in Reno chart you will only see a Breck when there is a price price movement whereas in a candle chart you'll see a candle when there is a Time M or you can say when the time moves because every minute or every interval there will be a new candle so let's take an example let's say you have a stock and its current price is 100 okay and brick size is five right now so I've given five unit as a break size so what does this mean and remember that the reference this is my starting point my starting point is 100 uh whatever the starting point you will take accordingly the chart will slightly change okay slightly I'll I'll show you the what do I mean by this in few minute now if the price moves from 100 to 101 nothing will happen if the price moves to 104 then also nothing will happen why because brick size is five so a new brick will only form if there is a price movement of at least 5 point either in the upward direction or in the downward direction if it moves to 106 you'll see a one positive break if it moves to 94 then you will see a negative break so there is a requirement of at least five point mement so let's say at 10:15 there was a price movement of uh 6 point so you will see a green brick okay so the lower level will be 100 and the upper level will be 105 okay you will only see a green brick if there is a price movement of whatever the brick size is now let's say it took 1 hour and in 1 hour again there was a movement of 5 point so right now we are on 11:15 so a NE brick now at 11 15 the price has moved to 111 so you'll see a new green brick now the brick size will be same but this level will be 110 so now we have three levels one is 100 one is 11 one is 111 now see the difference here the difference is 1 hour now in 5 minute itself there was a major movement and the price has moved to let's say 116 so in 5 minute this is 5 minute this is 60 Minute okay uh this is 11 uh 20 at 1120 price has moved to 106 so you'll see another brick another green brick so the price should be above what it should be above 105 only then you will see a green brick so this is the difference okay you will only see a break if there is a prim movement I can show you so here you can uh right now what is the brick size in this uh chart the brick size is 25 and these are the 25 brick uh size break that you can see I'll just change the time frame to 30 minute to 1 minute and I'll show you show you now this break you look at the time here you can see the time okay this is 11:58 the next brick is 12 the next brick is 12:16 then 12:34 so the difference in the time is different okay a new brick can form at any time this is one of the most important thing that you should understand about Reno and this is what makes Reno so different it only creates a candle or it only creates a break when there is a P Price movement it doesn't care about the time it might take 1 hour it might take one day it might take one year uh if you have a big brick size so if you want to uh create a successful Reno strategy this brick size is the parameter that you need to think about if you can come up with a good brick size then you can uh make the strategy work otherwise your strategy will fail now right now we are at at let's say 10 okay wait this is 105 this will be 115 not 105 my bad uh so let's say the current price is 116 and from 116 let's say it has moved to uh 111 again okay so from 116 it has moved to 111 do you think anything will happen no because right now the price is between this so nothing will happen so the price should at least move below this level okay even if it moves below this level still I'll not see any new candle because the price should breach this level so if the current price from 116 if it moves to 104 okay if there is a 10o move only then I'll see a negative brick so this will be this is green brick this is red brick and when will I see see the red brick when there is a PR price movement of 10 points so from 116 so let's say here we have 116 and in 1 minute okay in 1 minute so we have 1129 21 price has moved to to 104 so there is a 10o movement only then you'll see a red break okay now when you will see a next red brick you'll see a next red brick if the price moves to below 100 let's say it has moved to 99 so you'll see another red brick uh this will be this level is what this level is 100 okay and let's say it has moved below 100 that is 99 so this is how green bricks are formed and this is how red bricks are formed now we when whatever candle size you choose like if you choose 1 minute it will look at the closing price of 1 minute if you take 5 minute it will look at the closing price of 5 minute and accordingly the Reno chart will change as per the uh time frame okay now a few more thing that you should know what is the closing price so here when you look at this green candle what will be the closing price the closing price of this candle will be 105 okay the closing price of this candle will be what 110 uh closing price of this candle will be 105 now what will be the closing price of this red candle this red candle closing price will be 105 again okay so from 115 closing price it has moved to 105 and then the next closing price will be what next closing price will be 100 so this is how the closing price changes so when it's a green brick let's say this is a green brick okay so what will be the Open high low close open will be this high will be this low will be this and close will be with this so high and low will be same for a green Breck and open and low will be same for the green Breck but what if it's a red brick if it's a red brick this is my open okay this is my high and this is my low and this is my close so in case of a red break open and high will be same and low and close will be same now why am I telling you this because when we will look at the python code we are going to get the closing prices so we I have written a code to convert that closing prices to a open high low close of a brick size of a candle brick now why do I do this because if you want to calculate any indicator uh you might need like open high low close so even a even a Reno can have a open high low close but this is how the Open high low close will look like I'll just show you let's take a look at a green brick so see here you can see open high L close I want you to notice that when I hover on a green brick my open and my high sorry my open and my low is same you can see when it's a green brick my open and low is same I want you to notice that open and low is same okay uh and my high and close is same so when I'm on a green brick my high and my close is also same but this changes when I'm on a green brick so when I'm on a green brick my open and high is same you can see here here open and high so when I'm on a green brick open and high is same low and close is same so when you're on a red brick the Open high low is slightly different when you're on a green brick open high low is slightly different so this is how uh Reno chart Works uh let's try to understand the strategy now you you can see right now I'm on Bank Nifty uh so the date is uh 21st of Feb okay first thing is you'll go to the candle and just change this to Reno okay this is the first step then we have to uh change the Reno size briak size so click on setting and here you will see the box size so for Simplicity purpose I'm taking 50 even if you divide 4897 four with 1,000 you will get 48.9 if you approximate it you'll get 49 so either we'll take 49 of 50 as a brick size so this is our Breck size uh now once you change the candle chart to Reno chart and you have added The Brak size then you have to add two indicators so click on this function and just search for uh dachan channel so this daen channel what it will do it will take the last 20 candle so you can see the input is 20 so it will take the last 20 break and it will calculate the high and low okay so I'll show you one example so here from this point to this point this was the high and this was the low from this point to this point this was the high and this was the low so all it is doing it is taking the high and low as the input see if I click on setting I click on setting the input is high and low like in if you'll understand it in the code part that the input it takes is high and low and it gives me the upper lower and uh lower and bases so I don't need this basis I just need upper and lower this is the first indicator you have to add uh then you have to add another another indicator called awesome oscillator so awesome oscillator tells us the momentum in which direction the momentum is if it is positive it means it is in upward direction if it is negative it means it's a in the downward Direction okay so I'll show you both the example one for short and one for long so at this point you see this is the dachan high this is the dachan low and this is The Brak size so this brick has breeed the upper High okay so I'll just show you the theory for the strategy so this is the theory uh chart type is traditional Reno chart and notice that that my uh candle size is 1 minute okay so this is a 1 minute strategy time frame is 1 minute brick size will be whatever the index value is divide by th000 so if you divide uh our bank Nifty let's say the bank Nifty is 50,000 if you divide 50,000 uh with 1,000 you'll get 50 as a brick size so these are our brick size indicator used is daan Channel and awesome oscillator also you can take whatever brick size you want uh this is the default value but if you want you can have it as 100 150 whatever you think is good enough now what is a buy rule so this is our buy condition a buy is triggered now when I say buy I mean to say I we will take the add the money call option I will buy add the money call option that's what I mean when I say long so the Reno close the Reno close above the upper down chain channel so you can see this brick is closing above the daon channel so this is my first condition satisfied this is where my first condition is satisfied and the other condition is of the given base plus half the brick size now this half the brick size I call it buffer okay you can change this buffer as per your requirement uh the default value will be half of the brick size so this is the first brick that breaches the uh upper level plus half the brick size it means here around here I will draw a line for you me just draw a horizontal line so it will be somewhere around here okay so first The Brak should breach the upper level plus the current price should be above half of the brick size so this half of the brick brick size I call it buffer okay so remember two parameters I have explained one is the brick size and one is the buffer so if you go to my code you will see two parameter uh one is the brick size and one is the buffer so if you don't specify if you write here zero it will take the brick size at spot price divide by th000 and if you write buffer as zero it will take half of The Brak size okay so this is where I'm going to go long long means I'm going to buy the call ATM call option after going long I'll have a fixed take profit and stop loss now this also you can give any value you want let's say the take profit is 100 point and stop loss is 100 point so again I have two variable one is take profit and one is stop loss so you can take take profit as 100 point and stop loss as 100 Point okay so let me just add one more thing here or I'll just add two more lines horizontal line so if I go with 200 so each brick is 50 point so 200 100 points will be around somewhere here this will be my take profit and I'll add another horizontal line this will be my stop loss right so this will be to 100 this will be 100 so 100 is my take profit 100 is my stop loss so if the current price so at this point I've taken the position if it reaches here this is where I'm going to close half of the position okay so let me read the theory for you so what is our buying condition the Reno class closes above the upper daon level plus half of the brake size and the awesome oscillator should also be green so at this point you can see awesome oscillator is also green right so my buying condition is satisfied uh buy order placement buy the nearest ATM option example if bank nift is this we will buy this ATM option what is the short rule short rule is similar I'll show you example in few minute but there are three exit condition first exit condition is partial exit exit half of the quantity upon reaching the certain Target so this is my target one this is my the only Target that I have so if the current price reaches here this is where I'll close half of the position so minimum two lot I have to buy I cannot run the strategy with a single lot minimum two lot I want because I want to close half of the lot when the first take profit hits so at this point I'll close half of the take profit and I will change the stop loss from this to this level okay I'll repeat when the price reaches from this level to this level that is from when it reaches from 49221 to 49321 uh I'm going to close half of my position half of the call position and my stop- loss will change from 49,1 1119 to 49221 okay so this will become my current price this will become my stop price and I have closed half of the position and from this point onward I'll keep doing the trailing so as the price moves up up I'll also move the stop- loss initially the stop loss will be as it is it won't be a trailing stop loss once I close half of the position that is when I'll convert my stop- loss to a trailing stop loss okay that's what I've have written here partial exit exit half the position upon reaching a certain level trailing stop-loss exit the remaining quantity based on trailing stop loss and there is one more condition that is uh intraday exit if the target reaches the uh stop loss or the trailing stop loss or if it is not triggered exit by 315 you can change this time as per your requirement I'll just repeat again so what is my entry condition entry condition is my Reno brick should close above the high plus the buffer right now the buffer is half of the brick size so this is where I'll enter I'll have a fixed take profit and fix stop loss so take profit will be this stop loss will be this if my current price reaches the take profit I'll close half of the position uh and I'll change the stop loss my stop loss will change from here to here and as the price moves up I'll keep the trail I'll do the trailing okay the stop loss will also move with the price so when will we close this position then if this is the case so we will mostly close somewhere here okay around this price we will close the position due to stop loss set trailing stop loss it right so we would have made some profit of 200 300 point I don't know whatever so this is one example of uh long okay where I'm taking position in the call leg similarly here we will have a put condition so this is where the dawn uh the brick is breaching the lower dachan level and plus half of the brick size so at this point I will go short when I say short I mean to say I will buy the put leg right and then we'll have a take profit and a stop loss so this will be my take profit this will be stop- loss So at around this point I'm going to close half of the position and I'll keep doing the trailing trailing trailing and I think at around this point we will close half of uh we will close our entire position due to stop loss it okay so let me read it again so there are three exit condition partial exit exit half the quantity upon reaching a certain Target trailing stop loss exit the remaining quantity based on the trailing stoploss exit last condition is we'll exit our position at 315 re-entry after TSL or SL hit if my if I close my position due to stop loss or trailing stop loss we will re-enter when any brick closes above or below the daon level so if the same condition is satisfied again so see here I would have closed the position here this closing is because of because of my um uh trailing stop loss but if okay now I have moved to the next day if this would have happened in the single day I would have re-entered here but since the day has changed so I won't be taking any more trade on this day also we have only got like one shot trade but on some day you might get like more than one trade okay like if Market extremely moves up and extremely moves down so you will at least take two trades in that situation otherwise we will only take one trade if the market is in One Direction okay so this is the re-entry configurable parameter these are all the parameters that we can change now let me explain you how to use the code you have understood the strategy now now if you scroll down first you have to uh install all these packages now remember in case of jupyter notebook whenever you run this in the first uh in the morning so you have to install this again and again in jupyter Notebook it's not like you have installed it once and then you don't have to install it you have to install each day again and again this is not the case for a local computer if you copy this entire code paste it in your local computer in your code or somewhere if you run the code then you don't have to install every day one downside of Jupiter or one downside of Google collab is you have to install the packages every day the first thing uh once you have installed it I can show you how it looks like when you run it for the first time it will ask you to restart the terminal so let's see then I have the parameter I'll just explain you the parameter how to change the parameter this is where uh you have to change your client ID and secret key write your own client ID secret key don't use mine uh then write write your redirect U so my redirect URI is fao.com in your case it will be something else let me run it again this might happen when you run it for the first time so run it again you can see it is installing how to know see right now it is checking for this library then it is now it is checking for this library in few minute it will check for the next Library so if in the first time when you are executing the code if it throws some error don't worry just run it again now it is installing setup tools so let it run till then I'll explain you the other parameter so the strategy name is Reno option buying uh index name now if you want to run it in Bank Nifty just change it to Nifty bank if you want to run it on nifty50 change this to nifty50 exchange will be n and this will be the ticker now let's say you want to run this on some uh MCX contract future contract so just make a ticker variable and write your MCX contract here so I'll just change this to March and this is my MCX contract you you don't have to change these things just uncomment this write this and this strategy will run for uh crude oil March future if you want to run it on nifty50 just write here nifty50 that's it now we have strike Count Strike count means how many far out of the money and in the money we want you can keep it 10 don't need to change this strike difference uh see if you take nifty 50 strike difference is 50 that is the difference between two strike is 50 if you take Bank Nifty the strike difference will be 100 okay now if you want to do paper trading just write your paper if you want to do life trading just change this to life okay this is the only thing you have to change if you want to do life trading uh exchange will be so our time zone will be isia Kolkata now why I have done this because jupyter notebook time zone I don't know so if I write if I specifically mention the time zone so this strategy will run as per the Indian time zone start time end time you can keep your start time whatever you want I'm keeping it 9:30 my end time will be 1515 so I'll change this to 1515 this will be my end time quantity now make sure that you write the correct quantity for each uh instrument it is different for Bank Nifty it is different for Nifty it is different and minimum two lot I want okay so if I go for uh maybe MCX crude oil so I have to write here 200 uh I think if I go for Nifty Bank Nifty it is 30 right so I have to minimum go for 60 quantity Bank Nifty so make sure you write the correct quantity at least two lots we want now Trail Point uh now what is this Trail Point Trail point is the value which represent after how much Point Movement we want to Trail okay let's say the current price is 50,000 okay and our uh stop loss is uh 49,900 now my tra point is five it means if the if the bank Nifty moves by 5 point I'll Trail my stop by 5 point if the bank Nifty moves by 5 point I'll Trail the stop- loss by 5 point this is how I'm going to do the trailing so at least five point movement I want so this variable also you can keep it whatever you want but uh five or six sounds like a good parameter so if there's a fiveo move then only we will Trail or you can also keep it 10 if there is a 10o move only then I will Trail da Channel we are taking the input as 20 brick size okay so I have explained you the brick size is the most important one if you are not sure what to Brick size leave it zero if you leave it zero we will take the value whatever the spot price is divide with th000 or you can give your own value you can let's say you want brick size to be 100 so you can write here 100 uh buffer U if you don't if you write it zero buffer will be whatever the brick size is divide by two in this case it will be 50 or you can give your own buffer like 30 okay then take profit and stop loss point so again you can give whatever you want if you are someone who want uh 2 is to one risk ratio uh reward risk reward ratio so you can write take profit as uh maybe 200 and stop loss is 100 so this is 2 is to one reward ratio but remember when the take profit reaches I'm closing half of the position so other half will we will still gain more profit by doing the trailing stop loss right so this is how you need to change the parameters uh and once you if you can run this I think this is done executing I'll just clear all the output just click on clear all cell output I think this is done this is also done now I'll run this this is also done once you execute all the code this is where you'll find the uh remaining code if you click on show code you'll see the entire code right this code you can copy it in your computer and run it in your local system also if you want to hide it just click on hide now when I run this what will happen it will ask me to enter access token you see it is asking me to write the URL so just click on this link verify yourself once you verify yourself you go to your redirect URL in my casee it is fap.com I'll just copy this and I will paste it here now what will happen if I press enter you will see when I press enter it will create a new access.txt file here see if I press enter I'll just press enter it will create access.txt file you you can see access.txt file is created and the strategy is started so I executed the code so when you execute it you'll get some output like this you can comment all the unnecessary part of the code but if you scroll down uh you will see the time being printed after every 12 second I'm running my main strategy after 12 second that's why you will see right now the time is uh here it is showing 15450 if I scroll down it is showing 15 15 if I scroll down is showing 15 28 if I scroll down it showing 1539 by looking at this number by looking at this time you'll come to know if the strategy is running or not okay once you done running the strategy once everything has happened uh at the end you can run this piece of code this will show you list of all the trades executed okay so I've have executed in my local computer you can see it has created uh this file called Rano uh trades ranko option buying and there is a date so this is this is something I executed uh last to last day so you can see the date 2025 D12 -9 so if you just run this piece of code it will show you this data frame or you can say this CSV file now let me go through the entire uh file and I'll show you how the trade looks like so this strategy I executed on uh crude oil and the brick size was three okay so at exactly 1211 my buying condition was satisfied so I I'm buying and this is what this was the add the money option for crude oil and this was the price okay uh and this was my stop price this was my take profit this was my spot price so my spot price was 274 and my take profit is 279 and my stop price is 269 okay I I think the brick size was six maybe six uh or five I don't remember some there was some brick size if you just subtract this from this you'll come to know I think the brick size was five okay so brick size was five so accordingly I have a take profit and the St stop loss this happened at 1211 now exactly at 1231 I sold half of my position so the price has moved from 274 to 280 that is it has breached the take profit when it breaches the take profit I close half of the position right and then at uh 12:32 in 2 minute I have to Trail again because the price has moved up by 3 point so from 280 it has moved to 283 so my stop loss has changed stop loss was initially it was 274 now it is 278 from 283 it has moved to 289 so my stop loss has also moved to 284 so see we are just trailing from 289 it has moved to 294 so again I'm trailing so my new stop loss is uh 289 and finally at around 1341 so all this trailing happened around 12 to 1 uh 13 so at 1341 the price moved down and it breached my stop- loss which was uh 284 and we sold half of our position so now my final uh quantity zero so I started with 60 I closed half of the position I trailed it and at around uh 1341 I closed the remaining half and at 1350 again my buy condition was satisfied okay uh so this is what this is again call so again it the the market is going up so that's why I bought the call option so after buying so see the spot price was 297 from 2 295 it moved to 310 so at 310 I closed half of the position and then I trailed and then again I sold the remaining position so this is how the trade log looks like I'll show you another day so on this day it was not a good day so it continuously buy it continuously done buy and sell again and again so it made some losses you can see I'm buying 60 0 60 when you used to see 60 0 right it means I've have bought and I've closed the entire position due to stop loss it so I bought it at this price I closed at this price I bought it at this price I closed at this price then here I did some trailing 3030 you can see I did some trailing and then again I sold again buy again sold right so you can see whenever you see 60 Z it means I'm buying at full quantity I'm closing the full quantity closing the full quantity means I've never breached I've never is the take profit point so this is how the uh trades file will look like you will also see the lck file okay you will see a log file like this if you if you find if you if your strategy faces any error this is where you have to look uh look to understand the error now you can see at this point 1211 my strategy tried to place an order but the order didn't go through why because I don't have enough money in my account so the error and all you will see in this file you can see call by condition satisfy for this uh option option option contract at this price call sell condition was satisfied for half quantity for this option contract at this price but the order didn't go through why because I don't have enough money so then you can see updating stop price so current stop price was this the new stop price is this then current stop priz is this new stop priz is this so anything related to strategy if anything happens it is being updated in your log file so log file is important this uh trade. CSP file is also important to see all of your trade and the most important file is this pickle file okay each time when you execute the strategy a new pickle file will be created even here you will see there should be a pickle file you see there is a there is a pickle file here you see this pickle file if you want to run your strategy from scratch you have to delete this pickle file because what happens when you run the strategy your strategy stores some information information as in what is a call contract what is a put contract what is a stop price what is a take profit all this information it needs to save somewhere so that somewhere is the pickle file pickle file act s like a database so when you rerun the strategy without deleting the pickle file it will continue from where it has left so if I want to start from scratch I have to make sure that I delete this file even if you do some small changes even if I change the parameter any of the parameter if you change make sure you delete the pickle file after deleting the pickle file then you can start the strategy again but if you don't want to start the strategy from scratch then keep the pickle file and rerun it so it will continue from where it has left let's say you have taken the position you have taken taken the stop-loss uh First Take profit has also bridged now you need to Trail now this trailing was being executed but your code stopped for some reason so you can run the code again it will continue to Trail it will not take a new position right so this pickle file is quite important I'll repeat in this pickle file acts like a database if you don't delete this file if you run it it will continue from where it has left if you delete it and run it then it will it will start the strategy from scratch so this is how you have to use the code now we will move to the last part of our video where I'll explain you uh the entire code from start to end now let's try to understand the code now so first we have the input parameter so these are the parameters we have already discussed then we have the libraries these are all the libraries that we need then we have this code this code is used for SSL related error sometime what happens when you have multiple python or when you install python for the same time and when you use the websocket code uh in that case it throws an error related to SSL certificate if you write this that error will go away otherwise this code is not needed then I'm importing the logging Library I'm creating a log object here so you can see this is the configuration for the log file so I'm only going to store the info variable this will be uh info information this will be the name of the log file first there will be strategy name then it will be current date so each day when you run this strategy for the first time a new log file will be created file mode is upend because I want to keep the log that was written initially I don't want to erase the entire log content so that's why I've written file format as a uh sorry file mode as a then file format is first we will write the time and then we'll write the message so I'll show you one sample log file so this is how the log file will look like you'll see the time and you will see uh the log information uh then we need to authenticate ourself so if this access.txt file exist it's going to read the file if the file doesn't exist it's going to ask you to verify yourself so now you see I don't have any access.txt file when I run this for the first time so it will take me to the browser when you run it in the Google collab it will give you the link once you go to the link you have to verify yourself now I have already verified myself so it's not asking me to verify again but when you're doing this for the first time in a day so it will ask you to write your uh phone number password like authenticator key so these are all the things you have to write once you verify yourself it will take you to the redirect URL just copy this link the entire link you have to copy go to the terminal and paste it now when you do this what will happen it is going to create a access file so this is the file access 2025 to 21 so today's date is 21 that's why it's showing this uh date now in this file what I have this is the access token right and this access token remains valid for an entire day you don't have to worry about it so this entire code that I have written this code uh will get the access token from the Au code so let me just minimize this uh then I have created few objects like current time start time end time and I'm printing the start time and end time so if I run this you can see start time end time will be printed uh you see this is the start time and this is the end time after getting the start time end time I'm creating a fires object so we are I'm I'm just calling the fires model class to create an object uh I'm specifying the access token and all once I have the object now I need to fetch the option chain so uh this is the type of dictionary that you have to create so I've created a dictionary called Data where I have specified the ticker name so let me just check the ticker name one more time so this is the current ticker name I'll do one thing I'll just commment this let's do this for uh Bank Nifty so my index name is Bank Nifty exchange is NSE and this is the tier name that is exchange colon uh NSE colon Nifty Bank Dash index so here I am specifying the symbol name strike Count Strike count as in how far out of the money and in the money call and put you want and then time stamp is empty then I'm calling this option chain function uh this should give me the option chain whatever data I'm getting from that data I'm getting the expiry okay so I'll just print the expiry and show you I'll write print expiry run the code H you can see first it is printing the start time end time then it is printing the current expiry so the nearest expiry as of today is uh 270 to 2025 okay on 27 I have the nearest expiry so today is Friday so it's I think it is next Thursday so for each instrument the expiry will be different so how did I get the expiry I'm using the option chain function so in fires they have option chain function in option chain they give you list of all the expiries and from that list I've got the nearest expiry now after getting the nearest expiry uh I need to get the list of all the symbols right uh like here I have specified a argument called a value called strike count now what the strike count represent it represent that if I write 10 it means 10 out of the money call 10 in the money call 10 out of the money put 10 in the money put total 40 contracts uh 40 contracts data I will be getting right so I want to get the name of all these contracts so that's what I'm doing I'm specifying the expiry and I'm converting conting this into a data frame I'm converting the entire option chain to a data frame I'll just show you how the option chain data frame looks like so I'll write option chain and from this option chain data frame I'm getting the list of all the symbols okay after getting the symbol I'm getting the list of all the strikes and I'm calculating the strike difference because see if you take Bank Nifty the strike difference is 100 if you take Nifty the strike difference is 50 so for each instrument it is different so instead of uh you know manually typing it I've have written a small code which will calculate the strike price by looking at the option chain so I'm also printing the strike difference you can see right now strike difference is 100 uh I'll I'll just run it again and show you the option chain also how it looks so you can see this is how the option chain looks like uh there might be there should be a column where I have all the ticker name so yeah total how many contracts I have total 42 uh option contracts like all the contracts are 42 it also includes the index also if you look at the first row this is the index the other uh contracts are option contract okay uh so after getting the option contract uh after getting the option chain I'm getting the uh strike difference after getting the strike difference I'm also getting the spot price whatever the current spot prices now once I have the spot price I'm going to fetch the last 5 days data okay so you can see I'm I've made two variable one is f and one is p p represent the current date F represents subtracting 5 day from the current current date and I am going to fetch I'm going to call the history function so I'm basically calling 5 days historical data and you can see the resolution is one it means I'm fetching 1 minute data so whatever data I get I'm just filtering the data and after filtering the data I'm getting what is the open price what is the today's open price okay and I'm printing today's open price so let's see the output now I'll just do one thing I'll just comment the remaining code and I'll just show you till here let's run it h you can see first I'm getting the start time end time like before that I'm getting the access token uh since the access file is available it is reading the data from that access file then it is printing the start time end time it is printing the current expiry the nearest expiry uh then it is printing the option chain uh then it is printing the strike difference strike difference is 100 Uh current spot price is 49035 and the open price is 4912 7 so this is the price at which the market was open for Bank Nifty and this is the current price now what if I change the instrument name okay so instead of nifty bank I'll write here nifty 50 okay so accordingly the other output will change see first I'm getting the start time end time remain the same now the expiry for nifty50 is 270 to uh 20 I think it is the same then this is the option chain uh this is the spot price current spot price and this is the the open price but you see the strike difference is different strike difference for uh Nifty is 50 and for Bank Nifty it is 100 so you just need to change uh the index name and accordingly you can run it on any instrument you want you can also run it on MCX also so you can see I have an example of crude oil I have one example of natural gas just make sure that you write the right instrument name and accordingly the code should work now once you have all this information uh now there are there were two parameters if you remember one was brick size and one is brick size represents what should be the brick size of the Rano chart so if my brick size is zero that is you have not specified the brick size so by default the brick size will be whatever the open price is divide by th000 so if I take an example of this nifty50 so right now nifty50 is 22822 so if you divide it with th000 I'll get 22.8 so 22.8 will be the brick size of this instrument right so that's what I'm doing this is the this will be the default value if you don't specify any brick size so if my brick size is zero this will be the default value and then we have a buffer so if if you have not specified any buffer so whatever the brick size is we'll divide it with two that will be our buffer so there are two things one is you specify your own brick size and buffer if you don't specify anything if you write there zero uh we will go with the default value and the default brick size is whatever the current price is open price is divide by 1,000 and buffer is whatever the brick size is divide by two so that's how we calculate buffer and brick size so if I run the code you will see buffer and brick size both will be printed so brick size is three and buffer is one now I'm approximating it keep in mind here I'm using the round function uh that's why it is showing me this or let me just run it again let's let me take a look at the open price so this was the open price 22 uh 8 uh 57 now if you divide it with th000 you'll get 22.8 when you approximate 22.8 you get three and what about buffer now buffer is it is showing me one because if you divide 3 by two and if you approximate it you'll get one what you can do you know if you don't want that then we can simply brick size divide by two or or let it be I think this this should be fine okay now I'll just show you what happens when I take Bank Nifty bank so I'll just change this to Nifty Bank and if I run the code accordingly uh the other things will also change so this is my wait wait let me just check I think I have specified something okay so see the problem is I specified the brick size here so let me change this to zero and change this to zero now it will take the default value so the default value for this will be this is my uh open price so my brick size is 49.13 and my buffer size is 24.5 65 and accordingly if I I change this to nifty 50 this is my brick size and this is my buffer so remember that if you don't if you write here zero then it will calculate the default value but if you specify some value it will take those value okay after calculating the brick size and uh the buffer then I created two more Function One function is fetch hlc which simply fetches the historical data and one is candle Reno refresh okay so fetch hlc we have already discussed it multiple time it it will just call the history function uh it takes the interval and the duration first is the ticker name second is interval as in like what should be the time frame or resolution uh 1 minute 5 minute whatever duration as in like how many days worth of data you want 1 Day 2 days 5 days and this will give you a data frame okay so this code I've already discussed so I'll not go in much detail then we have a very important function uh which is the core of this entire strategy which is candle renco refresh I'll come to this function in few minute okay I'll just skip this for now I have few more function which I want to discuss like there is a function called get otm option what this function will do it will take the spot price it will take the side as in call or put and the point point as in how much far out of the money you want so if you write here zero it will give you add the money if you write points as zero it means I want zero point out of the money zero point in the money which which represents at the money right so I'm getting I'm calling this get otm option I'm specifying the spot price I specified call and I've written zero so if you take the spot price this will be my current call option and put option so let me show you if I run the code uh right now the spot price is 22857 so if you go with this the nearest call option will be 22800 and nearest put option will be 22 80 so this will be my call and putut option right so this is how uh we use this get otm option to get the add the money call and add the money put now we are not only getting the option name we are also getting the strike so I'm also getting the strike but I'm not saving it anywhere then I write a simple log that we have we have started the Str we have started we are starting the strategy then I created two function one is three functions one is called store one is called load now what store function will do it will simply store the strategy information into a pickle file and what Lo function will do it will simply load the data from that pickle file and bring it back into a python file now why am I doing this because sometime you run your strategy and in between the day in between while running the strategy for some reason you have to stop the code or the code stop by itself so if that happens you don't want to start the strategy again you want to start from where you have left La last time right so if you have already taken some position you should know on which instrument I've taken the position at what price I've taken if there is any stop loss if there's and take profit all this information you need to retrieve if you are rerunning the code I cannot the I cannot get this information from the broker because this is strategy related information so I'm using the store function to store the data and each time when I run my code every uh 12 second or every 30 second I'll again read the data from that pickle file so pickle file acts like a database for me then I have created a function called take limit position this function will simply take a limit order okay you can see it is taking the ticker name action as in buy or sell cont how much quantity you want and the limmit price so now even if you don't send this limit price it's fine because what I'm doing here I'm just fetching the current price again so whenever you call this St limit position first thing I'll do is I'll fetch the current price uh after fetching the current like fing the current price as in I'm going to call the quote function now inside the quote function I will get the ask price and the bid price I'm dividing the I'm adding ask plus bid and dividing it with two so that I can get the current price and whatever the current price is I'll place the limit on that price how do I know it's a limit order because the type is one if you refer to uh the documentation version three if you go to order placement you can see when the type is one it represent Market order when type is two it represent um sorry when type is one it represent limit order when type is two it represent Market order now in this code type is one it means it's a limit order let's say you want to place a market order what you will do you will just change this to to two and you'll just remove this limit price to zero change this limit price to zero right now it is the limit price you just have to change it to zero so it's up to you if you want to place a limit order go with limit order if you want to place a market order go with the market order so that's what this function is doing uh then I have another important function which is called Chase order now this function is related to this limit position now once you place a limit order what might happen when you place the order and the current price there might be some difference between them let me give you an example let's say you have placed a limit order at 100 and the market price is 103 like the current price is 103 now your order will not get filled until unless your until unless the current market price comes back to your limit price right now this involves the risk of uh order not getting executed right because I'm still waiting and the market keeps going up so my order will never get execute so in order to stop this problem what I'm doing is I'm going to move my limit price toward the current market price so if the current market price is 103 I'll move my 100 slowly slowly to 103 when I say slowly I mean to say by 10 PESA so if it is 100 I'll move it to 101 then 102 then 103 and I'm going to do this every 5 Second so after every 5 second I'm going to modify my order if it is 100 change it to 101 100 .1 if it is uh 100 100.1 change it to 100.2 in the after next 5 Second now what this will do this will make sure that if there is a price difference between my limit price and the current price so we will slowly and steadily come close and eventually my order will get filled so the only purpose of this code is to get the fill there is no other reason why I've written this code because I've have seen that lot of time the order never gets filled so I'll just go through the code so that you understand so in order to implement this code the important data that you want is the current order book so it takes the order book as a input after getting the order book I check if the order book is empty or not if there is no order there is no point in running this code so if this if I have something in the order book only then I'll run and then I'll also filter all the uh all the orders which are which are not filled yet so you see I'm shortlisting all the orders with the status equal to six now what is the status equal to six again you have to ref to the documentation so if you go to appendix and if you scroll down you will find a section where they have specified the different status see these are the order status so if it is one it means it is cancelled now look at six if it is six it means it is pending pending means uh broker has accepted the order now it is just waiting for the price to get fill the price will only get a fill when the current price and the limit price matches right so it's just waiting for that so I'm going to short list all all the orders who are in the status six it means they are in the pending State okay after getting all the pending State order I'm I'm going to check I'm going to get the name and I'm going to get the current price the symbol name because I might have orders on different instrument right so I will first get the name of that instrument I'll get the current price of the instrument again to get the current price again I'm using the code function so code function gives me the current price I'm going to use the bid and ask if you want you can change it to just using the ask or the bid depending on the type of order you are placing uh once I have the name of the ticker and the current price of the ticker I've written a simple code uh which is valid for the limit order so you can see I have written for o1 square bracket type equal to 1 it means I'm going to only do this for the orders which are limit order so if the current order is limit order get the name get the ID get the limit price get the quantity get all the information and if the current price is greater than the limit price increase the limit price by 10 PESA else decrease the limit price by 10 PESA once you do all this thing you just need to call the modify order function so what the modify order function will do it will simply move your limit price toward the current price by 10 PESA every 5 Second now have haven't written the code to run this every 5 Second That is what is coming in the next part but yeah this code I'm going I'm planning to run it every 5 Second you can also run it every 10 second or every 20 second depending on how quickly you want the fil right right so now we will go to the last part uh where I have written a function called main strategy code inside the main strategy code I've written a while loop which will continuously run the code I'm getting the current time and at the end I'm sleeping for 1 second now this strategy that I have written will run after every 1 second only okay it will not run within millisecond or microc I've added a pause of 1 second so for it will wait for 1 second then run the code wait for one second and then run the code now in an entire minute I have three different code I have written first if the current time is greater than the end time plus 2 minute so if let's say your end time is 1515 and you have reached 1517 if that is the case this code will be executed and what do I have here okay here I'm just calling the exit function so exit function will simply exit the code then I'm I've written another code to get the pnl and uh run this Chase order function every 5 Second remember this Chase order function which I've just explained I want to run this every 5 Second right so here I've written a simple condition that if my second is in range 0595 it means we have created a list of 0 then 5 then 10 then 15 right so if the current second is either 0 5 15 10 uh 20 then I'm going to run this code now what do I have in this code first I'm fetching the order book okay I'm converting the order book to a data frame then I'm calling the chase order function after calling the chase order function I'm also fetching the position function like getting the current position and from the current position I'm getting the current pnl two things I'm planning to do every 5 second one is uh doing that chasing order thing and the second is getting the current pnl because when I run the strategy I should know uh what is the current pnl of the strategy so that's the reason I'm calling the uh position function and from the position uh data that I'm getting from that I'm getting the pnl uh information right so that's what I'm doing in this code and in the last code uh I'm going to run this code every 12 second now this is something that depends on you if you want to run this strategy every 10 second you can change this to 10 this number the here see here I've written 12 can change this to 10 if you want to run your strategy every 5 Seconds change it to five but remember this is not a realtime data strategy okay I'm going to run this strategy only if a candle has formed because I'm looking at the closing of the candle I'm not concerned with the price every second I'm only concerned with the price after every minute so it doesn't matter how many times you run the code for an entire minute the code will give you the same output okay so even if you run it like two times in a minute even that is fine so you just need to change this to 30 if I write here 30 what it means that after every 30 second I have to run the code so it will run twice in a minute if I write 12 what it means it means it is going to run for five times in a single minute but for for all the five time the out the nothing will change all the five execution will give the same output why because the data that I'm feeding that is candle data will only change once a new candle has formed okay so what do I do every 12 second first I fetch the historical data so you can see I'm fetching one minute data for the last 6 days I'm going to do this every 12 second once I have the data I'm getting the closing price whatever the closing price is and once I have the closing price uh and then I'm calling this candle Reno refresh function now this candle Reno refresh function I'm going to run every 12 second keep in mind I'm running it every 12 second and what does it require this function what does it require it requires the name of the ticker and the historical data when I say historical data I mean to say candle data this candle Reno refresh function is going to take in the candle data and in return it is going to give me the Reno information so now let's go to this function which is the most important function called candle renco refresh function so this is that function now what am I doing here it is taking the ticker name and it is taking the data data this data is uh historical data okay then I'm printing candle renco refresh I am getting the rodf value so here this mpf do plot function is going to convert the candle data into renco Data now and that ranco data I'm converting into into ranco DF variable which is a data frame so I can just show you how this data frame looks like uh like I think I'm saving it here uh yeah this is how that renco data frame looks like so here you will see the time stamp here you'll see the Reno closing price these are the Reno Reno closing price and this is the Reno size three okay and I have yeah these are the three columns that I get which are quite important now after getting the Reno closing price so I'm getting renco closing prices from the candle uh data right so that information I'm getting inside this rodf data frame I created another function called count Bricks now this is a optional code if you want you can remove this what this code is doing it is going to count how many braks do we have at any instance I'll show you an example so if I go here let's say I on this Breck right now this Breck so at this Breck how many negative bricks do I have I have 1 2 3 4 so at this brick the number will be four let's say I'm on this brick right now so when you are on this brick how many positive bricks you have you have 1 2 3 4 5 6 7 so here the number of bricks will be seven so I just have to count how many negative or positive bricks do I have at any instance so this simple code is doing that like I have made a function called count brick which will uh count how many bricks we have and these count I'm saving it in the position uncore count column so here I have a column called position count which counts how many breaks we have you can see I have one break two break then again we have minus one break minus 2 break 1 2 3 then - 1 - 2 - 3 - 4 - 5 - 10 so right now it's going to - 10 right so this is how we are going to use uh this function to calculate how many positive or negative braks we have now once you have calculated this I also need to calculate open high low close because Reno also have open high low close see if you go to see here you can see we have uh the Open high low close whenever you are on any brick it will show you what is open high low close so I wanted to calculate this from the closing prices because that MPL Finance library is giving me closing prices of the Breck so from the closing prices I wanted to calculate this open higho close uh which is quite easy so I've written this extra piece of code uh which will simply calculate open high low close from the given uh from the given uh closing price of brick size and then I'm adding these data into the column open high low close so if I go to the data you can see first I have the closing price this Reno bricks are the closing price then I have the Reno size then I have position count then I have open high low close so I want you to take some time and compare these open high low closes uh with the Open high low closes of the uh chart that you're getting on the on the website most probably it will be same like 90 to 95% of time of this uh 95% of the time it is same in some cases you'll see the difference that is is because of the reference point if you if you change the reference point then accordingly the uh brick slightly changes but most of the time it Remains the Same okay after calculating open high low close now I need to calculate donchan okay donchan Channel if you remember this indicator requires high and low column okay so for those of you who don't know what is Donan Donan is a simple indicator which calculates the Max and the low of the previous period so if if you take 20 days it will see like in the last is what is the high and what is the low and it will give you two levels so uh let me just add it here and show you if I just add function I'll write da Chan Channel now I have taken the the the input is 20 so it will take the last 20 can last 20 Bricks and calculate the high and low so for the last 20 brick here high and low was this now that the price has moved down so this is the low and this is the high high is also changing so this is how like the donen channel works you can research more about it uh now I am calculating it by using the panda Library so you can see I'm just calling ta. Donan and this ta is the panda Library it takes the high data and the low data it takes the lower length and the higher length so if I if if I go at the top you can see this is my Donan lower Don and higher uh try to keep both of them as the same if you want 20 period dach Channel just keep both of them as 20 so here my daen high and low uh is calculated and I'm saving it in the two column called DC lower and DC upper so if I go to my data I have two more column after open high low close one is DC lower one is DC higher upper so this is daan lower this is Donan upper now I also want to calculate awesome oscillator so again I'm using the panda St library to calculate awesome oscillator I'm saving it in the awesome column right after doing all this thing I'm just returning this Reno data frame so this entire data frame is returned by this uh candle Reno refresh function now remember the most important part is that this function I'm calling like it doesn't matter when you call but this will only give you a different output once you feed a new candle to this now if I take one minute candle so after a minute only I'll get a I'll see a new candle right until and unless a minute has passed that candle has not formed so in a single minute no matter how many times you call the output will remain the same why because the data that you're feeding is the same the data will change once the minute has passed okay once you go to the next minute now you have a new candle now when you feed this new candle data into this function only then you'll get a separate output okay so I've told you in the beginning itself that this is not a strategy which requires realtime data of uh of the spot I just want the closing price of the spot now it can be beneficial to me it can be uh risky for me at the same time so we go with uh we can go in any of the ways right so this is also a reason why this strategy is relatively simp easy to implement here if you see I'm not using any websocket why I'm not using websocket because I don't need to fetch realtime data of all the tickers I will just fetch it whenever it is required that is after every minute after every 20 second after every 50 second whatever uh feels convenient to me so in this case I'm going to call this candle ranko refresh after every 12 second that that is five times it will be executed in a minute but the data and the output will remain same in all the five in all the five API calls or you can say in all the file all the five uh function call right and I'm you can see I'm saving it into the CSP file you if you want you can comment this if you want to see the data uncomment this otherwise comment this now if the account type is paper I'm going to call this paper order function else I'm going to call this real order function now these type of code I only have to write it in the Indian market because in uh if you go for any International broker there they have a feature of paper trading in the broker level itself so in on the broker side itself if I change the setting to paper trading I can I just have to write a single piece of code but in Indian broker I seen they for some reason they don't provide paper trading environment so that's why uh in my code itself I have created two different function one is paper order and one is real order now both the code are exactly the same the only difference is my paper order will not place a real order into the market whereas my real order function will place a actual order onto the broker that is the only difference so I'll not explain you both the function I'll only explain you one of the function and you'll see you'll understand both because everything is same the only difference is here I will be making an actual uh function call which function call there is only one function which takes the position which is take limit position take limit Position will take the position and there is another function called exit position so exit position function takes the name of the ticker and it will CL chose the position for that particular ticker okay so let's understand the code so this is where our account type variable kicks in so if my account type is paper I'm going to run this code otherwise I'm going to run this code if my account type is paper it means we are in the paper trading environment first thing I will do is I'll check for the pickle file do I have the pickle file if yes I'll read the data from that pickle file if no I'll create my new data fresh data okay now how does this fresh data looks like first I'm going to create a data frame a data frame where I'll store all the trade information then I'll uh create a dictionary okay so here I'm creating a dictionary and these all information I'm storing in the dictionary if you look at all the information these are all the important information that I might require uh at the later part of our strategy like the call name the put name the call flag put flag remember the call flag call flag put flag helps me to determine what is the status of that order or what is the status of that instrument then brick size then we have number of Trades then we have buffer we have stop price we have take profit we have initial quantity we have the current quantity we have the entry price we have the call entry price we have the put entry price so these are all the information that I need to store so I've have created a dictionary out of it so when I store or retrieve the data from the pickle file this is the data that I'm storing and retrieving and where is everything stored in a variable called paper info variable the same type of variable I created here the variable name is uh real info variable so there are two variable one is paper info one is real info in paper info uh and real info I'm storing the same data uh the data doesn't change okay now let's understand the paper order function or I'll just explain you the real order function that will be much easier to explain uh now just remember that in real order function if you see I'm calling a function called take limit position see there is a function called exit position there is a function called take limit position this is the only two things that is added extra in the real order rest everything Remains the Same you can just compare it you'll come to know now let me explain you the last function of our strategy which is the order function so when will this function be executed as per my code this function real order or the paper order function should execute after every 12 seconds okay so after every 12 second I'm going to feed the spot price and the rodf and rodf remember rodf Remains the Same for an ENT entire minute renod DF will not change its value until and unless a new candle has formed so renod DF Remains the Same Same But the spot price will change every 12 second the spot price will change okay uh then I'm I've created the global variable real info I'm also getting the brick size the buffer the ticker and the quantity I'm getting the ranko low high ranko close from the Rano DF variable I'm also getting the chain low chain High and the awesome oscillator value so these are all the values that I'm reading for from the rank DF itself okay uh and I'm also reading the call entry price and the put entry price and I'm printing everything so this code if you want you can remove it it's just printing nothing else uh the first condition that I have written is if my current time is greater than if my current time is greater than the start time and less than the end time and the second code I've written is if my current time is greater than end time okay so this code will be executed if you are within the start and end time this code will be executed if you have reached the end time and at the end I'm also return another thing which will simply store this data into our CSP file the trade information so this code will save the trade information into our CSP file and at the end what I'm doing I'm just calling the store function remember whenever I call this real order or the real order real order or the paper order function I should always store the data to the pickle file right so every 12 second I'm going to store this data this 12 you can change it if you want you can change this to 30 you can change it to 15 whatever feels okay to you right now what to do if my current time is greater than end time if my current time is greater than end time then I have to check if I have the call uh position or not and I also have to check for the put position and remember that if my call flag is equal to one this represent that I have a call position if my put flag is equal to one this represent that I have a put position so if I have a call call position I'm going to get the current price I'm going to get the call Price current price like from the code function I'm going to get the current price I I'll write this into our CSV file and at the end I'll just call the exit position function so exit position function is an amazing function which just take which just with which just takes the name of the ticker and it closes the position for that ticker the same thing I'll do for the put flag also if my put flag is equal to one it means if I already have a put position in that case I'll close the put position so I'm just calling the exit position function and here I have to just send the put name okay also I'm also uh writing the data to our CSV file and I'm also keeping a log of it so I'll just show you the log file so if I go here um here I don't have an okay I don't have any log otherwise I would have shown you that when we reach the end time this would have be executed okay this is what we do when we reach the end time so I'll just repeat again what we do when we reach the end time we will check if I have a call position if I have a call position just close the call position look for put position do I have a put position if yes then close the put position that's all I'll do when I reach the end time now what if I am between the current time and the start time okay this is where the actual code lies so let's go through it one by one so I'm going to fetch all the information from my uh database that is the pickle file so first I'll get the call name I'll get the pull name put name I'll get the call flag I'll get the put flag I'll get the spot stop price I'll get the spot profit price I'll get the initial quantity I'll get the current quantity now see this strategy doesn't look at the uh option price it only looks at the spot price so that's why the stop loss and the take profit uh exist for the spot price so that's why I've written two variable one is spot stop price and one is spot profit price okay then I have the initial quantity and current quantity why do I have two different variable because when I take a position my initial and the current quantity will remain the same when I close half of my position in that case my initial quantity and the current quantity will change initial quantity will be whatever quantity I have entered current quantity will be uh whatever quantity I have closed like whatever the current quantity is minus the closed quantity that will be my current quantity so this will change both the values will change depending on where we are uh in which stage we are in the strategy if you are in the initial stage of entering both remain the same if you have closed half of the position one both of them will be different then I have few conditions okay each and every condition is important so let's go through it one by one so first I have my call condition uh call entry condition and this is my put entry condition so I can just write here check call by condition so what is my call by condition my call by condition is that my chain high should be less than ranko high okay I'll just explain you quickly what was my uh what was my entry condition so I'll just add uh first okay so right now this is bang Nifty this is Nifty right so here I have okay let's let's take an example of nifty itself I need to add another indicator called awesome awesome oscillator so these two things are needed okay so I would have taken a long at around this brick why because first it has breached the upper level and my and my awesome oscillator is also positive so what is this condition this condition is that my closing price this will be the closing price is greater than the ranko high right so you can see that's my first condition here so right now we talking about buy so my ranko high is greater than the chain High my ranko high is greater than the chain high this is the first condition and my call flag should be equal to zero call flag zero means what it means I have not taken any position put flex should also be zero because I want to take one position at a time I don't want to take position in both call and put at the same time if I'm taking position in call I'll not take position in put if I'm taking position input I'll not take position in call so that's why I've written to return a quote to check for both the flag and then spot price should be greater than Reno High plus buffer so here the first condition would be satisfied and when the price reaches here my second condition will be satisfied when the closing price is greater than uh High plus buffer right so part price is greater than Reno High plus buffer and awesome oscillator should be greater than zero this is my buy condition okay what is my sell Condition it's uh opposite to that so let's take an example here so at this candle at this break my first condition will be satisfied what is the first condition that my closing price is less than the ranko low you can see my chain low my ranko low is less than the chain low this is the first condition and the second condition will be satisfied when the current price reaches half of The Brak size around here so you can see if my spot price is less than Reno low minus buffer this is my second condition and I will also check for the the flag my call flag should be equal to zero put flag should be equal to zero and my awesome oscillator should be negative so around here I would have taken the position right so these are all the condition that I check for uh buying put leg this was call leg this was put leg now if I take a look inside the code it's pretty straightforward first I'm going to get the otm option at motm Option M otm option as in like I'm getting the ATM option because uh the the out of the money py is zero in short I'm getting the ATM option once I get the ATM option name I'm fetching I'm calling the code function code function will tell me what is the current price so I'm getting the current price after getting getting the current price I'm calculating the profit price and the stop price so what is my profit price my profit price will be Spot Price Plus take profit Point spot and my stop loss will be stop spot price minus stop loss point so here right now what is my take profit and stop loss if you see uh my take profit is 5 point stop loss is 5 point you can change this like maybe you can keep it like uh maybe 30 point and 30 point now whatever your uh appetite is for the risk uh whatever the risk to reward ratio you want these two parameter you can change you can keep take profit has 60 and you can keep uh stop loss as 20 this will be 1 is to 3 risk reward but I want to keep it simple so this will be 30 and this will also be 30 remember the bigger the value value you keep uh the chances of that hitting is is uh less okay like I don't know like I I hope you understand this I'm not going in much detail into this okay so once I have the call Price I'm calculating the profit price spot profit price and the spot stop price I'll write this all this information to our CSV file and then I'll uh update my real info variable what all things I'll update I'll update the call name I'll update the call flag you can see I've changed the call flag to one I'll change the quantity initial quantity current quantity Remains the Same I'll change the stop price I'll change the take profit price I'll change the entry price I'll change the call entry price so everything I'll change and then at the end I'll just call the take limit position function this function will simply place a limit order for the given call option right uh if you take a look at the put by condition I'm doing the same I'm getting the put Name by using the get otm option function I'm getting the current price uh I'm calculating the profit price and the stop price I'm updating the data frame I'm updating the real info variable and at the end I'm calling the take limit position function both in both the function I'm my position is long here I'm going long on call here I'm going long on put okay now what to do if you already have the position so this code and this code represent when to enter now this this code and this code represent what to do once you have entered so if my call flag is equal to one what it means it means I have already entered the position if you have already entered first thing is you should get the current price get the current call Price uh if you have entered the put get the current put price and then check for three condition so here I have three condition this is my first condition this is my second condition this is my third condition so first thing I will check is has the spot price breached the stop price or not so you can see if the spot price is less than the spot stop price it means that we have we are we our current spot has reached the uh stop price so we have to close right so if this happens I'm going to update the real info variable I'll change the current quantity and initial quantity to zero I'll change the stop price to zero everything will be zero okay and I'll just call the exit position function exit position function will close the call leg why I'm closing because I have reached the stop price that's why if I have not reached the stop price I need to check have I reached the take profit right so this is where I'm going to close half of my position so if my spot price is greater than the profit price and if the initial quantity and current quantity is zero okay if this is the case what I have to do I have to close half of the position so uh while updating the real info variable you can see my current quantity is initial quantity divide by two my spot stock price will also change because when I close the position for the first time when I close half of the position my stop price also changes I'm going to change my stop price to the trailing to the trailing stop price right so I have to update the spot stop price and then I have to update the take profit also take profit will become zero I've already closed half of the position I've have already achieved the take profit now the take profit will be zero I'll write this information to our uh data frame I'll log this information and I'll also call the take limit position function which will close half of the position if let's say my quantity is 10 uh so it will close five quantity and it will keep the other five open now if this is not the case if I have not if I have breached the take profit then what to do now I have to Trail the stop- loss now when should I Trail the stop loss only if my initial quantity is not equal to current quantity think about it if my current and initial quantity is same it means what it means I have not closed anything yet if my current quantity and initial quantity is different it means I have closed half of my position and from this point onward I have to Trail the stop- loss right so this is the code to Trail the stop- loss how to Trail the stop loss if my spot price is greater than uh spot stop price plus stoploss plus Trail Point okay if this is the case then only I'll Trail the stop price how to Trail the stop price it's very easy you just have to calculate the new stop price which is spot price minus the stop loss point right this is how I'll calculate the new stop loss so we will just trade if let's say Bank Nifty is at 50,000 and ,00 was the take profit so once 50,000 reach to 5,00 we will close half of the position and let's say the take profit and is 100 so now when it moves by 100 if it moves to ,200 I'll change the stop loss to 50,00 If It Moves to ,300 I'll move it to ,200 If It Moves to 400 I'll move it to 300 so this is how we are going to Trail Okay so when I Trail I don't have to take a position I just need to update the stop loss new stop price so that's what I'm doing I'm just updating and I'm just writing this information into the trail variable uh I'm updating the real info variable and I'm logging this information to the lock file okay the exact same thing we do inside our code for put also so first I'll get the current put price then I'll check if the spot price is greater than the uh stop price if that is the case we have to close everything if my spot price is less than the stop profit price and if the initial quantity is equal to the current quantity okay if this is the case what I'll do I'll close half of the position right here you can see my current quantity and uh uh initial quantity will change then the last condition is if my current and initial quantity is different what to do we just need to Trail the stop loss if you have noticed like most of the code I repeat sometime people think that you know you have to write the entire thing from scratch but that's not the case you just write a code and then you just repeat the same thing again and again so that's it for the strategy this is how you implement the entire code

💬 Trader reviews (traded this? tell others what really happened)

No reviews yet — be the first. Real experiences help other traders more than any backtest.

User opinions, not investment advice. Reviews are moderated before publishing.