forked from hung96ad/MQL4_Experts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
800BB.mq4
162 lines (151 loc) · 5.82 KB
/
800BB.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
//+------------------------------------------------------------------+
//| 800BB.mq4 |
//| Copyright 2019, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property description "EA to trade when price breaks through BB and returns back into the bands. Customizable period."
#include "../Libraries/util.mq4"
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
extern double Risk_Pct = 2; //Risk percentage per trade.
extern int MAGIC = 68792; // Magic number
extern double TP_ATR_Multiplier = 1.5; // ATR multiplier for profit target
extern double SL_ATR_Multiplier = 1; // ATR multiplier for sl
extern int ATR_PERIOD = 14; // ATR period
extern bool DRAW_POSSIBLE_TRADES=false; // Draw a vertical line if there is a possible trade.
extern int BB_PERIOD = 800; // BB period
extern int DEVIATION = 2; // STD deviation for BB
double Slippage=5;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
enum MODE_PRICE_STATUS
{
NOTHING,
CROSSED_BELOW_LOWER,
CROSSED_ABOVE_LOWER,
CROSSED_ABOVE_HIGHER,
CROSSED_BELOW_HIGHER,
};
int PreviousPriceStatus=NOTHING,CurrentPriceStatus=NOTHING;
double point;
static datetime lastbar;
bool TradesOpen;
string longLine="",shortLine="";
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
//---
lastbar=Time[0];
TradesOpen=false;
double NrOfDigits=Digits;
double PipAdjust = 0;
if(NrOfDigits==5 || NrOfDigits==3) PipAdjust=10;
else
if(NrOfDigits==4 || NrOfDigits==2) PipAdjust=1;
point=Point*PipAdjust;
Slippage*=PipAdjust;
return 0;
//---
return(0);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
bool ExitSignal() {return false;}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
if(NewCandle(lastbar))
{
double upperBand = iBands(NULL, 0, BB_PERIOD, DEVIATION, 0, PRICE_CLOSE, MODE_UPPER, 1);
double lowerBand = iBands(NULL, 0, BB_PERIOD, DEVIATION, 0, PRICE_CLOSE, MODE_LOWER, 1);
if(OrdersTotal()==0)
{
TradesOpen=false;
Comment("No trades open");
} else {
Comment("TradesOpen");
}
// if the current candle opened above lowerBand
if(Open[0]>=lowerBand)
{ // Long trades
if(Open[1]<=lowerBand || PreviousPriceStatus==CROSSED_BELOW_LOWER)
{
Print("PRICE closed below BBAND");
if(DRAW_POSSIBLE_TRADES)
{
longLine="GOLONG"+DoubleToStr(Time[0]);
ObjectCreate(longLine,OBJ_VLINE,0,Time[0],0);
ObjectSetInteger(0,longLine,OBJPROP_COLOR,clrGreen);
}
MakeTrade(false,TradesOpen);
}
}
if(Open[0]<=upperBand)
{ // Short trades
if(Open[1]>=upperBand || PreviousPriceStatus==CROSSED_ABOVE_HIGHER)
{
Print("Price closed above BBAND");
if(DRAW_POSSIBLE_TRADES)
{
shortLine="GOLONG"+DoubleToStr(Time[0]);
ObjectCreate(shortLine,OBJ_VLINE,0,Time[0],0);
ObjectSetInteger(0,shortLine,OBJPROP_COLOR,clrRed);
}
MakeTrade(true,TradesOpen);
}
}
if(Open[1] > upperBand || Close[1] > upperBand) {PreviousPriceStatus = CROSSED_ABOVE_HIGHER;}
if(Open[1] < lowerBand || Close[1] < lowerBand) {PreviousPriceStatus = CROSSED_BELOW_LOWER;}
if(Open[1]<upperBand && Close[1]<upperBand && Open[1]>lowerBand && Close[1]>lowerBand)
{PreviousPriceStatus=NOTHING;}
}
}
//+------------------------------------------------------------------+
int MakeTrade(bool sell,bool tradesOpen)
{
double ATR=iATR(NULL,0,ATR_PERIOD,1);
double SlPriceDiff=ATR*SL_ATR_Multiplier;
double pipsToSl=SlPriceDiff/point;
double lots=CalculateLotSize(Risk_Pct,pipsToSl);
double sl,tp,openPrice;
int tradeType,result=-1;
string description="";
bool volumeCheck=CheckVolumeValue(lots,description);
if(!volumeCheck)
{
Print(description);
return -1;
}
RefreshRates();
if(sell)
{
sl = Bid + pipsToSl*point;
tp = Bid - TP_ATR_Multiplier * ATR;
tradeType = OP_SELL;
openPrice = Bid;
} else {
sl = Ask - pipsToSl*point;
tp = Ask + TP_ATR_Multiplier * ATR;
tradeType = OP_BUY;
openPrice = Ask;
}
if(!tradesOpen)
{
result=OrderSend(Symbol(),tradeType,lots,openPrice,Slippage,sl,tp,NULL,MAGIC,0,clrLightBlue);
TradesOpen=true;
NotifySmartPhone("800BB");
}
return result;
}
//+------------------------------------------------------------------+