# ChartInk

OpenAlgo supports integration with Chartink for automated trading based on scanner alerts. This integration allows you to:

* Create and manage trading strategies
* Configure symbols with quantities and product types
* Currently Supports only NSE and BSE Symbols
* Handle intraday and positional strategies
* Automate order placement based on Chartink alerts
* Auto square-off positions for intraday strategies

<figure><img src="https://17901342-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmBwEhITzgv0O0fEGIIRN%2Fuploads%2FFLip0tuzLIz4ikGmtfg4%2FScreenshot%202024-12-17%20at%203.38.04%E2%80%AFPM.png?alt=media&#x26;token=4b0df4bf-303a-4b4a-990e-5a862defb6b9" alt=""><figcaption></figcaption></figure>

### Requirements

ChartInk Paid Account (Supports Webhook Features to send Alerts to OpenAlgo)

Ngrok with Custom Domain Configuration (Provides Webhooks to Chartink/Tradingview) if hosted locally in Laptop/Desktop.&#x20;

Configure the .env file with Custom Domain  (you cannot run Chartink by default with localhost or 127.0.0.1:5000)

{% embed url="<https://www.youtube.com/watch?v=z9PWo2djAEk>" %}

### Order Processing System

#### Queue Management

OpenAlgo uses a dual-queue system to handle orders efficiently:

1. Regular Order Queue (Entry Orders):
   * Handles BUY and SHORT orders
   * Processes up to 10 orders per second
   * Orders are batched for maximum throughput
   * Example: 50 BUY orders complete in \~5 seconds
2. Smart Order Queue (Exit Orders):
   * Handles SELL and COVER orders
   * Maintains 1-second delay between orders
   * Higher priority than entry orders
   * Example: 50 SELL orders complete in \~50 seconds
3. Multiple Strategy Handling:
   * All strategies share the same queues
   * Exit orders always processed before entries
   * Rate limits maintained across strategies
   * Each strategy respects its trading hours
4. Auto Square-off Processing:
   * Uses smart order queue
   * 1-second delay between position closures
   * Processes positions sequentially
   * Example: 20 positions take \~20 seconds to close

### Setting Up a Strategy

<figure><img src="https://17901342-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmBwEhITzgv0O0fEGIIRN%2Fuploads%2FDy7z7PlGGd9DIfJEPFLU%2FScreenshot%202024-12-17%20at%203.39.35%E2%80%AFPM.png?alt=media&#x26;token=e37b9ff2-d686-46d7-a078-d0ce8f86c583" alt=""><figcaption></figcaption></figure>

1. Go to the Chartink section in OpenAlgo
2. Click "New Strategy" button
3. Fill in the strategy details:
   * Name: A unique name for your strategy (will be prefixed with 'chartink\_')
   * Type: Choose between Intraday or Positional
   * For Intraday strategies:
     * Start Time: Trading start time (default: 09:15)
     * End Time: Trading end time (default: 15:00)
     * Square Off Time: Auto square-off time (default: 15:15)

<figure><img src="https://17901342-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmBwEhITzgv0O0fEGIIRN%2Fuploads%2FcLUUiCqXrmNQTFmfzc6Q%2FScreenshot%202024-12-17%20at%204.31.16%E2%80%AFPM.png?alt=media&#x26;token=37007ea9-e0a6-428e-a7b5-5fb228740c12" alt=""><figcaption></figcaption></figure>

### Alert Name Keywords

<figure><img src="https://17901342-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmBwEhITzgv0O0fEGIIRN%2Fuploads%2FfBGyEVWbpQ5DCozJwTIQ%2FScreenshot%202024-12-17%20at%203.41.35%E2%80%AFPM.png?alt=media&#x26;token=ef83d64f-c144-4e30-88d7-158408c6387a" alt=""><figcaption></figcaption></figure>

The alert name in Chartink MUST include one of these keywords (not case-sensitive):

1. **BUY** - For entering long positions
   * Examples:
     * "BUY Alert"
     * "Alert for BUY 2024-12-13"
     * "Supertrend BUY Signal"
