: | : | :期货量化学习 | :期货量化 |
返回列表 发帖

[MC源码] Supertrend 的 PowerLanguage(MultiCharts)指標範例

[MC源码] Supertrend 的 PowerLanguage(MultiCharts)指標範例

  1. // Supertrend Indicator for PowerLanguage (MultiCharts)
  2. // Basic version
  3. Inputs:
  4.     ATRLength(10),
  5.     Multiplier(3.0);

  6. Variables:
  7.     ATR(0),
  8.     UpperBand(0),
  9.     LowerBand(0),
  10.     Trend(1),
  11.     Supertrend(0),
  12.     PrevSupertrend(0);

  13. // Calculate ATR
  14. ATR = AvgTrueRange(ATRLength);

  15. // Calculate Basic Bands
  16. UpperBand = (High + Low) / 2 + Multiplier * ATR;
  17. LowerBand = (High + Low) / 2 - Multiplier * ATR;

  18. // Supertrend Calculation
  19. If CurrentBar = 1 then
  20. begin
  21.     Supertrend = UpperBand;
  22. end
  23. else
  24. begin
  25.     if Close > PrevSupertrend then
  26.         Supertrend = MaxList(LowerBand, PrevSupertrend)
  27.     else
  28.         Supertrend = MinList(UpperBand, PrevSupertrend);

  29.     if Close > Supertrend then
  30.         Trend = 1
  31.     else if Close < Supertrend then
  32.         Trend = -1;
  33. end;

  34. // Save for next bar
  35. PrevSupertrend = Supertrend;

  36. // Plot
  37. Plot1(Supertrend, "Supertrend");
  38. Plot2(UpperBand, "UpperBand");
  39. Plot3(LowerBand, "LowerBand");

  40. if Trend = 1 then
  41.     SetPlotColor(1, Green)
  42. else
  43.     SetPlotColor(1, Red);
复制代码
這段程式碼會:

    計算 ATR。

    用 ATR 來生成上下帶。

    判斷超趨勢線。

    畫出 Supertrend 線並變色(綠色為多頭,紅色為空頭)。

论坛官方微信、群(期货热点、量化探讨、开户与绑定实盘)
 
期货论坛 - 版权/免责声明   1.本站发布源码(包括函数、指标、策略等)均属开放源码,用意在于让使用者学习程序化语法撰写,使用者可以任意修改语法內容并调整参数。仅限用于个人学习使用,请勿转载、滥用,严禁私自连接实盘账户交易
  2.本站发布资讯(包括文章、视频、历史记录、教材、评论、资讯、交易方案等)均系转载自网络主流媒体,内容仅为作者当日个人观点,本网转载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。本网不对该类信息或数据做任何保证。不对您构成任何投资建议,不能依靠信息而取代自身独立判断,不对因使用本篇文章所诉信息或观点等导致的损失承担任何责任。
  3.本站发布资源(包括书籍、杂志、文档、软件等)均从互联网搜索而来,仅供个人免费交流学习,不可用作商业用途,本站不对显示的内容承担任何责任。请在下载后24小时内删除。如果喜欢,请购买正版,谢谢合作!
  4.龙听期货论坛原创文章属本网版权作品,转载须注明来源“龙听期货论坛”,违者本网将保留追究其相关法律责任的权力。本论坛除发布原创文章外,亦致力于优秀财经文章的交流分享,部分文章推送时若未能及时与原作者取得联系并涉及版权问题时,请及时联系删除。联系方式:http://www.qhlt.cn/thread-262-1-1.html
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

Supertrend(有買賣訊號)
  1. // Supertrend Indicator with Buy/Sell Arrows for PowerLanguage (MultiCharts)
  2. // Advanced version with signals
  3. Inputs:
  4.     ATRLength(10),
  5.     Multiplier(3.0);

  6. Variables:
  7.     ATR(0),
  8.     UpperBand(0),
  9.     LowerBand(0),
  10.     Trend(1),
  11.     Supertrend(0),
  12.     PrevSupertrend(0),
  13.     PrevTrend(1);

  14. // Calculate ATR
  15. ATR = AvgTrueRange(ATRLength);

  16. // Calculate Basic Bands
  17. UpperBand = (High + Low) / 2 + Multiplier * ATR;
  18. LowerBand = (High + Low) / 2 - Multiplier * ATR;

  19. // Supertrend Calculation
  20. If CurrentBar = 1 then
  21. begin
  22.     Supertrend = UpperBand;
  23.     PrevTrend = 1;
  24. end
  25. else
  26. begin
  27.     if Close > PrevSupertrend then
  28.         Supertrend = MaxList(LowerBand, PrevSupertrend)
  29.     else
  30.         Supertrend = MinList(UpperBand, PrevSupertrend);

  31.     if Close > Supertrend then
  32.         Trend = 1
  33.     else if Close < Supertrend then
  34.         Trend = -1;
  35. end;

  36. // Detect Trend Change for Buy/Sell Signals
  37. if Trend <> PrevTrend then
  38. begin
  39.     if Trend = 1 then
  40.         Alert("Buy Signal");

  41.     if Trend = -1 then
  42.         Alert("Sell Signal");
  43. end;

  44. // Save for next bar
  45. PrevSupertrend = Supertrend;
  46. PrevTrend = Trend;

  47. // Plot Supertrend
  48. Plot1(Supertrend, "Supertrend");
  49. Plot2(UpperBand, "UpperBand");
  50. Plot3(LowerBand, "LowerBand");

  51. if Trend = 1 then
  52.     SetPlotColor(1, Green)
  53. else
  54.     SetPlotColor(1, Red);

  55. // Plot Buy/Sell Arrows
  56. if Trend <> PrevTrend then
  57. begin
  58.     if Trend = 1 then
  59.         PlotPaintBar(High, Low, Open, Close, "Buy", Green)
  60.     else if Trend = -1 then
  61.         PlotPaintBar(High, Low, Open, Close, "Sell", Red);
  62. end;
