Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Articles

Automated trading with aliceblue - python

AliceBlue API in Python - Intraday Trading sample strategy

Here's a basic intraday trading strategy using the AliceBlue API in Python. This example demonstrates a simple moving average crossover strategy:

```python
from alice_blue import *
import pandas as pd

# Initialize AliceBlue API
username = "YOUR_USERNAME"
password = "YOUR_PASSWORD"
api_secret = "YOUR_API_SECRET"
twoFA = "YOUR_2FA_OTP"

# Create AliceBlue object
alice = AliceBlue(username=username, password=password, access_token=api_secret, master_contracts_to_download=['NSE', 'BSE', 'MCX'], session_type=AliceBlue.ALICEBLUE)

# Login to AliceBlue
alice.login()

# Fetch historical data for a stock (e.g., Nifty50)
symbol = "NIFTY50"
today = datetime.date.today()
historical_data = alice.get_instrument_by_symbol('NSE', symbol)
data = alice.get_ohlc(api_key=historical_data, from_date=datetime.datetime(today.year, today.month, today.day, 9, 15), to_date=datetime.datetime(today.year, today.month, today.day, 15, 30), interval='minute', continuous=True)

# Convert data to DataFrame
df = pd.DataFrame(data)

# Calculate moving averages
df['MA50'] = df['close'].rolling(window=50).mean()
df['MA200'] = df['close'].rolling(window=200).mean()

# Determine trading signals based on crossover
df['Signal'] = 0
df['Signal'][50:] = np.where(df['MA50'][50:] > df['MA200'][50:], 1, 0)
df['Position'] = df['Signal'].diff()

# Implement trading logic
for i, row in df.iterrows():
    if row['Position'] == 1:
        print("Buy:", row['close'])
        # Place buy order using AliceBlue API
        # alice.place_order(transaction_type=TransactionType.Buy, instrument=historical_data, quantity=1, order_type=OrderType.Market, product_type=ProductType.Delivery)
    elif row['Position'] == -1:
        print("Sell:", row['close'])
        # Place sell order using AliceBlue API
        # alice.place_order(transaction_type=TransactionType.Sell, instrument=historical_data, quantity=1, order_type=OrderType.Market, product_type=ProductType.Delivery)
```

Make sure to replace `"YOUR_USERNAME"`, `"YOUR_PASSWORD"`, and `"YOUR_API_SECRET"` with your actual AliceBlue account credentials and API secret. You'll also need to provide your two-factor authentication (2FA) OTP in the `twoFA` variable.

This example fetches historical minute-by-minute data for a specified stock (e.g., Nifty50) and calculates the 50-day and 200-day moving averages. It then generates buy and sell signals based on the crossover of these moving averages and places orders accordingly using the AliceBlue API. Remember to uncomment and fill in the appropriate code for placing buy and sell orders, and ensure that you have the necessary permissions and approvals to execute trades.

caa February 19 2024 143 reads 0 comments Print

0 comments

Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Sign In
Not a member yet? Click here to register.
Forgot Password?