Pine script 我该如何使用我';你是否进行了回溯测试,以实际交易?提供了示例代码

Pine script 我该如何使用我';你是否进行了回溯测试,以实际交易?提供了示例代码,pine-script,Pine Script,我已经找到了一些公共策略,并将它们作为指导来发展我对pine脚本的理解。我将粘贴我试图修改的脚本 一般来说,我对pine脚本和编码非常陌生,但我理解编码的一般思想,似乎正在学习pine脚本的语法 代码如下: //@version=3 strategy("Moving Average and/or Bbands bot V1.1", shorttitle="Strategy", overlay=true, pyramiding=1000) //Make the backtest numbers

我已经找到了一些公共策略,并将它们作为指导来发展我对pine脚本的理解。我将粘贴我试图修改的脚本

一般来说,我对pine脚本和编码非常陌生,但我理解编码的一般思想,似乎正在学习pine脚本的语法

代码如下:

//@version=3
strategy("Moving Average and/or Bbands bot V1.1", shorttitle="Strategy", overlay=true, pyramiding=1000)

//Make the backtest numbers more legible depending on the market you're trading, altcoin, forex, or commodities.
qty = 1

// If you're trading an altcoin, make this true and the backtest numbers are now equivalent to 1 satoshi
isALT = input(false, "Altcoin")

if isALT
    qty:= 100000000

// If you're trading forex, make this true and the backtest numbers are now equivalent to $0.0001
isForex = input(false, "Forex")
if isForex
    qty:= 10000

//* Backtesting Period Selector | Component *//
//* https://www.tradingview.com/script/eCC1cvxQ-Backtesting-Period-Selector-Component *//
//* https://www.tradingview.com/u/pbergden/ *//
//* Modifications made *//
testStartYear = input(1, "Backtest Start Year") 
testStartMonth = input(8, "Backtest Start Month")
testStartDay = input(25, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)

testStopYear = input(999999, "Backtest Stop Year")
testStopMonth = input(9, "Backtest Stop Month")
testStopDay = input(26, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)

testPeriod() =>
    time >= testPeriodStart and time <= testPeriodStop ? true : false
/////////////// END - Backtesting Period Selector | Component ///////////////

//* Heiken Ashi Candles *//
isHA = false

data = isHA ? heikenashi(tickerid) : tickerid

o = security(data, period, open)
h = security(data, period, high)
l = security(data, period, low)
c = security(data, period, close)

g = c > o
r = c < o

col = c > o ? green : red

plotcandle(o, h, l, c, "Heiken Ashi", col, black)

//Initial open logic, needs to be set at the beginning as this is affected by most of the following settings
long = na
short = na 

//* Moving Average Logic *\\
// Enable this to only long or short if you are above or below the Moving Average
useMA = input(true, "Use Moving Average Cross")
ma1Input = input(50, "Moving Average 1")
ma2Input = input(200, "Moving Average 2")

ma1 = sma(c, ma1Input)
ma2 = sma(c, ma2Input)

maLong = c > ma1 and ma1 > ma2
maShort = c < ma1 and ma1 < ma2

ma1Plot = na
ma2Plot = na

if useMA
    ma1Plot := ma1
    ma2Plot := ma2
    long := maLong
    short := maShort

plot(ma1Plot, "ma1", blue)
plot(ma2Plot, "ma2", orange)

//* Bollinger Bands Logic *\\
// Enable this to only long or short if you are above or below the Bollinger Bands

useBbands = input(false, "Use Bollinger Bands")

bblength = input(20, minval=1)
mult = input(2.0, minval=0.001, maxval=50)
basis = sma(c, bblength)
dev = mult * stdev(c, bblength)
upper = basis + dev
lower = basis - dev

basisPlot = na
p1Plot = na
p2Plot = na

if useBbands
    long := c < lower
    short := c > upper
    basisPlot := basis
    p1Plot := upper
    p2Plot := lower

if useBbands and useMA
    long := c < lower and maLong
    short := c > upper and maShort

plot(basisPlot, color=red)
p1 = plot(p1Plot, color=blue)
p2 = plot(p2Plot, color=blue)
fill(p1, p2)

//////////////////////////
//* Strategy Component *//
//////////////////////////

// Count your long short conditions for more control with Pyramiding
sectionLongs = 0
sectionLongs := nz(sectionLongs[1])
sectionShorts = 0
sectionShorts := nz(sectionShorts[1])

if long
    sectionLongs := sectionLongs + 1
    sectionShorts := 0

if short
    sectionLongs := 0
    sectionShorts := sectionShorts + 1

// Pyramiding Inputs

