Supertrend 的 PowerLanguage(MultiCharts)指標範例
[code]// Supertrend Indicator for PowerLanguage (MultiCharts)
// Basic version
Inputs:
ATRLength(10),
Multiplier(3.0);
Variables:
ATR(0),
UpperBand(0),
LowerBand(0),
Trend(1),
Supertrend(0),
PrevSupertrend(0);
// Calculate ATR
ATR = AvgTrueRange(ATRLength);
// Calculate Basic Bands
UpperBand = (High + Low) / 2 + Multiplier * ATR;
LowerBand = (High + Low) / 2 - Multiplier * ATR;
// Supertrend Calculation
If CurrentBar = 1 then
begin
Supertrend = UpperBand;
end
else
begin
if Close > PrevSupertrend then
Supertrend = MaxList(LowerBand, PrevSupertrend)
else
Supertrend = MinList(UpperBand, PrevSupertrend);
if Close > Supertrend then
Trend = 1
else if Close < Supertrend then
Trend = -1;
end;
// Save for next bar
PrevSupertrend = Supertrend;
// Plot
Plot1(Supertrend, "Supertrend");
Plot2(UpperBand, "UpperBand");
Plot3(LowerBand, "LowerBand");
if Trend = 1 then
SetPlotColor(1, Green)
else
SetPlotColor(1, Red);
[/code]這段程式碼會:
計算 ATR。
用 ATR 來生成上下帶。
判斷超趨勢線。
畫出 Supertrend 線並變色(綠色為多頭,紅色為空頭)。 Supertrend(有買賣訊號)[code]
// Supertrend Indicator with Buy/Sell Arrows for PowerLanguage (MultiCharts)
// Advanced version with signals
Inputs:
ATRLength(10),
Multiplier(3.0);
Variables:
ATR(0),
UpperBand(0),
LowerBand(0),
Trend(1),
Supertrend(0),
PrevSupertrend(0),
PrevTrend(1);
// Calculate ATR
ATR = AvgTrueRange(ATRLength);
// Calculate Basic Bands
UpperBand = (High + Low) / 2 + Multiplier * ATR;
LowerBand = (High + Low) / 2 - Multiplier * ATR;
// Supertrend Calculation
If CurrentBar = 1 then
begin
Supertrend = UpperBand;
PrevTrend = 1;
end
else
begin
if Close > PrevSupertrend then
Supertrend = MaxList(LowerBand, PrevSupertrend)
else
Supertrend = MinList(UpperBand, PrevSupertrend);
if Close > Supertrend then
Trend = 1
else if Close < Supertrend then
Trend = -1;
end;
// Detect Trend Change for Buy/Sell Signals
if Trend <> PrevTrend then
begin
if Trend = 1 then
Alert("Buy Signal");
if Trend = -1 then
Alert("Sell Signal");
end;
// Save for next bar
PrevSupertrend = Supertrend;
PrevTrend = Trend;
// Plot Supertrend
Plot1(Supertrend, "Supertrend");
Plot2(UpperBand, "UpperBand");
Plot3(LowerBand, "LowerBand");
if Trend = 1 then
SetPlotColor(1, Green)
else
SetPlotColor(1, Red);
// Plot Buy/Sell Arrows
if Trend <> PrevTrend then
begin
if Trend = 1 then
PlotPaintBar(High, Low, Open, Close, "Buy", Green)
else if Trend = -1 then
PlotPaintBar(High, Low, Open, Close, "Sell", Red);
end;
[/code] Supertrend 自動下單策略(PowerLanguage / MultiCharts)[code]
// Supertrend Auto-Trading Strategy for PowerLanguage (MultiCharts)
Inputs:
ATRLength(10),
Multiplier(3.0);
Variables:
ATR(0),
UpperBand(0),
LowerBand(0),
Trend(1),
Supertrend(0),
PrevSupertrend(0),
PrevTrend(1);
// Calculate ATR
ATR = AvgTrueRange(ATRLength);
// Calculate Basic Bands
UpperBand = (High + Low) / 2 + Multiplier * ATR;
LowerBand = (High + Low) / 2 - Multiplier * ATR;
// Supertrend Calculation
If CurrentBar = 1 then
begin
Supertrend = UpperBand;
PrevTrend = 1;
end
else
begin
if Close > PrevSupertrend then
Supertrend = MaxList(LowerBand, PrevSupertrend)
else
Supertrend = MinList(UpperBand, PrevSupertrend);
if Close > Supertrend then
Trend = 1
else if Close < Supertrend then
Trend = -1;
end;
// --- Auto Trading Logic ---
// When trend changes to up, buy
If Trend = 1 and PrevTrend = -1 then
begin
Buy ("SupertrendBuy") Next Bar at Market;
end;
// When trend changes to down, sell short
If Trend = -1 and PrevTrend = 1 then
begin
SellShort ("SupertrendSell") Next Bar at Market;
end;
// Optional: Exit rules (simple)
// Exit long if trend turns down
If MarketPosition = 1 and Trend = -1 then
begin
Sell ("SupertrendExitLong") Next Bar at Market;
end;
// Exit short if trend turns up
If MarketPosition = -1 and Trend = 1 then
begin
BuyToCover ("SupertrendExitShort") Next Bar at Market;
end;
// Save for next bar
PrevSupertrend = Supertrend;
PrevTrend = Trend;
[/code]趨勢轉為上升時 → 市價做多。
趨勢轉為下跌時 → 市價做空。
趨勢反轉也自動 平倉原來的單。
乾淨純粹的範例,適合再加上風控(如停損、停利)等等。 Supertrend 自動交易策略版 (PowerLanguage / MultiCharts)[code]
// Supertrend Strategy for Auto Trading (MultiCharts PowerLanguage)
// Version: Entry + Exit rules
Inputs:
ATRLength(10),
Multiplier(3.0);
Variables:
ATR(0),
UpperBand(0),
LowerBand(0),
Trend(1),
Supertrend(0),
PrevSupertrend(0),
PrevTrend(1);
// Calculate ATR
ATR = AvgTrueRange(ATRLength);
// Calculate Bands
UpperBand = (High + Low) / 2 + Multiplier * ATR;
LowerBand = (High + Low) / 2 - Multiplier * ATR;
// Supertrend Calculation
If CurrentBar = 1 then
begin
Supertrend = UpperBand;
PrevTrend = 1;
end
else
begin
if Close > PrevSupertrend then
Supertrend = MaxList(LowerBand, PrevSupertrend)
else
Supertrend = MinList(UpperBand, PrevSupertrend);
if Close > Supertrend then
Trend = 1
else if Close < Supertrend then
Trend = -1;
end;
// --- Entry and Exit Rules ---
// Trend Change -> Entry Signals
if Trend <> PrevTrend then
begin
if Trend = 1 then
begin
// Close Short Position first
if MarketPosition < 0 then
BuyToCover ("ExitShort") next bar at market;
// Open Long Position
Buy ("LongEntry") next bar at market;
end
else if Trend = -1 then
begin
// Close Long Position first
if MarketPosition > 0 then
Sell ("ExitLong") next bar at market;
// Open Short Position
SellShort ("ShortEntry") next bar at market;
end;
end;
// Save for next bar
PrevSupertrend = Supertrend;
PrevTrend = Trend;
// --- Optional: Plot Supertrend Line ---
Plot1(Supertrend, "Supertrend");
if Trend = 1 then
SetPlotColor(1, Green)
else
SetPlotColor(1, Red);
[/code][code]
趨勢變多(Trend從-1變1):平空單 ➔ 做多進場
趨勢變空(Trend從1變-1):平多單 ➔ 做空進場
進場用 next bar at market(下一根直接市價單)
自動畫 Supertrend 線,顏色隨趨勢變
[/code]
页:
[1]