Skip to content

Updated on 05/02/24

Table of Contents

  1. Important Considerations
  2. Options
  3. Default Values for Testing
  4. JSON Examples
  5. Pinescript Examples

Important Considerations

While this guide provides a comprehensive overview of various order types and strategies, it is advisable to familiarize yourself with simpler order types first. Please note that TradingView, the platform that generates these alerts, is not a brokerage service. Hence, data discrepancies, such as latency or minor price feed differences, may exist when executing more complex orders in a real trading environment.

Additionally, ensure that you cross-reference the ticker symbols received from TradingView with the corresponding expiry month and year on NinjaTrader. For instance, if you are using the NQ1! symbol from TradingView, the appropriate NinjaTrader ticker would be NQ JUN24 as of the date of this guide.

If you have any questions about which variables can be utilized in the TradingView alert system, feel free to check out the official TradingView page on How to Use a Variable in Alert.

Options

  • Account: (Required)
  • Instrument: (Required)
  • Alert: (Required)
  • Qty: (Required)
  • Order Type: (Required)
  • Limit Price: (Optional)
  • Stop Price: (Optional)
  • TIF: (Required)

Default Values for Testing

  • Account: Sim101
  • Ticker: NQ JUN24
  • Quantity (qty): 1

Payload-Testing-Page

To help confirm your connection and payloads are being properly setup and deployed, we’ve created a Payload(s) Test Page.

PayloadTest

Log in to NinjaTrader 8 and establish a connection. Then, start NinjaView and activate the Ngrok Webhook. Test this setup by visiting the “Payload Test Page,” where you’ll enter your Ngrok webhook URL and desired payload, and click “Send Request.” This process should facilitate a smooth execution of your order, using a standard “Market Long” to confirm a stable connection.

NEW! Trade Copier (Separate accounts with comma’s using any alert type)

{
  "alert": "Market Long",
  "account": "Sim101,Sim102,Sim103,Otherbroker,Otherpropfundaccts",
  "ticker": "NQ JUN24"
}
        

Custom Comment Line For Any Payload

Raw JSON

{
    "comment": "Anything You want"
}

          

PAID/LOCKED INDICATOR/STRATEGY

{
    "comment": "Anything You want"
}

          

Open Source w/Strings

comment = '{ "comment": "Anything You Want" }'



          

Raw JSON

Simple Market Long

Raw JSON

{
  "alert": "Market Long",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "1"
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "Market {{strategy.order.action}}",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "{{strategy.order.contracts}}"
}
          

Open Source w/Strings

MarketLong = '{ "alert": "Market Long", ' + 
  '"account": "' + str.tostring(acct) + '", ' + 
  '"ticker": "' + str.tostring(ticker) + '", ' + 
  '"qty": "' + str.tostring(qty) + '" }'


          

Simple Market Short

Raw JSON

{
  "alert": "Market Short",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "1"
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "Market {{strategy.order.action}}",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "{{strategy.order.contracts}}"
}
          

Open Source w/Strings

MarketShort = '{ "alert": "Market Short", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '" }'


          

Simple Close All Then Market Order Long (For closing short and opening long/flipping)

Raw JSON

{
  "alert": "Close Then Market Buy",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "1"
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "Close Then Market {{strategy.order.action}}",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "{{strategy.order.contracts}}"
}
          

Open Source w/Strings

CloseThenMarketBuy = '{ "alert": "Close Then Market Buy", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '" }'


          

Simple Close All Then Market Order Short(For closing long and opening short/flipping)

Raw JSON

{
  "alert": "Close Then Market Sell",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "1"
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "Close Then Market {{strategy.order.action}}",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "{{strategy.order.contracts}}"
}
          

Open Source w/Strings

CloseThenMarketSell = '{ "alert": "Close Then Market Sell", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '" }'


          

Simple Close All Positions

Raw JSON

{
  "alert": "Market Close",
  "account": "Sim101",
  "ticker": "NQ JUN24"
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "Market {{strategy.order.action}}",
  "account": "Sim101",
  "ticker": "NQ JUN24"
}
          

Open Source w/Strings

MarketClose = '{ "alert": "Market Close", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '" }'



          

Long with Limit Order

Raw JSON

{
  "alert": "Limit Long",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "1",
  "order_type": "LIMIT",
  "limit_price": "14000.50",
  "tif": "DAY"
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "Limit {{strategy.order.action}}",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "{{strategy.order.contracts}}",
  "order_type": "LIMIT",
  "limit_price": "{{close}}",
  "tif": "DAY"
}
          

Open Source w/Strings

LimitLong = '{ "alert": "Limit Long", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"order_type": "LIMIT", ' +
  '"limit_price": "' + str.tostring(entry_limit_long) + '", ' +
  '"tif": "DAY" }'


          

Short with Limit Order

Raw JSON

{
  "alert": "Limit Short",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "1",
  "order_type": "LIMIT",
  "limit_price": "13900.50",
  "tif": "DAY"
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "Limit {{strategy.order.action}}",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "{{strategy.order.contracts}}",
  "order_type": "LIMIT",
  "limit_price": "{{close}}",
  "tif": "DAY"
}
          

Open Source w/Strings

LimitShort = '{ "alert": "Limit Short", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"order_type": "LIMIT", ' +
  '"limit_price": "' + str.tostring(entry_limit_long) + '", ' +
  '"tif": "DAY" }'


          

Setting Short Positions Market Stop Order

Raw JSON

{
  "alert": "STOPMARKET Long",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "1",
  "order_type": "STOPMARKET",
  "stop_price": "13900",
  "tif": "DAY"
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "STOPMARKET {{strategy.order.action}}",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "{{strategy.order.contracts}}",
  "order_type": "STOPMARKET",
   "stop_price": "13900",
  "tif": "DAY"
}
          

Open Source w/Strings

LongConditionSTOP = '{ "alert": "STOPMARKET Long", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"order_type": "STOPMARKET", ' +
  '"stop_price": "' + str.tostring(market_stop_price_short) + '", ' +
  '"tif": "DAY" }'


          

Setting Long Positions Market Stop Order

Raw JSON

{
  "alert": "STOPMARKET Short",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "1",
  "order_type": "STOPMARKET",
  "stop_price": "13900",
  "tif": "DAY"
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "STOPMARKET {{strategy.order.action}}",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "{{strategy.order.contracts}}",
  "order_type": "STOPMARKET",
   "stop_price": "13900",
  "tif": "DAY"
}
          

Open Source w/Strings

ShortConditionStop = '{ "alert": "STOPMARKET Short", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"order_type": "STOPMARKET", ' +
  '"stop_price": "' + str.tostring(market_stop_price_long) + '", ' +
  '"tif": "DAY" }'


          

