首次尝试的策略在回测中未生成订单。

huangapple go评论69阅读模式
英文:

First attempt at a strategy not generating orders in back testing

问题

我是一个非常新的松树编程者(尽管我有C语言类似的语言经验)。

今天我编写了我的第一个用于回测的策略,但不管我怎么做,它都不会在测试范围内生成任何订单。没有编译错误。是否有人可以帮忙看看?

最初,我认为可能是时间范围的问题,但我似乎通过使用时间戳(字符串)覆盖来解决了这个问题,所以我有点不知所措可能是什么问题。顶部的注释解释了我的尝试。如果这是一些非常简单的东西,提前道歉。我仍然在学习的第一周。

// 这个策略有简单的规则 
// 多头条件:
// 1. 价格收盘低于EMA200
// 2. 价格收盘高于Trend Trader Strategy by HPotter线(另一种策略是用前一根柱子的收盘价穿越TTS线进行测试,目前已注释掉)
// 3. STC指标由shayankm制定的是绿色的
// 空头条件:
// 1. 价格收盘高于EMA200
// 2. 价格收盘低于Trend Trader Strategy by HPotter线(另一种策略是用前一根柱子的收盘价穿越TTS线进行测试,目前已注释掉)
// 3. STC指标由shayankm制定的是红色的
// 平仓条件:
// 下一根K线收盘(此策略旨在在1-5分钟时间框架或二元期权上进行高频交易测试)

strategy("具有STC指标的趋势交易策略", overlay=true, initial_capital=1000, commission_type=strategy.commission.percent, commission_value=0.1, calc_on_every_tick=true, default_qty_type=strategy.percent_of_equity, default_qty_value=2, currency=currency.USD, pyramiding=0, backtest_fill_limits_assumption=0, slippage=0, process_orders_on_close=true, max_bars_back=100, linktoseries=false, shorttitle="")

// 回测输入
// startDate = input("0-01-01T00:00:00", "开始日期(YYYY-MM-DD)")
// endDate = input("2022-12-31T23:59:59", "结束日期(YYYY-MM-DD)")

// 将开始和结束日期转换为时间戳
startDateTimeStamp = timestamp("Jan 01 1900 00:00:00")
endDateTimeStamp = timestamp("Jan 01 2100 00:00:00")

// 计算EMA 200
ema200 = ta.ema(close, 200)

// 由HPotter制定的Trend Trader策略
Length = input.int(21, minval=1)
Multiplier = input.float(3, minval=0.000001)
avgTR = ta.wma(ta.atr(1), Length)
highestC = ta.highest(Length)
lowestC = ta.lowest(Length)
hiLimit = highestC[1] - avgTR[1] * Multiplier
loLimit = lowestC[1] + avgTR[1] * Multiplier
trendLine = 0.0
pos = 0.0
trendLine := close > hiLimit and close > loLimit ? hiLimit :
             close < loLimit and close < hiLimit ? loLimit : nz(trendLine[1], close)
pos := close > trendLine[1] ? 1 : close < trendLine[1] ? -1 : nz(pos[1], 0)

// 由shayankm制定的STC指标
EEEEEE = input(12, 'Length')
BBBB = input(26, 'FastLength')
BBBBB = input(50, 'SlowLength')

AAAA(BBB, BBBB, BBBBB) =>
    fastMA = ta.ema(BBB, BBBB)
    slowMA = ta.ema(BBB, BBBBB)
    AAAA = fastMA - slowMA
    AAAA

AAAAA(EEEEEE, BBBB, BBBBB) =>
    AAA = input(0.5)
    var CCCCC = 0.0 // 持久变量
    var DDD = 0.0 // 持久变量
    var DDDDDD = 0.0 // 持久变量
    var EEEEE = 0.0 // 持久变量
    BBBBBB = AAAA(close, BBBB, BBBBB)
    CCC = ta.lowest(BBBBBB, EEEEEE)
    CCCC = ta.highest(BBBBBB, EEEEEE) - CCC
    CCCCC := CCCC > 0 ? (BBBBBB - CCC) / CCCC * 100 : nz(CCCCC[1])
    DDD := na(DDD[1]) ? CCCCC : DDD[1] + AAA * (CCCCC - DDD[1])
    DDDD = ta.lowest(DDD, EEEEEE)
    DDDDD = ta.highest(DDD, EEEEEE) - DDDD
    DDDDDD := DDDDD > 0 ? (DDD - DDDD) / DDDDD * 100 : nz(DDDDDD[1])
    EEEEE := na(EEEEE[1]) ? DDDDDD : EEEEE[1] + AAA * (DDDDDD - EEEEE[1])
    EEEEE

