Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- strategy("EMA 5/5 Day Long/Short with 4-Year Cycle Filter", overlay=true, initial_capital=1000, currency=currency.USD)
- // Create an input for the EMA period in the settings
- len = input.int(title="EMA Length", defval=5, minval=1)
- src = input(close, title="Source")
- ema = ta.ema(src, len)
- // Define the 4-year cycle
- halvingYear = input(2011) // Set the last halving year
- yearDifference = year(time) - halvingYear
- bearMarket = (yearDifference % 4 == 3) // Bear market is typically the third year after halving
- // Plot the 5 EMA and bear market filter
- plot(ema, title="5 EMA", color=color.blue)
- bgcolor(bearMarket ? color.red : na, transp=90, title="Bear Market")
- // Trade Logic
- if barstate.isconfirmed
- longCondition = close > ema
- shortCondition = close < ema
- exitLongCondition = close < ema
- exitShortCondition = close > ema
- if (longCondition and not bearMarket)
- strategy.entry("Long", strategy.long)
- if (exitLongCondition)
- strategy.close("Long")
- if (shortCondition and bearMarket)
- strategy.entry("Short", strategy.short)
- if (exitShortCondition)
- strategy.close("Short")
- // Calculate the Average Entry Price
- var float avgEntryPrice = na
- if strategy.position_size != 0
- avgEntryPrice := strategy.position_avg_price
- else
- avgEntryPrice := na
- // Plot the average entry line with extension
- plot(avgEntryPrice, color=#2196f3, style=plot.style_linebr, linewidth=1, title="Average Entry Price", force_overlay=true)
- // Dynamically Update Label with Real-Time Intrabar Calculations
- var label pctLabel = na
- if not na(avgEntryPrice)
- if pctLabel != na
- label.delete(pctLabel)
- // Calculate percentage change and unrealized P/L
- pctChange = (close - avgEntryPrice) / avgEntryPrice * 100.0
- unrealizedPL = (close - avgEntryPrice) * strategy.position_size
- // Create a label with dynamically updated values
- pctLabel := label.new(bar_index, avgEntryPrice, "P&L%: " + str.tostring((close - avgEntryPrice) / avgEntryPrice * 100.0, "#.##") + "\nAvg Price: " + str.tostring(avgEntryPrice, "#.##") + "\nUnrealized P/L: " + str.tostring((close - avgEntryPrice) * strategy.position_size, "#.##"), style=label.style_label_left, yloc=yloc.price, color=(close - avgEntryPrice) >= 0 ? #2196f3 : color.new(color.red, 0), textcolor=color.white, size=size.normal)
- else
- if pctLabel != na
- label.delete(pctLabel)
- // Dashboard Table
- i_tableTextSize = input.string(title="Dashboard Size", defval="Small", options=["Auto", "Huge", "Large", "Normal", "Small", "Tiny"], group="Dashboards")
- table_text_size(s) =>
- switch s
- "Auto" => size.auto
- "Huge" => size.huge
- "Large" => size.large
- "Normal" => size.normal
- "Small" => size.small
- => size.tiny
- tableTextSize = table_text_size(i_tableTextSize)
- i_showDashboard = input.bool(title="Performance Summary", defval=true, group="Dashboards", inline="Show Dashboards")
- f_fillCell(_table, _column, _row, _title, _value, _bgcolor, _txtcolor) =>
- _cellText = _title + "\n" + _value
- table.cell(_table, _column, _row, _cellText, bgcolor=_bgcolor, text_color=_txtcolor, text_size=tableTextSize)
- if i_showDashboard
- var bgcolor = color.new(color.black, 0)
- newWin = (strategy.wintrades > strategy.wintrades[1]) and (strategy.losstrades == strategy.losstrades[1]) and (strategy.eventrades == strategy.eventrades[1])
- newLoss = (strategy.wintrades == strategy.wintrades[1]) and (strategy.losstrades > strategy.losstrades[1]) and (strategy.eventrades == strategy.eventrades[1])
- varip int winRow = 0
- varip int lossRow = 0
- varip int maxWinRow = 0
- varip int maxLossRow = 0
- if newWin
- lossRow := 0
- winRow := winRow + 1
- if winRow > maxWinRow
- maxWinRow := winRow
- if newLoss
- winRow := 0
- lossRow := lossRow + 1
- if lossRow > maxLossRow
- maxLossRow := lossRow
- var table dashTable = table.new(position.top_right, 1, 15, border_width=1)
- if barstate.islastconfirmedhistory
- lastTime = strategy.position_size == 0 ? strategy.closedtrades.exit_time(strategy.closedtrades - 1) : time
- dollarReturn = strategy.netprofit
- _profit = (strategy.netprofit / strategy.initial_capital) * 100
- _numOfDaysInStrategy = (lastTime - strategy.closedtrades.entry_time(0)) / (1000 * 3600 * 24)
- _numOfDaysInStrategy := _numOfDaysInStrategy != 0 ? _numOfDaysInStrategy : 1
- f_fillCell(dashTable, 0, 3, "Percent Per Day", str.tostring(_profit / _numOfDaysInStrategy, '##.###') + "%", _profit > 0 ? color.teal : color.maroon, color.white)
- _winRate = (strategy.wintrades / strategy.closedtrades) * 100
- f_fillCell(dashTable, 0, 4, "Percent Profitable:", str.tostring(_winRate, '##.##') + "%", _winRate < 50 ? color.maroon : _winRate < 75 ? #999900 : color.teal, color.white)
- f_fillCell(dashTable, 0, 5, "Profit Factor:", str.tostring(strategy.grossprofit / strategy.grossloss, '##.###'), strategy.grossprofit > strategy.grossloss ? color.teal : color.maroon, color.white)
- f_fillCell(dashTable, 0, 6, "Total Trades:", str.tostring(strategy.closedtrades), bgcolor, color.white)
- f_fillCell(dashTable, 0, 7, "Winning Trades:", str.tostring(strategy.wintrades, '######'), bgcolor, color.white)
- f_fillCell(dashTable, 0, 8, "Max Wins In A Row:", str.tostring(maxWinRow, '######'), bgcolor, color.white)
- f_fillCell(dashTable, 0, 9, "Losing Trades:", str.tostring(strategy.losstrades, '######'), bgcolor, color.white)
- f_fillCell(dashTable, 0, 10, "Max Losses In A Row:", str.tostring(maxLossRow, '######'), bgcolor, color.white)
Advertisement
Comments
-
- Best to use it with the Ticker = Bitcoin all time history index
Add Comment
Please, Sign In to add comment