In recent years, there has been a significant increase in the use of automated trading strategies in the financial markets. With the advent of powerful tools and libraries such as Algomojo and yfinance, it has become easier than ever before to develop and deploy trading strategies using programming languages like Python.
If you’re interested in learning how to write your first Python trading strategy, you’re in the right place! In this blog post, we’ll explore the basics of developing a trading strategy using Algomojo and yfinance.
Whether you’re a beginner or an experienced trader, this guide will provide you with a solid foundation for building and testing your own trading strategies. So, let’s get started and learn how to harness the power of Python and Algomojo to develop a successful trading strategy!
1)Access to Algomojo Free API + Free Algo Platform
2)Python 3.x installed with Visual Studio Code Editor
3)Install Python Libraries – Algomojo, yfinance
pip install algomojo pip install yfinance
4)Knowlege of using PlaceSmartOrder Algomojo Function
5)Knowledge of Handling dataframes
yfinance python Library
The yfinance Python library is a popular and widely used package for retrieving financial data from Yahoo Finance. yfinance is a user-friendly and well-documented library that provides an easy way to access financial data, making it an ideal choice for beginners. yfinance is an open-source library and is free to use, making it a cost-effective option for automated trading.
While yfinance is a widely used library, the data it retrieves from Yahoo Finance may not always be reliable, and it may not be suitable for professional trading purposes. For professional trading it is highly recommended to use Authentic data vendors API or Broker API
Here is a simple EMA Crossover – Stop and Reverse Strategy with Smart Order Execution Features and provision to slice larger orders
#For Complete Algomojo Python Documentation Visit - https://pypi.org/project/algomojo/ #Coded by Rajandran R - www.marketcalls.in / www.algomojo.com from algomojo.pyapi import * import yfinance as yf import time import threading #set the StrategyName, broker code, Trading symbol, exchange strategy = "EMA Crossover" broker = "an" symbol = "RELIANCE-EQ" exchange = "NSE" quantity = 10 # Set the API Key and API Secret key obtained from Algomojo MyAPI Section algomojo = api(api_key="46ad9245cca3dfe957deb235a39d02a3", api_secret="30ca108cfb91f458babd2681fb6d0817") # Define function to run the strategy def ema_crossover(): # Get historical data for the stock using Yahoo Finance API stock = yf.Ticker("RELIANCE.NS") # Set initial values for variables position = 0 # Main loop while True: # Get historical price data from 5min timeframe df = stock.history(period="1d", interval="5m") close = df.Close.round(2) # Calculate short-term and long-term EMAs shortEMA = df.Close.ewm(span=10, adjust=False).mean().round(2) longEMA = df.Close.ewm(span=20, adjust=False).mean().round(2) # Determine the crossover point positive_crossover = shortEMA[-2] > longEMA[-2] and shortEMA[-3] < longEMA[-3] negative_crossover = shortEMA[-2] < longEMA[-2] and shortEMA[-3] > longEMA[-3] # Place an order if there's a crossover and we don't already have a position if positive_crossover and position<=0: # Update position variable position = quantity # Place Smart market buy order response = algomojo.PlaceSmartOrder(broker=broker , strategy=strategy, exchange=exchange, symbol=symbol, action="BUY", product="MIS", pricetype="MARKET", quantity=quantity, price="0", position_size=position, trigger_price="0", disclosed_quantity="0", amo="NO", splitorder="NO", split_quantity="1") print ("Buy Order Response :",response) # Close position if there's a crossover and we already have a position elif negative_crossover and position>=0: # Update position variable position = quantity*-1 # Place Smart market sell order response = algomojo.PlaceSmartOrder(broker=broker, strategy=strategy, exchange=exchange, symbol=symbol, action="SELL", product="MIS", pricetype="MARKET", quantity=quantity, price="0", position_size=position, trigger_price="0", disclosed_quantity="0", amo="NO", splitorder="NO", split_quantity="1") print ("Sell Order Response :",response) # Print current position and price data print("Position:", position) print("LTP:", close[-1]) print("Short EMA:", shortEMA[-2]) print("Long EMA:", longEMA[-2]) print("Positive crossover:", positive_crossover) print("Negative crossover:", negative_crossover) # Wait for 15 seconds before checking again time.sleep(15) # Create and start a new thread to run the strategy t = threading.Thread(target=ema_crossover) t.start()