-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathrnmSonicRobot.mql4
199 lines (181 loc) · 9.45 KB
/
rnmSonicRobot.mql4
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <stderror.mqh>
#include <stdlib.mqh>
//+----------------------------------------------------------------------------------------------+
//| SONIC System.mq4|
//| Copyright © 2006 RNM. |
//| |
//+----------------------------------------------------------------------------------------------+
//+----------------------------------------------------------------------------------------------+
//| Variables Setting .- |
//+----------------------------------------------------------------------------------------------+
extern string indName = "SonicR Solid Dragon-Trend (White)";
extern double pipAPer=50;
double point;
datetime currentTimeStamp;
double dineroTotal;
double volumen;
int digits, ticket = 0;
string informacion, simbolo, ordAbierta;
double dragon_min;
double dragon_max;
double dragon;
double trend;
bool primeraOP=true;
//+----------------------------------------------------------------------------------------------+
//| expert initialization function |
//+----------------------------------------------------------------------------------------------+
int init()
{ simbolo = Symbol();
if ((MarketInfo(simbolo,MODE_DIGITS)==2) || (MarketInfo(simbolo,MODE_DIGITS)==3)) { point=0.01; digits=2; }
if ((MarketInfo(simbolo,MODE_DIGITS)==4) || (MarketInfo(simbolo,MODE_DIGITS)==5)) { point=0.0001; digits=4; }
}
//+----------------------------------------------------------------------------------------------+
//| expert start function |
//+----------------------------------------------------------------------------------------------+
int start()
{ if(currentTimeStamp == Time[0]) return (0);
currentTimeStamp = Time[0];
actInfoIndicadores();
if(ticket==0)
{
if (dragon_min > trend && (ordAbierta== "OP_SELL" || primeraOP == true) && anguloCorrecto("BUY") == true && DiffPrecioActual("BUY")== true ) {
primeraOP = false;
abrirOrden("OP_BUY", false);
}
if (dragon_max < trend && (ordAbierta== "OP_BUY" || primeraOP == true) && anguloCorrecto("SELL") == true && DiffPrecioActual("SELL")== true ) {
primeraOP = false;
abrirOrden("OP_SELL", false);
}
}
else
{
if(OrderSelect(ticket,SELECT_BY_TICKET)==true)
{
datetime ctm=OrderCloseTime();
if (ctm>0) {
ticket=0;
return(0);
}
}
else
Print("OrderSelect failed error code is",GetLastError());
if (ordAbierta == "OP_BUY" && dragon_min <= trend ) cerrarOrden(false);
else if (ordAbierta == "OP_SELL" && dragon_max >= trend ) cerrarOrden(false);
}
}
//+----------------------------------------------------------------------------------------------+
//| expert deinitialization function |
//+----------------------------------------------------------------------------------------------+
int deinit()
{ return(0);
}
//+----------------------------------------------------------------------------------------------+
//| Updating the values of the indicators variables TREND / EMOTIONS / SENTIMENT / EXTREME |
//+----------------------------------------------------------------------------------------------+
string actInfoIndicadores()
{
dragon_max=iCustom(NULL, 0, indName, 0, 1);
dragon_min=iCustom(NULL, 0, indName, 1, 1);
dragon=iCustom(NULL, 0, indName, 4, 1);
trend=iCustom(NULL, 0, indName, 5, 1);
}
//+----------------------------------------------------------------------------------------------+
//| Open Order.- |
//+----------------------------------------------------------------------------------------------+
void abrirOrden(string tipoOrden, bool log)
{ RefreshRates();
double volumen = AccountBalance() * point;
double pip = point * pipAPer;
ticket = 0;
while( ticket <= 0)
{ if (tipoOrden == "OP_BUY") ticket=OrderSend(simbolo, OP_BUY, volumen, Ask, 3, 0/*Bid - (point * 100)*/, Ask + (point * 50), "Orden Buy" , 16384, 0, Green);
if (tipoOrden == "OP_SELL") ticket=OrderSend(simbolo, OP_SELL, volumen, Bid, 3, 0/*Ask + (point * 100)*/, Bid - (point * 50), "Orden Sell", 16385, 0, Red);
if (ticket<=0) Print("Error opening order ", tipoOrden , " : ", ErrorDescription( GetLastError() ) );
} ordAbierta = tipoOrden;
if (log==true) mostrarOrden();
}
//+----------------------------------------------------------------------------------------------+
//| Select Order so it can be executed in the next steps.- |
//+----------------------------------------------------------------------------------------------+
bool seleccionarOrden(string accion)
{ if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)==false)
{ Print("Error Seleccionando Orden para ", accion, " : ", ErrorDescription(GetLastError()) );
return(false);
} return( true);
}
//+----------------------------------------------------------------------------------------------+
//| Close Order.- |
//+----------------------------------------------------------------------------------------------+
void cerrarOrden(bool log)
{ if (seleccionarOrden("cerrarOrden")==false) return(0);
double lote=OrderLots();
double price=0;
bool band =false;
if (OrderType()== 0) price = Bid; //OP_BUY - buying position.-
if (OrderType()== 1) price = Ask; //OP_SELL - selling position.-
RefreshRates();
while (band==false)
{ band=OrderClose( ticket, lote, price, 3, CLR_NONE);
if (band==false && GetLastError()==4108) //In case the order was closed on stop loss.
{ ticket=0; band = true;
}
} ticket=0;
if (log==true) mostrarOrden();
}
//+----------------------------------------------------------------------------------------------+
//| Angle check for Dragon Indicator.- |
//+----------------------------------------------------------------------------------------------+
bool anguloCorrecto(string op)
{
double dragon0;
if (op == "BUY"){
dragon0=iCustom(NULL, 0, indName, 4, 3);
if ( (dragon - dragon0) > 0.0002 ) return(true);
else return(false);
}
if (op == "SELL"){
dragon0=iCustom(NULL, 0, indName, 4, 3);
if ( (dragon0-dragon) > 0.0002 ) return(true);
else return(false);
}
}
//+----------------------------------------------------------------------------------------------+
//| Checking current Price against inf or sup Dragon value.- |
//+----------------------------------------------------------------------------------------------+
bool DiffPrecioActual(string op){
if (op == "BUY"){
if ( Ask - dragon_max > 0.0015 ) return(false);
else return(true);
}
if (op == "SELL"){
if ( dragon_min - Bid > 0.0015 ) return(false);
else return(true);
}
}
//+----------------------------------------------------------------------------------------------+
//| Show Order Open prices and info (used for debugging and control).- |
//+----------------------------------------------------------------------------------------------+
void mostrarOrden()
{ if (seleccionarOrden("mostrarOrden")==false) return(0);
Print ("===========================================================");
Print ( informacion );
Print ("Ask = " , DoubleToStr( Ask ,digits ));
Print ("Bid = " , DoubleToStr( Bid ,digits ));
Print ("pipsAPerder = " , pipAPer );
Print ("ticket number = #", OrderTicket() );
Print ("open time = " , TimeToStr( OrderOpenTime()) );
Print ("trade operation= " , OrderType() );
Print ("amount of lots = " , DoubleToStr( OrderLots() ,digits ));
Print ("open price = " , DoubleToStr( OrderOpenPrice() ,digits ));
Print ("Stop Loss = " , DoubleToStr( OrderStopLoss() ,digits ));
Print ("Take Profit = " , DoubleToStr( OrderTakeProfit(),digits ));
Print ("close time = " , OrderCloseTime() );
Print ("close price = " , DoubleToStr( OrderClosePrice(),digits ));
Print ("commission = " , DoubleToStr( OrderCommission(),digits ));
Print ("profit = " , DoubleToStr( OrderProfit() ,digits ));
Print ("comment = " , OrderComment() );
Print ("AccountBalance = " , AccountBalance() );
}
//+----------------------------------------------------------------------------------------------+
//| End.- |
//+----------------------------------------------------------------------------------------------+