1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| show_performance = input.bool(true, 'Show Monthly Performance ?', group='Performance - credits: @QuantNomad') prec = input(2, 'Return Precision', group='Performance - credits: @QuantNomad') if show_performance new_month = month(time) != month(time[1]) new_year = year(time) != year(time[1]) eq = strategy.equity bar_pnl = eq / eq[1] - 1 cur_month_pnl = 0.0 cur_year_pnl = 0.0 Current Monthly P&L cur_month_pnl := new_month ? 0.0 : (1 + cur_month_pnl[1]) * (1 + bar_pnl) - 1 Current Yearly P&L cur_year_pnl := new_year ? 0.0 : (1 + cur_year_pnl[1]) * (1 + bar_pnl) - 1 Arrays to store Yearly and Monthly P&Ls var month_pnl = array.new_float(0) var month_time = array.new_int(0) var year_pnl = array.new_float(0) var year_time = array.new_int(0) last_computed = false if (not na(cur_month_pnl[1]) and (new_month or barstate.islastconfirmedhistory)) if (last_computed[1]) array.pop(month_pnl) array.pop(month_time) array.push(month_pnl , cur_month_pnl[1]) array.push(month_time, time[1]) if (not na(cur_year_pnl[1]) and (new_year or barstate.islastconfirmedhistory)) if (last_computed[1]) array.pop(year_pnl) array.pop(year_time) array.push(year_pnl , cur_year_pnl[1]) array.push(year_time, time[1]) last_computed := barstate.islastconfirmedhistory ? true : nz(last_computed[1]) Monthly P&L Table var monthly_table = table(na) if (barstate.islastconfirmedhistory) monthly_table := table.new(position.bottom_right, columns = 14, rows = array.size(year_pnl) + 1, border_width = 1) table.cell(monthly_table, 0, 0, "", bgcolor = #cccccc) table.cell(monthly_table, 1, 0, "Jan", bgcolor = #cccccc) table.cell(monthly_table, 2, 0, "Feb", bgcolor = #cccccc) table.cell(monthly_table, 3, 0, "Mar", bgcolor = #cccccc) table.cell(monthly_table, 4, 0, "Apr", bgcolor = #cccccc) table.cell(monthly_table, 5, 0, "May", bgcolor = #cccccc) table.cell(monthly_table, 6, 0, "Jun", bgcolor = #cccccc) table.cell(monthly_table, 7, 0, "Jul", bgcolor = #cccccc) table.cell(monthly_table, 8, 0, "Aug", bgcolor = #cccccc) table.cell(monthly_table, 9, 0, "Sep", bgcolor = #cccccc) table.cell(monthly_table, 10, 0, "Oct", bgcolor = #cccccc) table.cell(monthly_table, 11, 0, "Nov", bgcolor = #cccccc) table.cell(monthly_table, 12, 0, "Dec", bgcolor = #cccccc) table.cell(monthly_table, 13, 0, "Year", bgcolor = #999999) for yi = 0 to array.size(year_pnl) - 1 table.cell(monthly_table, 0, yi + 1, str.tostring(year(array.get(year_time, yi))), bgcolor = #cccccc) y_color = array.get(year_pnl, yi) > 0 ? color.new(color.teal, transp = 40) : color.new(color.gray, transp = 40) table.cell(monthly_table, 13, yi + 1, str.tostring(math.round(array.get(year_pnl, yi) * 100, prec)), bgcolor = y_color, text_color=color.new(color.white, 0)) for mi = 0 to array.size(month_time) - 1 m_row = year(array.get(month_time, mi)) - year(array.get(year_time, 0)) + 1 m_col = month(array.get(month_time, mi)) m_color = array.get(month_pnl, mi) > 0 ? color.new(color.teal, transp = 40) : color.new(color.gray, transp = 40) table.cell(monthly_table, m_col, m_row, str.tostring(math.round(array.get(month_pnl, mi) * 100, prec)), bgcolor = m_color, text_color=color.new(color.white, 0))
|