//+------------------------------------------------------------------+
//| pobaifanzhuan.mq4 |
//| Copyright 2022, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
int mBars = 50;
double mCloseList[50];
double mSortCloseList[50];
double mTempOpen;
double mSamePriceOffset = 0.0003;
double lots = 0.1;
double radio = 0.0001;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// 半小时执行一次
double open = iOpen(Symbol(),PERIOD_M30,0);
if (mTempOpen == open) {
return;
}
mTempOpen = open;
//填充收盘价集合
initCloseList();
// 获取支撑价位
double support = getSupport();
// 获取压力
double pressure = getPressure();
if (support == 0 && pressure == 0) {
// 没有有效支撑和压力
return;
}
double priceOffset = mSortCloseList[mBars - 1] - mSortCloseList[0];
double close = iClose(Symbol(),PERIOD_M30,1);
// 突破支撑
if (close < support && support != 0) {
OrderSend(Symbol(),OP_SELL, lots, Bid, 3, Bid + priceOffset * 0.6 , Bid - 1.2*priceOffset , " mysell", 0, 0, clrGreen);
}
// 突破压力
if (close > pressure && pressure != 0) {
OrderSend(Symbol(),OP_BUY, lots, Ask, 3, Ask - priceOffset * 0.6 , Ask + 1.2*priceOffset , " mybuy", 0, 0, clrGreen);
}
// 突破支撑反转
double low = iLow(Symbol(),PERIOD_M30,1);
double lastOpen = iOpen(Symbol(),PERIOD_M30,1);
double lastHigh = iHigh(Symbol(),PERIOD_M30,1);
if (low < support && close > support && close > lastOpen) {
double offset = close - low + 5 * radio;
OrderSend(Symbol(),OP_BUY, lots, Ask, 3, Ask - offset , Ask + 2 * offset , " mybuy", 0, 0, clrGreen);
}
//突破压力反转
if (lastHigh > pressure && close < pressure && close < lastOpen) {
double offsetSell = lastHigh - close + 5 * radio;
OrderSend(Symbol(),OP_SELL, lots, Bid, 3, Bid + offsetSell , Bid - 2*offsetSell , " mysell", 0, 0, clrGreen);
}
}
//+------------------------------------------------------------------+
void initCloseList()
{
for (int i = 0; i< mBars; i++) {
mCloseList[i] = Close[i+2];
mSortCloseList[i] = Close[i+2];
}
ArraySort(mSortCloseList);
Print("最高价:"+ mSortCloseList[mBars - 1] + " 最低价:"+mSortCloseList[0]);
Comment("最高价:"+ mSortCloseList[mBars - 1] + " 最低价:"+mSortCloseList[0]);
}
double getSupport()
{
double low1 = mSortCloseList[0];
double low2 = mSortCloseList[1];
Print("low1:"+low1 + " -- low2:"+ low2);
if(MathAbs(low1 - low2) <= mSamePriceOffset && checkKValid(low1, low2)){
return low1;
}
return 0;
}
double getPressure()
{
double high1 = mSortCloseList[mBars - 1];
double high2 = mSortCloseList[mBars - 2];
Print("high1:"+high1 + " -- high2:"+ high2);
if(MathAbs(high1 - high2) <= mSamePriceOffset && checkKValid(high1, high2)){
return high1;
}
return 0;
}
bool checkKValid(double price1, double price2){
int positionK1;
int positionK2;
for(int i = 0; i < mBars; i++) {
if (mCloseList[i] == price1) {
positionK1 = i;
} else if (mCloseList[i] == price2) {
positionK2 = i;
}
}
// 两个K线距离大于2,且不是首尾的K线就是有效支撑和压力
if(MathAbs(positionK1 - positionK2) > 2 && positionK1 != 0 && positionK1 != mBars - 1
&& positionK2 != 0 && positionK2 != mBars - 1) {
return true;
}
return false;
}