目的应该是确定哪个值带来最好的结果/利润。
在两条平均线的简单示例中,应在 185 到 195 之间检查值“x_avg”以确定哪个值产生最多利润。
我希望能够用 FOR-NEXT 函数解决这个问题。任何其他可能性都可以。
问题是在 FOR-NEXT 循环中,某些功能起作用。例如: barstate.islast 或 close[3] 或其他任何东西
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © flashpit
//@version=4
strategy("FOR NEXT", overlay=true)
// ###############################################################################################################################################
// *************************************************** Average Kauf_Stop System ****************************************************
//x_avg = input(title="Avg - X " , minval=0, maxval=1000, step=1, defval=183)
y_avg = input(title="Avg - Y " , minval=0, maxval=1000, step=1, defval=580)
// ************************************************************************************************
varip aktueller_profit = 0.0
varip best_profit = 0.0
varip best_wert_x = 0
varip einkauf = 0.0
varip verkauf = 0.0
varip trades = 0
varip z = 0
varip x = 1
varip g = 0
varip x_avg = 183
longCondition = crossover (sma(close, x_avg + 1), sma(close, y_avg))
if (longCondition)
strategy.order("My Long Entry Id", strategy.long,1 )
shortCondition = crossunder (sma(close, x_avg + 1), sma(close, y_avg)) and strategy.opentrades > 0
if (shortCondition)
strategy.order("My Short Entry Id", strategy.short, 1)
for x = 1 to 5 by 1
z:= z + 1
longCondition01 = crossover (sma(close, x_avg + x), sma(close, y_avg))
if (longCondition01)
einkauf:= close
shortCondition01 = crossunder (sma(close, x_avg + x), sma(close, y_avg)) and einkauf > 0
if (shortCondition01)
verkauf:= close
aktueller_profit:= aktueller_profit + (verkauf - einkauf)
einkauf:= 0
verkauf:= 0
trades:= trades + 1
if barstate.islastconfirmedhistory //z > ((bar_index + 1) / x) - 10 and //aktueller_profit > best_profit and
best_profit:= aktueller_profit
best_wert_x:= x_avg + x
g:= g + 1
//aktueller_profit:= 0
//plotshape (crossunder (sma(close, x_avg), sma(close, y_avg)), title="DownTrend Begins", location=location.abovebar, style=shape.triangledown, size=size.normal, color=color.red)
//plotshape (crossover (sma(close, x_avg), sma(close, y_avg)), title="UpTrend Begins", location=location.belowbar, style=shape.triangleup, size=size.normal, color=color.green)
// ************************************************************************************************
var Daten_Label_RU = table.new(position.bottom_left, columns = 2, rows = 1, bgcolor = color.gray, frame_color = color.gray, frame_width = 2, border_color = color.gray, border_width = 2)
table.cell(table_id = Daten_Label_RU, column = 0, row = 0, text = "Best Profit "+
"\nAkt Profit "+
"\nTrades "+
"\nZ "+
"\nG "+
"\nBest X ", text_color = color.white, text_halign = text.align_left)
table.cell(table_id = Daten_Label_RU, column = 1, row = 0, text = tostring(best_profit)+
"\n" +tostring(aktueller_profit)+
"\n" +tostring(trades)+
"\n" +tostring(z)+
"\n" +tostring(g)+
"\n" +tostring(best_wert_x), text_color = color.white, text_halign = text.align_right)