2. **SELL** - For exiting long positions or regular selling
   * Examples:
     * "SELL Alert"
     * "Alert for SELL 2024-12-13"
     * "Supertrend SELL Signal"
3. **SHORT** - For entering short positions (selling first)
   * Examples:
     * "SHORT Alert"
     * "Alert for SHORT 2024-12-13"
     * "Supertrend SHORT Signal"
4. **COVER** - For exiting short positions (buying to cover)
   * Examples:
     * "COVER Alert"
     * "Alert for COVER 2024-12-13"
     * "Supertrend COVER Signal"

<figure><img src="https://17901342-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmBwEhITzgv0O0fEGIIRN%2Fuploads%2FeuTAC8UqPrOgmTvZTd79%2FScreenshot%202024-12-17%20at%204.10.01%E2%80%AFPM.png?alt=media&#x26;token=ee657bc1-1552-4525-8402-595e2979d3d7" alt=""><figcaption></figcaption></figure>

#### Keyword Rules

* Keywords can appear anywhere in the alert name
* Keywords are not case-sensitive (buy/BUY/Buy all work)
* Alert will fail if no valid keyword is found
* Only one action will be taken even if multiple keywords are present

### Configuring Symbols

After creating a strategy, you need to configure the symbols to trade:

1. Click "Configure Symbols" on your strategy
2. Add symbols individually:
   * Search and select Symbol (with exchange badge)
   * Select Exchange (NSE by default)
   * Enter Quantity
   * Select Product Type (MIS for Intraday, CNC for Positional)
3. Or bulk add symbols using CSV format:

   ```
   RELIANCE,NSE,10,MIS
   HDFCBANK,NSE,5,CNC
   TATASTEEL,BSE,15,MIS
   ```

<figure><img src="https://17901342-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmBwEhITzgv0O0fEGIIRN%2Fuploads%2FicBa3W2htSkyuUPWZYzq%2FScreenshot%202024-12-17%20at%203.42.54%E2%80%AFPM.png?alt=media&#x26;token=60f95cc4-2f14-4dc2-b57a-4ce63a2d94a9" alt=""><figcaption></figcaption></figure>

### Setting Up Chartink Alert

**Creating a Scanner**

