r/learnpython • u/YaswanthBangaru • Feb 07 '21
My first proper project on Github, Yay!!
After about 2-3 years of being on and off into coding, I managed to actually finish a project end to end. It isn't very fancy but it gives me immense pleasure to finally achieve this milestone in this line of my life. I really appreciate everyone here for being so helpful and also many thanks for all the support.
The project utilizes the cool Python module Streamlit to create a visual interface to build/analyze a Portfolio management strategy using the RandomForest Regressor to predict the prices for n days ahead. It uses the random grid search for inside cross-validation to optimize the hyper parameters(different for every equity) for a chosen number of equities(stocks) by fetching the data from the nsepy module with the ability to select the timeline along with a backtester to test the strategy created.
Anyone could, infact, add a function to this script in the main long-short-equity class appropriately to use different models for prediction I believe, it's of course not investment advice but a simple representative strategy+backtester for budding systematic trading enthusiasts.
PS: Please give me suggestions on how I could make this code better and/or the pros and cons of my code.
Github: Long Short Equities
Edit: Thanks for the awards strangers :)
7
u/bickhaus Feb 07 '21 edited Feb 07 '21
Two suggestions: (1) add a license and (2) in bold caps, explicitly state that this does not constitute investment advice, it is provided as is, is only for testing and research, not to be relied upon for investing, etc...I’ll leave it to you to come up with the exact language to use as I do not consider this comment to be adequate legal advice or to constitute the creation of an attorney-client relationship.
Edit: note that adding a license like GPL or MIT will disclaim all warranties already , but I would still also specifically disclaim the code’s ability to be a tool for reliable investing to CYA...and this is still not meant to constitute the adequate legal advice or form an attorney-client relationship!
3
u/YaswanthBangaru Feb 07 '21
Hey, thanks for the inputs, I will check out other investment(for fun) related git repos and add it accordingly ;)
2
Feb 07 '21
Which features are you using for the regressor?
2
u/YaswanthBangaru Feb 07 '21
I am using all the actual data directly available from the model, to it I added a few seasonality related features (which I copied from one of the functions of fastai :p), to be specific, these ["Year","Month","Week","Day","Dayofweek","Dayofyear", "Is_month_end", "Is_month_start", "Is_quarter_end","Is_quarter_start","Is_year_end","Is_year_start"]. Along with those, I added some 4 lag moments to utilize the autocorrelations of the dependent variable if any.
2
u/ChocolateMilkMustach Feb 07 '21
Very neat, but comments! At least tell us what each module will do. If I put this in, when this happens, then this happens. An example of what you expect to come out when you put something in. Another example of what could cause this module 2 error, integers instead of characters and vice versa. Hell I even forget what my code does if I haven't touched it for a while. It just helps to have a refresher of what it's supposed to do after 6 months of not looking at it
1
u/YaswanthBangaru Feb 07 '21
That's very true, I picked this up from where I left it 6 months ago, it was a mess, I will start working on doing comments. Appreciate the feedback :)
2
u/Deadlift420 Feb 07 '21
You have a bit of a God class going on. I would break it up into smaller classes that each have their own function. Look up SOLID principles.
In this situation its not a big project but for future reference you'll want to break it down a little.
1
u/YaswanthBangaru Feb 07 '21
lol, I am kinda new to the whole ambience of Classes. Will try to work on it, are you suggesting I should use inheritance and stuff?
2
u/St_Melangell Feb 07 '21
Congrats on completing your first project! I’m a beginner so a lot of the details of it went over my head a bit, but it sounds impressive.
2
u/YaswanthBangaru Feb 07 '21
Hey, I was always overwhelmed with looking at long complex scripts, still do, but the best part about coding is, the more you do it a fine day occurs where you wake up from bed and open your text editor and it feels like ''Dude! This shit was never difficult eh!!". The point is to not worry about that day and keep doing it :)
2
1
Feb 07 '21
I like the idea of requirements.txt. why don't you also include the commands to create a new environment and activate that evironment in the same file. That will save your users a few steps.
1
u/YaswanthBangaru Feb 07 '21
you also include the commands to create a new environment and activate that evironment in the same file
Good thoughts, will add that part. Thank you for the suggestions.
2
2
2
u/pmt541 Feb 07 '21
Nice!
Just some tips:
Add some docstrings and explain what each function does as well as the overall script. Use something like Google's standard to write the docstrings.
Some of your formatting is inconsistent, such as number of whitespaces between functions and spaces between logical operators. Check the PEP8 document to see good practices - most importantly you should be consistent.
Things like line 198 should be avoided. Just write it clearly. It is basically going to run just as fast using a conventional for-loop on modern computers and will improve readability.
You've used object orientated programming principles, but there is an awful lot going on in this class. It may help to create another class or two and simplify the code further.
Read some documentation on how to write good commit messages before making these changes.
1
u/_qw4hd Feb 07 '21
Thank you u/YaswanthBangaru for sharing this with us. I've learned something new from your project. Streamlit is a very nice way for creating UI for data science applications. If you didn't know about this project like me you have to check out streamlit gallery section with cool examples.
1
u/BananaPralines Feb 07 '21
I'm just glad you said 2-3 years and not 2-3 months. This is great and inspirational.
1
u/degzx Feb 08 '21
Cool stuff ! One suggestion on the analytics, I will frame it as a question- is RF really suited for time series forecasting?
-9
0
Feb 07 '21
[deleted]
9
u/YaswanthBangaru Feb 07 '21
You have a few comments here and there explaining what something is doing, but there's nothing anywhere that explains (especially to your future self) why you did it that way.
That's very true, I did this project, partially, 6 months ago and when I got back to it last week, it pretty much felt like I didn't do any of it. I had to figure out line after line to understand what's happening in the script exactly. It's a lesson learnt definitely.
if ever? Is there a reason why you shouldn't remove any of that commented-out code? No idea, we can't answer any of those questions, so it's smelly.
haha, I just don't know, I planned to take more inputs from the user and later realized they would be redundant as I was trying to ask the same things again and again indirectly, but then I was doubtful if I might need it later on and then I felt, may be, I would remove them when I polish the code, if ever it seems!
Appreciate the time you took to give me a feedback. Thank you so much! I believe I have learnt some things.
6
12
u/MrNubishly Feb 07 '21
Could you please dumb it down for me? Im not too sure if i understand yet , im a bit new to coding, sorry