Setting Short Positions Limit Stop Order

Raw JSON

{
  "alert": "STOPLIMIT Long",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "1",
  "order_type": "STOPLIMIT",
  "limit_price": "0",
  "stop_price": "13999.50",
  "tif": "DAY"
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "STOPLIMIT {{strategy.order.action}}",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "{{strategy.order.contracts}}",
  "order_type": "STOPLIMIT",
  "limit_price": "0",
   "stop_price": "13900",
  "tif": "DAY"
}
          

Open Source w/Strings

LongConditionStopLimit = '{ "alert": "STOPLIMIT Long", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"order_type": "STOPLIMIT", ' +
  '"limit_price": "0", ' +
  '"stop_price": "' + str.tostring(limit_stop_price_short) + '", ' +
  '"tif": "DAY" }'


          

Setting Long Positions Limit Stop Order

Raw JSON

{
  "alert": "STOPLIMIT Short",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "1",
  "order_type": "STOPLIMIT",
  "limit_price": "0",
  "stop_price": "13999.50",
  "tif": "DAY"
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "STOPLIMIT {{strategy.order.action}}",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "{{strategy.order.contracts}}",
  "order_type": "STOPLIMIT",
  "limit_price": "0",
   "stop_price": "13900",
  "tif": "DAY"
}
          

Open Source w/Strings

ShortConditionStopLimit = '{ "alert": "STOPLIMIT Short", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"order_type": "STOPLIMIT", ' +
  '"limit_price": "0", ' +
  '"stop_price": "' + str.tostring(limit_stop_price_long) + '", ' +
  '"tif": "DAY" }'


          

OCO Market Order Long

Raw JSON

{
    "alert": "OCO Market Long",
    "account": "Sim1",
    "ticker": "NQ JUN24",
    "qty": "5",
    "take_profit_price": "16000.00",
    "stop_price": "15500.00",
    "tif": "DAY",
    "oco_id": "x"
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
    "alert": "OCO Market {{strategy.order.action}}",
    "account": "Sim1",
    "ticker": "NQ JUN24",
    "qty": "{{strategy.order.contracts}}",
    "take_profit_price": "16000.00",
    "stop_price": "15500.00",
    "tif": "DAY",
    "oco_id": "x"
}
          

Open Source w/Strings

OCOMarketLong = '{ "alert": "OCO Market Long", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"take_profit_price": "' + str.tostring(take_profit_long) + '", ' +
  '"stop_price": "' + str.tostring(stop_price_long) + '", ' +
  '"tif": "DAY", ' +
  '"oco_id": "' + str.tostring(OrderID) + '" }'


          

OCO Market Order Short

Raw JSON

{
    "alert": "OCO Market Short",
    "account": "Sim1",
    "ticker": "NQ JUN24",
    "qty": "5",
    "take_profit_price": "15500.00",
    "stop_price": "16000.00",
    "tif": "DAY",
    "oco_id": "x"
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
    "alert": "OCO Market {{strategy.order.action}}",
    "account": "Sim1",
    "ticker": "NQ JUN24",
    "qty": "{{strategy.order.contracts}}",
    "take_profit_price": "15500.00",
    "stop_price": "16000.00",
    "tif": "DAY",
    "oco_id": "x"
}
          

Open Source w/Strings

OCOMarketShort = '{ "alert": "OCO Market Short", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"take_profit_price": "' + str.tostring(take_profit_long) + '", ' +
  '"stop_price": "' + str.tostring(stop_price_long) + '", ' +
  '"tif": "DAY", ' +
  '"oco_id": "' + str.tostring(OrderID) + '" }'


          

Close Then OCO Market Long(For closing short and opening long/flipping)

Raw JSON

{
  "alert": "Close Then OCO Market Long",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "1",
  "order_type": "MARKET",
  "take_profit_price": "17940",
  "stop_price": "17930",
  "tif": "DAY",
  "oco_id": "x",
  "comment": "Closing existing position and placing new OCO Market Long order"
}

          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "Close Then OCO Market {{strategy.order.action}}",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "{{strategy.order.contracts}}",
  "order_type": "MARKET",
  "take_profit_price": "17940",
  "stop_price": "17930",
  "tif": "DAY",
  "oco_id": "x",
  "comment": "Closing existing position and placing new OCO Market Long order"
}

          

Open Source w/Strings

CloseOCOMarketLong = '{ "alert": "Close Then OCO Market Long", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"order_type": "MARKET", ' +
  '"take_profit_price": "' + str.tostring(take_profit_long) + '", ' +
  '"stop_price": "' + str.tostring(stop_price_long) + '", ' +
  '"tif": "DAY", ' +
  '"oco_id": "' + str.tostring(OrderID) + '", ' +
  '"comment": "Closing existing position and placing new OCO Market Long order" }';



          

Close Then OCO Market Short(For closing long and opening short/flipping)

Raw JSON

{
  "alert": "Close Then OCO Market Short",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "1",
  "order_type": "MARKET",
  "take_profit_price": "17930",
  "stop_price": "17940",
  "tif": "DAY",
  "oco_id": "x",
  "comment": "Closing existing position and placing new OCO Market Long order"
}

          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "Close Then OCO Market {{strategy.order.action}}",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "{{strategy.order.contracts}}",
  "order_type": "MARKET",
  "take_profit_price": "17930",
  "stop_price": "17940",
  "tif": "DAY",
  "oco_id": "x",
  "comment": "Closing existing position and placing new OCO Market Long order"
}

          

Open Source w/Strings

CloseOCOMarketLong = '{ "alert": "Close Then OCO Market Short", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"order_type": "MARKET", ' +
  '"take_profit_price": "' + str.tostring(take_profit_long) + '", ' +
  '"stop_price": "' + str.tostring(stop_price_long) + '", ' +
  '"tif": "DAY", ' +
  '"oco_id": "' + str.tostring(OrderID) + '", ' +
  '"comment": "Closing existing position and placing new OCO Market Long order" }';



          

OCO STOPLIMIT Entry Long

Raw JSON

 
{
  "alert": "OCO STOPLIMIT Entry Long",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "1",
  "entry_stop_price": "17907",
  "limit_price": "17915",
  "take_profit_price": "17940",
  "initial_stop_loss_price": "17890",
  "tif": "DAY",
  "oco_id": "x"
}
          

