Pine script Tradingview战略测试仪没有';t将订单累加到同一交易中
我尝试编写一个如下工作的策略:Pine script Tradingview战略测试仪没有';t将订单累加到同一交易中,pine-script,Pine Script,我尝试编写一个如下工作的策略: 在开始条件下,通过strategy.entry(“买入”,strategy.long,500)买入X笔金额 用strategy.exit(“exit”,“buy”,limit=Take\u Profit\u price,when=strategy.position\u size>0)设置获利回吐订单,比如说上涨3% 还可以使用strategy.entry(“buy”,strategy.long,500,limit=safety_price”)设置较低的购买订单(第
strategy("Test Multiple Orders", overlay=true,
max_labels_count=500, pyramiding=100, initial_capital=10000, calc_on_order_fills=false)
start_deal_condition = crossover(sma(close, 50), sma(close, 100))
//----------------------------------------------
//STRATEGY STUFF STARTS HERE
//how many safety orders were placed
var float count_placed_safety_orders = 0
//variable to hold next TP price
var float take_profit_price = na
//safety order price value
var float safety_order_price = na
//0 - nothing
//1 - base order executed
//2 - safety order executed
var int last_performed_action = 0
//buy condition for base order
if start_deal_condition and (strategy.position_size <= 0)
//enter with market order
strategy.entry("buy", strategy.long, 500)
last_performed_action := 1
//if we are in an order calculate TP target based on configured %TP
if (strategy.position_size > 0)
take_profit_price := strategy.position_avg_price * 1.03
//place the first safety order (SO)
if (strategy.opentrades == 1) and (count_placed_safety_orders == 0)
count_placed_safety_orders := count_placed_safety_orders + 1
safety_order_price := round(strategy.position_avg_price-
(strategy.position_avg_price*3/100), 4)
//place a safety order as limit order
strategy.entry("buy", strategy.long, 500, limit=safety_order_price)
//mark that a safety order was placed
last_performed_action := 2
//after the SO is filled we need to place next SO order
if (strategy.opentrades > count_placed_safety_orders)
safety_order_price := round(strategy.position_avg_price-
((strategy.position_avg_price*3/100)*
count_placed_safety_orders), 4)
count_placed_safety_orders := count_placed_safety_orders + 1
//place next safety order as limit order
strategy.entry("buy", strategy.long, 500, limit=safety_order_price)
//mark that a safety order was placed
last_performed_action := 2
strategy.exit("exit", "buy", limit=take_profit_price, when=strategy.position_size > 0)
//if we exited on this candle bar we want to cancel remaining safety order(s)
condition_for_cancel_open_orders = (high >= take_profit_price)
and (strategy.position_size == 0)
and (last_performed_action == 2)
strategy.cancel("buy", when=condition_for_cancel_open_orders)
//if we are not in an order ANYMORE we need to reset variables for next trade
//variables to reset:take_profit_price, count_placed_safety_orders
if (strategy.position_size == 0)
last_performed_action := 0
take_profit_price := na
safety_order_price := na
count_placed_safety_orders := 0
策略(“测试多个订单”,overlay=true,
最大标签计数=500,聚合=100,初始资本=10000,按订单计算填充=false)
开始\交易\条件=交叉(sma(关闭,50),sma(关闭,100))
//----------------------------------------------
//战略从这里开始
//下了多少安全命令
var浮动计数\已下订单\安全\订单=0
//保持下一个TP价格的变量
var浮动收益价格=na
//安全订单价格值
var浮动安全\u订单\u价格=na
//0-无
//1-执行的基本订单
//2-执行安全令
var int上次执行的操作=0
//基本订单的购买条件
如果开始交易条件和(策略位置大小0)
获取利润价格:=策略位置平均价格*1.03
//下达第一个安全订单(SO)
如果(strategy.opentrades==1)和(count\u placed\u safety\u orders==0)
安全订单数量:=安全订单数量+1
安全订单价格:=整轮(策略位置平均价格-
(战略位置平均价格*3/100),4)
//将安全命令作为限制命令放置
策略.输入(“买入”,策略.多头,500,限额=安全\订单\价格)
//标记已下达安全令
上次执行的\u操作:=2
//订单填好后,我们需要下一个订单
如果(strategy.opentrades>count\u下单\u安全\u订单)
安全订单价格:=轮(策略、位置、平均价格)-
((战略位置平均价格*3/100)*
计数(已下订单、安全订单),4)
安全订单数量:=安全订单数量+1
//将下一个安全订单作为限制订单
策略.输入(“买入”,策略.多头,500,限额=安全\订单\价格)
//标记已下达安全令
上次执行的\u操作:=2
策略.退出(“退出”,“买入”,限制=获利\价格,何时=策略.头寸\大小>0)
//如果我们离开此烛台,我们希望取消剩余的安全订单
取消未结订单的条件=(高>=获利价格)
和(strategy.position_size==0)
和(上次执行的动作==2)
策略.取消(“购买”,当=取消未结订单的条件)
//如果我们不再处于订单中,我们需要为下一笔交易重置变量
//要重置的变量:获取\利润\价格、计数\下单\安全\订单
if(strategy.position_size==0)
上次执行的\u操作:=0
获取利润价格:=na
安全订单价格:=na
安全订单数量:=0