월간 성과 PnL 달력 전략

저자:차오장, 날짜: 2024-01-08 16:16:58
태그:

img

전반적인 설명

이 전략의 주요 기능은 월별 성과를 실시간으로 추적하고 표시하는 것입니다. 그것은 실시간으로 테이블에서 각 달의 이익과 손실을 업데이트 할 것입니다.

원칙

이 전략은 다음과 같은 단계를 통해 월간 성과 추적을 달성합니다.

  1. 이 달에 거래가 종료되었는지 여부를 기록하기 위해 변수를 정의합니다.
  2. 각 달의 특정 이익과 손실 가치를 기록하기 위해 변수를 정의하십시오.
  3. 시간에 해당하는 달을 기록하기 위해 변수를 정의합니다.
  4. 이 달에 거래가 종료되면 월간 PnL와 달을 해당 배열에 기록합니다.
  5. 표에서 지정된 위치에서 달 제목을 표시합니다.
  6. 월간 PnL 배열을 가로질러 PnL 값을 테이블의 해당 달 열에 표시합니다.
  7. 같은 해의 정보를 동시에 표시합니다.
  8. 각 해의 PnL을 요약하십시오.
  9. 색을 사용하여 긍정과 부정 PnL를 구분합니다.

위는 이 전략의 기본 단계와 작동 원칙입니다.

이점 분석

이러한 월간 성과 추적 전략은 다음과 같은 장점을 가지고 있습니다.

  1. 실시간 업데이트를 통해 언제든 최신 PnL 상태를 볼 수 있습니다.
  2. 특정 월간 PnL의 직관적인 표시
  3. 매 달의 세부사항을 명확하게 볼 수 있도록 정밀하게 정렬했습니다.
  4. 직관적인 비교로 색을 사용하여 월간 PnL를 보다 시각적으로 대조합니다.
  5. 통계적 합은 또한 연간 PnL 합을 제공합니다.
  6. 테이블 위치, 크기 등을 조정할 수 있는 사용자 정의 표시

위험 분석

이 전략에는 또한 몇 가지 위험이 있습니다.

  1. 복잡한 논리는 오류로 인해 정확하지 않은 데이터를 유발할 수 있습니다.
  2. 너무 자주 테이블을 갱신하면 성능에 영향을 줄 수 있습니다.
  3. 배열 데이터 저장은 덜 효율적입니다.
  4. 시간 및 달 변환 계산이 잘못 될 수 있습니다.

위험은 다음을 통해 줄일 수 있습니다.

  1. 올바른 논리를 보장하기 위해 엄격한 코드 검토
  2. 너무 많이 갱신하는 것을 방지하기 위해 갱신 빈도를 최적화하십시오.
  3. 더 효율적인 데이터 구조를 사용하여 데이터를 저장합니다.
  4. 시간 및 달 변환을 위한 예외 처리 추가.

최적화

이 전략의 최적화:

  1. 월별 데이터를 저장하기 위해 사전과 같은 더 효율적인 데이터 구조를 사용하세요.
  2. 연간 수익 계산을 추가합니다.
  3. 변화율과 같은 다른 메트릭을 추가합니다.
  4. 환전 곡선을 시각적으로 표시합니다.
  5. 통계에 대한 사용자 정의 시간 범위를 지원합니다.
  6. CSV 파일로 데이터를 내보내는 것을 지원합니다.

이 방법은 전략의 기능과 사용자 경험을 향상시킬 수 있습니다.

결론

이 전략은 배열과 테이블을 사용하여 실시간 추적 및 월간 수익을 표시하는 핵심 기능을 달성합니다. 간단하고 효율적이며 사용하기 쉽습니다. 또한 몇 가지 장점이 있지만 여전히 예방해야 할 몇 가지 위험이 있습니다. 논리 및 성능의 최적화로 더 완벽하고 강력해질 수 있습니다. 전반적으로 매우 실용적인 월간 성능 추적 전략입니다.


