Algomojo allows traders to execute strategies using webhooks, providing seamless integration with trading systems. In this blog, weβll explore how to send strategy orders via webhook in Python using the requests
library.
π Overview
The function PlaceStrategyOrder
sends a BUY/SELL order with a specified date to the Algomojo webhook. Users can automate their strategy execution by integrating this function into their trading system.
π Improved Python Code for Placing a Strategy Order
Hereβs the optimized function:
import requests
import json
def PlaceStrategyOrder(webhook_url, date, action):
try:
payload = {
"date": date,
"action": action
}
headers = {
"Content-Type": "application/json"
}
response = requests.post(webhook_url, json=payload, headers=headers)
response_data = response.json() # Parse JSON response
if response.status_code == 200 and response_data.get("status") == "success":
return f"Order placed successfully: {response_data}"
else:
return f"Error placing order: {response_data.get('error_msg', 'Unknown error')}"
except requests.exceptions.RequestException as e:
return f"Network error: {e}"
except json.JSONDecodeError:
return "Error decoding response JSON"
except Exception as e:
return f"Unexpected error: {e}"
π₯ Key Improvements
β
Better Error Handling β Catches network errors, JSON decoding errors, and unexpected exceptions.
β
Validates HTTP Status Code β Ensures response status is 200 OK
before checking for success.
β
Handles API Response Properly β Parses JSON correctly and extracts error messages when necessary.
π How to Run the Python Function and Call It Correctly
1οΈβ£ Install Python and Required Libraries
Make sure you have Python installed on your system. If not, download it from Python.org.
Next, install the required requests
library by running this command in your terminal or command prompt:
pip install requests
2οΈβ£ Where to Keep the Function Source?
- Create a separate Python file (e.g.,
strategy_order.py
) and save the function inside it. - This makes it reusable for multiple scripts.
strategy_order.py
import requests
import json
def PlaceStrategyOrder(webhook_url, date, action):
try:
payload = {
"date": date,
"action": action
}
headers = {
"Content-Type": "application/json"
}
response = requests.post(webhook_url, json=payload, headers=headers)
response_data = response.json()
if response.status_code == 200 and response_data.get("status") == "success":
return f"Order placed successfully: {response_data}"
else:
return f"Error placing order: {response_data.get('error_msg', 'Unknown error')}"
except requests.exceptions.RequestException as e:
return f"Network error: {e}"
except json.JSONDecodeError:
return "Error decoding response JSON"
except Exception as e:
return f"Unexpected error: {e}"
3οΈβ£ How to Call the Function in place_order.py
Now, create another Python file (place_order.py
) and call the function from strategy_order.py
.
place_order.py
from strategy_order import PlaceStrategyOrder
# Copy and paste the webhook URL from the strategy screen
webhook_url = "<YOUR_WEBHOOK_URL>"
# Define order parameters
date = "2025-03-18 15:30:00" # Set the order execution date and time
action = "BUY" # Choose between "BUY" or "SELL"
# Place the strategy order
response = PlaceStrategyOrder(webhook_url, date, action)
# Print the response from the webhook
print(response)
4οΈβ£ Run the Python Script
Save both files in the same directory and run place_order.py
:
python place_order.py
If the order is successfully placed, you will see:
{
"status": "success",
"data": {
"signal_ids": [
3516986,
3516987
]
}
}
If there is an error, it will return:
{
"status": "error",
"error_msg": "Strategy Code does not exist/inactive",
"error_type": "E"
}
π Common Issues & Fixes
β requests.exceptions.ConnectionError: [Errno 110] Connection timed out
β Fix: Check your internet connection or ensure the webhook URL is correct.
β Error decoding response JSON
β Fix: The API response may not be valid JSON. Use print(response.text)
to inspect the output.
β Error placing order: Strategy Code does not exist/inactive
β Fix: Ensure the webhook URL is correct and your strategy is active in Algomojo.
π― Conclusion
By using this Python function, traders can seamlessly integrate Algomojo webhooks into their trading automation. This ensures fast, reliable, and accurate order execution.
π Start automating your strategy orders today! Let us know if you need further enhancements or support.
π Learn More: Algomojo API Documentation