PAID/LOCKED INDICATOR/STRATEGY

 
{
  "alert": "OCO STOPLIMIT Entry Long",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "{{strategy.order.contracts}}",
  "entry_stop_price": "{{strategy.entry_stop_price}}",
  "limit_price": "{{strategy.limit_price}}",
  "take_profit_price": "17940",
  "initial_stop_loss_price": "17890",
  "tif": "DAY",
  "oco_id": "x",
  "comment": "Placing new OCO STOPLIMIT Entry Long order"
}
          

Open Source w/Strings

 
OCOStopLimitEntryLong = '{ "alert": "OCO STOPLIMIT Entry Long", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"entry_stop_price": "' + str.tostring(entry_stop_price) + '", ' +
  '"limit_price": "' + str.tostring(limit_price) + '", ' +
  '"take_profit_price": "' + str.tostring(take_profit_price) + '", ' +
  '"initial_stop_loss_price": "' + str.tostring(initial_stop_loss_price) + '", ' +
  '"tif": "DAY", ' +
  '"oco_id": "' + str.tostring(oco_id) + '", ' +
  '"comment": "Placing new OCO STOPLIMIT Entry Long order" }';
          

OCO STOPLIMIT Entry Short

Raw JSON

 
{
  "alert": "OCO STOPLIMIT Entry Short",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "1",
  "entry_stop_price": "17915",
  "limit_price": "17907",
  "take_profit_price": "17890",
  "initial_stop_loss_price": "17940",
  "tif": "DAY",
  "oco_id": "x"
}
          

PAID/LOCKED INDICATOR/STRATEGY

 
{
  "alert": "OCO STOPLIMIT Entry Short",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "{{strategy.order.contracts}}",
  "entry_stop_price": "{{strategy.entry_stop_price}}",
  "limit_price": "{{strategy.limit_price}}",
  "take_profit_price": "17890",
  "initial_stop_loss_price": "17940",
  "tif": "DAY",
  "oco_id": "x"
}
          

Open Source w/Strings

 
OCOStopLimitEntryShort = '{ "alert": "OCO STOPLIMIT Entry Short", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"entry_stop_price": "' + str.tostring(entry_stop_price) + '", ' +
  '"limit_price": "' + str.tostring(limit_price) + '", ' +
  '"take_profit_price": "' + str.tostring(take_profit_price) + '", ' +
  '"initial_stop_loss_price": "' + str.tostring(initial_stop_loss_price) + '", ' +
  '"tif": "DAY", ' +
  '"oco_id": "' + str.tostring(oco_id) + '" }';
          

OCO MARKETLIMIT Entry Long

Raw JSON

 
{
  "alert": "OCO STOPMARKET Entry Long",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "1",
  "entry_stop_price": "17910",
  "initial_stop_loss_price": "17890",
  "take_profit_price": "17930",
  "tif": "DAY",
  "oco_id": "x"
}
          

PAID/LOCKED INDICATOR/STRATEGY

 
{
  "alert": "OCO MARKETLIMIT Entry Short",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "{{strategy.order.contracts}}",
  "entry_stop_price": "{{strategy.entry_stop_price}}",
  "take_profit_price": "17890",
  "initial_stop_loss_price": "17930",
  "tif": "DAY",
  "oco_id": "x"
}
          

Open Source w/Strings

 
OCOMarketLimitEntryShort = '{ "alert": "OCO MARKETLIMIT Entry Short", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"entry_stop_price": "' + str.tostring(entry_stop_price) + '", ' +
  '"take_profit_price": "' + str.tostring(take_profit_price) + '", ' +
  '"initial_stop_loss_price": "' + str.tostring(initial_stop_loss_price) + '", ' +
  '"tif": "DAY", ' +
  '"oco_id": "' + str.tostring(oco_id) + '" }';
          

OCO MARKETLIMIT Entry Long

Raw JSON

 
{
  "alert": "OCO STOPMARKET Entry Short",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "1",
  "entry_stop_price": "17910",
  "initial_stop_loss_price": "17930",
  "take_profit_price": "17890",
  "tif": "DAY",
  "oco_id": "x"
}
          

PAID/LOCKED INDICATOR/STRATEGY

 
{
  "alert": "OCO MARKETLIMIT Entry Long",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "{{strategy.order.contracts}}",
  "entry_stop_price": "{{strategy.entry_stop_price}}",
  "take_profit_price": "17930",
  "initial_stop_loss_price": "17890",
  "tif": "DAY",
  "oco_id": "x"
}
          

Open Source w/Strings

 
OCOMarketLimitEntryLong = '{ "alert": "OCO MARKETLIMIT Entry Long", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"entry_stop_price": "' + str.tostring(entry_stop_price) + '", ' +
  '"take_profit_price": "' + str.tostring(take_profit_price) + '", ' +
  '"initial_stop_loss_price": "' + str.tostring(initial_stop_loss_price) + '", ' +
  '"tif": "DAY", ' +
  '"oco_id": "' + str.tostring(oco_id) + '" }';
          

OCO Limit Order Long(Not supported)

This order type is not supported at this time. 

{
    "alert": "OCO Limit Long",
    "account": "' + str.tostring(acct) + '",
    "ticker": "' + str.tostring(ticker) + '",
    "qty": "' + str.tostring(qty) + '",
    "limit_price": "' + str.tostring(entry_limit_long) + '",
    "take_profit_price": "' + str.tostring(take_profit_long) + '",
    "stop_price": "' + str.tostring(stop_price_long) + '",
    "tif": "DAY"
}
del>
        

OCO Limit Order Short(Not supported)

This order type is not supported at this time. 

{
    "alert": "OCO Limit Short",
    "account": "' + str.tostring(ticker) + '",
    "ticker": "NQ JUN24",
    "qty": "' + str.tostring(qty) + '",
    "limit_price": "' + str.tostring(entry_limit_short) + '",
    "take_profit_price": "' + str.tostring(take_profit_short) + '",
    "stop_price": "' + str.tostring(stop_price_short) + '",
    "tif": "DAY"
}

        

Cancel OCO Long TP and SL / Change OCO Long TP and SL (Used for setting TP/SL On Any Ordertype)

Raw JSON

{
    "alert": "Adjusted OCO Long",
    "account": "Sim1",
    "ticker": "NQ JUN24",
    "qty": "4",
    "take_profit_price": "15800.00",
    "stop_price": "15300.00",
    "tif": "DAY",
    "oco_id": "x"
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
    "alert": "Adjusted OCO {{strategy.order.action}}",
    "account": "Sim1",
    "ticker": "NQ JUN24",
    "qty": "{{strategy.order.contracts}}",
    "take_profit_price": "16000.00",
    "stop_price": "15500.00",
    "tif": "DAY",
    "oco_id": "{{strategy.order.id}}"
}
          

Open Source w/Strings