/*backtest
start: 2023-01-01 00:00:00
end: 2024-01-07 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MAURYA_ALGO_TRADER

//@version=5
strategy("Monthly Performance", overlay=true)

period = input(20, "Length")

longCondition =   close > high[20]  //ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition = close < low[20]  //ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)



// Copy below code to end of the desired strategy script
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                 monthly pnl performance  by Dr. Maurya @MAURYA_ALGO_TRADER                        //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
show_performance = input.bool(true, 'Show Monthly Monthly Performance ?', group='Monthly Performance')

dash_loc_mp = input("Bottom Right","Location"  ,options=["Top Right","Bottom Right","Top Left","Bottom Left", "Middle Right","Bottom Center"]  ,group='Monthly Performance', inline = "performance")

text_size_mp = input('Small',"Size"  ,options=["Tiny","Small","Normal","Large"]  ,group='Monthly Performance', inline = "performance")

var table_position_mp = dash_loc_mp == 'Top Left' ? position.top_left :
  dash_loc_mp == 'Bottom Left' ? position.bottom_left :
  dash_loc_mp == 'Middle Right' ? position.middle_right :
  dash_loc_mp == 'Bottom Center' ? position.bottom_center :
  dash_loc_mp == 'Top Right' ? position.top_right : position.bottom_right
  
var table_text_size_mp = text_size_mp == 'Tiny' ? size.tiny :
  text_size_mp == 'Small' ? size.small :
  text_size_mp == 'Normal' ? size.normal : size.large

/////////////////
text_c = color.white

/////////////////////////////////////////////

// var bool new_month = na
new_month = ta.change(month) //> 0 ? true : false
newest_month = new_month and strategy.closedtrades >= 1
strategy.initial_capital = 50000
// profit
only_profit = strategy.netprofit
initial_balance = strategy.initial_capital

// month number
var int month_number = na
month_number := (ta.valuewhen(newest_month, month(time), 0)) //and month(time) > 1 ? (ta.valuewhen(newest_month, month(time), 0) - 1) :  12 //1 to 12

//month_year
var int month_time = na
month_time := ta.valuewhen(newest_month, time, 0) - 2419200000 


var int m_counter = 0
if newest_month
    m_counter += 1



// current month values
var bool new_year = na
new_year := ta.change(year)
curr_m_pnl = only_profit - nz(ta.valuewhen(newest_month, only_profit, 0), 0)
curr_m_number = newest_month ? ta.valuewhen(newest_month, month(time), 0) : month(time)
curr_y_pnl = (only_profit - nz(ta.valuewhen(new_year, only_profit, 0),0)) 



var float [] net_profit_array = array.new_float()
var int [] month_array = array.new_int()
var int [] month_time_array = array.new_int()


if newest_month
    array.push(net_profit_array, only_profit)
    array.push(month_array, month_number)
    array.push(month_time_array, month_time)



var float [] y_pnl_array = array.new_float()
var int [] y_number_array = array.new_int()
var int [] y_time_array = array.new_int()

newest_year = ta.change(year) and strategy.closedtrades >= 1
get_yearly_pnl = nz(ta.valuewhen(newest_year, strategy.netprofit, 0) - nz(ta.valuewhen(newest_year, strategy.netprofit, 1), 0), 0)
get_m_year = ta.valuewhen(newest_year, year(time), 1)
get_y_time = ta.valuewhen(newest_year, time, 0)

if newest_year
    array.push(y_pnl_array, get_yearly_pnl)
    array.push(y_number_array, get_m_year)
    array.push(y_time_array, get_y_time)
var float monthly_profit = na
var int column_month_number = na
var int row_month_time = na

 


var testTable = table.new(position = table_position_mp, columns = 14, rows = 40, bgcolor = color.rgb(7, 226, 242, 38), border_color = color.white, border_width = 1)
if barstate.islastconfirmedhistory and show_performance
    table.cell(table_id = testTable, column = 0, row = 0, text = "YEAR", text_color = text_c, text_size=table_text_size_mp)
    table.cell(table_id = testTable, column = 1, row = 0, text = "JAN", text_color = text_c, text_size=table_text_size_mp)
    table.cell(table_id = testTable, column = 2, row = 0, text = "FEB", text_color = text_c, text_size=table_text_size_mp)
    table.cell(table_id = testTable, column = 3, row = 0, text = "MAR", text_color = text_c, text_size=table_text_size_mp)
    table.cell(table_id = testTable, column = 4, row = 0, text = "APR", text_color = text_c, text_size=table_text_size_mp)
    table.cell(table_id = testTable, column = 5, row = 0, text = "MAY", text_color = text_c, text_size=table_text_size_mp)
    table.cell(table_id = testTable, column = 6, row = 0, text = "JUN", text_color = text_c, text_size=table_text_size_mp)
    table.cell(table_id = testTable, column = 7, row = 0, text = "JUL", text_color = text_c, text_size=table_text_size_mp)
    table.cell(table_id = testTable, column = 8, row = 0, text = "AUG", text_color = text_c, text_size=table_text_size_mp)
    table.cell(table_id = testTable, column = 9, row = 0, text = "SEP", text_color = text_c, text_size=table_text_size_mp)
    table.cell(table_id = testTable, column = 10, row = 0, text = "OCT", text_color = text_c, text_size=table_text_size_mp)
    table.cell(table_id = testTable, column = 11, row = 0, text = "NOV", text_color = text_c, text_size=table_text_size_mp)
    table.cell(table_id = testTable, column = 12, row = 0, text = "DEC", text_color = text_c, text_size=table_text_size_mp)
    table.cell(table_id = testTable, column = 13, row = 0, text = "YEAR P/L", text_color = text_c, text_size=table_text_size_mp)

    for i = 0 to (array.size(y_number_array) == 0 ? na : array.size(y_number_array) - 1)
        row_y = year(array.get(y_time_array, i)) - year(array.get(y_time_array, 0)) + 1
        table.cell(table_id = testTable, column = 13, row = row_y, text = str.tostring(array.get(y_pnl_array , i), "##.##") + '\n' + '(' + str.tostring(array.get(y_pnl_array , i)*100/initial_balance, "##.##") + ' %)', bgcolor = array.get(y_pnl_array , i) > 0 ? color.green : array.get(y_pnl_array , i) < 0 ? color.red : color.gray, text_color = color.rgb(0, 0, 0), text_size=table_text_size_mp)
    curr_row_y = array.size(month_time_array) == 0 ? 1 : (year(array.get(month_time_array, array.size(month_time_array) - 1))) - (year(array.get(month_time_array, 0))) + 1
    table.cell(table_id = testTable, column = 13, row = curr_row_y, text = str.tostring(curr_y_pnl, "##.##") + '\n' + '(' + str.tostring(curr_y_pnl*100/initial_balance, "##.##") + ' %)', bgcolor = curr_y_pnl > 0 ? color.green : curr_y_pnl < 0 ? color.red : color.gray, text_color = color.rgb(0, 0, 0), text_size=table_text_size_mp)
    

    for i = 0 to (array.size(net_profit_array) == 0 ? na : array.size(net_profit_array) - 1)
        monthly_profit := i > 0 ? ( array.get(net_profit_array, i) - array.get(net_profit_array, i - 1) ) : array.get(net_profit_array, i) 
        column_month_number := month(array.get(month_time_array, i)) 
        row_month_time :=((year(array.get(month_time_array, i))) - year(array.get(month_time_array, 0)) ) + 1 
        table.cell(table_id = testTable, column = column_month_number, row = row_month_time, text = str.tostring(monthly_profit, "##.##") + '\n' + '(' + str.tostring(monthly_profit*100/initial_balance, "##.##") + ' %)', bgcolor = monthly_profit > 0 ? color.green : monthly_profit < 0 ? color.red : color.gray, text_color = text_c, text_size=table_text_size_mp)
        table.cell(table_id = testTable, column = 0, row =row_month_time, text = str.tostring(year(array.get(month_time_array, i)), "##.##"), text_color = text_c, text_size=table_text_size_mp)
       
    curr_row_m = array.size(month_time_array) == 0 ? 1 : (year(array.get(month_time_array, array.size(month_time_array) - 1))) - (year(array.get(month_time_array, 0))) + 1
    table.cell(table_id = testTable, column = curr_m_number, row = curr_row_m, text = str.tostring(curr_m_pnl, "##.##") + '\n' + '(' + str.tostring(curr_m_pnl*100/initial_balance, "##.##") + ' %)', bgcolor = curr_m_pnl > 0 ? color.green : curr_m_pnl < 0 ? color.red : color.gray, text_color = text_c, text_size=table_text_size_mp)
    table.cell(table_id = testTable, column = 0, row =curr_row_m, text = str.tostring(year(time), "##.##"), text_color = text_c, text_size=table_text_size_mp)

//============================================================================================================================================================================

더 많은