Transcript for:
Building an AI-Driven Hedge Fund

this tutorial will show you how to build an AI agent directed hedge fund with less than 100 lines of code these AI agents will handle market analysis sentiment tracking macroeconomic insights strategy development and risk assessment most agentic AI Frameworks rely on Specialized tool calls to access the internet but we're skipping that entirely by leveraging the perplexity sonar API a realtime search enabled model that provides up tothe minute data by the end you'll have a fully functional pipeline capable of generating a datadriven investment strategy for any stock before we get into the code let's break down how this Aid driven hedge fund will work we'll be using a modular AI framework where each agent is responsible for a different aspect of investment decisionmaking first our Market data analyst will retrieve key financial metrics for a stock then the sentiment analyst will evaluate the latest news and social media trends to gauge Market sentiment this is followed by our macroeconomic analyst who will assess broader economic factors like interest rates and GDP Trends once we have all this data our quantitative strategist will synthesize the insights shared by each of the previous agents into a trading strategy and finally the risk manager will evaluate the potential risks before we finalize our investment decision let's talk a bit about the AI stack we'll be using to power this hedge fund at the core we're using Lang chain a framework for building agentic workflows it helps helps us structure multiple AI agents efficiently allowing each one to perform a specialized role for our AI model we're leveraging sonar reasoning through the perplexity sonar API which is essentially a souped up version of Deep seek R1 unlike traditional models that rely on static data sets sonar provides realtime search enabled insights giving us up to-the-minute Market data new sentiment and macroeconomic factors with this lightweight yet powerful setup we can get expert level Financial Insights in under 100 lines of code before we begin I want to emphasize that this video is not Financial or investment advice it's an educational tutorial on how to create agentic llm workflows in Python llms are still experimental and can have high error rates even with an advanced model like sonar reasoning so you need to apply critical thinking and subject matter expertise when reviewing the results start by going to so . perplexity doai and click Start building this will bring you to a quick start guide which details how to set up an API key click API settings page which will prompt you to log in or create a new account and then direct you to the API page you'll need to purchase credits to use the perplexity sonar API which is slightly more expensive than what you would pay for open ai's 03 Mini model but for reference I loaded up $3 worth of credit credits to play around with the API and only used up about 50 cents preparing this tutorial once you pay you can refresh the page and generate an API key we'll now get started with preparing our python environment create a new virtual or cond environment with python version 3.9 then use pip to install Lang chain Lang chain community and IPI kernel next shift to a code editor that supports Jupiter notebook books and import the libraries and the necessary modules prompt template will be used to structure our AI prompts llm chain will build the individual agents chat perplexity will allow us to access the sonar API directly through Lang chain and sequential chain will orchestrate the flow of inputs and outputs between agents now we initialize our AI model using an API key and configure it to use sonar reasoning I'm going to set the model's temperature to 0.5 to ensure a balance between creative and consistent responses a lower temperature makes responses more precise and deterministic while a higher temperature allows for more diverse and exploratory insights you should tweak this based on your needs with the model prepared let's set up the full pipeline of five specialized agents starting with the market data analyst this agent is built using llm chain where the llm parameter links it to the instance of the perplexity sonar model we just initialized the prompt parameter structures the request using prompt template ensuring a clear and well-formatted query within prompt template the input variables parameter allows us to dynamically insert a stock ticker symbol so the model fetches data for any company the template itself requests key financial metrics such as stock price trading volume valuation ratios and recent Trends giving us structured market snapshot finally the output key parameter stores the response under Market data making it accessible to the next agent in our Pipeline with the market data analyst set up we now move on to the remaining four agents each following a similar structure but with different inputs and outputs the sentiment analyst and macroeconomic analyst also take in a stock ticker but instead of financial metrics they conduct real-time searches to assess Market sentiment and broader economic conditions the quantitative strategist is where everything comes together instead of taking just a ticker it processes the combined outputs of the first three agents Market data sentiment and macroeconomic Analysis to generate a structured investment strategy finally the risk manager evaluates this strategy identifying potential risks like volatility liquidity constraints or broader Market instability it then provides a risk assessment ensuring our AI powered hedge fund isn't just generating trades but also considering risk exposure you can customize these prompts and add additional agents into the workflow based on your specific use case and investment thesis now that all five agents are set up we use sequential chain to orchestrate their flow we create a chain starting with the first three data collector and synthesizer agents followed by the quantitative strategist and the risk manager these have to be in a precise order so that the output of each agent feeds into the input of the next one the order doesn't really matter for the first three since they are not feeding into one another next the input variables parameter ensures we only need a ticker symbol to initialize the chain while output variables store structured results after each agent does their work okay so with everything set up we can Define the Run AI hedge fund function which executes our full AI pipeline the function takes in a ticker symbol AS indicated by ticker colon string which means the input must be a string the arrow followed by none at the end specifies that this function doesn't return anything it simply processes the data and prints the results inside the function we call sequential agent passing in the ticker and triggering our pipeline once the analysis is complete the results are printed section by section so we can see the reasoning of the agents I'm going to run this now and speed up the tape we can now look through the output and learn more about the stock and potential strategy I asked the risk manager agent to give a four bullet point summary risk assessment and we can see that the agent has in fact provided this final risk assessment of the strategy produced by the quantitative strategist the usefulness of this AI hedge fund depends on your trust in the model and its real-time search capabilities during several trial runs I found the sonar perplexity model to be fairly accurate in pulling the latest upto-date data but don't take my word for it try it out yourself if you found this tutorial useful make sure to like comment and subscribe to the Deep charts channel for more videos on how to leverage the latest Ai and machine learning tools