Skip to content





Ninjaview Pinescript Alert Library Documentation


Ninjaview Pinescript Alert Library Documentation

Introduction

The NinjaView library is designed to simplify the process of creating and managing complex trading orders on TradingView. By encapsulating the complexity of JSON payload syntax into straightforward function calls, the library enables traders to focus on strategy development without worrying about syntax errors or the intricacies of JSON formatting. This library is particularly useful for automating trading strategies and indicators, allowing for seamless integration with the NinjaView webhook system for order execution.

How It Works

The library provides a set of predefined functions, each corresponding to a specific order type or trading action. These functions accept parameters like action (buy/sell), account details, ticker symbols, quantities, prices, and more, generating a JSON-formatted message ready to be sent to the NinjaView webhook.

Usage in Strategies

When used within a TradingView strategy, the library’s functions should be called in conjunction with the strategy.entry, strategy.exit, or strategy.close functions. The generated message from the library function is then assigned to the alert_message parameter. For the alert to function correctly, the message field in the TradingView alert setup should contain {{strategy.order.alert_message}}. This placeholder is replaced by the actual message from the alert_message parameter when the alert triggers, ensuring the correct order information is sent to the NinjaView system.

Usage in Indicators

The NinjaView library can also be used within TradingView indicators to generate alerts for trading actions. Unlike strategies, when using the library within an indicator, you don’t need to include {{strategy.order.alert_message}} in the TradingView alert message field. Instead, you can directly use the message generated by the library function.

Setting Up Webhooks in TradingView Alerts

To ensure that alerts generated by the NinjaView library are correctly processed, users must configure their TradingView alerts to send messages to the NinjaView webhook URL. This is done by setting the “Webhook URL” field in the TradingView alert setup to the provided NinjaView webhook endpoint. The content type should be set to application/json, and the message/body should contain {{strategy.order.alert_message}} for strategies or the direct output from the library function for indicators.

Conclusion

The NinjaView library streamlines the process of creating and managing complex orders by providing easy-to-use functions that abstract away the JSON payload complexity. Whether used in strategies or indicators, the library enhances the automation capabilities of TradingView scripts, making it easier for traders to execute sophisticated trading strategies through the NinjaView webhook system.

Remember to thoroughly test your strategies and indicators in a simulation environment before executing live trades, as trading involves risks.

Key:

  • Variable: White
  • Command: Purple
  • Values: Green
  • Parameters : Blue
  • Comment: Gold


simpleMarketBuyMessage = ninjaView.SimpleMarket("buy", account, ticker, qty, "Entering Long Position")
simpleMarketSellMessage = ninjaView.SimpleMarket("sell", account, ticker, qty, "Exiting Long Position")

closeAllPositionsMessage = ninjaView.CloseAllPositions("sell", account, ticker, "Closing All Positions")

closeThenMarketBuyMessage = ninjaView.CloseThenMarket("buy", account, ticker, qty, "Close Then Buy")
closeThenMarketSellMessage = ninjaView.CloseThenMarket("sell", account, ticker, qty, "Close Then Sell")

limitOrderBuyMessage = ninjaView.LimitOrder("buy", account, ticker, qty, limitPrice, "DAY", "Placing Buy Limit Order")
limitOrderSellMessage = ninjaView.LimitOrder("sell", account, ticker, qty, limitPrice, "DAY", "Placing Sell Limit Order")

stopMarketBuyMessage = ninjaView.StopMarket("buy", account, ticker, qty, stopPrice, "DAY", "Placing Buy Stop Market Order")
stopMarketSellMessage = ninjaView.StopMarket("sell", account, ticker, qty, stopPrice, "DAY", "Placing Sell Stop Market Order")

stopLimitBuyMessage = ninjaView.StopLimit("buy", account, ticker, qty, stopPrice, "DAY", "Placing Buy Stop Limit Order")
stopLimitSellMessage = ninjaView.StopLimit("sell", account, ticker, qty, stopPrice, "DAY", "Placing Sell Stop Limit Order")

ocoMarketMessage = ninjaView.OCOMarket("buy", account, ticker, qty, takeProfitPrice, stopPrice, "OCOId", "DAY", "Placing OCO Market Order")

closeThenOCOMarketMessage = ninjaView.CloseThenOCOMarket("buy", account, ticker, qty, takeProfitPrice, stopPrice, "OCOId", "DAY", "Close Then OCO Market Order")