AdjustedOCOLong = '{ "alert": "Adjusted OCO Long", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"take_profit_price": "' + str.tostring(adjusted_take_profit_long) + '", ' +
  '"stop_price": "' + str.tostring(adjusted_stop_price_long) + '", ' +
  '"tif": "DAY", ' +
  '"oco_id": "' + str.tostring(OrderID) + '" }'


          

Cancel OCO Short TP and SL / Change OCO Short TP and SL(Used for setting TP/SL On Any Ordertype)

Raw JSON

{
    "alert": "Adjusted OCO Short",
    "account": "Sim1",
    "ticker": "NQ JUN24",
    "qty": "3",
    "take_profit_price": "15700.00",
    "stop_price": "16200.00",
    "tif": "DAY",
    "oco_id": "x"
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
    "alert": "Adjusted OCO {{strategy.order.action}}",
    "account": "Sim1",
    "ticker": "NQ JUN24",
    "qty": "{{strategy.order.contracts}}",
    "take_profit_price": "15500.00",
    "stop_price": "16000.00",
    "tif": "DAY",
    "oco_id": "{{strategy.order.id}}"
}
          

Open Source w/Strings

AdjustedOCOShort = '{ "alert": "Adjusted OCO Short", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"take_profit_price": "' + str.tostring(adjusted_take_profit_short) + '", ' +
  '"stop_price": "' + str.tostring(adjusted_stop_price_short) + '", ' +
  '"tif": "DAY", ' +
  '"oco_id": "' + str.tostring(OrderID) + '" }'


          

Cancel All OCO Bracket’s (Cancel TP’s and SL’s)

{
    "alert": "Cancel OCO"
}
        

ATM Strategy Market Long

Raw JSON

{
  "alert": "ATM Strategy Market",
  "strategy": "ATMStrategyName",
  "strategy_id": "UniqueName",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "5",
  "order_type": "MARKET",
  "alert": "buy",
  "limit_price": "",
  "stop_price": "",
  "tif": "DAY",
  "oco_id": "",
  "order_id": ""
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "ATM Strategy Market",
  "strategy": "ATMStrategyName",
  "strategy_id": "UniqueName",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "5",
  "order_type": "MARKET",
  "alert": "{{strategy.order.action}}",
  "limit_price": "",
  "stop_price": "",
  "tif": "DAY",
  "oco_id": "",
  "order_id": ""
}
          

Open Source w/Strings

ATMStrategyMarketLong = '{ "alert": "ATM Strategy Market", ' +
  '"strategy": "ATMStrategyName", ' +
  '"strategy_id": "UniqueName", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"order_type": "MARKET", ' +
  '"alert": "buy", ' +
  '"limit_price": "", ' +
  '"stop_price": "", ' +
  '"tif": "DAY", ' +
  '"oco_id": "", ' +
  '"order_id": "' + str.tostring(OrderID) + '" }'


          

ATM Strategy Market Short

Raw JSON

{
  "alert": "ATM Strategy Market",
  "strategy": "ATMStrategyName",
  "strategy_id": "UniqueName",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "5",
  "order_type": "MARKET",
  "alert": "sell",
  "limit_price": "",
  "stop_price": "",
  "tif": "DAY",
  "oco_id": "",
  "order_id": ""
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "ATM Strategy Market",
  "strategy": "ATMStrategyName",
  "strategy_id": "UniqueName",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "5",
  "order_type": "MARKET",
  "alert": "{{strategy.order.action}}",
  "limit_price": "",
  "stop_price": "",
  "tif": "DAY",
  "oco_id": "",
  "order_id": ""
}
          

Open Source w/Strings

ATMStrategyMarketShort = '{ "alert": "ATM Strategy Market", ' +
  '"strategy": "ATMStrategyName", ' +
  '"strategy_id": "UniqueName", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"order_type": "MARKET", ' +
  '"alert": "sell", ' +
  '"limit_price": "", ' +
  '"stop_price": "", ' +
  '"tif": "DAY", ' +
  '"oco_id": "", ' +
  '"order_id": "' + str.tostring(order_id) + '" }'


          

ATM Strategy Limit Long

Raw JSON

{
  "alert": "ATM Strategy Limit Long",
  "strategy": "ATMStrategyName",
  "strategy_id": "UniqueName",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "5",
  "order_type": "Limit",
  "limit_price": "15980",
  "stop_price": "",
  "tif": "DAY",
  "oco_id": "",
  "order_id": ""
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "ATM Strategy Limit {{strategy.order.action}}",
  "strategy": "ATMStrategyName",
  "strategy_id": "UniqueName",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "5",
  "order_type": "Limit",
  "limit_price": "15980",
  "stop_price": "",
  "tif": "DAY",
  "oco_id": "",
  "order_id": ""
}
          

Open Source w/Strings

ATMStrategyLimitLong = '{ "alert": "ATM Strategy Limit Long", ' +
  '"strategy": "ATMStrategyName", ' +
  '"strategy_id": "UniqueName", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"order_type": "Limit", ' +
  '"limit_price": "' + str.tostring(limitlongprice) + '", ' +
  '"stop_price": "", ' +
  '"tif": "DAY", ' +
  '"oco_id": "", ' +
  '"order_id": "' + str.tostring(order_id) + '" }'


          

ATM Strategy Limit Short

Raw JSON

{
  "alert": "ATM Strategy Limit Short",
  "strategy": "ATMStrategyName",
  "strategy_id": "UniqueName",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "5",
  "order_type": "Limit",
  "limit_price": "15980",
  "stop_price": "",
  "tif": "DAY",
  "oco_id": "",
  "order_id": ""
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "ATM Strategy Limit {{strategy.order.action}}",
  "strategy": "ATMStrategyName",
  "strategy_id": "UniqueName",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "5",
  "order_type": "Limit",
  "limit_price": "15980",
  "stop_price": "",
  "tif": "DAY",
  "oco_id": "",
  "order_id": ""
}
          

Open Source w/Strings

ATMStrategyLimitShort = '{ "alert": "ATM Strategy Limit Short", ' +
  '"strategy": "ATMStrategyName", ' +
  '"strategy_id": "UniqueName", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"order_type": "Limit", ' +
  '"limit_price": "' + str.tostring(limitshortprice) + '", ' +
  '"stop_price": "", ' +
  '"tif": "DAY", ' +
  '"oco_id": "", ' +
  '"order_id": "' + str.tostring(order_id) + '" }'


          

Close Then ATM Strategy Market Long/Buy (For closing short and opening long/flipping)

Raw JSON

