這支 當沖 EA 運用了不同指標背離作為進出場的條件 在今年表現得相當突出亮眼
多單進場方式 ~ KD 低檔背離 掛單突破進場做多。
空單進場方式 ~ KD 高檔背離 掛單突破進場做空。
#include <Trade\Trade.mqh>
#include <MagicMT5_函數庫V1.mqh>
ENUM_TIMEFRAMES 時間週期 = PERIOD_M30 ;
int OnInit()
{
LoadEA = TimeCurrent();
return(INIT_SUCCEEDED);
}
void OnTick()
{
if(AccountInfoDouble(ACCOUNT_BALANCE) <= 資金風控)
{
Alert("********** 資金不足 *************");
return;
}
BarNumber = iBarShift(Symbol(),時間週期,LoadEA);
BarSinceExit = BarNumber-CloseOrderNo ;
if((BarNumber == 1 && BarNumber != JudgeNo))
{
多單進場單號 = Buy_at_MARKET(Symbol(),Lots,0,0,"1st_K",MagicNumber) ;
LX_CloseByTicket(多單進場單號,Lots) ;
CloseOrderNo = iBarShift(Symbol(),時間週期,LoadEA);
}
if(BarNumber != JudgeNo)
{
換K棒();
//時間濾網
允許交易時段 = (getTM_hour(TimeCurrent()) >= 8 );
}
Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
AccountBalance=AccountInfoDouble(ACCOUNT_BALANCE);
Tickvalue=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE);
SP = NormalizeDouble(MathAbs(Ask-Bid),Digits()) ;
//+------------------------------------------------------------------+
//|計算交易手數
//+------------------------------------------------------------------+
if(Lots_AutoCal == true)
{
Lots = get_dynamic_lot_size(Lots_Even,Symbol(),RiskPercent,AccountBalance,SL) ;
Lots = MathMin(0.3,MathMax(0.01,Lots)) ;
}
if(允許交易時段 == true)
{
//+------------------------------------------------------------------+
//|多單進場 KD 低檔背離
//+------------------------------------------------------------------+
LE_Cond = (KD_LDiv_01()) ;
{
if(多單部位() == 0 && 空單部位() == 0 && BarNumber != OrderBarNo)
{
買入價格 = NormalizeDouble(MathMax(High[1],MathMax(High[2],High[3])),Digits()) ;
if(BarSinceExit > 1 && EntriesToday(MagicNumber,Symbol()) < 1)
{
// 空手+掛單突破買入
if(Close[1] < 買入價格)
{
多單進場單號 = Buy_at_STOP(Symbol(),MagicNumber,買入價格,Lots,TP,SL,"BUY STOP",3600) ;
OrderBarNo = iBarShift(Symbol(),時間週期,LoadEA);
}
} // end of BarSinceExit > 1} // end of 空手且不同根K棒() == true
} // end of LE_Cond == true
//+------------------------------------------------------------------+
//|空單進場 KD 高檔背離
//+------------------------------------------------------------------+
SE_Cond = (KD_HDiv_01()) ;
if(SE_Cond == true)
{
if(多單部位() == 0 && 空單部位() == 0 && BarNumber != OrderBarNo)
{
賣出價格 = NormalizeDouble((OpenD[1]+OpenD[2])*0.5,Digits());
if(BarSinceExit > 1 && EntriesToday(MagicNumber,Symbol()) < 1)
{
// 空手+掛單突破賣出
if(Close[1] > 賣出價格)
{
空單進場單號 = Short_at_STOP(Symbol(),MagicNumber,賣出價格,Lots,TP,SL,"Short STOP",3600) ;
OrderBarNo = iBarShift(Symbol(),時間週期,LoadEA);
} } //end of BarSinceExit > 1
} // end of 空手且不同根K棒() == true
} // end of SE_Cond == true
} //end of 允許交易時段 == true
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
if(當沖 == true && (getTM_hour(TimeCurrent()) == 當沖出場時間 || getTM_hour(TimeCurrent()) == 1))
{
if(多單部位() > 0 || 空單部位() > 0)
{
當沖平倉();
}
}
{
//+------------------------------------------------------------------+
//| Buy Exit Method 多單 以MACD 高檔背離及固定停損平倉
//+------------------------------------------------------------------+
多單進場價格 = LE_EntryPrice(MagicNumber,多單進場單號);
多單停利價格 = NormalizeDouble(多單進場價格 + TP * Point(),Digits()) ;
多單停損價格 = NormalizeDouble(多單進場價格 - SL * Point(),Digits()) ;
LX_Cond = ((MACD_HDiv_02() == true) || (Close[1] >= 多單停損價格 && Bid < 多單停損價格)) ;
//---------------------------------------------------------多單出場
if(LX_Cond == true && BarNumber != CloseOrderNo && BarNumber != OrderBarNo && LE_BarsSinceEntry(MagicNumber,多單進場單號,時間週期) > 0)
{
LX_CloseByTicket(多單進場單號,Lots) ;
if(多單部位() == 0)
{
CloseOrderNo = iBarShift(Symbol(),時間週期,LoadEA) ;
}
}
} // end of 多單部位() > 0
if(空單部位() > 0)
{
//+------------------------------------------------------------------+
//| Short Exit Method 空單 以RSI 低檔背離及固定停損平倉
//+------------------------------------------------------------------+
空單進場價格 = SE_EntryPrice(MagicNumber,空單進場單號);
空單停利價格 = NormalizeDouble(空單進場價格 - TP * Point(),Digits()) ;
空單停損價格 = NormalizeDouble(空單進場價格 + SL * Point(),Digits()) ;
SX_Cond = ( (RSI_LDiv_02() == true) || (Close[1] <= 空單停損價格 && Ask > 空單停損價格)) ;
if(SX_Cond == true && BarNumber != CloseOrderNo && SE_BarsSinceEntry(MagicNumber,空單進場單號,時間週期) > 0)
{
SX_CloseByTicket(空單進場單號,Lots) ;
if(空單部位() == 0)
{
CloseOrderNo = iBarShift(Symbol(),時間週期,LoadEA) ;
}
}
} // end of 空單部位() > 0
JudgeNo = iBarShift(Symbol(),時間週期,LoadEA);
} //end of onTick
//+------------------------------------------------------------------+
//| 自訂函數庫 |
//+------------------------------------------------------------------+
int 多單部位()
{
int count;
count = get_TradeCounts(Symbol(), MagicNumber,POSITION_TYPE_BUY) ;
return count ;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int 空單部位()
{
int count;
count = get_TradeCounts(Symbol(), MagicNumber,POSITION_TYPE_SELL) ;
return count ;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void 換K棒()
{
// 新K棒刪除所有掛單
if(total_pending_order_count(Symbol(), MagicNumber,-1) != 0)
{
delete_pending_orders_all(Symbol(), MagicNumber, -1, 0x0000ff);
}
Set_OHLC_Bar_Series(); //保留
Set_OHLC_Day_Series(); //保留
Get_OHLC_Bar(10) ; //保留
Get_OHLC_Day(10) ; //保留
set_BarInfo(); //保留
set_KD() ;
//+--------------------------------------------------------+
//| 設定 開高低收 陣列序列
//+--------------------------------------------------------+
//********BarPrice Array variables
double Open[], High[], Low[], Close[],Range[],Body[],UPshadow[],DNshadow[] ;
double OpenD[], HighD[], LowD[], CloseD[] ;
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void Set_OHLC_Bar_Series()
{
ArraySetAsSeries(Open,true);
ArraySetAsSeries(High,true);
ArraySetAsSeries(Low,true);
ArraySetAsSeries(Close,true);
}
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void Get_OHLC_Bar(int argCount)
{
get_OpenData(Symbol(),時間週期,argCount,Open) ;
get_HighData(Symbol(),時間週期,argCount,High) ;
get_LowData(Symbol(),時間週期,argCount,Low) ;
get_CloseData(Symbol(),時間週期,argCount,Close) ;
}
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void set_BarInfo()
{
ArrayResize(Range,ArraySize(Open)) ;
ArraySetAsSeries(Range,true) ;
ArrayResize(Body,ArraySize(Open)) ;
ArraySetAsSeries(Body,true) ;
ArrayResize(UPshadow,ArraySize(Open)) ;
ArraySetAsSeries(UPshadow,true) ;
ArrayResize(DNshadow,ArraySize(Open)) ;
ArraySetAsSeries(DNshadow,true) ;
for(int i = 0; i < ArraySize(Open)-1; i=i+1)
{
Range[i] = High[i]-Low[i];
Body[i] = MathAbs(Close[i]-Open[i]);
UPshadow[i] = High[i]-MathMax(Close[i],Open[i]);
DNshadow[i] = MathMin(Close[i],Open[i])-Low[i];
}
}
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void Set_OHLC_Day_Series()
{
ArraySetAsSeries(OpenD,true);
ArraySetAsSeries(HighD,true);
ArraySetAsSeries(LowD,true);
ArraySetAsSeries(CloseD,true);
}
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void Get_OHLC_Day(int argCount)
{
get_OpenData(Symbol(),PERIOD_D1,argCount,OpenD) ;
get_HighData(Symbol(),PERIOD_D1,argCount,HighD) ;
get_LowData(Symbol(),PERIOD_D1,argCount,LowD) ;
get_CloseData(Symbol(),PERIOD_D1,argCount,CloseD) ;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void 當沖平倉()
{
if((getTM_hour(TimeCurrent()) == 當沖出場時間 || getTM_hour(TimeCurrent()) == 1))
{
if(多單部位() > 0 && BarNumber != CloseOrderNo)
LX_CloseByTicket(多單進場單號,Lots) ;
if(空單部位() > 0 && BarNumber != CloseOrderNo)
SX_CloseByTicket(空單進場單號,Lots) ;
CloseOrderNo = iBarShift(Symbol(),時間週期,LoadEA) ;
}
}
//| 指標及通道計算
//+------------------------------------------------------------------+
int h_KA2,h_DA2,h_KB2,h_DB2 ;
double a_KA2[], a_DA2[], a_JA2[], a_KB2[], a_DB2[], a_JB2[] ;
void set_KD()
{
h_KA2 = iStochastic(Symbol(),時間週期,LenA2,3,3,MODE_EMA,STO_LOWHIGH) ;
ArraySetAsSeries(a_KA2,true);
h_DA2 = iStochastic(Symbol(),時間週期,LenA2,3,3,MODE_EMA,STO_LOWHIGH) ;
ArraySetAsSeries(a_DA2,true);
h_KB2 = iStochastic(Symbol(),時間週期,LenB2,3,3,MODE_SMA,STO_LOWHIGH) ;
ArraySetAsSeries(a_KB2,true);
h_DB2 = iStochastic(Symbol(),時間週期,LenB2,3,3,MODE_SMA,STO_LOWHIGH) ;
ArraySetAsSeries(a_DB2,true);
get_IndexData(h_KA2,0,0,5,a_KA2) ;
get_IndexData(h_DA2,1,0,5,a_DA2) ;
get_IndexData(h_KB2,0,0,5,a_KB2) ;
get_IndexData(h_DB2,1,0,5,a_DB2) ;
ArraySetAsSeries(a_JA2, true);
ArrayResize(a_JA2,ArraySize(a_KA2)) ;
for(int i = 0;i < ArraySize(a_JA2); i++)
{
a_JA2[i] = a_DA2[i]*3-a_KA2[i]*2 ;
}
ArraySetAsSeries(a_JB2, true);
ArrayResize(a_JB2,ArraySize(a_KB2)) ;
for(int i = 0;i < ArraySize(a_JB2); i++)
{
a_JB2[i] = a_DB2[i]*3-a_KB2[i]*2 ;
}
}
//+------------------------------------------------------------------+
//| 進出場條件
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| RSI 低檔背離
//+------------------------------------------------------------------+
bool RSI_LDiv_02()
{
int h_rsi1,h_rsi2 ;
double a_rsi1[],a_rsi2[] ;
int low1,low2 ;
h_rsi1 = iRSI(Symbol(),PERIOD_CURRENT,LenA1,PRICE_CLOSE) ;
ArrayResize(a_rsi1, LenA1);
ArraySetAsSeries(a_rsi1,true) ;
h_rsi2 = iRSI(Symbol(),PERIOD_CURRENT,LenA1*2,PRICE_CLOSE) ;
ArrayResize(a_rsi2, LenA1*2);
ArraySetAsSeries(a_rsi2,true) ;
low1 = iLowest(Symbol(), 時間週期,MODE_LOW,LenA1,1);
low2 = iLowest(Symbol(), 時間週期,MODE_LOW,LenA1*2,1);
get_IndexData(h_rsi1,0,0,ArraySize(a_rsi1),a_rsi1) ;
get_IndexData(h_rsi2,0,0,ArraySize(a_rsi2),a_rsi2) ;
if((a_rsi1[low1] > a_rsi2[low2] && low1 == low2))
{
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| MACD 高檔背離
//+------------------------------------------------------------------+
bool MACD_HDiv_02()
{
int h_dif1,h_macd1 ;
double a_dif1[],a_macd1[],a_osc1[],a_dif2[],a_macd2[],a_osc2[] ;
int high1,high2 ;
high1 = iHighest(Symbol(), 時間週期,MODE_HIGH,LenA1,1);
high2 = iHighest(Symbol(), 時間週期,MODE_HIGH,LenA1 * 2,1);
h_dif1 = iMACD(Symbol(),時間週期,12,26,9,PRICE_WEIGHTED);
ArraySetAsSeries(a_dif1, true);
ArraySetAsSeries(a_dif2, true);
h_macd1 = iMACD(Symbol(),時間週期,12,26,9,PRICE_WEIGHTED);
ArraySetAsSeries(a_macd1, true);
ArraySetAsSeries(a_macd2, true);
get_IndexData(h_dif1,0,0,LenA1,a_dif1) ;
get_IndexData(h_macd1,1,0,LenA1,a_macd1) ;
get_IndexData(h_dif1,0,0,LenA1*2,a_dif2) ;
get_IndexData(h_macd1,1,0,LenA1*2,a_macd2) ;
ArrayResize(a_osc1, ArraySize(a_dif1));
ArraySetAsSeries(a_osc1, true);
ArrayResize(a_osc2, ArraySize(a_dif2));
ArraySetAsSeries(a_osc2, true);
for(int i = 0;i < ArraySize(a_osc1); i++)
{
a_osc1[i] = a_dif1[i]-a_macd1[i] ;
}
for(int i = 0; i < ArraySize(a_osc2); i=i+1)
{
a_osc2[i] = a_dif2[i]-a_macd2[i] ;
}
if(a_osc1[high1] < a_osc2[high2] && high1 == high2)
{
return true;
}
return false;
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| KD 高檔背離
//+------------------------------------------------------------------+
bool KD_HDiv_01()
{
if((ArrayMaximum(High) !=0 && ArrayMaximum(High) <= 2) && (ArrayMaximum(a_KA2) > 2) && a_KA2[1] < 70)
{
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| KD 低檔背離
//+------------------------------------------------------------------+
bool KD_LDiv_01()
{
if((ArrayMinimum(Low) !=0 && ArrayMinimum(Low) <= 2) && (ArrayMinimum(a_KA2) > 2) && a_KA2[1] > 30)
{
return true;
}
return false;
}
//+------------------------------------------------------------------+
交易商品 XAUUSD 樣本內區間 2019/1/1 ~2022/12/31 交易手數 固定 0.1手
當沖
沒有留言:
張貼留言