ocoStopLimitMessage = ninjaView.OCOStopLimit("buy", account, ticker, qty, entryStopPrice, limitPrice, takeProfitPrice, initialStopPrice, "OCOId", "DAY", "Placing OCO Stop Limit Order")

ocoMarketLimitMessage = ninjaView.OCOMarketLimit("buy", account, ticker, qty, entryStopPrice, limitPrice, takeProfitPrice, initialStopPrice, "OCOId", "DAY", "Placing OCO Market Limit Order")

ocoAdjustMessage = ninjaView.OCOAdjust("adjust", account, ticker, qty, newTakeProfitPrice, newStopPrice, "OCOId", "DAY", "Adjusting OCO Orders")

cancelAllOCOMessage = ninjaView.CancelAllOCO("Canceling All OCO Orders")

marketATMMessage = ninjaView.MarketATM("ATMStrategyName", "ATMStrategyId", "buy", account, ticker, qty, "OCOIdATM", "OrderIdATM", stopPrice, "DAY", "Placing Market ATM Order")

closeThenMarketATMMessage = ninjaView.CloseThenMarketATM("ATMStrategyName", "ATMStrategyId", "sell", account, ticker, qty, "OCOIdATM", "OrderIdATM", stopPrice, "DAY", "Close Then Market ATM Order")

limitOrderCancelATMMessage = ninjaView.LimitOrderCancelATM("ATMStrategyName", "ATMStrategyId", "Canceling Limit Order ATM")

Example

Basic example of a market order and exit

//@version=5

import dudeowns/NinjaView/1 as ninjaView

strategy("Simplified NinjaView Strategy", overlay=true)

account = input("Sim101", "Account")
ticker = input("ES 03-24", "Ticker")
qty = input(1, "Quantity")

smaLength = input(20, "SMA Length")

sma = sma(close, smaLength)

buySignal = close > sma
sellSignal = close < sma

if buySignal
    alert_message = ninjaView.SimpleMarket("buy", account, ticker, qty, "Entering Long Position")
    strategy.entry("Long", strategy.long)
    
if sellSignal
    alert_message = ninjaView.SimpleMarket("sell", account, ticker, qty, "Exiting Long Position")
    strategy.entry("Short", strategy.short)

Add the Script to the Chart:

  1. Copy the provided script and add it to your TradingView chart.
  2. Insert strategy.order.alert_message into Alert Message:
    • Right-click on the chart.
    • Select “Add Alert” from the menu.
    • In the alert configuration window, go to the “Message” field.
    • Insert {{strategy.order.alert_message}} into the “Message” field. This placeholder will be replaced with the actual alert message generated by the strategy.
  3. Enable/Insert Webhook on Notifications Tab:
    • In the same alert configuration window, go to the “Notifications” tab.
    • Check the box for “Webhook URL” and insert the provided NinjaView webhook endpoint into the input field.
    • Set the content type to application/json.
  4. Configure Alert Conditions:
    • Define your alert conditions based on the strategy’s entry and exit conditions.
    • For example, you might set an alert condition for when the strategy enters a long position (based on longCondition) or exits all positions (based on exitCondition).
  5. Create the Alert:
    • Once you’ve configured all the alert settings, click on the “Create” button to save the alert.

Disclaimer for Ninja-View.com:

Ninja-View.com specializes in providing tools for forward testing trading strategies on a paper basis.

  • [NinjaView reserves the right to enforce its intellectual property rights to the fullest extent of the law.]
  • General Risk Disclosure: Trading, even on a simulation basis, involves risks and is not suitable for all participants.
  • Hypothetical Performance Disclosure: Forward testing results are hypothetical and do not guarantee future performance.
  • Testimonial Disclosure: Testimonials may not be indicative of all user experiences.
  • Live Trade Room Disclosure: Discussions and content are for educational purposes only.
  • Independent Entity Disclosure: NinjaView is an independent entity and is not representative of, nor endorsed by, NinjaTrader. The purpose of linking to NinjaTrader is purely promotional. NinjaTrader neither represents nor has any affiliation with NinjaView.

Users are responsible for understanding and complying with any relevant regulations, including those of the NFA or CFTC.

© 2024 NinjaView. All rights reserved.