[英]how to refer to previous entry price stored in temp variable in pinescript?
我有一些基本的 MA 交叉策略指標,我想實施更好的策略,僅在價格高於之前買入時賣出,但我不知道如何在 PINE 語法中做到這一點,有什么想法嗎?
這是簡單的代碼,這個工作正常,它打開 LONG 或關閉 LONG 取決於交叉 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 語法編寫
// 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 < close
不起作用
太好了,順便問一下,你知道為什么我在策略中定義的 initial_capital 值 =1000 與利潤不對應嗎? 即使我改變資本數量,利潤仍然顯示相同的結果。
//@version=4
strategy(title="My_MA_strategy", shorttitle="MyMA Strategy_0.04", format=format.price, initial_capital=1000, overlay=true)
例如,第 1 屆貿易展利潤 154 美元,即使只有 0.26% 的利潤(不可能從 1000 資本獲得)百分比顯示還可以,但利潤不行。
試圖改變 Market tpe,但這里沒有進展,仍然顯示相同的利潤
利用:
var float price = 0.0
或者
var float price = 0
或者
var price = 0.
這樣您的變量就被聲明為“float”類型。 第一種更明確的形式更可取,因為它更清楚地表明了你的意圖。
[編輯 2021.05.06 10:34 — LucF]
如果沒有 position 的情況下,條目的if
塊應該只輸入一次,否則在交易期間它的條件可以多次為真,並且每次都會改變entryprice
,這是您不希望的。
利用:
if (crossover(outShort,outLong)) and strategy.position_size == 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.