有谁能帮我理解一下我写的代码有什么问题吗?
我很抱歉,我是一个新的程序员,我发现调试过程是困难的。我所犯的错误就在标题里。
第39行:“if”不能用作变量或函数名。
//@version=5
strategy(title="Trend Following Strategy", overlay=true, pyramiding = true)
//Trend Filter Variables
ma_slow = ta.ema (close, 200) // moving average
ma_fast = ta.ema (close, 50) // moving average
ma_prev_high= ta.highest(ma_slow, 500) //Previous high of the EMA
trending_Market = ma_slow >= ma_prev_high//Current ma value is greater than previous ma value
//Trigger Condition
[macdLine, macdSig, macdHist ] = ta.macd(close, 12, 26, 9) // get MACD indicator
trigger = ma_fast > ma_slow and close > ma_slow and macdLine < 0 and macdSig <0 and macdLine > macdSig and macdHist[1] > 0 and macdHist[2] > 0
//Stop Loss Variables
prev_lows = ta.lowest(low, 150) //Price action Low
atr = ta.atr(14) //Average True Range - Stop Loss Buffer
price_stop = 0.0 //Stop initially set as null
//Long Condition
longCondition = trending_Market and trigger //go long if market is trending and trigger is hit
if (longCondition)
strategy.entry("long", strategy.long)
//Once in the position declare the below variables
if (strategy.position_size> 0)
// Stop Loss Computation
stopValue = prev_lows - atr //Stop loss value including ATR buffer
price_stop := math.max(stopValue, price_stop[1] ) //Trailing condition for SL
else
price_stop := 0 // If no position set SL to null
//Conditions for entering a second position adjusting for risk already in the portfolio
//If trigger is hit while I have an open position and the trrailing SL has moved so that the position does not contain any risk
//- then buy a full unit of risk
if (strategy.position_size> 0 and longCondition and math.avg(strategy.opentrades.entry_price(1),close)< price_stop)// If SL is above the average price of the 2 entries
strategy.entry("long2_fullunit", strategy.long)
//If trigger is hit while I have an open position and SL is below the average price -
//i.e. there is still risk present from the initial position
if (strategy.position_size> 0 and longCondition and math.avg(strategy.opentrades.entry_price(1),close)> price_stop)// If SL is below the average price
//Declaraing Risk management variables
avg_price = math.avg(strategy.opentrades.entry_price(1),close)//Average price of the initial entry and the current price at which the second postion will execute
lot2 = (avg_price/(avg_price - price_stop))*10000// Lot size per 1 unit of risk at current SL and average price
lot2_qty = lot2/close //Investment amount in share quantity
risk_adjusted_qty = lot2 - strategy.position_size // The difference in the proposed quantity of shares to purchase in the second postion and thequantity of shares already held
strategy.entry("long2_risk_adjusted_unit", strategy.long, qty = risk_adjusted_qty ) // enter the second trade at a risk adjusted ammount, keeping risk always at 1R.
//exit condition
if (strategy.position_size>0) //apply the exit strategy with variables declared upon entry
strategy.exit(id= "Stop Loss", stop= price_stop ) 发布于 2022-09-28 10:04:36
条件结构中的本地块必须缩进四个空格或一个制表符。据我所见,您有三个空格,如果我修复了这些缩进问题,就没有错误。
//@version=5
strategy(title="Trend Following Strategy", overlay=true, pyramiding = true)
//Trend Filter Variables
ma_slow = ta.ema (close, 200) // moving average
ma_fast = ta.ema (close, 50) // moving average
ma_prev_high= ta.highest(ma_slow, 500) //Previous high of the EMA
trending_Market = ma_slow >= ma_prev_high//Current ma value is greater than previous ma value
//Trigger Condition
[macdLine, macdSig, macdHist ] = ta.macd(close, 12, 26, 9) // get MACD indicator
trigger = ma_fast > ma_slow and close > ma_slow and macdLine < 0 and macdSig <0 and macdLine > macdSig and macdHist[1] > 0 and macdHist[2] > 0
//Stop Loss Variables
prev_lows = ta.lowest(low, 150) //Price action Low
atr = ta.atr(14) //Average True Range - Stop Loss Buffer
price_stop = 0.0 //Stop initially set as null
//Long Condition
longCondition = trending_Market and trigger //go long if market is trending and trigger is hit
if (longCondition)
strategy.entry("long", strategy.long)
//Once in the position declare the below variables
if (strategy.position_size> 0)
// Stop Loss Computation
stopValue = prev_lows - atr //Stop loss value including ATR buffer
price_stop := math.max(stopValue, price_stop[1] ) //Trailing condition for SL
else
price_stop := 0 // If no position set SL to null
//Conditions for entering a second position adjusting for risk already in the portfolio
//If trigger is hit while I have an open position and the trrailing SL has moved so that the position does not contain any risk
//- then buy a full unit of risk
if (strategy.position_size> 0 and longCondition and math.avg(strategy.opentrades.entry_price(1),close)< price_stop)// If SL is above the average price of the 2 entries
strategy.entry("long2_fullunit", strategy.long)
//If trigger is hit while I have an open position and SL is below the average price -
//i.e. there is still risk present from the initial position
if (strategy.position_size> 0 and longCondition and math.avg(strategy.opentrades.entry_price(1),close)> price_stop)// If SL is below the average price
//Declaraing Risk management variables
avg_price = math.avg(strategy.opentrades.entry_price(1),close)//Average price of the initial entry and the current price at which the second postion will execute
lot2 = (avg_price/(avg_price - price_stop))*10000// Lot size per 1 unit of risk at current SL and average price
lot2_qty = lot2/close //Investment amount in share quantity
risk_adjusted_qty = lot2 - strategy.position_size // The difference in the proposed quantity of shares to purchase in the second postion and thequantity of shares already held
strategy.entry("long2_risk_adjusted_unit", strategy.long, qty = risk_adjusted_qty ) // enter the second trade at a risk adjusted ammount, keeping risk always at 1R.
//exit condition
if (strategy.position_size>0) //apply the exit strategy with variables declared upon entry
strategy.exit(id= "Stop Loss", stop= price_stop )https://stackoverflow.com/questions/73879231
复制相似问题