{
  "alert": "Close Then ATM Strategy Market",
  "strategy": "ATMStrategyName",
  "strategy_id": "UniqueName",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "5",
  "order_type": "MARKET",
  "alert": "buy",
  "limit_price": "",
  "stop_price": "",
  "tif": "DAY",
  "oco_id": "",
  "order_id": ""
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "Close Then ATM Strategy Market",
  "strategy": "ATMStrategyName",
  "strategy_id": "UniqueName",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "5",
  "order_type": "MARKET",
  "alert": "{{strategy.order.action}}",
  "limit_price": "",
  "stop_price": "",
  "tif": "DAY",
  "oco_id": "",
  "order_id": ""
}
          

Open Source w/Strings

CloseThenATMStrategyMarket = '{ "alert": "Close Then ATM Strategy Market", ' +
  '"strategy": "ATMStrategyName", ' +
  '"strategy_id": "UniqueName", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"order_type": "MARKET", ' +
  '"alert": "buy", ' +
  '"limit_price": "", ' +
  '"stop_price": "", ' +
  '"tif": "DAY", ' +
  '"oco_id": "", ' +
  '"order_id": "' + str.tostring(order_id) + '" }'


          

Close Then ATM Strategy Market Short/Sell(For closing long and opening short/flipping)

Raw JSON

{
  "alert": "Close Then ATM Strategy Market",
  "strategy": "ATMStrategyName",
  "strategy_id": "UniqueName",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "5",
  "order_type": "MARKET",
  "alert": "sell",
  "limit_price": "",
  "stop_price": "",
  "tif": "DAY",
  "oco_id": "",
  "order_id": ""
}
          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "Close Then ATM Strategy Market",
  "strategy": "ATMStrategyName",
  "strategy_id": "UniqueName",
  "account": "Sim101",
  "ticker": "NQ JUN24",
  "qty": "5",
  "order_type": "MARKET",
  "alert": "{{strategy.order.action}}",
  "limit_price": "",
  "stop_price": "",
  "tif": "DAY",
  "oco_id": "",
  "order_id": ""
}
          

Open Source w/Strings

CloseThenATMStrategyMarket = '{ "alert": "Close Then ATM Strategy Market", ' +
  '"strategy": "ATMStrategyName", ' +
  '"strategy_id": "UniqueName", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"ticker": "' + str.tostring(ticker) + '", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"order_type": "MARKET", ' +
  '"alert": "sell", ' +
  '"limit_price": "", ' +
  '"stop_price": "", ' +
  '"tif": "DAY", ' +
  '"oco_id": "", ' +
  '"order_id": "' + str.tostring(order_id) + '" }'


          

ATM Limit Order Cancel

Raw JSON

{
    "alert": "Stop ATM Strategy",
    "strategy": "YourStrategyName",
    "strategy_id": "YourStrategyID"
}

          

PAID/LOCKED INDICATOR/STRATEGY

{
    "alert": "Stop ATM Strategy",
    "strategy": "YourStrategyName",
    "strategy_id": "YourStrategyID"
}

          

Open Source w/Strings

StopATMStrategy = '{ "alert": "Stop ATM Strategy", ' +
  '"strategy": "' + str.tostring(strategy) + '", ' +
  '"strategy_id": "' + str.tostring(strategyid) + '" }'



          

ATM Limit Order Cancel

(BETA) ATM TD Ameritrade Options (SPY Only)

Raw JSON

{
  "alert": "atm strategy options",
  "strategy": "ATMSPY",
  "account": "SimTDA",
  "symbol": "SPY",
  "contractType": "CALL",
  "strikeCount": "1",
  "range": "ITM",
  "fromDate": "20240214",
  "toDate": "20240214",
  "expMonth": "ALL",
  "optionType": "ALL",
  "alert": "buy",
  "qty": "1",
  "order_type": "MARKET",
  "limit_price": "",
  "stop_price": "",
  "tif": "DAY",
  "oco_id": "",
  "order_id": ""
}

          

PAID/LOCKED INDICATOR/STRATEGY

{
  "alert": "atm strategy options",
  "strategy": "ATMSPY",
  "account": "SimTDA",
  "symbol": "SPY",
  "contractType": "CALL",
  "strikeCount": "1",
  "range": "ITM",
  "fromDate": "20240214",
  "toDate": "20240214",
  "expMonth": "ALL",
  "optionType": "ALL",
  "alert": "buy",
  "qty": "{{strategy.order.contracts}}",
  "order_type": "MARKET",
  "tif": "DAY"
}





          

Open Source w/Strings

ATMSPYOptionsStrategy = '{ "alert": "atm strategy options", ' +
  '"strategy": "ATMSPY", ' +
  '"account": "' + str.tostring(acct) + '", ' +
  '"symbol": "SPY", ' +
  '"contractType": "CALL", ' +
  '"strikeCount": "' + str.tostring(strikeCount) + '", ' +
  '"range": "ITM", ' +
  '"fromDate": "' + str.tostring(fromDate) + '", ' +
  '"toDate": "' + str.tostring(toDate) + '", ' +
  '"expMonth": "ALL", ' +
  '"optionType": "ALL", ' +
  '"alert": "buy", ' +
  '"qty": "' + str.tostring(qty) + '", ' +
  '"order_type": "MARKET", ' +
  '"tif": "DAY" }';







          

Pinescript Examples

1. Simple Market Long

//@version=5
strategy("Simple Market Long", shorttitle="SML", overlay=true)

longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
acct = "Sim101"
ticker = "NQ JUN24"
qty = 1

MarketLong = '{ "alert": "Market Long", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '", "qty": "' + str.tostring(qty) + '" }'
CloseAll = '{ "alert": "Close All", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '" }'

if (longCondition)
  strategy.entry("Long", strategy.long, alert_message = MarketLong)
  strategy.exit("Long Exit", from_entry="Long", profit=10, loss=10, alert_message = CloseAll)
      

1.1 Simple Market Short

//@version=5
strategy("Simple Market Short", shorttitle="SMS", overlay=true)

shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
acct = "Sim101"
ticker = "NQ JUN24"
qty = 1

MarketShort = '{ "alert": "Market Short", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '", "qty": "' + str.tostring(qty) + '" }'
CloseAll = '{ "alert": "Close All", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '" }'

if (shortCondition)
    strategy.entry("Short", strategy.short, alert_message = MarketShort)
    strategy.exit("Short Exit", from_entry="Short", profit=10, loss=10, alert_message = CloseAll)

        

2. Simple Close All Positions

//@version=5
strategy("Close All Positions Example", shorttitle="CAE", overlay=true)

closeAllCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
acct = "Sim101"
ticker = "NQ JUN24"

