itsadrianxv/tradeblazer_skills / skills/tb-bar-strategy
tb-bar-strategy
VerifiedUse when generating, modifying, explaining, or reviewing TBQ3 Bar-based trading strategies with OnBar, OnInit, OnReady, entries, exits, MarketPosition, stop profit/loss, sizing, slippage, commission, multi-symbol, or cross-period logic.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 2.8 KB |
| references/bar-patterns.md | markdown | 3.6 KB |
| references/bar-pitfalls.md | markdown | 2.1 KB |
references/bar-patterns.md
View on GitHub# Bar 策略短模板
这些模板用于提示结构和关键约束。正式生成前,复杂函数语义仍应查 `docs` 原文。
## 四周突破
```cpp
Vars
Numeric highLine;
Numeric lowLine;
Events
OnBar(ArrayRef<Integer> indexs)
{
highLine = Highest(High[1], 20);
lowLine = Lowest(Low[1], 20);
If(MarketPosition <> 1 And High >= highLine)
Buy(1, Max(Open, highLine));
If(MarketPosition <> -1 And Low <= lowLine)
SellShort(1, Min(Open, lowLine));
}
```
要点:用 `High[1]`、`Low[1]` 排除当前 Bar;用 `MarketPosition` 避免突破后连续加仓。
## 均线交叉
```cpp
Params
Integer fastLen(10);
Integer slowLen(30);
Vars
Numeric fastMa;
Numeric slowMa;
Events
OnBar(ArrayRef<Integer> indexs)
{
fastMa = Average(Close, fastLen);
slowMa = Average(Close, slowLen);
If(MarketPosition <> 1 And fastMa CrossOver slowMa)
Buy(1, Open);
If(MarketPosition <> -1 And slowMa CrossOver fastMa)
SellShort(1, Open);
}
```
要点:如果不确认 `CrossOver` 语法,查函数文档或改用文档支持的函数写法。
## 固定保证金开仓
```cpp
Params
Numeric money(10); // 固定保证金,单位万
Vars
Numeric myPrice;
Numeric lots;
Events
OnInit()
{
SetMarginRate(0.1);
SetInitCapital(1000000);
}
OnBar(ArrayRef<Integer> indexs)
{
myPrice = Open;
lots = IntPart(money * 10000 / (myPrice * ContractUnit * BigPointValue * MarginRatio));
If(lots < 1) lots = 1;
If(MarketPosition <> 1)
Buy(lots, Open);
}
```
要点:固定市值开仓不乘 `MarginRatio`;固定保证金开仓需要乘 `MarginRatio`。
## 止盈止损
```cpp
Vars
Numeric minPoint;
Numeric entryPrice;
Numeric exitPrice;
Numeric takeProfitSet(30);
Numeric stopLossSet(20);
Events
OnBar(ArrayRef<Integer> indexs)
{
minPoint = MinMove * PriceScale;
entryPrice = AvgEntryPrice;
If(MarketPosition == 1 And BarsSinceEntry >= 1)
{
If(High >= entryPrice + takeProfitSet * minPoint)
{
exitPrice = entryPrice + takeProfitSet * minPoint;
If(Open > exitPrice) exitPrice = Open;
Sell(0, exitPrice);
}
Else If(Low <= entryPrice - stopLossSet * minPoint)
{
exitPrice = entryPrice - stopLossSet * minPoint;
If(Open < exitPrice) exitPrice = Open;
Sell(0, exitPrice);
}
}
Else If(MarketPosition == -1 And BarsSinceEntry >= 1)
{
If(Low <= entryPrice - takeProfitSet * minPoint)
{
exitPrice = entryPrice - takeProfitSet * minPoint;
If(Open < exitPrice) exitPrice = Open;
BuyToCover(0, exitPrice);
}
Else If(High >= entryPrice + stopLossSet * minPoint)
{
exitPrice = entryPrice + stopLossSet * minPoint;
If(Open > exitPrice) exitPrice = Open;
BuyToCover(0, exitPrice);
}
}
}
```
要点:止盈和止损同时满足时,Bar 级数据无法判断先后,应说明风险或切换更小周期。
## 平仓延迟反手
```cpp
Vars
Bool needLong(False);
Bool needShort(False);
Events
OnBar(ArrayRef<Integer> indexs)
{
If(/* 多头信号 */)
{
If(MarketPosition == -1)
{
BuyToCover(0, Open);
needLong = True;
}
Else If(MarketPosition == 0)
Buy(1, Open);
}
If(needLong And MarketPosition == 0 And BarsSinceExit >= 1)
{
Buy(1, Open);
needLong = False;
}
}
```
要点:延迟反手要明确状态变量何时置位和清除,避免同一 Bar 既平又开造成回测歧义。
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |