[See Description] Shorting based on Sentiment Analysis signals – Python for Finance 11

what is going on everybody welcome to another Python with finance tutorial video using quanto peon and zipline in this tutorial we're gonna mean just kind of building on the last one which is where we were using the CSV fetcher to fetch sentiment data on companies and then we're trading based on that sentiment data so here we found that we actually did quite well we did 83.9% on returns compared to the market's 52.6 so that's pretty exciting but there are a few things that we have to think about first of all this is trading basically october 2012 all the way late october 2012 till now the basically the regime of the market so to speak has been up alright the market hasn't done anything but steadily increase and well that's great and it's great that we saw that hey we perform really well in a rising market we haven't really seen how well we perform in a sideways market besides maybe like right here as the markets going kind of sideways up and down i can't really decide if it wants to continue rising or not we're seeing at least here we've lost some money now that's not really indicative of what we're gonna do in a true sideways market but what we know is that we don't know how this strategy will perform in a downtrending market or even in a sideways training market although the market rarely will trend sideways for a long time stocks like certain companies might but the market it's pretty rare for especially for strategy like this obviously for you know over the course of a week we might do nothing but this strategy is more of a long-term kind of thing so what we need to do is we need to regardless of how it might affect our returns it may it may actually increase returns it may lower returns whatever we need to short companies we have to have that in there and this is just a good time to talk about bias right so bias right now we have to recognize that the current market regime is just rise rise rise rise but we need to have handling for market regimes that are maybe fall fall fall fall fall so that's what we're going to be doing here and so to start we're pretty happy anyways with our Sharpe ratio and our drawdown kind of hi it'd be nice to see that go down I don't like a 12.8 we can basically see it's probably right here or it occurred and but the sharp is good we'd like to see it above two beta we'd like to lower beta quite a bit especially for quanto pian but if we can we would love to see beta lo on a strategy like this where it really is a heavy buy-and-hold kind of strategy anyway let's go ahead and get started and like before we've saw that shorting allowed us to pretty significantly lower beta so what we've got here is we've got basically our trading logic so far and what I'm gonna go ahead and do is add a bit of logic right up at the top so this is an initialize I'm just gonna add a whoops context dot shorting equals true and that's just for now but this is how we can toggle shorting so right now we're just gonna leave it true but as time when it goes on you might decide to use something that toggles whether or not you're willing to short and personally I think a great way to do that is one of our previous strategies where basically predicted market regimes where we just had the MA crossovers right if the MA if the short ma crossed over the long MA we were like hey this is a rising market let's play and if it was falling we're like no it's a falling market let's short and that actually proved to be a pretty successful strategy at predicting regimes and we actually did pretty well we had a low beta and good returns we didn't meet the market but we did predict the market well enough okay so that could be a starting point for predicting market regimes and then applying either this strategy or some other strategy that has kind of two sides to it one that does shorting and one that does longing so anyway we've got that is done here with shorting like a little toggle for now we'll leave it there but you might have something in the handle data that will toggle it on or off so then what we're gonna do is we'll come on down basically to like right here you've got your sentiment logic for investing so we can even say begin long logic and then we'll come on down here and then we can add another thing that is like begin shorting logic and then here we'll just say if context short so if contacts are shorting equals true at the top that statement rings true and we will run if it says false it won't run so if contexts out shorting first let's let's write some quick code here we'll have a ma one and we'll have an MA – like we did before let's make these in pretty long ma so we'll say data for the stock and that is going to equal or data ma VG and this is a shorter one we'll make this 100 moving average and then for the second one paste move that extra space we'll make that a 300 moving average so keep in mind this is 4 per on a per stock basis so instead of predicting the entire market regime this is basically the the regime for that specific company but generally companies if you look at companies especially the companies that we're choosing which are kind of big companies they're gonna follow the market really closely so anyways we've got moving average one moving average to rates then we're gonna ask the question if and then we're gonna say if sentiment is less than or equal to negative 3 ie if it's negative 3 so won't have anything greater or less than negative 3 right now and the current position position equals 0 so we don't have this company or we're not already shorting it and whatever so we're shorting I would expect the current position to be a negative when you short a company but that's not how it works it is a positive number so even though when you short a company you really aren't having a position in that company so I wish they would maybe tweak that a little bit too so it was like a negative but anyway and I might be wrong but at least from my findings using output to the log there is no negative position even when you're shorting it's it's called a positive so if sentiment is less than or equal to 3 and the current negative 3 and the current position is equal to 0 then we're interested but then we have to ask if ma 2 is greater than ma one so basically if the short moving average is below the the long one so that says hey the market might turning down so then we're gonna say if cash is greater than the context investment underscores sighs so if we have the money to execute this kind of short great we're gonna say order value so we want to order a value of whatever that stock is and then here we we do use a negative and that's context investment underscore size and as I'm writing this I wonder if we can possibly reference the value we're invested in a company and that's how we might know whether or not we were invest or like whether or not we are actually shorting a company I'm not really positive that that's good enough but that would be kind of interesting if that would work the other thing that we want to do is because we don't know what companies we know what companies we have positions in but we don't know what companies were shorting or at least I don't know but now that I'm writing and I wonder if maybe we could reference it that way well we want to have is another parameter that I'm just gonna add up here at the top there's context out shorting and I'm gonna have a context shorts and that's just gonna be a list and then coming down here what we're gonna do is when we actually short a company what we want to do is we're gonna append it to our little list so context shorts dot append s so I append that company so now we can always reference the shorts list to see are we currently shorting that company and again there might they're almost certain I'd be really surprised if there was no way to find out what companies you're shorting on quanto peon I just couldn't figure it out so I'm using my own method for this it's kind of like the cash thing I'm sure there's a way to like track your cash per stock in the handle data loop I'm just not seeing it and I'd rather be safe than sorry so might as well know for sure you're tracking it so coming on down here that's it for our you know to execute a short but how about getting out of a short because we need to get out of short sometimes so to get out of a short we basically we have to decide at what point what logic basically would we want out so we're gonna say L if well if the sentiment is greater than or equal to a negative one so at negative three we short at negative two will hold our short at negative 1 or higher we're saying let's get out of this short so if that's the case and then we're gonna check to make sure we have the company and or we have positions in the company so if that's the case and our current underscore position is greater than zero because they use positive positions even for short and s in context shorts so our little list of short of companies that were shorting if all of that is the case then we're gonna go ahead and order underscore target and we want to order target that stock zero so we're saying we want to have zero of that company now you could quit here but the the next most pythonic thing to do would be to do context short start remove s so remove s from that list you you really don't have to I mean ok everything's satisfied in this order target but you want to go ahead and do that to be the most pythonic thing so at this point I think we're ready to rhumble we might have errors or whatnot but let's go ahead and let's run a fullback test check our dates here run fullback test and we'll see how this strategy does so so when we move on from this just just talk about something else for waiting for this to load when we move on from this like this strategy uses daily data so you have to start considering how we might convert a strategy once we're content with a backtest to a strategy that can be traded live so in order to try to strategy live on quanto pian what you have to do is you have to run a minute back test and it doesn't really matter what the outcome of that minute back test is you just simply have to run a minute back test so for this strategy it's really set up to be a daily strategy but what we could do is we could put everything in in a rebalance function right like we had before rebalance it once a day and that's all we do so we could do something like that but because centex is a like a paid api and stuff I just wanted to show what the CSV fetcher can do but I'm not too focused on keeping us kind of stuck on using the sent text data because not everyone is going to want to trade sentiment analysis data and not everybody is going to you know want to pay for the API so so we'll probably leave the syntax stuff behind after this back test and so far it looks like we're doing pretty well actually I hope to see our beta come come down it's slowly dropping so it'd be kind of nice to see it drop uh-huh any better alpha so far in a really solid sharp so anyways in the next tutorial what we'll be doing is we'll probably start building back on that long short strategy that we worked on so that one immediately identifies market regimes as far as like rising or falling and then we can create a strategy that's good in a rising market and I strategy that's good in a falling market and go from there basically so anyways that's probably what we're going to be doing in the coming videos but as far as this video is concerned wow it's good good so 101 think we were gonna do that much better by shorting but when you allow yourself to short a company then basically you're allowing yourself to make a lot more trades and that's exactly what we've done apparently one thing that's always good to do when you're working with quant opium like I was saying before when you trade a strategy on quanto pian they're they're just a it's just a platform for you to do back tests so it's not their job but to make sure your logic is sound in your back tests so understand that and so like I was saying before it's really easy to like spend way more money than you possibly have so at any given point you want to check these bars and let's say you're starting with a million dollars if one of these bars is like 50 million you bought 50 million companies and you go back to your code like look because chances are you have you've some sort of error there or you're you're allowing yourself to take on leverage because quanto peon is there to let you do anything you want they're just there to back test that logic whatever the logic is you code so keep that in mind so anyways this strategy actually did pretty good we'd like tripled from the market Wow alpha 0.53 like to see it higher but that's okay beta 0.2 to look at us we can actually compete in the the quanto peon contest with that number sharp 2.26 awesome Sortino information ratio pretty low volatility ish but 18% drawdown not happy with that but with a 156 percent return and that low of a beta I'd say we're cooking pretty good here so personally I'll probably work a lot on this strategy but for the purpose of the tutorial we'll probably do something else now we'll probably move off the sin tax data if you want to work with the sin tax data you can always contact me and stuff like that and we can you know work on something but anyway that's it for this tutorial the other thing I will just say is like okay say you've got a return of 156 okay and you're just you're having yourself a party you the first thing you should do when you see a return like this is first check those transaction bars make sure everything looks as it should also you want to go to your currency daily positions and gains that's pretty useful one but also to go to I think it's transaction details you can see like all every step of the way everything your strategy actually did so you want to come through here check to make sure you know you don't like all my positions are about you know the same size like this one we're selling a double position in Gamestop but for the most part everything's a hundred thousand okay so and that was kind of our intended logic was that we would only have a one you know we'd have ten possible positions and only in each you know company would be a one tenth of our cash so about a hundred thousand that's where we want to be so like you go through here and you're looking at that and you're like okay that's great that's kind of what we wanted and then also go back to your algorithm and really go through line by line and make sure that the logic actually makes sense okay because again in the back test you it's just really easy to produce leverage and you don't you don't want to do that because then when you go to trade it live you might not have that lab leverage and then also leverage is dangerous okay you might make a lot more money but you might lose a lot if we applied leverage to this script for example we could apply to three or even four actually we can apply five times the leverage in theory possibly to the script and our drawdown would be you know eighteen times five possibly or more or less depends but so you can apply leverage and in theory this this strategy would perform really well with leverage but you have to be so careful with this because again we're only testing in a very specific rising market we're only testing like luckily now we have a shorting and given our other risk metrics we're actually doing really well so that's kind of why we use risk metrics is because you know it kind of can help us with overfitting but it's not like a fail-safe against overfitting so anyway that's a pretty exciting return on the scripts so hopefully you guys have been enjoying up to this point that's a good one couldn´t number to have but anyway probably what we'll do is we'll move off this and get started kind of making maybe our own quanto being specific or something like that strategy but anyways if you guys have any questions or comments up to this point please feel free to leave them below otherwise as always thanks for watching thanks for all the support and subscriptions in its hill next time

test attribution text

Add Comment