CloseAll = '{ "alert": "Close All", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '" }'

if (closeAllCondition)
    strategy.close_all(alert_message = CloseAll)

        

3. Long with Limit Order

//@version=5
strategy("Long with Limit Order", shorttitle="LLO", overlay=true)

longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
acct = "Sim101"
ticker = "NQ JUN24"
qty = 1

limit_long_price = close - 5
Limit Long = '{ "alert": "Limit Long", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '", "qty": "' + str.tostring(qty) + '", "limit_price": "' + str.tostring(limit_long_price) + '", "tif": "DAY" }'
CloseAll = '{ "alert": "Close All", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '" }'

if (longCondition)
    strategy.entry("Long", strategy.long, limit=limit_price, alert_message = Limit Long)
    strategy.exit("Long Exit", from_entry="Long", profit=10, loss=10, alert_message = CloseAll)

        

3.1 Short with Limit Order

//@version=5
strategy("Short with Limit Order", shorttitle="SLO", overlay=true)

shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
acct = "Sim101"
ticker = "NQ JUN24"
limit_short_price = close + 5

Limit Short = '{ "alert": "Limit Short", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '", "qty": "' + str.tostring(qty) + '", "order_type": "LIMIT", "limit_price": "' + str.tostring(limit_short_price) + '", "tif": "DAY" }'
CloseAll = '{ "alert": "Close All", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '" }'

if (shortCondition)
    strategy.entry("Short", strategy.short, limit=limit_price, alert_message = Limit Short)
    strategy.exit("Short Exit", from_entry="Short", profit=10, loss=10, alert_message = CloseAll)

        

4. Long with Stop Market Order

//@version=5
strategy("Long with Stop Market Order", shorttitle="LSMO", overlay=true)

var float entryPrice = na
var float stopPrice = na

longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
acct = "Sim101"
ticker = "NQ JUN24"
qty = 1

LongStopMarket = '{ "alert": "Long", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '", "qty": "' + str.tostring(qty) + '", "order_type": "STOPMARKET", "stop_price": "' + str.tostring(stopPrice) + '", "tif": "DAY" }'
CloseAll = '{ "alert": "Close All", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '" }'


if (longCondition)
    entryPrice := close
    stopPrice := entryPrice - 10
    strategy.entry("Long", strategy.long, alert_message = LongStopMarket)


if (na(stopPrice) == false) and (low <= stopPrice)
    strategy.close("Long", alert_message = CloseAll)
    entryPrice := na
    stopPrice := na


        

4.1 Short with Stop Market Order

//@version=5
strategy("Short with Stop Market Order", shorttitle="SSMO", overlay=true)

var float entryPrice = na
var float stopPrice = na

shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
acct = "Sim101"
ticker = "NQ JUN24"
qty = 1

ShortStopMarket = '{ "alert": "Short", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '", "qty": "' + str.tostring(qty) + '", "order_type": "STOPMARKET", "stop_price": "' + str.tostring(Market_Short_stopPrice) + '", "tif": "DAY" }'
CloseAll = '{ "alert": "Close All", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '" }'


if (shortCondition)
    entryPrice := close
    stopPrice := entryPrice + 10
    strategy.entry("Short", strategy.short, alert_message = ShortStopMarket)


if (na(stopPrice) == false) and (high >= stopPrice)
    strategy.close("Short", alert_message = CloseAll)
    entryPrice := na
    stopPrice := na


        

5. Long with Stop Limit Order

//@version=5
strategy("Long with Stop Limit Order", shorttitle="LSLO", overlay=true)

var float entryPrice = na
var float stopPrice_Long = na
var float limitPrice_Long = na

longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))

acct = "Sim101"
ticker = "NQ JUN24"
qty = 1

LongStopLimit = '{ "alert": "Long", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '", "qty": "' + str.tostring(qty) + '", "order_type": "STOPLIMIT", "limit_price": "' + str.tostring(limitPrice_Long) + '", "stop_price": "' + str.tostring(stopPrice_Long) + '", "tif": "DAY" }'
CloseAll = '{ "alert": "Close All", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '" }'

if (longCondition)
    entryPrice := close
    stopPrice_Long := entryPrice - 5
    limitPrice_Long := stopPrice_Long - 1
    strategy.entry("Long", strategy.long, alert_message = LongStopLimit)
    strategy.exit("Long Exit", from_entry="Long", limit=limitPrice_Long, stop=stopPrice_Long, alert_message=CloseAll)


        

5.1 Short with Stop Limit Order

//@version=5
strategy("Short with Stop Limit Order", shorttitle="SSLO", overlay=true)

var float entryPrice = na
var float stopPrice_Short = na
var float limitPrice_Short = na

shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))

acct = "Sim101"
ticker = "NQ JUN24"
qty = 1

ShortStopLimit = '{ "alert": "Short", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '", "qty": "' + str.tostring(qty) + '", "order_type": "STOPLIMIT", "limit_price": "' + str.tostring(limitPrice_Short) + '", "stop_price": "' + str.tostring(stopPrice_Short) + '", "tif": "DAY" }'
CloseAll = '{ "alert": "Close All", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '" }'
    

if (shortCondition)
    entryPrice := close
    stopPrice_Short := entryPrice + 5
    limitPrice_Short := stopPrice_Short + 1
    strategy.entry("Short", strategy.short, alert_message = ShortStopLimit)
    strategy.exit("Short Exit", from_entry="Short", limit=limitPrice_Short, stop=stopPrice_Short, alert_message=CloseAll)

        

6.1 OCO Market Order Long

//@version=5
strategy("OCO Market Order Long Example", shorttitle="OCOMOL", overlay=true)

// Input fields for account details and quantity
acct = input("Sim101", title="Account")
ticker = input("NQ JUN24", title="Ticker")
qty = input(1, title="Quantity")

// Tracking the trade direction, where 1 is long, -1 is short, and 0 is flat
var int tradeDirection = 0

// Storing the entry price for calculating take profit and stop loss levels
var float entryPrice = na

// Setting the initial take profit and stop loss prices to Not-a-Number
var float takeProfitPrice = na
var float stopLossPrice = na

// Condition to enter a long trade
longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
// Exiting the trade based on take profit or stop loss
exitCondition = (close > takeProfitPrice) or (close < stopLossPrice)