mAAAAA = AAAAA(EEEEEE, BBBB, BBBBB)
mColor = mAAAAA > mAAAAA[1] ? color.new(color.green, 20) : color.new(color.red, 20)

// 空头进场条件
enterShort = close < trendLine[1] and ema200 > close and mColor == color.red // 每个在趋势线下的位置的收盘价
//enterShort = ta.crossunder(close, trendLine) and ema200 > close and mColor == color.red // 趋势线交叉下的收盘价

// 多头进场条件
enterLong = close > trendLine[1] and ema200 < close and mColor == color.green // 每个在趋势线下的位置的收盘价
//enterLong = ta.crossover(close, trendLine) and

<details>
<summary>英文:</summary>

I am a very new Pine coder (though I have experience in c-like languages). 

I have written my first strategy for back testing today, but no matter what I do, it isn&#39;t generating any orders in the testing range. There are no compile errors. Is there anyone out there who might be able to take a look at it?

I initially thought it might be a time range issue, but I seem to have that covered by using the timestamp(string) override, so I am a bit at a loss as to what may be the issue. The comments at the top explain what I am trying to do. Apologies in advance if it is something moronically simple. I am still in my first week of learning.


//@version=5
//This strategy has simple rules
//LONG when
//1. the price closes below the EMA200
//2. the price closes above the Trend Trader Strategy by HPotter line (an alternative strategy is for the close price that crosses over the TTS line in the previous bar - currently commnted out for testing)
//3. the STC Indicator by shayankm is green
//SHORT when
//1. the price closes above the EMA200
//2. the price closes below the Trend Trader Strategy by HPotter line (an alternative strategy is for the close price that crosses below the TTS line in the previous bar - currently commnted out for testing)
//3. the STC Indicator by shayankm is red
//CLOSE when
//the next bar closes (This strategy is intended to be tested for high frequency trading on 1-5 minute time frames or binary options)

strategy("Trend Trader Strategy with STC Indicator", overlay=true, initial_capital=1000, commission_type=strategy.commission.percent, commission_value=0.1, calc_on_every_tick=true, default_qty_type=strategy.percent_of_equity, default_qty_value=2, currency=currency.USD, pyramiding=0, backtest_fill_limits_assumption=0, slippage=0, process_orders_on_close=true, max_bars_back=100, linktoseries=false, shorttitle="")

// Backtesting Inputs
//startDate = input("0-01-01T00:00:00", "Start Date (YYYY-MM-DD)")
//endDate = input("2022-12-31T23:59:59", "End Date (YYYY-MM-DD)")

// Convert start and end dates to timestamps
startDateTimeStamp = timestamp("Jan 01 1900 00:00:00")
endDateTimeStamp = timestamp("Jan 01 2100 00:00:00")

// Calculate EMA 200
ema200 = ta.ema(close, 200)

// Trend Trader Strategy by HPotter
Length = input.int(21, minval=1)
Multiplier = input.float(3, minval=0.000001)
avgTR = ta.wma(ta.atr(1), Length)
highestC = ta.highest(Length)
lowestC = ta.lowest(Length)
hiLimit = highestC[1] - avgTR[1] * Multiplier
loLimit = lowestC[1] + avgTR[1] * Multiplier
trendLine = 0.0
pos = 0.0
trendLine := close > hiLimit and close > loLimit ? hiLimit :
close < loLimit and close < hiLimit ? loLimit : nz(trendLine[1], close)
pos := close > trendLine[1] ? 1 : close < trendLine[1] ? -1 : nz(pos[1], 0)

// STC Indicator by shayankm
EEEEEE = input(12, 'Length')
BBBB = input(26, 'FastLength')
BBBBB = input(50, 'SlowLength')

AAAA(BBB, BBBB, BBBBB) =>
fastMA = ta.ema(BBB, BBBB)
slowMA = ta.ema(BBB, BBBBB)
AAAA = fastMA - slowMA
AAAA

