我有一个网格代码,它在10个不同级别打开订单,但我想设置每个级别的最大未结交易,例如,在级别1最多3笔交易,在级别2最多3笔(最多6笔未结交易(,等等。
我已经用strategy.pentrades编码了它,但它需要总的公开交易,而不是每个级别的公开交易。。。我不知道该怎么做。任何帮助或指南都可能有所帮助,感谢
检查if Short1、if Short2等…
//@version=5
strategy(title = 'Grid - Short™',
overlay = true,
calc_on_every_tick = true,
initial_capital = 1000,
commission_type = strategy.commission.percent,
commission_value = 0.06,
pyramiding = 100,
default_qty_type = strategy.percent_of_equity,
process_orders_on_close = true,
close_entries_rule = 'ANY')
//---------------------------------------------------------------
startDate = input.int(title="START DAY DATE",
defval=1, minval=1, maxval=31,
group = '1. DATE RANGE: Select the date range for backtesting',
tooltip = "Enter the day of start backtesting.")
startMonth = input.int(title="START MONTH",
defval=1, minval=1, maxval=12,
group = '1. DATE RANGE: Select the date range for backtesting',
tooltip = "Enter the month of start backtesting.")
startYear = input.int(title="START YEAR",
defval=2018, minval=1800, maxval=2100,
group = '1. DATE RANGE: Select the date range for backtesting',
tooltip = "Enter the year of start backtesting.")
endDate = input.int(title="END DAY DATE",
defval=30, minval=1, maxval=31,
group = '1. DATE RANGE: Select the date range for backtesting',
tooltip = "Enter the day of end backtesting.")
endMonth = input.int(title="END MONTH",
defval=12, minval=1, maxval=12,
group = '1. DATE RANGE: Select the date range for backtesting',
tooltip = "Enter the month of end backtesting.")
endYear = input.int(title="END YEAR",
defval=2022, minval=1800, maxval=2100,
group = '1. DATE RANGE: Select the date range for backtesting',
tooltip = "Enter the year of end backtesting.")
inDateRange = (time >= timestamp(syminfo.timezone, startYear,
startMonth, startDate, 0, 0)) and
(time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))
//---------------------------------------------------------------
PercentProfit = input.float(defval = 0.50,
title = "GRID PERCENT VALUE",
group = "2. GRID CUSTOM SETTING",
tooltip = "This feature refers to the percentage value of each grid. The value you enter here will allow you to create your custom grid.",
confirm = true)
LowerLimit = input.price(defval = 0.00,
title = 'LOW GRID POSITION',
group = "2. GRID CUSTOM SETTING",
confirm = true,
tooltip = "This feature allows you to set the initial price level of your grid. Click on a price level to start.")
feePerTrade = input.float(defval = 0.06, minval = 0.00, maxval = 100,
title = "FEE PER TRADE",
group = "2. GRID CUSTOM SETTING",
tooltip = "Set the commission cost your broker charges you for each trade.",
confirm = true)
MaxTradesOpen = input(1)
//---------------------------------------------------------------
x1 = (100 + (PercentProfit * 1)) / 100
x2 = (100 + (PercentProfit * 2)) / 100
x3 = (100 + (PercentProfit * 3)) / 100
x4 = (100 + (PercentProfit * 4)) / 100
x5 = (100 + (PercentProfit * 5)) / 100
x6 = (100 + (PercentProfit * 6)) / 100
x7 = (100 + (PercentProfit * 7)) / 100
x8 = (100 + (PercentProfit * 8)) / 100
x9 = (100 + (PercentProfit * 9)) / 100
x10 = (100 + (PercentProfit * 10)) / 100
UpperLimit = LowerLimit * x10
//---------------------------------------------------------------
Linea_A = float(LowerLimit * x1)
Linea_B = float(LowerLimit * x2)
Linea_C = float(LowerLimit * x3)
Linea_D = float(LowerLimit * x4)
Linea_50Percent = float(LowerLimit * x5)
Linea_E = float(LowerLimit * x6)
Linea_F = float(LowerLimit * x7)
Linea_G = float(LowerLimit * x8)
Linea_H = float(LowerLimit * x9)
//---------------------------------------------------------------
Short1 = ta.crossover(close, Linea_A) and inDateRange
Short2 = ta.crossover(close, Linea_B) and inDateRange
Short3 = ta.crossover(close, Linea_C) and inDateRange
Short4 = ta.crossover(close, Linea_D) and inDateRange
Short5 = ta.crossover(close, Linea_50Percent) and inDateRange
Short6 = ta.crossover(close, Linea_E) and inDateRange
Short7 = ta.crossover(close, Linea_F) and inDateRange
Short8 = ta.crossover(close, Linea_G) and inDateRange
Short9 = ta.crossover(close, Linea_H) and inDateRange
Short10 = ta.crossover(close, UpperLimit) and inDateRange
//---------------------------------------------------------------
isExit1 = ta.crossunder(close, LowerLimit)
isExit2 = ta.crossunder(close, Linea_A)
isExit3 = ta.crossunder(close, Linea_B)
isExit4 = ta.crossunder(close, Linea_C)
isExit5 = ta.crossunder(close, Linea_D)
isExit6 = ta.crossunder(close, Linea_50Percent)
isExit7 = ta.crossunder(close, Linea_E)
isExit8 = ta.crossunder(close, Linea_F)
isExit9 = ta.crossunder(close, Linea_G)
isExit10 = ta.crossunder(close, Linea_H)
//---------------------------------------------------------------
plot(LowerLimit, color = color.purple, linewidth = 2)
plot(UpperLimit, color = color.purple, linewidth = 2)
plot(Linea_A, color = color.blue, linewidth = 1)
plot(Linea_B, color = color.blue, linewidth = 1)
plot(Linea_C, color = color.blue, linewidth = 1)
plot(Linea_D, color = color.blue, linewidth = 1)
plot(Linea_50Percent, color = color.blue, linewidth = 1)
plot(Linea_E, color = color.blue, linewidth = 1)
plot(Linea_F, color = color.blue, linewidth = 1)
plot(Linea_G, color = color.blue, linewidth = 1)
plot(Linea_H, color = color.blue, linewidth = 1)
//---------------------------------------------------------------
fill(plot(LowerLimit),
plot(Linea_A),
color = color.new(color.purple, 90))
fill(plot(Linea_A),
plot(Linea_B),
color = color.new(color.purple, 85))
fill(plot(Linea_B),
plot(Linea_C),
color = color.new(color.purple, 80))
fill(plot(Linea_C),
plot(Linea_D),
color = color.new(color.purple, 70))
fill(plot(Linea_D),
plot(Linea_50Percent),
color = color.new(color.purple, 60))
fill(plot(Linea_50Percent),
plot(Linea_E),
color = color.new(color.purple, 60))
fill(plot(Linea_E),
plot(Linea_F),
color = color.new(color.purple, 70))
fill(plot(Linea_F),
plot(Linea_G),
color = color.new(color.purple, 80))
fill(plot(Linea_G),
plot(Linea_H),
color = color.new(color.purple, 85))
fill(plot(Linea_H),
plot(LowerLimit),
color = color.new(color.purple, 90))
//---------------------------------------------------------------
if Short1 and strategy.opentrades <MaxTradesOpen
strategy.entry(id = "SHORT1",
direction = strategy.short,
qty = 0.1)
if isExit1
strategy.exit(id = "EXIT1",
from_entry = "SHORT1",
qty = 100,
stop = LowerLimit,
profit = LowerLimit)
if Short2 and strategy.opentrades <MaxTradesOpen
strategy.entry(id = "SHORT2",
direction = strategy.short,
qty = 0.1)
if isExit2
strategy.exit(id = "EXIT2",
from_entry = "SHORT2",
qty = 100,
stop = Linea_A,
limit = Linea_A)
if Short3 and strategy.opentrades <MaxTradesOpen
strategy.entry(id = "SHORT3",
direction = strategy.short,
qty = 0.1)
if isExit3
strategy.exit(id = "EXIT3",
from_entry = "SHORT3",
qty = 100,
stop = Linea_B,
limit = Linea_B)
if Short4 and strategy.opentrades <MaxTradesOpen
strategy.entry(id = "SHORT4",
direction = strategy.short,
qty = 0.1)
if isExit4
strategy.exit(id = "EXIT4",
from_entry = "SHORT4",
qty = 100,
stop = Linea_C,
limit = Linea_C)
if Short5 and strategy.opentrades <MaxTradesOpen
strategy.entry(id = "SHORT5",
direction = strategy.short,
qty = 0.1)
if isExit5
strategy.exit(id = "EXIT5",
from_entry = "SHORT5",
qty = 100,
stop = Linea_D,
limit = Linea_D)
if Short6 and strategy.opentrades <MaxTradesOpen
strategy.entry(id = "SHORT6",
direction = strategy.short,
qty = 0.1)
if isExit6
strategy.exit(id = "EXIT6",
from_entry = "SHORT6",
qty = 100,
stop = Linea_50Percent,
limit = Linea_50Percent)
if Short7 and strategy.opentrades <MaxTradesOpen
strategy.entry(id = "SHORT7",
direction = strategy.short,
qty = 0.1)
if isExit7
strategy.exit(id = "EXIT7",
from_entry = "SHORT7",
qty = 100,
stop = Linea_E,
limit = Linea_E)
if Short8 and strategy.opentrades <MaxTradesOpen
strategy.entry(id = "SHORT8",
direction = strategy.short,
qty = 0.1)
if isExit8
strategy.exit(id = "EXIT8",
from_entry = "SHORT8",
qty = 100,
stop = Linea_F,
limit = Linea_F)
if Short9 and strategy.opentrades <MaxTradesOpen
strategy.entry(id = "SHORT9",
direction = strategy.short,
qty = 0.1)
if isExit9
strategy.exit(id = "EXIT9",
from_entry = "SHORT9",
qty = 100,
stop = Linea_G,
limit = Linea_G)
if Short10 and strategy.opentrades <MaxTradesOpen
strategy.entry(id = "SHORT10",
direction = strategy.short,
qty = 0.1)
if isExit10
strategy.exit(id = "EXIT10",
from_entry = "SHORT10",
qty = 100,
stop = Linea_H,
limit = Linea_H)
//---------------------------------------------------------------
trades = int(strategy.closedtrades)
initialCapital = float(strategy.initial_capital)
grossReturn = trades * PercentProfit
gainCapital = (initialCapital * grossReturn) / 100
totalCapital = initialCapital + gainCapital
feeCost = (totalCapital * feePerTrade) / 100
totalFeeCost = feeCost * trades
netCapital = totalCapital - totalFeeCost - initialCapital
//---------------------------------------------------------------
i_position = input.string(defval = "Bottom Right",
title = "Table Placement",
options = ["Top Right", "Middle Right", "Bottom Right"],
group = "3. Table Characteristics")
position = i_position == "Top Right" ? position.top_right : i_position == "Middle Right" ? position.middle_right : position.bottom_right
i_w = input.int(title = "Width",
defval = 14,
group = "3. Table Characteristics")
i_h = input.int(title = "Height",
defval = 8,
group = "3. Table Characteristics")
i_text_size = input(title = "Text Size",
defval = "Normal")
text_size = i_text_size == "Normal" ? size.normal : i_text_size == "Auto" ? size.auto : i_text_size == "Auto" ? size.auto : i_text_size == "Tiny" ? size.tiny
: i_text_size == "Small" ? size.small : i_text_size == "Large" ? size.large : size.huge
i = input.bool(title = "1. Capital",
defval = true,
group = "4. Enable Rows",
tooltip = "Initial capital of the strategy.")
i_1 = input.bool(title = "2. Gross Returns",
defval = true,
group = "4. Enable Rows",
tooltip = "Total strategy return. Contains the initial capital plus the sum of the gains.")
i_2 = input.bool(title = "3. Gain Percent",
defval = true,
group = "4. Enable Rows",
tooltip = "Percentage return of strategy gains.")
i_3 = input.bool(title = "4. Commission Cost",
defval = true,
group = "4. Enable Rows",
tooltip = "Percentage return of strategy gains.")
i_4 = input.bool(title = "5. Net Returns",
defval = true,
group = "4. Enable Rows",
tooltip = "Total commission cost.")
i_5 = input.bool(title = "6. Closed Trades",
defval = true,
group = "4. Enable Rows",
tooltip = "Total trades.")
var table perfTable = table.new(position, 2, 11, frame_color = color.purple, frame_width = 1, border_width = 1)
//---------------------------------------------------------------
if i
table.cell(perfTable, 0, 0, "1. GROSS RETURN",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.black, 0),
width = i_w,
height = i_h,
text_size = text_size,
text_halign = text.align_left)
if i
table.cell(perfTable, 1, 0,
text = str.tostring(totalCapital) + " $",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.black, 0),
width = i_w,
height = i_h,
text_size = text_size)
if i_1
table.cell(perfTable, 0, 1, "2. INITIAL CAPITAL",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.black, 0),
width = i_w,
height = i_h,
text_size = text_size,
text_halign = text.align_left)
if i_1
table.cell(perfTable, 1, 1, text = str.tostring(initialCapital) + " $",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.black, 0),
width = i_w,
height = i_h,
text_size = text_size)
if i_2
table.cell(perfTable, 0, 2, "3. GAIN",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.black, 0),
width = i_w,
height = i_h,
text_size = text_size,
text_halign = text.align_left)
if i_2
table.cell(perfTable, 1, 2,
text = str.tostring(grossReturn) + " %",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.black, 0),
width = i_w,
height = i_h,
text_size = text_size)
if i_3
table.cell(perfTable, 0, 3, "4. FEE COST",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.black, 0),
width = i_w,
height = i_h,
text_size = text_size,
text_halign = text.align_left)
if i_3
table.cell(perfTable, 1, 3,
text = str.tostring(totalFeeCost) + " $",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.black, 0),
width = i_w,
height = i_h,
text_size = text_size)
if i_4
table.cell(perfTable, 0, 4, "5. NET RETURN",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.black, 0),
width = i_w,
height = i_h,
text_size = text_size,
text_halign = text.align_left)
if i_4
table.cell(perfTable, 1, 4,
text = str.tostring(netCapital) + " $",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.black, 0),
width = i_w,
height = i_h,
text_size = text_size)
if i_5
table.cell(perfTable, 0, 6, "6. CLOSED TRADES",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.black, 0),
width = i_w,
height = i_h,
text_size = text_size,
text_halign = text.align_left)
if i_5
table.cell(perfTable, 1, 6,
text = str.tostring(trades),
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.black, 0),
width = i_w,
height = i_h,
text_size = text_size)
//---------------------------------------------------------------
我会为每个级别的创建一个整数计数器
var int count1 = 0
var int count2 = 0
....
每当达到特定级别时,递增相关计数器
if level_1_hit
count1 += 1
if level_2_hit
count2 += 1
// Enter 3 trades at level 1 maximum
if level_1_hit and count1 < 3
strategy.entry(...)