if (longCondition)
    entryPrice := close
    takeProfitPrice := entryPrice + 10 // Example: setting take profit level 10 points above entry
    stopLossPrice := entryPrice - 10  // Example: setting stop loss level 10 points below entry
    // Constructing the JSON alert for the OCO Market Long order
    OCOMarketLong = '{ "alert": "OCO Market Long", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '", "qty": "' + str.tostring(qty) + '", "take_profit_price": "' + str.tostring(takeProfitPrice) + '", "stop_price": "' + str.tostring(stopLossPrice) + '", "tif": "DAY" }'
    // Entering the long position
    strategy.entry("Long", strategy.long, qty=qty, alert_message = OCOMarketLong)
    tradeDirection := 1

if (exitCondition and tradeDirection == 1)
    // Closing the long position
    strategy.close("Long", qty=qty)
    // Resetting trade direction
    tradeDirection := 0
    // Resetting entry price, take profit, and stop loss prices
    entryPrice := na
    takeProfitPrice := na
    stopLossPrice := na

    

6.1 OCO Market Order Short

//@version=5
strategy("OCO Market Order Short Example", shorttitle="OCOMOS", overlay=true)
// Input fields for account details and quantity
acct = input("Sim101", title="Account")
ticker = input("NQ JUN24", title="Ticker")
qty = input(1, title="Quantity")

// Tracking the trade direction, where 1 is long, -1 is short, and 0 is flat
var int tradeDirection = 0

// Storing the entry price for calculating take profit and stop loss levels
var float entryPrice = na

// Setting the initial take profit and stop loss prices to Not-a-Number
var float takeProfitPrice = na
var float stopLossPrice = na

// Condition to enter a short trade
shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
// Exiting the trade based on take profit or stop loss
exitCondition = (close < takeProfitPrice) or (close > stopLossPrice)

if (shortCondition)
entryPrice := close
takeProfitPrice := entryPrice - 10 // Example: setting take profit level 10 points below entry
stopLossPrice := entryPrice + 10 // Example: setting stop loss level 10 points above entry
// Constructing the JSON alert for the OCO Market Short order
OCOMarketShort = '{ "alert": "OCO Market Short", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '", "qty": "' + str.tostring(qty) + '", "take_profit_price": "' + str.tostring(takeProfitPrice) + '", "stop_price": "' + str.tostring(stopLossPrice) + '", "tif": "DAY" }'
// Entering the short position
strategy.entry("Short", strategy.short, qty=qty, alert_message = OCOMarketShort)
tradeDirection := -1

if (exitCondition and tradeDirection == -1)
// Closing the short position
strategy.close("Short", qty=qty)
// Resetting trade direction
tradeDirection := 0
// Resetting entry price, take profit, and stop loss prices
entryPrice := na
takeProfitPrice := na
stopLossPrice := na

7. OCO Limit Order Long (Not Supported)

//@version=5
//This order type is not supported at this time. 
//This order type is not supported at this time. 
//This order type is not supported at this time. 
strategy("OCO Limit Order Long Example", shorttitle="OCOLOL", overlay=true)
// Input fields for account details and quantity
acct = input("Sim101", title="Account")
ticker = input("NQ JUN24", title="Ticker")
qty = input(1, title="Quantity")

// Tracking the trade direction, where 1 is long, -1 is short, and 0 is flat
var int tradeDirection = 0

// Storing the entry price for calculating take profit and stop loss levels
var float entryPrice = na

// Setting the initial take profit and stop loss prices to Not-a-Number
var float takeProfitPrice = na
var float stopLossPrice = na
var float entryLimit Long = na

// Condition to enter a long trade
longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
// Exiting the trade based on take profit or stop loss
exitCondition = (close > takeProfitPrice) or (close < stopLossPrice)

if (longCondition)
entryLimit Long := close + 5 // Example: setting limit order 5 points above current price
entryPrice := entryLimit Long
takeProfitPrice := entryPrice + 10 // Example: setting take profit level 10 points above entry
stopLossPrice := entryPrice - 10 // Example: setting stop loss level 10 points below entry
// Constructing the JSON alert for the OCO Limit Long order
OCOLimit Long = '{ "alert": "OCO Limit Long", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '", "qty": "' + str.tostring(qty) + '", "limit_price": "' + str.tostring(entryLimit Long) + '", "take_profit_price": "' + str.tostring(takeProfitPrice) + '", "stop_price": "' + str.tostring(stopLossPrice) + '", "tif": "DAY" }'
// Entering the long position
strategy.entry("Long", strategy.long, qty=qty, limit=entryLimit Long, alert_message = OCOLimit Long)
tradeDirection := 1

if (exitCondition and tradeDirection == 1)
// Closing the long position
strategy.close("Long", qty=qty)
// Resetting trade direction
tradeDirection := 0
// Resetting entry price, take profit, and stop loss prices
entryPrice := na
takeProfitPrice := na
stopLossPrice := na

7.1 OCO Limit Order Short (Not Supported)

//@version=5
//This order type is not supported at this time. 
//This order type is not supported at this time. 
//This order type is not supported at this time. 
strategy("OCO Limit Order Short Example", shorttitle="OCOLOS", overlay=true)
// Input fields for account details and quantity
acct = input("Sim101", title="Account")
ticker = input("NQ JUN24", title="Ticker")
qty = input(1, title="Quantity")

// Tracking the trade direction, where 1 is long, -1 is short, and 0 is flat
var int tradeDirection = 0

// Storing the entry price for calculating take profit and stop loss levels
var float entryPrice = na

// Setting the initial take profit and stop loss prices to Not-a-Number
var float takeProfitPrice = na
var float stopLossPrice = na
var float entryLimit Short = na

// Condition to enter a short trade
shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
// Exiting the trade based on take profit or stop loss
exitCondition = (close < takeProfitPrice) or (close > stopLossPrice)

if (shortCondition)
entryLimit Short := close - 5 // Example: setting limit order 5 points below current price
entryPrice := entryLimit Short
takeProfitPrice := entryPrice - 10 // Example: setting take profit level 10 points below entry
stopLossPrice := entryPrice + 10 // Example: setting stop loss level 10 points above entry
// Constructing the JSON alert for the OCO Limit Short order
OCOLimit Short = '{ "alert": "OCO Limit Short", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '", "qty": "' + str.tostring(qty) + '", "limit_price": "' + str.tostring(entryLimit Short) + '", "take_profit_price": "' + str.tostring(takeProfitPrice) + '", "stop_price": "' + str.tostring(stopLossPrice) + '", "tif": "DAY" }'
// Entering the short position
strategy.entry("Short", strategy.short, qty=qty, limit=entryLimit Short, alert_message = OCOLimit Short)
tradeDirection := -1

if (exitCondition and tradeDirection == -1)
// Closing the short position
strategy.close("Short", qty=qty)
// Resetting trade direction
tradeDirection := 0
// Resetting entry price, take profit, and stop loss prices
entryPrice := na
takeProfitPrice := na
stopLossPrice := na

