Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- strategy("Strategy Template", overlay = true, pyramiding = 4, max_boxes_count = 500, max_labels_count = 500, initial_capital = 30000)
- //Ubiquitous Inputs
- bear_bull = input.string("Bull", "Bull, Bear, or Both", options=["Both", "Bull", "Bear"])
- lookback = input.int(12, "***")
- bullEntryTicks = input.int(4, "Bull - Number of ticks to add to entry price")
- bearEntryTicks = input.int(4, "Bear - Number of ticks to add to entry price")
- stopLossTrailingTF = input(false, "Trailing stop loss")
- bullStopLossPercent = input.float(25, "Bull - Stop loss Percent (divides by 10e5, i.e. 25/10e5*100 = .025%)")
- bearStopLossPercent = input.float(25, "Bear - Stop loss Percent (divides by 10e5, i.e. 25/10e5*100 = .025%)")
- bullTakeProfitTF = input(false, "Bull - T/F for take profit")
- bullTakeProfitPercent = input.float(25, "Bull Take Profit Percent (divides by 10e5, i.e. 25/10e5*100 = .025%)")
- bearTakeProfitTF = input(true, "Bear - T/F for take profit")
- bearTakeProfitPercent = input.float(25, "Bear Take Profit Percent (divides by 10e5, i.e. 25/10e5*100 = .025%)")
- sizeGrow = input(false, "T/F for grow initial position size")
- minExcessMargin = input(1.625, "Min Port Size Over Margin Req. (for sizeGrow)")
- maxPos = input(5, "Max Position Size")
- string startTime = input("0800", "Start time in military")
- string endTime = input("1500", "End time in military")
- posSize = 1
- if sizeGrow
- posSize := math.min(maxPos, int(math.round(strategy.equity/minExcessMargin/close, 0)))
- else
- posSize := 1
- tradeHours = startTime + "-" + endTime
- timeAllow() => time(timeframe = timeframe.period, session = tradeHours, timezone = "America/Chicago")
- endTimeHour = str.tonumber(str.substring(endTime, 0, 2))
- timeAdjust = 0
- if timeframe.isintraday
- timeAdjust := int(60 - str.tonumber(timeframe.period)*2)
- barsSinceLastEntry() =>
- strategy.opentrades > 0 ? bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades - 1) : na
- stopLossBull = bullStopLossPercent/10000
- stopLossBear = bearStopLossPercent/10000
- bullTakeProfitPercent := bullTakeProfitPercent/10000
- bearTakeProfitPercent := bearTakeProfitPercent/10000
- /////////////////////////////////////////////////////////////////////////////////////
- //Strategy logic - Logic here, then add to if logic below (bullEntryLogic) for conditions met or unmet
- //Long Enter Logic - Fill in here
- bullEntryLogic = array.new_bool()
- if timeAllow()
- array.push(bullEntryLogic, true)
- else
- array.push(bullEntryLogic, false)
- //Ex.
- if ta.ema(close, 10) > ta.ema(close, 20)
- array.push(bullEntryLogic, true)
- else
- array.push(bullEntryLogic, false)
- //Long Close Logic -- Fill in here
- bullCloseLogic = array.new_bool()
- if barsSinceLastEntry() > 1
- array.push(bullCloseLogic, true)
- else
- array.push(bullCloseLogic, false)
- //Long Trade Logic
- if array.every(bullEntryLogic) and (bear_bull == "Bull" or bear_bull == "Both")
- // label.new(bar_index, high + 20, str.tostring(currDrawDown))
- bullEntryPrice = close + bullEntryTicks*syminfo.mintick
- bullStopLoss = math.round_to_mintick(bullEntryPrice*stopLossBull)
- alertComment = "Long: Stop Loss: " + str.tostring(bullStopLoss)
- strategy.order("Long Open Order", strategy.long, qty=posSize, limit=bullEntryPrice, stop=bullStopLoss, comment=alertComment, alert_message=alertComment)
- if strategy.position_size > 0
- bullStopLoss = math.round_to_mintick(strategy.opentrades.entry_price(strategy.opentrades -1)*stopLossBull)
- if bullTakeProfitTF == true
- bullTakeProfit = math.round_to_mintick(bullTakeProfitPercent*close/syminfo.mintick)
- bullTPPrice = bullTakeProfit*syminfo.mintick + strategy.opentrades.entry_price(strategy.opentrades - 1)
- alertComment = "Long: Stop Loss: " + str.tostring(bullTPPrice) + " - Take Profit: " + str.tostring(bullTPPrice)
- strategy.exit("Long StopLoss/TakeProfit", "Long Open Order", qty=math.abs(strategy.position_size),
- profit=bullTakeProfit, stop=bullStopLoss, comment=alertComment,alert_message=alertComment)
- else
- alertComment = "Long: Stop Loss: " + str.tostring(bullStopLoss)
- strategy.exit("Long StopLoss", "Long Open Order", qty=math.abs(strategy.position_size),
- stop=bullStopLoss, comment=alertComment)
- //Bull Close Logic
- if (hour==endTimeHour and minute==timeAdjust) or array.every(bullCloseLogic)
- strategy.order("Long Close Order", strategy.short, qty=math.abs(strategy.position_size), limit=close)
- /////////////////////////////////////////////////////////////////////////////////////
- //Short Enter Logic - Fill in here
- bearEntryLogic = array.new_bool()
- if timeAllow()
- array.push(bearEntryLogic, true)
- else
- array.push(bearEntryLogic, false)
- //Short Close Logic - Fill in here
- bearCloseLogic = array.new_bool()
- if barsSinceLastEntry() > 1
- array.push(bearCloseLogic, true)
- else
- array.push(bearCloseLogic, false)
- //Short Trade Logic
- if array.every(bearEntryLogic) and (bear_bull == "Bear" or bear_bull == "Both")
- // label.new(bar_index, high + 40, str.tostring(math.round(array.get(dailyEMAArr, 0), 2)))
- bearEntryPrice = close - bearEntryTicks*syminfo.mintick
- bearStopLoss = math.round_to_mintick(bearEntryPrice*stopLossBear)
- alertComment = "Short: Stop Loss: " + str.tostring(bearStopLoss)
- strategy.order("Short Open Order", strategy.short, qty=posSize, limit=bearEntryPrice, stop=bearStopLoss, comment=alertComment, alert_message=alertComment)
- if strategy.position_size < 0
- bearStopLoss = math.round_to_mintick(strategy.opentrades.entry_price(strategy.opentrades -1)*stopLossBear)
- if bearTakeProfitTF == true
- bearTakeProfit = math.round_to_mintick(bearTakeProfitPercent*close/syminfo.mintick)
- bearTPPrice = strategy.opentrades.entry_price(strategy.opentrades - 1) - bearTakeProfit*syminfo.mintick
- alertComment = "Short: Stop Loss: " + str.tostring(bearStopLoss) + " - Take Profit: " + str.tostring(bearTPPrice)
- strategy.exit("Short StopLoss/TakeProfit", "Short Open Order", qty=math.abs(strategy.position_size),
- profit=bearTakeProfit, stop=bearStopLoss, comment=alertComment, alert_message=alertComment)
- // strategy.exit("Short StopLoss", "Short Open Order", qty=math.abs(strategy.position_size),
- // profit=bearTakeProfit, stop=bearStopLoss, comment=alertComment)
- else
- alertComment = "Short: Stop Loss: " + str.tostring(bearStopLoss)
- strategy.exit("Short StopLoss", "Short Open Order", qty=math.abs(strategy.position_size),
- stop=bearStopLoss, comment=alertComment)
- //Bear Close Logic
- if (hour==endTimeHour and minute==timeAdjust) or (array.every(bearCloseLogic)) //and strategy.openprofit > 350
- strategy.order("Short Close Order ", strategy.long, qty=math.abs(strategy.position_size), limit=close)
Advertisement
Comments
-
- download all types of premium tradingview indicators codes available on telegram - https://t.me/tradingview_premium_indicator
Add Comment
Please, Sign In to add comment
Advertisement