1. Goto [Chartink Scanner Dashboard](https://chartink.com/scan_dashboard)
2. Click "Create a new scanner" button
3. Create the Scanner conditions and Press Save Scan
4. Enter an alert name that includes one of the action keywords

<figure><img src="https://17901342-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmBwEhITzgv0O0fEGIIRN%2Fuploads%2FeoOlYBO60jrbTFhNuf2Q%2FScreenshot%202024-12-17%20at%203.47.00%E2%80%AFPM.png?alt=media&#x26;token=558afcd5-30b9-4804-a5f3-eb94f01b02eb" alt=""><figcaption></figcaption></figure>

Creating Alert

1. Once the Scanner is Saved Click Create Alert
2. Find the "Webhook url(optional)" field
3. Copy and paste your strategy's webhook URL:

   ```
   https://your-openalgo-domain/chartink/webhook/<webhook-id>
   ```
4. Configure other alert settings as needed
5. Click "Save alert" button

<figure><img src="https://17901342-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmBwEhITzgv0O0fEGIIRN%2Fuploads%2FjEpMFFVgEZjsEPey5ZcE%2FScreenshot%202024-12-17%20at%203.52.43%E2%80%AFPM.png?alt=media&#x26;token=9a1b1139-9bb4-4f8f-bda0-503fbd1420b3" alt=""><figcaption></figcaption></figure>

### How It Works

1. When your scanner conditions are met, Chartink sends an alert to your webhook URL
2. OpenAlgo receives the alert and:
   * Validates the webhook ID
   * Checks if strategy is active
   * Validates the alert name for action keyword
   * For intraday strategies, checks if within trading hours
   * Matches symbols from alert with your configured symbols
   * Places orders according to your configuration

#### Intraday Trading

For intraday strategies:

* Orders are only placed between Start Time and End Time
* At Square Off Time, all open positions are automatically closed
* Uses MIS product type for better leverage

#### Positional Trading

For positional strategies:

* Orders can be placed any time during market hours
* No automatic square-off
* Uses CNC product type for delivery trades

### Order Placement

When a Chartink alert is received:

* For new positions:

  ```
  {
    "apikey": "your-api-key",
    "strategy": "Strategy Name",
    "symbol": "SYMBOL",
    "exchange": "NSE/BSE",
    "action": "BUY/SELL/SHORT/COVER",
    "product": "MIS/CNC",
    "pricetype": "MARKET",
    "quantity": "configured-quantity"
  }
  ```
* For square-off (intraday):

  ```
  {
    "apikey": "your-api-key",
    "strategy": "Strategy Name",
    "symbol": "SYMBOL",
    "exchange": "NSE/BSE",
    "action": "SELL",
    "product": "MIS",
    "pricetype": "MARKET",
    "quantity": "0",
    "position_size": "0"
  }
  ```

### Strategy Management

#### Activation/Deactivation

* Active strategies process incoming alerts
* Inactive strategies ignore alerts
* Toggle status from strategy view

#### Symbol Management

* Add/remove symbols any time
* Update quantities as needed
* View all configured symbols with exchange badges
* Bulk import for multiple symbols
* Delete confirmation for symbol removal

#### Time Controls

For intraday strategies:

* Start Time: When to start accepting alerts
* End Time: When to stop accepting alerts
* Square Off Time: When to close all positions

### Best Practices

1. Use Chartink Custom Watchlists
   * Create custom watchlists with only your trading symbols
   * Avoid scanning large portfolios like NIFTY500, NIFTY200
   * Helps manage positions efficiently
   * Reduces processing time for orders
   * Better control over trading universe
2. Test your strategy with small quantities first
   * Verify order placement
   * Check position management
   * Monitor square-off process
   * Validate webhook integration
3. Use proper stop-losses in your Chartink scanner
   * Implement risk management rules
   * Set appropriate price conditions
   * Add volume filters if needed
4. Monitor the first few alerts
   * Verify order execution
   * Check processing times
   * Validate symbol matching
   * Ensure proper position tracking
5. Keep your webhook URL private
   * Don't share webhook URLs
   * Regularly check for unauthorized alerts
   * Monitor strategy activity
6. Include action keyword in scan name
   * Use clear, consistent naming
   * Follow keyword rules strictly
   * Avoid multiple keywords
7. For intraday strategies:
   * Ensure orders are placed during trading hours
   * Monitor square-off at configured time
   * Plan for processing delays with large lists
8. For large symbol lists:
   * Entry orders process faster (10/sec)
   * Exit orders take longer (1/sec)
   * Plan strategy timing accordingly
   * Consider splitting into multiple strategies

### Error Handling

OpenAlgo handles various error scenarios:

* Invalid webhook IDs
* Missing action keywords in alert names
* Inactive strategies
* Outside trading hours
* Symbol mismatches
* Order placement failures

All errors are logged and can be viewed in the API analyzer.

### Limitations

1. Only supports NSE and BSE exchanges
2. Intraday square-off is all-or-nothing
3. No partial position closures
4. No modification of existing orders
5. Market orders only
6. Alert name must contain valid action keyword

### Security

* Each strategy has a unique webhook ID
* API keys are required for order placement
* Session validation for web interface
* Secure storage of credentials
* Rate limiting on endpoints
* Confirmation dialogs for deletions

### Troubleshooting

1. Check strategy status (active/inactive)
2. Verify trading hours for intraday
3. Confirm symbol configurations
4. Check alert name contains valid keyword
5. Check API analyzer for errors
6. Verify webhook URL in Chartink

### Support

For issues or questions:

1. Check the logs in API analyzer
2. Review error messages
3. Contact support with:
   * Strategy ID
   * Error details
   * Time of occurrence
   * Relevant logs
   * Alert name and webhook URL used
