// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © syam-mohan-vs @ T7 - wwww.t7wealth.com www.t7trade.com //This educational code describes the fundamentals of pine scripting language and integration with Indian discount broker Dhan. This strategy has not been tested or recommended for live trading. //@version=5 strategy("Pine & Dhan - Moving Average Crossover Strategy", overlay=true) //Remove excess signals exrem(condition1, condition2) => temp = false temp := na(temp[1]) ? false : not temp[1] and condition1 ? true : temp[1] and condition2 ? false : temp[1] ta.change(temp) == true ? true : false // Define MA period ma_period = input(20, title = "MA Length") opt_level = input.int(1, title = "Depth", minval=0, maxval=20, step=1, tooltip='ITM depth can be varied from 1 to 20.') // Define target and stop loss levels target_percentage = input.float(title="Target Profit (%)", defval=2.0) stop_loss_percentage = input.float(title="Stop Loss (%)", defval=1.0) // Calculate the MA ma = ta.sma(close, ma_period) // Entry conditions long_entry = close >= ma short_entry = close < ma // Calculate target and stop loss prices target_price = long_entry ? strategy.position_avg_price + (close * (target_percentage / 100)) : strategy.position_avg_price - (close * (target_percentage / 100)) stop_loss_price = short_entry ? strategy.position_avg_price + (close * (stop_loss_percentage/ 100)) : strategy.position_avg_price - (close * (stop_loss_percentage / 100)) long_entry := exrem(long_entry,short_entry) short_entry := exrem(short_entry,long_entry) //close price of last confirmed bar is used to compute option strike below upBound_buy = close upBound_sell = 0.0 downBound_short = close downBound_cover = 0.0 //Ensure that the open contract is the one that is sold while position exit if(strategy.position_size > 0 and strategy.position_size[1] == 0) upBound_sell := upBound_buy[1] else upBound_sell := upBound_sell[1] if(strategy.position_size < 0 and strategy.position_size[1] == 0) downBound_cover := downBound_short[1] else downBound_cover := downBound_cover[1] //auto strike selection based on user selected depth callbuytext = math.floor(upBound_buy/ 100) * 100 - opt_level * 100 callselltext = math.floor(upBound_sell/ 100) * 100 - opt_level * 100 putbuytext = math.ceil(downBound_short / 100) * 100 + opt_level * 100 putselltext = math.ceil(downBound_cover / 100) * 100 + opt_level * 100 // Plot the MA plot(ma, color=color.blue, linewidth=2, title="MA") // Plot the entry and exit signals plotshape(long_entry, style=shape.arrowup, color=color.green, size=size.small,location = location.belowbar) plotshape(short_entry, style=shape.arrowdown, color=color.red, size=size.small,location = location.abovebar) //Find absolute value of positon size to exit position properly size = math.abs(strategy.position_size) // uncomment for Options, Comment for Futures //Replace the relevant field of JSON string with your account specific values long_msg = '{"secret":"C0B2u","alertType":"multi_leg_order","order_legs":[{"transactionType":"B","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"NIFTY1","instrument":"OPT","productType":"I","sort_order":"1","price":"0","option_type":"CE","strike_price":"'+str.tostring(callbuytext) + ".0"+'","expiry_date":"2024-07-11"}]}' short_msg = '{"secret":"C0B2u","alertType":"multi_leg_order","order_legs":[{"transactionType":"B","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"NIFTY","instrument":"OPT","productType":"I","sort_order":"1","price":"0","option_type":"PE","strike_price":"'+str.tostring(putbuytext) + ".0"+'","expiry_date":"2024-07-11"}]}' long_exit_msg = '{"secret":"C0B2u","alertType":"multi_leg_order","order_legs":[{"transactionType":"S","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"NIFTY","instrument":"OPT","productType":"I","sort_order":"1","price":"0","option_type":"CE","strike_price":"'+str.tostring(callselltext) + ".0"+'","expiry_date":"2024-07-11"}]}' short_exit_msg = '{"secret":"C0B2u","alertType":"multi_leg_order","order_legs":[{"transactionType":"S","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"NIFTY","instrument":"OPT","productType":"I","sort_order":"1","price":"0","option_type":"PE","strike_price":"'+str.tostring(putselltext) + ".0"+'","expiry_date":"2024-07-11"}]}' // uncomment for Futures, Comment for Options //Replace these four JSON strings with those generated from user Dhan account for Nifty futures // long_msg = '{"secret":"C0B2u","alertType":"multi_leg_order","order_legs":[{"transactionType":"B","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"NIFTY1!","instrument":"FUT","productType":"I","sort_order":"1","price":"0"}]}' // long_exit_msg = '{"secret":"C0B2u","alertType":"multi_leg_order","order_legs":[{"transactionType":"S","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"NIFTY1!","instrument":"FUT","productType":"M","sort_order":"1","price":"0"}]}' // short_msg = '{"secret":"C0B2u","alertType":"multi_leg_order","order_legs":[{"transactionType":"S","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"NIFTY1!","instrument":"FUT","productType":"M","sort_order":"1","price":"0"}]}' // short_exit_msg = '{"secret":"C0B2u","alertType":"multi_leg_order","order_legs":[{"transactionType":"B","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"NIFTY1!","instrument":"FUT","productType":"M","sort_order":"1","price":"0"}]}' // Submit orders based on signals if(strategy.position_size == 0) if long_entry strategy.order("Long", strategy.long,alert_message=long_msg) if short_entry strategy.order("Short", strategy.short,alert_message=short_msg) if(strategy.position_size > 0) if(short_entry) strategy.order("Short", strategy.short, qty = size, alert_message=short_msg) else strategy.exit("Long Exit", from_entry="Long", qty = size, stop=stop_loss_price, limit= target_price, alert_message=long_exit_msg) if(strategy.position_size < 0) if(long_entry) strategy.order("Long", strategy.long, qty = size, alert_message=long_msg) else strategy.exit("Short Exit", from_entry="Short", qty = size, stop=stop_loss_price, limit= target_price, alert_message=short_exit_msg)