Pine:如何同时打开更多的多头订单?(金字塔式) [英] Pine: How open more Long orders in parallel ? (pyramiding)

查看:134
本文介绍了Pine:如何同时打开更多的多头订单?(金字塔式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 Pine 策略在 MA 交叉时开多头头寸,只有在收盘价高于开单时才平仓,而且我只有在利润超过 2% 时才设置平仓 (((price/close)-1)*(-1)*100)>2策略运行良好,订单仅在盈利时关闭,但问题是我遗漏了许多交易,因为一般来说,由于我的策略,每个新多头的开始价格都高于之前的交易,因此当价格下跌时,不会打开其他多头.我认为这是金字塔式打开更多订单的工作,但如何做呢?假设价格比上次开仓的多头价格下跌 5%,那么应该在新的买入信号到来时开新的多头订单 (crossover(outShort,outLong)

I have this Pine strategy to open Long position when MA cross, and close position only if close price is higher than open order and also I put condition to close only if profit is more than 2% (((price/close)-1)*(-1)*100)>2 Strategy works fine, orders are closed only in profit but issue is that many trades I am missing because in general due to my strategy each new Long starts in higher price then previos trade, So when price drops, no other Long is opened. I think this is job for pyramiding to open more orders, but how to do it ? Lets say if price drops 5% from last opened Long price, then new Long order should be opened, when new buy signal is coming (crossover(outShort,outLong)

// Strategy functions
var  price = 0.0


if (crossover(outShort,outLong)) and afterStartDate and strategy.position_size == 0 and close < 59000
    price := close
    strategy.entry(id="Long", long=strategy.long)
    


if (crossunder(outShort,outLong)) and afterStartDate and ( price < close ) and (((price/close)-1)*(-1)*100)>2
    strategy.close(id="Long")

这是我完整的简单代码.默认策略从 5 月 1 日开始

Here is my full simple code. By default strategy start in 1st May

//@version=4

strategy(title="My_MA_strategy", shorttitle="MyMA Strategy_0.04", format=format.price,  initial_capital=1000, overlay=true)

startDate = input(title="Start Date", type=input.integer,
     defval=1, minval=1, maxval=31)
startMonth = input(title="Start Month", type=input.integer,
     defval=5, minval=1, maxval=12)
startYear = input(title="Start Year", type=input.integer,
     defval=2021, minval=1800, maxval=2100)
     
afterStartDate = (time >= timestamp(syminfo.timezone,
     startYear, startMonth, startDate, 0, 0))
     

// Inputs
i_oversold      = input(30, title="Oversold")
i_overbought    = input(70, title="Overbought")
len             = input(14, minval=1, title="Length")
src             = input(close, "Source", type = input.source)



lenShort = input(10, minval=1, title="LengthShort")
srcShort = input(close, title="SourceShort")
offsetShort = input(title="OffsetShort", type=input.integer, defval=0, minval=-500, maxval=500)
outShort = sma(srcShort, lenShort)
plot(outShort, color=color.green, title="MA_Short", offset=offsetShort)


lenLong = input(19, minval=1, title="LengthLong")
srcLong = input(close, title="SourceLong")
offsetLong = input(title="OffsetLong", type=input.integer, defval=0, minval=-500, maxval=500)
outLong = sma(srcLong, lenLong)
plot(outLong, color=color.red, title="MA_Long", offset=offsetLong)



// Strategy functions
var  price = 0.0


if (crossover(outShort,outLong)) and afterStartDate and strategy.position_size == 0
    price := close
    strategy.entry(id="Long", long=strategy.long)
    

if (crossunder(outShort,outLong)) and afterStartDate and ( price < close )
    strategy.close(id="Long")
   

推荐答案

我认为你必须在你的策略中声明金字塔.

I think you have to declare pyramiding in your strategy.

pyramiding (const integer) 允许的最大条目数同一个方向.如果值为 0,则只有一个挂单可以开同一个方向,追加挂单拒绝了.默认值为 0.

pyramiding (const integer) The maximum number of entries allowed in the same direction. If the value is 0, only one entry order in the same direction can be opened, and additional entry orders are rejected. The default value is 0.

strategy(title="MyStrategy", shorttitle="MS", pyramiding = 10)

这篇关于Pine:如何同时打开更多的多头订单?(金字塔式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