-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdark_point_3.mq4
199 lines (153 loc) · 5.74 KB
/
dark_point_3.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
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
//+------------------------------------------------------------------+
//| dark_point.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
//--- input parameters
input float LOT=0.01;
input int NO_OF_TRADES=3;
input int MAGIC=838;
int OnInit()
{
indicator = iCustom(Symbol(), PERIOD_CURRENT, "Dark Point", 0, 0, 0);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
int total_order = 0;
double lastTP = 0; //keep track of successful tp to prevent repeat entry (in consolidating market)
double lastSL = 0;
double indicator = 0;
void OnTick()
{
detectNewBar();
if(isNewBar){
indicator = iCustom(Symbol(), PERIOD_CURRENT, "Dark Point", 0, 0, 0);
if(indicator){
}else{
Print("Fix Indicator");
}
isNewBar = false;
}
if(TradingTime()){
NewOrder();
}
}
bool TradingTime(){
bool time = false;
if((Hour()>19 || Hour()<6)){
time = true;
}
return time;
}
void NewOrder(){
//take profit
string obj_name = ObjectName(ObjectsTotal()-1);
double tp_price = ObjectGet(obj_name, OBJPROP_PRICE1);
int tp2_object_index = ((ObjectsTotal()-4)/10*6)-1;
obj_name = ObjectName(tp2_object_index+4);
double tp2 = ObjectGet(obj_name, OBJPROP_PRICE1);
int tp1_object_index = ((ObjectsTotal()-4)/10*5)-1;
obj_name = ObjectName(tp1_object_index+4);
double tp1 = ObjectGet(obj_name, OBJPROP_PRICE1);
// Print((ObjectsTotal()-4)+";"+tp1_object_index);
int sl_object_index = ((ObjectsTotal()-4)/10)-1;
obj_name = ObjectName(sl_object_index+4);
double sl = ObjectGet(obj_name, OBJPROP_PRICE1);
// Print(sl+";"+tp_price);
bool orderType = tp_price>sl; //buy condition
double PRICE = (Ask+Bid)/2;
//Print(PRICE+"<>"+total_order);
if((orderType? (PRICE<tp_price && PRICE>sl) : (PRICE>tp_price && PRICE<sl))){ //monitor completion of order
if((TotalOrder(MAGIC)<NO_OF_TRADES) && total_order<NO_OF_TRADES){ // Limit number of trades per signal
if((lastTP!=tp_price) && (lastSL!=sl)){ //allow new orders after last order is complete
tp_price = (total_order==1?tp2:(total_order==0?tp1 : tp_price));
// Print("(orderType,"+obj_name+",tp)SIGNAL("+(orderType?"buy":"Sell")+","+sl+","+tp_price+")");
OrderSend(Symbol(),(orderType?OP_BUY:OP_SELL),LOT,Ask,0,sl,tp_price,0,MAGIC);//0,clrBlack
total_order++;
}
}else if((orderType? (PRICE>tp2) : (PRICE<tp2) )){ //when price reaches tp2 move stoploss to tp1
ModifyOrders(tp1,0,MAGIC); //tp=0 to catch overall trend
int max = MathRound(OrdersTotal()*0.7); //secure 70% of potential profit
CloseOrders(max,MAGIC);
}else if((orderType? (PRICE>tp_price) : (PRICE<tp_price) )){
ModifyOrders(tp2,0,MAGIC);
}
}else{ //order is now complete
if((lastTP!=tp_price) && (lastSL!=sl)){ //allow new orders after last order is complete
total_order=0;
lastTP = tp_price;
lastSL = sl;
Print("Total Order Reset");
}
}
}
void CloseOrders(int max,int magic){
for(int a=0;a<max;a++){
OrderSelect(a,SELECT_BY_POS);
if(OrderMagicNumber() == magic)
{
OrderSelect(a,SELECT_BY_POS);
double PRICE = (OrderType()==OP_BUY?Bid:Ask);
OrderClose(OrderTicket(),OrderLots(),PRICE,3,CLR_NONE);
}
}
}
void ModifyOrders(double sl,double tp,int magic){
for(int a=0;a<OrdersTotal();a++){
OrderSelect(a,SELECT_BY_POS);
if(OrderMagicNumber() == magic)
{
OrderSelect(a,SELECT_BY_POS);
OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0);//,clrBlack
//Print("(sl,tp)MODIFY("+sl+","+tp+")");
}
}
}
double TotalOrder(int magic)
{
double GetTotalOrder = 0;
for(int cnt = 0; cnt < OrdersTotal(); cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber() == magic)
{
GetTotalOrder++;
}
}
return(GetTotalOrder);
}
datetime Old_Time;
datetime New_Time[1];
bool isNewBar = true;
void detectNewBar(){ //monitor bar activity
// copying the last bar time to the element New_Time[0]
int copied=CopyTime(_Symbol,_Period,0,1,New_Time);
if(copied>0) // ok, the data has been copied successfully
{
if(Old_Time!=New_Time[0]) // if old time isn't equal to new bar time
{
Print("We have new bar here ",New_Time[0]," old time was ",Old_Time);
Old_Time=New_Time[0];// saving bar time
isNewBar = true;
}
}
else
{
Print("Error in copying historical times data, error =",GetLastError());
ResetLastError();
}
}