pyrl = input(1, "Pyramiding less than") // If your count is less than this number
pyre = input(0, "Pyramiding equal to") // If your count is equal to this number
pyrg = input(1000000, "Pyramiding greater than") // If your count is greater than this number

// These check to see your signal and cross references it against the pyramiding settings above
longCondition = long and sectionLongs <= pyrl or long and sectionLongs >= pyrg or long and sectionLongs == pyre
shortCondition = short and sectionShorts <= pyrl or short and sectionShorts >= pyrg or short and sectionShorts == pyre

// Get the price of the last opened long or short
last_open_longCondition = na
last_open_shortCondition = na
last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])

// Count your actual opened positions for things like getting your average order price
sectionLongConditions = 0
sectionLongConditions := nz(sectionLongConditions[1])
sectionShortConditions = 0
sectionShortConditions := nz(sectionShortConditions[1])

if longCondition
    sectionLongConditions := sectionLongConditions + 1
    sectionShortConditions := 0

if shortCondition
    sectionLongConditions := 0
    sectionShortConditions := sectionShortConditions + 1

// Check if your last postion was a long or a short
last_longCondition = na
last_shortCondition = na
last_longCondition := longCondition ? time : nz(last_longCondition[1])
last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])

in_longCondition = last_longCondition > last_shortCondition
in_shortCondition = last_shortCondition > last_longCondition

// Keep track of the highest high since you last opened a position
last_high = na
last_low = na
last_high_short = na
last_low_short = na
last_high := not in_longCondition ? na : in_longCondition and (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1])
last_high_short := not in_shortCondition ? na : in_shortCondition and (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1])
last_low := not in_shortCondition ? na : in_shortCondition and (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1])
last_low_short := not in_longCondition ? na : in_longCondition and (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1])

// Trailing Stop
isTS = input(false, "Trailing Stop")
tsi = input(0, "Activate Trailing Stop Price") / qty
ts = input(0, "Trailing Stop") / qty
long_ts = isTS and not na(last_high) and crossunder(low, last_high - ts) and longCondition == 0 and high >= (last_open_longCondition + tsi)
short_ts = isTS and not na(last_low) and crossover(high, last_low + ts) and shortCondition == 0 and low <= (last_open_shortCondition - tsi)
tsColor = isTS and in_longCondition and last_high >= (last_open_longCondition + tsi) ? blue : isTS and in_shortCondition and last_low <= (last_open_shortCondition - tsi) ? blue : white
tsiColor = isTS and in_longCondition and last_high >= (last_open_longCondition + tsi) ? white : isTS and in_shortCondition and last_low <= (last_open_shortCondition - tsi) ? white : blue
plot(isTS and in_longCondition ? last_open_longCondition + tsi : na, "Long Trailing", tsiColor, style=3, linewidth=2)
plot(isTS and in_shortCondition ? last_open_shortCondition - tsi : na, "Short Trailing", tsiColor, style=3, linewidth=2)
plot(isTS and in_longCondition and last_high >= (last_open_longCondition + tsi) ? last_high - ts : na, "Long Trailing", tsColor, style=2, linewidth=2)
plot(isTS and in_shortCondition and last_low <= (last_open_shortCondition - tsi) ? last_low + ts : na, "Short Trailing", tsColor, style=2, linewidth=2)

// Take profit
isTP = input(false, "Take Profit")
tp = input(0, "Take Profit") / qty
long_tp = isTP and crossover(high, last_open_longCondition + tp) and longCondition == 0
short_tp = isTP and crossunder(low, last_open_shortCondition - tp) and shortCondition == 0
tpColor = isTP and in_longCondition ? purple : isTP and in_shortCondition ? purple : white
plot(isTP and in_longCondition and last_high < last_open_longCondition + tp ? last_open_longCondition + tp : na, "Long TP", tpColor, style=3, linewidth=2)
plot(isTP and in_shortCondition and last_low > last_open_shortCondition - tp ? last_open_shortCondition - tp : na, "Short TP", tpColor, style=3, linewidth=2)

// Stop Loss
isSL = input(false, "Stop Loss")
sl = input(0, "Stop Loss") / qty
long_sl = isSL and crossunder(low, last_open_longCondition - sl) and longCondition == 0
short_sl = isSL and crossover(high, last_open_shortCondition + sl) and shortCondition == 0
slColor = isSL and in_longCondition and last_low_short > last_open_longCondition - sl ? red : isSL and in_shortCondition and last_high_short < last_open_shortCondition + sl ? red : white
plot(isSL and in_longCondition ? last_open_longCondition - sl : na, "Long SL", slColor, style=3, linewidth=2)
plot(isSL and in_shortCondition ? last_open_shortCondition + sl : na, "Short SL", slColor, style=3, linewidth=2)