8. Cancel OCO Long TP and SL / Change OCO Long TP and SL

//@version=5
strategy("Adjust OCO Long TP and SL", shorttitle="AOCOLTS", overlay=true)
// Input fields for account details and quantity
acct = input("Sim101", title="Account")
ticker = input("NQ JUN24", title="Ticker")
qty = input(1, title="Quantity")

// Tracking the trade direction, where 1 is long, -1 is short, and 0 is flat
var int tradeDirection = 0

// Storing the entry price for calculating take profit and stop loss levels
var float entryPrice = na

// Setting the initial take profit and stop loss prices to Not-a-Number
var float takeProfitPrice = na
var float stopLossPrice = na
var float adjustedTakeProfitLong = na
var float adjustedStopPriceLong = na

// Conditions to enter and exit the trade
longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
adjustCondition = ta.crossover(ta.sma(close, 7), ta.sma(close, 14))

if (longCondition)
entryPrice := close
takeProfitPrice := entryPrice + 10 // Example: setting take profit level 10 points above entry
stopLossPrice := entryPrice - 10 // Example: setting stop loss level 10 points below entry
// Constructing the JSON alert for the initial OCO Long order
OCOLong = '{ "alert": "OCO Long", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '", "qty": "' + str.tostring(qty) + '", "take_profit_price": "' + str.tostring(takeProfitPrice) + '", "stop_price": "' + str.tostring(stopLossPrice) + '", "tif": "DAY" }'
// Entering the long position
strategy.entry("Long", strategy.long, qty=qty, alert_message = OCOLong)
tradeDirection := 1

if (adjustCondition and tradeDirection == 1)
// Adjusting take profit and stop loss levels
adjustedTakeProfitLong := close + 15 // Example: adjusting take profit level to 15 points above current price
adjustedStopPriceLong := close - 5 // Example: adjusting stop loss level to 5 points below current price
// Constructing the JSON alert for the adjusted OCO Long order
adjustedOCOLong = '{ "alert": "Adjusted OCO Long", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '", "qty": "' + str.tostring(qty) + '", "take_profit_price": "' + str.tostring(adjustedTakeProfitLong) + '", "stop_price": "' + str.tostring(adjustedStopPriceLong) + '", "tif": "DAY" }'
// Updating the long position
strategy.exit("Adjust Long TP and SL", "Long", qty=qty, limit=adjustedTakeProfitLong, stop=adjustedStopPriceLong, alert_message=adjustedOCOLong)

8.1 Cancel OCO Short TP and SL / Change OCO Short TP and SL

//@version=5
strategy("Adjust OCO Short TP and SL", shorttitle="AOCOSTS", overlay=true)
// Input fields for account details and quantity
acct = input("Sim101", title="Account")
ticker = input("NQ JUN24", title="Ticker")
qty = input(1, title="Quantity")

// Tracking the trade direction, where 1 is long, -1 is short, and 0 is flat
var int tradeDirection = 0

// Storing the entry price for calculating take profit and stop loss levels
var float entryPrice = na

// Setting the initial take profit and stop loss prices to Not-a-Number
var float takeProfitPrice = na
var float stopLossPrice = na
var float adjustedTakeProfitShort = na
var float adjustedStopPriceShort = na

// Conditions to enter and exit the trade
shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
adjustCondition = ta.crossunder(ta.sma(close, 7), ta.sma(close, 14))

if (shortCondition)
entryPrice := close
takeProfitPrice := entryPrice - 10 // Example: setting take profit level 10 points below entry
stopLossPrice := entryPrice + 10 // Example: setting stop loss level 10 points above entry
// Constructing the JSON alert for the initial OCO Short order
OCOShort = '{ "alert": "OCO Short", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '", "qty": "' + str.tostring(qty) + '", "take_profit_price": "' + str.tostring(takeProfitPrice) + '", "stop_price": "' + str.tostring(stopLossPrice) + '", "tif": "DAY" }'
// Entering the short position
strategy.entry("Short", strategy.short, qty=qty, alert_message = OCOShort)
tradeDirection := -1

if (adjustCondition and tradeDirection == -1)
// Adjusting take profit and stop loss levels
adjustedTakeProfitShort := close - 15 // Example: adjusting take profit level to 15 points below current price
adjustedStopPriceShort := close + 5 // Example: adjusting stop loss level to 5 points above current price
// Constructing the JSON alert for the adjusted OCO Short order
adjustedOCOShort = '{ "alert": "Adjusted OCO Short", "account": "' + str.tostring(acct) + '", "ticker": "' + str.tostring(ticker) + '", "qty": "' + str.tostring(qty) + '", "take_profit_price": "' + str.tostring(adjustedTakeProfitShort) + '", "stop_price": "' + str.tostring(adjustedStopPriceShort) + '", "tif": "DAY" }'
// Updating the short position
strategy.exit("Adjust Short TP and SL", "Short", qty=qty, limit=adjustedTakeProfitShort, stop=adjustedStopPriceShort, alert_message=adjustedOCOShort)

9. Cancel All OCO Bracket’s (Cancel TP’s and SL’s)

//@version=5
strategy("Cancel All OCO Brackets", shorttitle="CAOCOB", overlay=true)
// Input fields for account details and quantity
acct = input("Sim101", title="Account")
ticker = input("NQ JUN24", title="Ticker")
qty = input(1, title="Quantity")

// Tracking the trade direction, where 1 is long, -1 is short, and 0 is flat
var int tradeDirection = 0

// Conditions to enter, exit, and adjust the trade
longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
cancelOCOCondition = ta.crossover(ta.sma(close, 7), ta.sma(close, 14))

// Constructing the JSON alert for cancelling all OCO brackets
cancelOCO = '{ "alert": "Cancel OCO" }'

if (longCondition)
// Entering a long position
strategy.entry("Long", strategy.long, qty=qty)
tradeDirection := 1

if (shortCondition)
// Entering a short position
strategy.entry("Short", strategy.short, qty=qty)
tradeDirection := -1

if (cancelOCOCondition)
// Cancelling all OCO brackets
strategy.close("Long", alert_message=cancelOCO)
strategy.close("Short", alert_message=cancelOCO)
tradeDirection := 0

plotshape(series=longCondition, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="Buy")
plotshape(series=shortCondition, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="Sell")
plotshape(series=cancelOCOCondition, title="Cancel OCO Signal", location=location.abovebar, color=color.purple, style=shape.labeldown, text="Cancel OCO")

Disclaimer for Ninja-View.com:

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

  • 2024 NinjaView. All rights reserved.
  • [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.