我想在绿线买入,在红线卖出。在每个柱线开始时,我想以绿线价格和/或以红线价格下达限价卖单,并在柱线时段结束或下一个柱线时段开始时取消它们没有填满。另外,如果有一个买单没有退出,我不想再买这个头寸了。
但是我下面的代码就是这样做的,我无法弄清楚我做错了什么。有人可以帮忙吗?
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © leestarcpen
//@version=4
strategy("vwap stdev strategy", overlay=true , calc_on_every_tick=true )
//User Input
period = input(title="Period", type=input.integer, defval = 4)
//Calculation
ohlcSumVolumeWeighted = 0.0
volumeSum = 0.0
stdevSumVolumeWeighted = 0.0
for i = 0 to period
ohlc = (high[i]+low[i]+close[i]+open[i])/4
stdev = sqrt(( pow(high[i]-ohlc,2) + pow(low[i]-ohlc,2) + pow(close[i]-ohlc,2) + pow(open[i]-ohlc,2))/(4-1))
ohlcSumVolumeWeighted := ohlcSumVolumeWeighted + ohlc*volume[i]
volumeSum := volumeSum + volume[i]
stdevSumVolumeWeighted := stdevSumVolumeWeighted + stdev*volume[i]
myVwap = ohlcSumVolumeWeighted/volumeSum
trueStdev = stdevSumVolumeWeighted/volumeSum
buyPrice = myVwap- 2.25*trueStdev
sellPrice = myVwap + 1.5*trueStdev
//Plot
plot(myVwap, title="vwap", color=color.blue, transp=0)
plot(myVwap+ 1.5*trueStdev, title="stdev upper lvl.1", color=color.red, transp=0)
plot(myVwap- 2.25*trueStdev, title="stdev lower lvl.2.5", color=color.lime, transp=0)
plot(myVwap- 3*trueStdev, title="stdev lower lvl.3", color=color.green, transp=0)
//Conditions
isBuyLvl = myVwap- 2.25*trueStdev >= low
isSellLvl = myVwap + 1.5*trueStdev <= high
strategy.order("buy",strategy.long, limit = buyPrice)