// Margin Call. Depending on your leverage, this will mimick a margin call at -80%.
isMargin = input(false, "Margin Call")
leverage = input(1, "Leverage")
long_call = last_open_longCondition - (0.8 + 0.2 * (1/leverage)) / leverage * last_open_longCondition
short_call = last_open_shortCondition + (0.78 + 0.2 * (1/leverage)) / leverage * last_open_shortCondition
long_call_signal = isMargin and crossunder(low, long_call)
short_call_signal = isMargin and crossunder(high, short_call)
marginColor = isMargin and in_longCondition and last_low_short > long_call ? black : isMargin and in_shortCondition and last_high_short < short_call ? black : white
plot(isMargin and in_longCondition ? long_call : na, "Long Margin", marginColor, style=3, linewidth=2)
plot(isMargin and in_shortCondition ? short_call : na, "Short Margin", marginColor, style=3, linewidth=2)

// Get the average price of your open positions and plot them
totalLongs = 0.0
totalLongs := nz(totalLongs[1])
totalShorts = 0.0
totalShorts := nz(totalShorts[1])
averageLongs = 0.0
averageLongs := nz(averageLongs[1])
averageShorts = 0.0
averageShorts := nz(averageShorts[1]) 

if longCondition
    totalLongs := totalLongs + last_open_longCondition
    totalShorts := 0.0

if shortCondition
    totalLongs := 0.0
    totalShorts := totalShorts + last_open_shortCondition

averageLongs := totalLongs / sectionLongConditions
averageShorts := totalShorts / sectionShortConditions

longProfit = averageLongs > 0 and close >= averageLongs ? green : red
shortProfit = averageShorts > 0 and close <= averageShorts ? green : red

plot1 = plot(averageLongs > 0 ? averageLongs : na, color=white)
plot2 = plot(close, color=white)
plot3 = plot(averageShorts > 0 ? averageShorts : na, color=white)

fill(plot1, plot2, color=longProfit, transp=50)
fill(plot2, plot3, color=shortProfit, transp=50)

//Enable this to double your order size every time your pyramid on top of an existing position. (Martingale strategy)
// useMartin = input(true, "Martingale")

// longMartin = 0
// longMartin := nz(longMartin[1])
// shortMartin = 0
// shortMartin := nz(shortMartin[1])

// // Check to see if this is our first order, set the order qty to 1
// if longCondition and sectionLongConditions == 1
//     longMartin := longMartin + 1
//     shortMartin := 0
// if shortCondition and sectionShortConditions == 1
//     longMartin := 0
//     shortMartin := shortMartin + 1

// confirm that this order is being added to an existing order
// if longCondition and sectionLongConditions > 1
//     longMartin := longMartin * 2
// if shortCondition and sectionShortConditions > 1
//     shortMartin := shortMartin * 2

// Close Conditions amalgamation for cleaner plots and signals
// Define the plot colors for each close condition
longCloseCol = na
shortCloseCol = na
longCloseCol := long_tp ? purple : long_sl ? maroon : long_ts ? blue : long_call_signal ? black : longCloseCol[1]
shortCloseCol := short_tp ? purple : short_sl ? maroon : short_ts ? blue : short_call_signal ? black : shortCloseCol[1]

// Create a single close for all the different closing conditions.
long_close = long_tp or long_sl or long_ts or long_call_signal ? 1 : 0
short_close = short_tp or short_sl or short_ts or short_call_signal ? 1 : 0

// Get the time of the last close
last_long_close = na
last_short_close = na
last_long_close := long_close ? time : nz(last_long_close[1])
last_short_close := short_close ? time : nz(last_short_close[1])

// Check for a close since your last open.
if long_close and last_long_close[1] > last_longCondition
    long_close := 0
if short_close and last_short_close[1] > last_shortCondition
    short_close := 0

if testPeriod()
    strategy.entry("Long", strategy.long, qty=qty, when=longCondition)
    strategy.entry("Short", strategy.short, qty=qty, when=shortCondition)

    strategy.close("Long", when=long_close)
    strategy.close("Short", when=short_close)