复制代码
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

Supertrend 自動下單策略(PowerLanguage / MultiCharts)
  1. // Supertrend Auto-Trading Strategy for PowerLanguage (MultiCharts)
  2. Inputs:
  3.     ATRLength(10),
  4.     Multiplier(3.0);

  5. Variables:
  6.     ATR(0),
  7.     UpperBand(0),
  8.     LowerBand(0),
  9.     Trend(1),
  10.     Supertrend(0),
  11.     PrevSupertrend(0),
  12.     PrevTrend(1);

  13. // Calculate ATR
  14. ATR = AvgTrueRange(ATRLength);

  15. // Calculate Basic Bands
  16. UpperBand = (High + Low) / 2 + Multiplier * ATR;
  17. LowerBand = (High + Low) / 2 - Multiplier * ATR;

  18. // Supertrend Calculation
  19. If CurrentBar = 1 then
  20. begin
  21.     Supertrend = UpperBand;
  22.     PrevTrend = 1;
  23. end
  24. else
  25. begin
  26.     if Close > PrevSupertrend then
  27.         Supertrend = MaxList(LowerBand, PrevSupertrend)
  28.     else
  29.         Supertrend = MinList(UpperBand, PrevSupertrend);

  30.     if Close > Supertrend then
  31.         Trend = 1
  32.     else if Close < Supertrend then
  33.         Trend = -1;
  34. end;

  35. // --- Auto Trading Logic ---
  36. // When trend changes to up, buy
  37. If Trend = 1 and PrevTrend = -1 then
  38. begin
  39.     Buy ("SupertrendBuy") Next Bar at Market;
  40. end;

  41. // When trend changes to down, sell short
  42. If Trend = -1 and PrevTrend = 1 then
  43. begin
  44.     SellShort ("SupertrendSell") Next Bar at Market;
  45. end;

  46. // Optional: Exit rules (simple)
  47. // Exit long if trend turns down
  48. If MarketPosition = 1 and Trend = -1 then
  49. begin
  50.     Sell ("SupertrendExitLong") Next Bar at Market;
  51. end;

  52. // Exit short if trend turns up
  53. If MarketPosition = -1 and Trend = 1 then
  54. begin
  55.     BuyToCover ("SupertrendExitShort") Next Bar at Market;
  56. end;

  57. // Save for next bar
  58. PrevSupertrend = Supertrend;
  59. PrevTrend = Trend;
复制代码
趨勢轉為上升時 → 市價做多。

趨勢轉為下跌時 → 市價做空。

趨勢反轉也自動 平倉原來的單。

乾淨純粹的範例,適合再加上風控(如停損、停利)等等。
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

Supertrend 自動交易策略版 (PowerLanguage / MultiCharts)
  1. // Supertrend Strategy for Auto Trading (MultiCharts PowerLanguage)
  2. // Version: Entry + Exit rules
  3. Inputs:
  4.     ATRLength(10),
  5.     Multiplier(3.0);

  6. Variables:
  7.     ATR(0),
  8.     UpperBand(0),
  9.     LowerBand(0),
  10.     Trend(1),
  11.     Supertrend(0),
  12.     PrevSupertrend(0),
  13.     PrevTrend(1);

  14. // Calculate ATR
  15. ATR = AvgTrueRange(ATRLength);

  16. // Calculate Bands
  17. UpperBand = (High + Low) / 2 + Multiplier * ATR;
  18. LowerBand = (High + Low) / 2 - Multiplier * ATR;

  19. // Supertrend Calculation
  20. If CurrentBar = 1 then
  21. begin
  22.     Supertrend = UpperBand;
  23.     PrevTrend = 1;
  24. end
  25. else
  26. begin
  27.     if Close > PrevSupertrend then
  28.         Supertrend = MaxList(LowerBand, PrevSupertrend)
  29.     else
  30.         Supertrend = MinList(UpperBand, PrevSupertrend);

  31.     if Close > Supertrend then
  32.         Trend = 1
  33.     else if Close < Supertrend then
  34.         Trend = -1;
  35. end;

  36. // --- Entry and Exit Rules ---
  37. // Trend Change -> Entry Signals
  38. if Trend <> PrevTrend then
  39. begin
  40.     if Trend = 1 then
  41.     begin
  42.         // Close Short Position first
  43.         if MarketPosition < 0 then
  44.             BuyToCover ("ExitShort") next bar at market;
  45.         // Open Long Position
  46.         Buy ("LongEntry") next bar at market;
  47.     end
  48.     else if Trend = -1 then
  49.     begin
  50.         // Close Long Position first
  51.         if MarketPosition > 0 then
  52.             Sell ("ExitLong") next bar at market;
  53.         // Open Short Position
  54.         SellShort ("ShortEntry") next bar at market;
  55.     end;
  56. end;

  57. // Save for next bar
  58. PrevSupertrend = Supertrend;
  59. PrevTrend = Trend;

  60. // --- Optional: Plot Supertrend Line ---
  61. Plot1(Supertrend, "Supertrend");

  62. if Trend = 1 then
  63.     SetPlotColor(1, Green)
  64. else
  65.     SetPlotColor(1, Red);
复制代码
  1. 趨勢變多(Trend從-1變1):平空單 ➔ 做多進場

  2. 趨勢變空(Trend從1變-1):平多單 ➔ 做空進場

  3. 進場用 next bar at market(下一根直接市價單)

  4. 自動畫 Supertrend 線,顏色隨趨勢變
复制代码
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

返回列表