我正在用影响策略结果的参数/变量回测我的策略。我想展示一下战略。净利润和战略。当参数/变量改变时,Closedtrades也随之改变。因此,我想在pineScript中显示一个表,其中strategy.netprofit, strategy.net。显示已关闭的交易和变量(per)的不同值。变化变量位于FOR循环旁边。我面临的问题是,虽然For正在改变变量(per),分配给表的值(strategy.netprofit和strategy. closetrades)没有改变,并且独立于由For循环引起的(per)的变化保持不变。下面结果表的快照。NetProfit, Total Closed Trades和每个变量变化的表,其中NetProfit, Total Closed Trades应该改变
//@version=5
strategy('MOST with Table', shorttitle='MOST with Table', overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity,
default_qty_value=100, commission_type=strategy.commission.percent, pyramiding=0, commission_value=0.2,
max_lines_count=500, max_labels_count=500, max_boxes_count=500, calc_on_every_tick=true)
var rodzajeAlertow = "=====Rodzaje dostepnych alertów====="
GL_odYear = input.int(2019,"od Rok", 1950, tooltip = "Rok od kiedy zaczynamy obliczenia", inline="odrok", group = rodzajeAlertow)
GL_odMonth = input.int(01, "Miesiąc", 1,12, tooltip = "Miesiąc od którego zaczynamy obliczenia", inline="odrok", group = rodzajeAlertow)
GL_odDay = input.int(03, "Dzień",1,31, tooltip = "dzień miesiąca od którego zaczynamy obliczenia", inline="odrok", group = rodzajeAlertow)
GL_doYear = input.int(2112,"do Rok", 1950, tooltip = "Rok do kiedy obliczenia", inline="dorok", group = rodzajeAlertow)
GL_doMonth = input.int(01, "Miesiąc", 1,12, tooltip = "Miesiąc do którego obliczenia", inline="dorok", group = rodzajeAlertow)
GL_doDay = input.int(01, "Dzień",1,31, tooltip = "dzień miesiąca do którego obliczenia", inline="dorok", group = rodzajeAlertow)
// === okono czasowe do obliczeń strategii ===
start = timestamp(GL_odYear, GL_odMonth , GL_odDay, 00, 00) // backtest start window
finish = timestamp(GL_doYear, GL_doMonth, GL_doDay, 23, 59) // backtest finish window
window() => time >= start and time <= finish ? true : false
var netProfitArray = array.new_float(40)
var Trail1Array = array.new_float(40)
var totalTradesArray = array.new_int(40)
var profitableTradesArray = array.new_int(40)
var profitFactorArray = array.new_float(40)
var iteracjaArray = array.new_int(40)
var float netProfit = na
var int totalTrades = 0
var int profitableTrades = 0
var float profitFactor = na
src = close
mova = 'KAMA'
//Kaufman Moving Average Adaptive (KAMA)
float xvnoise = na
float nfastend = na
float nslowend = na
float nsignal = na
float nnoise = na
float nefratio = na
float nsmooth = na
float s15 = na
float Trail1 =na
// Tworzenie tabeli
var table = table.new(position.top_right, 6, 500, color.orange, border_width = 1)
table.cell(table_id = table, column = 0, row = 0, text = "Net Profit")
table.cell(table_id = table, column = 1, row = 0, text = "Total Closed Trades")
table.cell(table_id = table, column = 2, row = 0, text = "Percent Profitable")
table.cell(table_id = table, column = 3, row = 0, text = "Profit Factor")
table.cell(table_id = table, column = 4, row = 0, text = "MA Type")
table.cell(table_id = table, column = 5, row = 0, text = "per")
for per =3 to 37
if mova == 'KAMA'
xvnoise := math.abs(src - src[1])
nfastend := 0.666
nslowend := 0.0645
nsignal := math.abs(src - src[per])
nnoise := math.sum(xvnoise, per)
nefratio := nnoise != 0 ? nsignal / nnoise : 0
nsmooth := math.pow(nefratio * (nfastend - nslowend) + nslowend, 2)
s15 := 0.0
s15 := nz(s15[1]) + nsmooth * (src - nz(s15[1]))
Trail1 := s15
AF2 = input.float(defval=.47, title='Percent', minval=0.1) / 100
SL2 = Trail1 * AF2 // Stop Loss
Trail2 = 0.0
iff_1 = Trail1 > nz(Trail2[1], 0) ? Trail1 - SL2 : Trail1 + SL2
iff_2 = Trail1 < nz(Trail2[1], 0) and Trail1[1] < nz(Trail2[1], 0) ? math.min(nz(Trail2[1], 0), Trail1 + SL2) : iff_1
Trail2 := Trail1 > nz(Trail2[1], 0) and Trail1[1] > nz(Trail2[1], 0) ? math.max(nz(Trail2[1], 0), Trail1 - SL2) : iff_2
Buy = ta.crossover(Trail1, Trail2)
Sell = ta.crossunder(Trail1, Trail2)
iff_3 = Trail2 > Trail1 ? -1 : 0
SR = Trail1 > Trail2 ? 1 : iff_3
if Buy and window()
strategy.entry("3", strategy.long, alert_message = "Buy będzie profit")
else if Sell and window()
strategy.close("3", "Sell" )
if barstate.islast or barstate.isrealtime
netProfit := strategy.netprofit
totalTrades := strategy.closedtrades
profitableTrades := strategy.wintrades
if totalTrades > 0
profitFactor := strategy.netprofit / (totalTrades - profitableTrades)
array.set(netProfitArray,per-3, netProfit)
array.set(totalTradesArray, per-3, totalTrades)
array.set(profitableTradesArray,per-3, profitableTrades)
array.set(profitFactorArray, per-3, profitFactor)
array.set(iteracjaArray, per-3, per)
if barstate.islast or barstate.isrealtime
for i = 3 to 37
table.cell(table, 0, i-2, str.tostring(array.get(netProfitArray,i-3)))
table.cell(table, 1, i-2, str.tostring(array.get(totalTradesArray,i-3)))
table.cell(table, 2, i-2, str.tostring(array.get(profitableTradesArray,i-3)))
table.cell(table, 3, i-2, str.tostring(array.get(profitFactorArray,i-3)))
table.cell(table, 4, i-2, str.tostring(mova))
table.cell(table, 5, i-2, str.tostring(array.get(iteracjaArray,i-3)))
在pinescript中,你一次只能有一个策略。
strategy.netprofit value是全球性的,适用于所有的交易和策略的所有变化。
所以当你循环使用per 3到37,当你看最后一栏只记录你的净利润时,你将总是记录相同的值。
你必须对策略的结果进行自己的计算,并独立地存储它们,以便能够循环并记录策略的每种变化的每个结果。