close

MA交叉

 

MA向上突破長MA時買進

MA向下突破長MA時賣出

 

 

Var: shortMA(0), longMA(0)   //定義變數

 

shortMA= MA(close,5)    //變數的計算值

longMA= MA(close,10)

 

 

If shortMA cross over longMA then   //條件1:MA向上交叉

   buy next bar at market

End If

 

If shortMA cross down longMA then   //條件2:MA向下交叉

   sell next bar at market

End If

 

 

 

//===================================================================

//讓程式更聰明 ==> 加一個濾網:趨勢向上時,條件1才執行; 趨勢向下時,條件2才執行(加入HugeLongMA)

 

 

Var: shortMA(0), longMA(0), HugeLongMA(0)   //定義變數

Var:HugeUp(False),HugeDown(False)

 

 

shortMA= MA(close,5)    //變數的計算值

longMA= MA(close,10)

HugeMA=MA(close,20)

 

HugeUp= HugeLongMA> HugeLongMA[1]  // 變數(HugeUp)裝變數(HugeLongMA )計算出來的值, 變數(HugeUp)就可以用來做判斷HugeUp= HugeMA - HugeMA[1] >0 這樣寫比較複雜; 可以在增加成HugeUp= HugeMA> HugeMA[1] and HugeMA[5]> HugeMA[10] 進一步判斷出長趨勢才執行 

 

HugeDown=HugeLongMA< HugeLongMA[1]

 

If shortMA cross over longMA and HugeUp then   //條件1:MA向上交叉

   buy next bar at market

End If

 

If shortMA cross down longMA and HugeDown then   //條件2:MA向下交叉

   sell next bar at market

End If

 

 

 

//===================================================================

//加濾網後的隱憂: 加濾網之後表示當中條件都成立之後才會執行動作指令,如果短線向下交叉但趨勢仍然向上,那程式就不會產生賣出訊號,直到兩個訊號同時出現時才會執行賣出動作,等到訊號同時發生,價格可能已經向下一大段了

 

//因此需要停損機制,防止非必要損失產生

//加停損機制(加入變數:MaxLoss(用來決定幾點停損的變數), 原始函數:EntryPrice(最新的成本價), MarketPosition(部位的方向), ExitLong, ExitShort)

 

 

Var: shortMA(0), longMA(0), HugeLongMA(0)  

Var:HugeUp(False),HugeDown(False),MaxLoss(33)  //定義MaxLoss的值是33

// 變數HugeUp要使用來判斷時,HugeUp(False),不用變數的一般形式HugeUp(0)

 

shortMA= MA(close,5)   

longMA= MA(close,10)

HugeMA=MA(close,20)

 

 

 

HugeUp= HugeLongMA> HugeLongMA[1]

HugeDown=HugeLongMA< HugeLongMA[1]

 

If shortMA cross over longMA and HugeUp then 

   buy next bar at market

End If

 

If shortMA cross down longMA and HugeDown then  

   sell next bar at market

End If

 

//停損機制====================

If MarketPosition> 0 then

   ExitLong next bar at EntryPrice(0)-MaxLoss Stop // 突破最新成本價往下33點時平多單

End If

 

If MarketPosition< 0 then

   ExitShort next bar at EntryPrice(0)+MaxLoss Stop // 突破最新成本價往上33點時平空單

End If

 

 

 

 

 

 

//===================================

//策略結果

Var: shortMA(0), longMA(0), HugeLongMA(0)  

Var:HugeUp(False),HugeDown(False),MaxLoss(33)  //定義MaxLoss的值是33

 

 

shortMA= MA(close,5)   

longMA= MA(close,10)

HugeLongMA=MA(close,20)

 

 

 

HugeUp= HugeLongMA>HugeLongMA[1]

HugeDown=HugeLongMA<HugeLongMA[1]

 

If shortMA cross over longMA and HugeUp then 

   buy next bar at market

End If

 

If shortMA cross under longMA and HugeDown then  

   sell next bar at market

End If

 

//停損機制====================

If MarketPosition> 0 then

   ExitLong next bar at EntryPrice(0)-MaxLoss Stop // 突破最新成本價往下33點時平多單

End If

 

If MarketPosition< 0 then

   ExitShort next bar at EntryPrice(0)+MaxLoss Stop // 突破最新成本價往上33點時平空單

End If

 

 

HTS_策略練習_均線交叉均線交叉策略_5分1均線交叉策略_日線均線交叉策略_日線1  

 

arrow
arrow
    全站熱搜

    旋風 發表在 痞客邦 留言(0) 人氣()