问题描述
我有一些基本的 MA 交叉策略指标,我想实施更好的策略,仅在价格高于之前买入时卖出,但我不知道如何在 PINE 语法中做到这一点,请问有什么想法吗?
I have some basic MA cross strategy indicator and I would like to implement better strategy, to sell only when price is higher than was bought before, but I am not sure how to do it in PINE syntax, any idea pls ?
这是一个简单的代码,这个代码运行良好,打开多头或关闭多头取决于交叉 MA :
Here is simple code, working fine this one, it open LONG or close LONG depends on cross MA :
// Strategy functions
if (crossover(outShort,outLong))
strategy.entry(id="Long", long=strategy.long)
if (crossunder(outShort,outLong))
strategy.close(id="Long")
这是我有问题的代码,我想仅在价格高于之前购买的价格时才出售,所以我添加了条件来检查新价格 price <close
,但不知道如何用 Pine 语法编写
And here is my problematic code, I would like to implement to sell only when price is higher than was bought before, so I added condition to check new price price < close
, but not sure how to write in Pine syntax
// Strategy implemented - but with errors
var entryprice = 0
if (crossover(outShort,outLong))
entryprice := close
strategy.entry(id="Long", long=strategy.long)
if (crossunder(outShort,outLong)) and ( entryprice < close )
strategy.close(id="Long")
这是屏幕截图,如果条件 entryprice 不起作用
Here is screenshot, logic still not works as expected, if condition entryprice < close
not works
太好了,顺便说一句,你知道为什么我在策略中定义的 initial_capital value =1000 与利润不对应吗?即使我改变了资本编号,利润仍然显示相同的结果.
Excelent, By the way do you know why my initial_capital value =1000 defined in strategy not correspond to the profit ? even if I change the capital number, profit show still same results.
//@version=4
strategy(title="My_MA_strategy", shorttitle="MyMA Strategy_0.04", format=format.price, initial_capital=1000, overlay=true)
例如第一次贸易展利润 154 美元,即使只有 0.26% 的利润(不可能来自 1000 资本)百分比显示正常,但利润不正常.
E.g. 1st trade show profit $154, even there is only 0,26% profit ( from 1000 capital not possible)Percentage shows OK, but profit not.
尝试更改 Market tpe ,但这里没有进展,仍然显示相同的利润
Tried to change Market tpe , but no progress here, still show same profit
推荐答案
使用:
var float price = 0.0
或
var float price = 0
或
var price = 0.
这样您的变量就被声明为float"类型.第一种更明确的形式更可取,因为它更清楚地声明了您的意图.
so that your variable is declared as type "float". The first, more explicit form is preferable because it declares your intent more clearly.
入场的if
块应该只输入一次,当没有头寸时,否则它的条件在交易过程中可以多次为真,并且entryprice
会改变每次都是你不想要的.
The if
block for entries should be entered only once, when there is no position, otherwise its condition can be true multiple times during a trade, and entryprice
will change each time, which you do not want.
使用:
if (crossover(outShort,outLong)) and strategy.position_size == 0
这篇关于如何参考存储在 pinescript 临时变量中的先前入场价格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!