-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMaEA2.mq4
102 lines (93 loc) · 6.82 KB
/
MaEA2.mq4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//+------------------------------------------------------------------+
//| MaEA2.mq4 |
//| Cui Long |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Cui Long"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#include "MaCheck2.mqh"
#include "OrderUtil.mqh"
#include "RsiCheck.mqh"
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
string logMsg;
gTickCount++;
if(IsNewBar())
{
logMsg = StringFormat("OnTick: ============== New bar (%d) ================", gTickCount);
LogInfo(logMsg);
}
// 1. orders accounting
int buyOrdersCnt = QueryCurrentOrders(OP_BUY);
int sellOrdersCnt = QueryCurrentOrders(OP_SELL);
// 2. check for open order
bool bOrderEmpty = (buyOrdersCnt == 0 && sellOrdersCnt == 0);
if(bOrderEmpty)
{
// 2. check for open order
// 没有持仓,检查开仓条件
int nDirect = CheckForOpenRSI();
if(nDirect != -1)
{
logMsg = "Catch open 1: direction = " + IntegerToString(nDirect);
LogInfo(logMsg);
OpenOrder(nDirect);
}
}else
{
// 有持仓,先检查平仓条件
int nDirect = CheckForCloseRSI();
if(nDirect != -1)
{
if(nDirect == OP_BUY)
{
logMsg = "Catch close 1: direction = " + IntegerToString(nDirect);
LogInfo(logMsg);
CloseOrders(nDirect);
CleanBuyOrdersCache();
// OpenOrder(OP_SELL);
}
if(nDirect == OP_SELL)
{
logMsg = "Catch close 2: direction = " + IntegerToString(nDirect);
LogInfo(logMsg);
CloseOrders(nDirect);
CleanSellOrdersCache();
// OpenOrder(OP_BUY);
}
}
// 如果不满足平仓条件,则再检查开仓条件,以便加仓
nDirect = CheckForOpenRSI();
if(nDirect != -1)
{
// 再次查询持仓情况
buyOrdersCnt = QueryCurrentOrders(OP_BUY);
sellOrdersCnt = QueryCurrentOrders(OP_SELL);
logMsg = "Catch open 2: direction = " + IntegerToString(nDirect);
LogInfo(logMsg);
OpenOrder(nDirect);
}
}
}
//+------------------------------------------------------------------+