AAAAA(EEEEEE, BBBB, BBBBB) =>
AAA = input(0.5)
var CCCCC = 0.0 //persistent variables
var DDD = 0.0//persistent variables
var DDDDDD = 0.0//persistent variables
var EEEEE = 0.0//persistent variables
BBBBBB = AAAA(close, BBBB, BBBBB)
CCC = ta.lowest(BBBBBB, EEEEEE)
CCCC = ta.highest(BBBBBB, EEEEEE) - CCC
CCCCC := CCCC > 0 ? (BBBBBB - CCC) / CCCC * 100 : nz(CCCCC[1])
DDD := na(DDD[1]) ? CCCCC : DDD[1] + AAA * (CCCCC - DDD[1])
DDDD = ta.lowest(DDD, EEEEEE)
DDDDD = ta.highest(DDD, EEEEEE) - DDDD
DDDDDD := DDDDD > 0 ? (DDD - DDDD) / DDDDD * 100 : nz(DDDDDD[1])
EEEEE := na(EEEEE[1]) ? DDDDDD : EEEEE[1] + AAA * (DDDDDD - EEEEE[1])
EEEEE

mAAAAA = AAAAA(EEEEEE, BBBB, BBBBB)
mColor = mAAAAA > mAAAAA[1] ? color.new(color.green, 20) : color.new(color.red, 20)

// Entry conditions for short positions
enterShort = close < trendLine[1] and ema200 > close and mColor == color.red //the close for every position under the trend line
//enterShort = ta.crossunder(close, trendLine) and ema200 > close and mColor == color.red //the close from the crossunder of the trend line

// Entry conditions for long positions
enterLong = close > trendLine[1] and ema200 < close and mColor == color.green //the close for every position under the trend line
//enterLong = ta.crossover(close, trendLine) and ema200 < close and mColor == color.green //the close from the crossover the trend line

// Execute short trades
if time >= startDateTimeStamp and time < endDateTimeStamp and enterShort
strategy.entry("Short", strategy.short)
//alert("Sell Short", alert.freq_once_per_bar_close)

// Execute long trades
if time >= startDateTimeStamp and time < endDateTimeStamp and enterLong
strategy.entry("Long", strategy.long)
//alert("Buy Long", alert.freq_once_per_bar_close)

// Close positions at the close of the next bar
if barstate.islast
if strategy.position_size[0] < 0 and barstate.isconfirmed
strategy.close("Short")

if strategy.position_size[0] &gt; 0 and barstate.isconfirmed
    strategy.close(&quot;Long&quot;)


</details>


# 答案1
**得分**: 1

你的代码有两个问题。

首要问题是`color.new(color.green, 20)`和`color.green`并不相同。因此,你的进入条件永远不会为`true`,因为你有一个条件像`mColor == color.green`。

你应该做的是,创建一个新变量来判断STC是否处于上升趋势(绿色),然后在你的条件中使用该变量。

```pinescript
mColor = mAAAAA > mAAAAA[1] ? color.new(color.green, 20) : color.new(color.red, 20)
is_green = mAAAAA > mAAAAA[1]
is_red = not is_green

enterShort = close < trendLine[1] and ema200 > close and is_red // 每个位于趋势线下方的位置的关闭
enterLong = close > trendLine[1] and ema200 < close and is_green // 每个位于趋势线下方的位置的关闭

下一个问题是,你不应该在回测脚本中使用barstate.islastbarstate.isconfirmed。在历史数据上,barstate.islast永远不会为true,而barstate.isconfirmed始终为true

英文:

Your code have two issues.

The first and main issue is that color.new(color.green, 20) and color.green are not the same. Therefore your entry conditions are never true because you have a condition like mColor == color.green.

What you should do is, you should create a new variable to see if STC is in an uptrend (green) and use that variable in your condition.

mColor = mAAAAA &gt; mAAAAA[1] ? color.new(color.green, 20) : color.new(color.red, 20)
is_green = mAAAAA &gt; mAAAAA[1]
is_red = not is_green

enterShort = close &lt; trendLine[1] and ema200 &gt; close and is_red //the close for every position under the trend line
enterLong = close &gt; trendLine[1] and ema200 &lt; close and is_green //the close for every position under the trend line

The next issue is, you don't want to use barstate.islast and barstate.isconfirmed in a backtest script. barstate.islast will never be true on the historical bars and barstate.isconfirmed will always be true on the historical bars.

huangapple
  • 本文由 发表于 2023年6月8日 21:00:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定