/@version=3
策略(“移动平均线和/或Bbands bot V1.1”,shorttitle=“策略”,叠加=真,聚合=1000)
//根据您交易的市场、硬币、外汇或商品,使回溯测试数字更清晰。
数量=1
//如果你交易的是一枚altcoin,那么将其设置为真,回测数字现在等于1 satoshi
isALT=输入(假,“Altcoin”)
如果我
数量:=100000000
//如果你在交易外汇,那么将这一点变为现实,回测数字现在等于0.0001美元
isForex=输入(假,“外汇”)
如果是外汇
数量:=10000
//*回溯测试周期选择器|组件*//
//* https://www.tradingview.com/script/eCC1cvxQ-Backtesting-Period-Selector-Component *//
//* https://www.tradingview.com/u/pbergden/ *//
//*修改*//
testStartYear=输入(1,“回溯测试开始年份”)
testStartMonth=输入(8,“反向测试开始月份”)
testStartDay=输入(25,“回溯测试开始日”)
testPeriodStart=时间戳(testStartYear、testStartMonth、testStartDay,0,0)
testStopYear=输入(999999,“回溯测试停止年”)
testStopMonth=输入(9,“反向测试停止月”)
testStopDay=输入(26,“反向测试停止日”)
testPeriodStop=时间戳(testStopYear、testStopMonth、testStopDay、0,0)
testPeriod()=>
时间>=测试周期开始和时间o
r=co?绿色:红色
图烛(o、h、l、c,“黑根阿希”,col,黑色)
//初始打开逻辑需要在开始时设置,因为这受以下大多数设置的影响
长=不适用
短=na
//*移动平均逻辑*\\
//如果您高于或低于移动平均线,则仅将此选项启用为“长”或“短”
useMA=输入(真,“使用移动平均线交叉”)
MA1输入=输入(50,“移动平均值1”)
MA2输入=输入(200,“移动平均2”)
ma1=sma(c,ma1输入)
ma2=sma(c,ma2输入)
maLong=c>ma1和ma1>ma2
maShort=c上部
基本点:=基本点
p1Plot:=上
p2Plot:=较低
如果是UseBands和useMA
长:=c<较低和较长
短:=c>上部和下部
绘图(基本点,颜色=红色)
p1=绘图(p1绘图,颜色=蓝色)
p2=绘图(p2绘图,颜色=蓝色)
填充(p1、p2)
//////////////////////////
//*战略部分*//
//////////////////////////
//计算你的长-短条件,以便更有效地控制金字塔
截面长度=0
sectionLongs:=nz(sectionLongs[1])
截面短路=0
sectionShorts:=nz(sectionShorts[1])
如果长
sectionLongs:=sectionLongs+1
分段短路:=0
如果短
截面长度:=0
分段短路:=分段短路+1
//聚合输入
pyrl=input(1,“聚合小于”)//如果您的计数小于此数字
pyre=input(0,“聚合等于”)//如果您的计数等于此数字
pyrg=input(1000000,“金字塔大于”)//如果您的计数大于此数字
//这些检查可以查看您的信号,并对照上面的金字塔设置进行交叉引用
longCondition=long和sectionLongs=pyrg或long和sectionLongs==pyre
shortCondition=short and sectionShorts=pyrg或short and sectionShorts==pyre
//获取上次开盘多头或空头的价格
最后一次打开时间条件=不适用
最后\u打开\u短条件=na
最后打开的长条件:=长条件?关闭:新西兰(最后一次打开状态[1])
最后打开的快捷条件:=快捷条件?关闭:nz(最后一个打开的快捷条件[1])
//计算你的实际持仓量,比如获得平均订单价格
sectionLongConditions=0
sectionLongConditions:=nz(sectionLongConditions[1])
sectionShortConditions=0
sectionShortConditions:=nz(sectionShortConditions[1])
如果长时间
sectionLongConditions:=sectionLongConditions+1
sectionShortConditions:=0
如果短条件
截面长度条件:=0
sectionShortConditions:=sectionShortConditions+1
//检查你上一个职位是长职位还是短职位
last_longCondition=na
last_shortCondition=na
last_longCondition:=longCondition?时间:新西兰(最后一个条件[1])
最后一个快捷条件:=快捷条件?时间:nz(最后一个条件[1])
in_longCondition=last_longCondition>last_shortCondition
in_shortCondition=last_shortCondition>last_longCondition
//跟踪自上次开仓以来的最高点
最后的高度=不适用
最后一次低=na
最后\u高\u短=不适用
最后\u低\u短=不适用
最后一个高:=不在长状态?na:处于长期状态和(na(上次高[1])或高>新西兰(上次高[1])?高:新西兰(上一次高[1])
最后一个高\u短:=不在短条件下?na:在短条件下和(na(上次高[1])或高>nz(上次高[1])?高:新西兰(上一次高[1])
最后一个低:=不在短条件下?na:处于短条件和(na(最后一个低[1])或低