<指標/策略 自動化交易> 篇中出現的代碼 以及 課件下載

pine速成视频 / 1847人浏览 / 1人评论
## macd策略2.0代碼(添加了策略初始化的代碼) >相關的視頻學習資料在大陸版抖音,tiktok,blibli,油管,上搜索'量化金城武'即可找到 - **macd策略2.0的代碼和注釋,添加了策略初始化的代碼** ``` //@version=5 strategy("MACD2.0") //macd指標2.0:將用上節課學到的input()函數把寫死的變量,改成可輸入參數(在設置裡可以修改) //策略初始化時間設置 useDateFilter = input.bool(true, title="啓用回測時間範圍限定", group="回測範圍") backtestStartDate = input.time(timestamp("1 Jan 2000"), title="開始時間", group="回測範圍") backtestEndDate = input.time(timestamp("1 Jan 2222"), title="結束時間", group="回測範圍") inTradeWindow = not useDateFilter or (time >= backtestStartDate and time < backtestEndDate) // 將 inTradeWindow 時間條件加入到你的判斷語句中 //第一步整理哪些是我們的可輸入參數 //1:基礎ema的長度(默認是ema12,ema26) fastlength = input.int(defval = 12,title = '快線長度') slowlength = input.int(defval = 26,title = '慢線長度') //2:係列值來源(默認是close) src = input(close,title = '基礎ema的計算來源') //3:計算macd2的長度 macdlength = input.int(defval = 9,title = '計算macd2的長度') //4:止盈止損設置 zhiying = input.float(defval = 0.02,title = '止盈') zhisun = input.float(defval = 0.01,title = '止損') //第一步計算出收盤價的ema12 fast = ta.ema(src,fastlength) //第二步計算出收盤價ema26 slow = ta.ema(src,slowlength) //第三步計算出macd值(ema12-ema26) macd1 = fast - slow //第四步計算出macd的指數平均值 macd2 = ta.ema(macd1,macdlength) //第五步畫出來 plot(macd1) plot(macd2,color = color.orange) //第六步设定入场条件 shangchuan = ta.crossover(macd1,macd2) //黄金交叉 xiachuan = ta.crossover(macd2,macd1) //死亡交叉 //第七步设定出场条件,算出買漲時候的止盈止損的價格 zy = strategy.position_avg_price * (1+zhiying) //止盈zhiying% zs = strategy.position_avg_price * (1-zhisun) //止损zhisun% //當做空的時候,止盈止損的價格是不一樣的 zy2 = strategy.position_avg_price * (1-zhiying) //止盈zhiying% zs2 = strategy.position_avg_price * (1+zhisun) //止损zhisun% //第八步逻辑执行 if inTradeWindow and shangchuan //加入了時間判斷條件 strategy.entry('long',strategy.long,comment = '做多') strategy.exit('exit','long',limit = zy ,stop = zs,comment_profit = '盈',comment_loss = '损') if inTradeWindow and xiachuan strategy.entry('short',strategy.short,comment = '做空') strategy.exit('exit','short',limit = zy2,stop = zs2,comment_profit = '盈',comment_loss = '损') ``` - **macd指標2.0的代碼和注釋,添加了條件變量和警報** ``` //@version=5 indicator("MACD2.0指標") //第一步計算出收盤價的ema12 fast = ta.ema(close,12) //第二步計算出收盤價ema26 slow = ta.ema(close,26) //第三步計算出macd值(ema12-ema26) macd = fast - slow //第四步計算出macd的指數平均值 emamacd = ta.ema(macd,9) //第五步畫出來 plot(macd) plot(emamacd,color = color.rgb(239, 198, 33)) //創建<上穿>/<下穿>事件 shangchuan = ta.crossover(macd,emamacd) //黄金交叉 xiachuan = ta.crossover(emamacd,macd) //死亡交叉 //用alertcondition()函數添加警報 alertcondition(shangchuan,'黄金交叉/做多') alertcondition(xiachuan,'死亡交叉/做空') ``` - **課件下載:** ##[谷歌雲盤](https://drive.google.com/file/d/1-fyt1DNYvyH2uGcfd27yNdIZzJCL3uun/view?usp=sharing) 百度網盤: 链接:https://pan.baidu.com/s/1iO4AMjlf-P8fK9zfDwhnBQ?pwd=npmz 提取码:npmz

1 条评论

林川HOLD
11月前
inTradeWindow = not useDateFilter or (time >= backtestStartDate and time = backtestStartDate and time

发表评论 取消回复

记住我的信息,方便下次评论
有人回复时邮件通知我