C 2 Advanced Order Types Pine Script
C 2 Advanced Order Types Pine Script
0 at
https://mozilla.org/MPL/2.0/
// © Gammapips
// Version: 0.339
// Published: 21.10.20 17:48:45
// Input Settings
fastSMALen = input( 15 )
slowSMALen = input( 50 )
// Strategy
slowSMA = sma(close, slowSMALen)
fastSMA = sma(close, fastSMALen)
goShort = crossunder(fastSMA, slowSMA)
goLong = crossover(fastSMA, slowSMA)
plot(slowSMA, title="Slow SMA", color=color.purple, linewidth=4)
plot(fastSMA, title="Fast SMA", color=color.orange, linewidth=2)
// 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
// 🏹 How to place a Take Profit
// 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
// 📚 A Take Profit (T/P) is a type of pending order that specifies the exact price
at which to close out an open position for a profit.
// 📚 If the market price does not reach the TP price, the take-profit order does
not get filled.
// https://www.investopedia.com/terms/t/take-profitorder.asp
// We can place take profit orders by using the "stop" and "limit" arguments with
strategy.exit() or strategy.order()
// Syntax for strategy.exit()
// strategy.exit(id, from_entry, qty, qty_percent, profit, limit, loss, stop,
trail_price,
// trail_points, trail_offset, oca_name, comment, when,
alert_message)
// // Use built in variable to show us our entry price. Works well for 1 order,
more entries will show an avg.
// tpLevel = strategy.position_avg_price * (1 + i_tp_pct)
// // The great thing about using the strategy.position_avg_price variable is we
will have a nice TP line
// // that will only plot when we want it to. This is because when the market is
flat the variable will return "na" which will not plot.
// // // 👇 Method 3: Really Complex. Make the strategy long only and sending a
short order, which will convert to a close. Why would you do this?
// // strategy.risk.allow_entry_in(strategy.direction.long)
// // if strategy.position_size > 0
// // strategy.entry(id="TP", long=false, limit=tpLevel)
// // // 👆
// // // 👇 Method 4: Works, kinda, but remember it's a market exit. Notice how this
doesn't execute until the next bar.
// // if high >= tpLevel
// // strategy.close("Go Long")
// // // 👆
// 📉📉📉 📉📉📉
// // 👇 Method 2: Complex. Use this if you can't use method 1. Notice the differnet
tick variable from Method 1.
// if strategy.position_size > 0 // If we don't check this, then an order will be
sent every bar
// strategy.order(id="TP", long=false, limit=tpLevel) // It's also important
to note that this order will only close 1 contract by default
// // 👆
// 👇 Method 4: Works, kinda, but remember it's a market exit. Notice how this
doesn't execute until the next bar.
// if high >= tpLevel
// strategy.close("Go Long")
// // 👆
// // A lot of people ask "What's the difference between strategy.close and
strategy.exit?"", so ive included this example to show you.
// 📉📉📉 📉📉📉
// 🎯
// 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
// 🏹 How to Place a Stop Loss
// 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
// 📚 A stop-loss (SL) order is an order to buy or sell a security when it reaches a
certain price.
// 📚 Stop-loss orders are designed to limit an investor’s loss on a position.
// 📚 When the price falls below the stop price the order becomes a market order and
executes at the next available price.
// https://www.investopedia.com/terms/s/stop-lossorder.asp
// We can place stop loss orders by using the "stop" and "limit" arguments with
strategy.exit() or strategy.order()
// NOTE: Do not be confused by the "stop" argument also on strategy.entry() This is
not for a stop loss, but for a stop order.
// // 👇 Method 4: Works, kinda, but remember it's a market exit. Notice how this
doesn't execute until the next bar.
// // if low <= slLevel
// // strategy.close("Go Long")
// // 👆
// 📉📉📉 📉📉📉
// 🎯
// 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
// 🏹 How to place a Stop-Limit Order
// 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
// Our two functions for placing market orders, limit orders and stop orders can
also place stop-limit orders.
// To send a stop-limit order we need to use both stop and limit arguments:
// 📉📉📉 📉📉📉
// OCOs are good for trading breakouts and volatility. They will also keep the
order book free of any forgotten orders.
// OCO orders can also be referred to as OCA orders. One-Cancels-All orders can
work exactly like OCO orders, but they are not limited to just a pair of orders.
// OCA orders encompass OCO orders. So when placing OCO orders we will use the OCA
argument.
// // // 👆
// // // 👇 Method 1: Simple way without OCO. Normally you would want to do this.
// // strategy.exit(id="Simple Exit", from_entry="Go Long", limit=TP, stop=SL)
// // // 👆
// 📉📉📉 📉📉📉
// 🎯
// 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
// 🏹 How to place a trailing stop
// 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
// What is a trailing Stop?
// A trailing stop is a modification of a typical stop order
// that can be set at a defined percentage or dollar amount away from a security's
current market price.
// For a long position, an investor places a trailing stop loss below the current
market price.
// For a short position, an investor places the trailing stop above the current
market price
// // Note: Sometimes the Stop and Activation Level won't plot if they are hit in
the same bar.
// longCondition = goLong and (time >= testPeriodStart)
// if longCondition
// strategy.entry("Go Long", strategy.long)
// strategy.exit("Trailing Stop", "Go Long", trail_points=trailPoints,
trail_offset=trailOffset, when=testType == 'Points')
// strategy.exit("Trailing Stop", "Go Long", trail_price=trailLevel,
trail_offset=trailOffset, when=testType == 'Price')
// 📉📉📉 📉📉📉
// 🎯