// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © kriswaters //@version=4 strategy("Long/Short Only Once a Day Strategy w/Custom Session Range",overlay=true, process_orders_on_close=true,calc_on_every_tick=true) //INPUTS showRiskReward = input(true,title="Show Risk/Reward Area",group="Risk Management") takeLong = input(true, title="Enter Long Position",group="Risk Management") takeShort = input(true, title="Enter Short Position",group="Risk Management") selectRange = input("1 Day",title="Select Range:",options=["1 Day","2 Day","3 Day","1 Week"],group="Strategy") firstEntryPerc = input(1.0, step=0.1, title='Base Long/Short Trigger %', type=input.float,group="Strategy") / 100 tpPerc = input(1.0, step=0.1, title='TP %', type=input.float,group="Strategy") / 100 activateStop = input(true,title="Activate Stop",group="Strategy") stopPerc = input(1.0, step=0.1, title='Stop %', type=input.float,group="Strategy") / 100 leverage = input("Cross x20",title="Leverage",group="Alert Message") capital = input("2%",title="Capital invested",group="Alert Message") server = input("#Cs2",title="Server",group="Alert Message") //INPUTS FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31,group="Backtest Range") FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12,group="Backtest Range") FromYear = input(defval = 2017, title = "From Year", minval = 1900,group="Backtest Range") ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31,group="Backtest Range") ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12,group="Backtest Range") ToYear = input(defval = 9999, title = "To Year", minval = 2017,group="Backtest Range") start = timestamp(FromYear, FromMonth, FromDay, 00, 00) finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) window() => time >= start and time <= finish ? true : false //FUNCTION is_newbar(res) => t = time(res) not na(t) and (na(t[1]) or t > t[1]) string multip = na if selectRange == "1 Day" multip := "1440" else if selectRange == "2 Day" multip := "2D" else if selectRange == "3 Day" multip := "3D" else multip := "1W" isNewDay = is_newbar(multip) //STRATEGY var float longEntryPrice = na, var float longTp = na, var float longStop = na var float shortEntryPrice = na, var float shortTp = na, var float shortStop = na var float openPrice = na var bool longTrigger = false, var bool shortTrigger = false string br = "\n" var string jsonText = na string stopTextLong = "",string stopTextShort= "" if strategy.position_size > 0 and longTrigger == false longTrigger := true longTp := strategy.position_avg_price * (1 + tpPerc) longStop := strategy.position_avg_price * (1 - stopPerc) if activateStop stopTextLong := "Stop : "+tostring(longStop) strategy.exit("Long Exit","Long", comment="EXIT LONG", limit=longTp, stop=longStop) else stopTextLong := "" strategy.exit("Long Exit","Long", comment="TP LONG", limit=longTp) jsonText := syminfo.ticker+' LONG'+br+"Entry : "+tostring(open)+" / "+tostring(close)+br+"Leverage : "+leverage+br+"Capital invested : "+capital+br+"Target : "+tostring(longTp)+br+stopTextLong alert(jsonText, freq=alert.freq_once_per_bar) if strategy.position_size < 0 and shortTrigger == false shortTrigger := true shortTp := strategy.position_avg_price * (1 - tpPerc) shortStop := strategy.position_avg_price * (1 + stopPerc) if activateStop stopTextShort := "Stop : "+tostring(shortStop) strategy.exit("Short Exit","Short", comment="EXIT SHORT", limit=shortTp, stop=shortStop) else stopTextShort := "" strategy.exit("Short Exit","Short", comment="TP SHORT", limit=shortTp) jsonText := syminfo.ticker+' SHORT'+br+"Entry : "+tostring(open)+" / "+tostring(close)+br+"Leverage : "+leverage+br+"Capital invested : "+capital+br+"Target : "+tostring(shortTp)+br+stopTextShort alert(jsonText, freq=alert.freq_once_per_bar) if isNewDay and barstate.isconfirmed and window() if strategy.position_size != 0 strategy.close_all(comment="AUTO CLOSE END OF THE DAY") openPrice := open if takeLong longTrigger := false longEntryPrice := open * (1 + firstEntryPerc) strategy.entry("Long",true,comment="LONG",stop=longEntryPrice,oca_name="entry",oca_type=strategy.oca.cancel) if takeShort shortTrigger := false shortEntryPrice := open * (1 - firstEntryPerc) strategy.entry("Short",false,comment="SHORT",stop=shortEntryPrice,oca_name="entry",oca_type=strategy.oca.cancel) //PLOTS bgcolor(isNewDay ? color.orange : na) plot(longEntryPrice,title="Long Entry",style=plot.style_circles,color=color.green) plot(shortEntryPrice,title="Short Entry",style=plot.style_circles,color=color.red) plot(openPrice,title="Daily Open Price",style=plot.style_circles,color=color.aqua) L1 = plot(showRiskReward and strategy.position_size > 0 and isNewDay == false ? longEntryPrice : na, color=color.green, linewidth=1, style=plot.style_linebr, title="Long Entry Price") L2 = plot(showRiskReward and strategy.position_size > 0 and isNewDay == false ? longTp : na, color=color.green, linewidth=1, style=plot.style_linebr, title="Long Take Profit Price") L3 = plot(showRiskReward and strategy.position_size > 0 and isNewDay == false and activateStop ? longStop : na, color=color.red, linewidth=1, style=plot.style_linebr, title="Long Stop Price") S1 = plot(showRiskReward and strategy.position_size < 0 and isNewDay == false ? shortEntryPrice : na, color=color.teal, linewidth=1, style=plot.style_linebr, title="Short Entry Price") S2 = plot(showRiskReward and strategy.position_size < 0 and isNewDay == false ? shortTp : na, color=color.teal, linewidth=1, style=plot.style_linebr, title="Short Take Profit Price") S3 = plot(showRiskReward and strategy.position_size < 0 and isNewDay == false and activateStop ? shortStop : na, color=color.maroon, linewidth=1, style=plot.style_linebr, title="Short Stop Price") fill(L1,L2,color=color.new(color.green,90)) fill(L1,L3,color=color.new(color.red,90)) fill(S1,S2,color=color.new(color.teal,90)) fill(S1,S3,color=color.new(color.maroon,90)) bgcolor(window() == false ? color.new(color.black,80):na,title="Backtest Date Range")