-
Notifications
You must be signed in to change notification settings - Fork 2
/
edit.mq4
172 lines (146 loc) · 5.29 KB
/
edit.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//+------------------------------------------------------------------+
//| MA Cross.mq4 |
//| Copyright 2013, Eugene Sia |
//| http://eugenesia.co.uk |
//+------------------------------------------------------------------+
/**
* This is a simple Metatrader 4 Expert Advisor I made to revise my
* MQL4, after a long hiatus. Hoping to re-explore automated forex
* trading!
*
* This EA trades based on a moving average crossovers, a common
* breakout strategy. When the short MA crosses the long MA, enter a
* trade.
*/
#property copyright "Copyright 2013, Eugene Sia"
#property link "http://eugenesia.co.uk"
//--- Constant definitions
// Prefix a unique identifier e.g. MACROSS so we don't conflict with
// other predefined constants.
// This defines the magic number for this EA. A magic number can be
// assigned to an order, so that orders opened by this EA have this magic
// number. This is how we distinguish between orders opened by this EA,
// and those opened by the user or other EAs.
// Ref: http://articles.mql4.com/145
#define MACROSS_MAGIC_NUM 20130715
#define MACROSS_OPEN_BUY_SIGNAL 1
#define MACROSS_OPEN_SELL_SIGNAL -1
#define MACROSS_NO_SIGNAL 0
//--- input parameters
// extern keyword defines parameters that can be set by the user in the
// "Expert properties" dialog.
extern int ShortMaPeriod = 10;
extern int LongMaPeriod = 50;
// These are in fractional pips, which are 0.1 of a pip.
extern int SL = 500;
extern int TP = 1600;
// Number of lots for each trade.
extern double Lots = 0.01;
/**
* Get moving average values for the most recent price points.
*
* Params:
* maPeriod: period of the MA.
* numValues: Number of values to insert into the returned array.
* ma: returned array of MA values, with ma[0] being the value for the
* current price, ma[1] the value for the previous bar's price, etc.
*
*/
void MaRecentValues(double& ma[], int maPeriod, int numValues = 3)
{
// i is the index of the price array to calculate the MA value for.
// e.g. i=0 is the current price, i=1 is the previous bar's price.
for (int i=0; i < numValues; i++)
{
ma[i] = iMA(NULL,0,maPeriod,0,MODE_EMA,PRICE_CLOSE,i);
}
}
/**void OCB(int OCB0, double OCB2, int Slippage)
{
if (!OrderClose(OCB0,OCB2,Bid,Slippage,Aqua))
Print("OrderClose BUYda muammo: ",GetLastError());
}
void OCS(int OCS0, double OCS2, int Slippage)
{
if (!OrderClose(OCS0,OCS2,Bid,Slippage,Red))
Print("OrderClose SELLda muammo: ",GetLastError());
}
* Check if we should open a trade.
*
* Returns: +1 to open a buy order, -1 to open a sell order, 0 for no action.
*/
int OpenSignal()
{
int signal;// = MACROSS_NO_SIGNAL;
// Execute only on the first tick of a new bar, to avoid repeatedly
// opening orders when an open condition is satisfied.
//if (Volume[0] > 1) return(0);
//---- get Moving Average values
double shortMa[3];
MaRecentValues(shortMa, ShortMaPeriod, 3);
double longMa[3];
MaRecentValues(longMa, LongMaPeriod, 3);
//---- buy conditions
if (shortMa[2] < longMa[2]
&& shortMa[1] > longMa[1] && Volume[0] == 1)
{
signal = MACROSS_OPEN_BUY_SIGNAL;
}
//---- sell conditions
if (shortMa[2] > longMa[2]
&& shortMa[1] < longMa[1] && Volume[0] == 1)
{
signal = MACROSS_OPEN_SELL_SIGNAL;
}
//----
return(signal);
}
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int signal = OpenSignal();
double TakeProfit=NormalizeDouble(TP,Digits);
double StopLoss=NormalizeDouble(SL,Digits);
int slippage = 30;
if (signal == MACROSS_OPEN_BUY_SIGNAL)
{
Print("Buy signal");
if (!OrderSend(Symbol(),OP_BUY,Lots,Ask,slippage,
Ask-StopLoss*Point, // Stop loss price.
Ask+TakeProfit*Point, // Take profit price.
NULL,MACROSS_MAGIC_NUM,0,Green))
Print(GetLastError());
}
else if (signal == MACROSS_OPEN_SELL_SIGNAL)
{
Print("Sell signal");
if (!OrderSend(Symbol(),OP_SELL,Lots,Bid,slippage,
Bid+StopLoss*Point, // Stop loss price.
Bid-TakeProfit*Point, // Take profit price.
NULL,MACROSS_MAGIC_NUM,0,Red))
Print(GetLastError());
}
//----
return(0);
}
//+------------------------------------------------------------------+