策略示例value-allocation.dsl
12strategy("value-allocation-dsl")34universe_code = input.string("value_allocation", title="Universe Code")5top_n = input.int(5, title="Top N", minval=1, maxval=12)6target_delta = input.float(0.35, title="Target Delta", minval=0.05, maxval=0.8, step=0.01)7qty = input.float(1, title="Contracts", minval=1, maxval=20, step=1)8market_name = input.string("us", title="Options Market", options=["us"])9max_candidates = input.int(12, title="Max Candidates", minval=1, maxval=12)1011symbols = universe.symbols(universe_code)1213plot(len(symbols), title="universe_size", precision=0)14plot(spread.count(), title="open_spreads", precision=0)1516candidate_items = []17for symbol in symbols18 upper_symbol = str.upper(symbol)19 excluded = upper_symbol == "SPY" or upper_symbol == "QQQ" or upper_symbol == "IBIT"20 if not excluded21 pe = request.fundamental("us-stocks", symbol, "pe", "filled")22 pb = request.fundamental("us-stocks", symbol, "pb", "filled")23 price = request.security("us-stocks", symbol, "1d", "close")24 pe_percentile = ta.percentrank_valid(pe, 630, 200)25 pb_percentile = ta.percentrank_valid(pb, 630, 200)26 price_percentile = ta.percentrank_valid(price, 630, 200)27 valuation_percentile = price_percentile28 if not na(pe_percentile) and not na(pb_percentile)29 valuation_percentile = math.avg(pe_percentile, pb_percentile)30 else if not na(pe_percentile)31 valuation_percentile = pe_percentile32 else if not na(pb_percentile)33 valuation_percentile = pb_percentile