-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTight Trailing Stops(manual).mq4
151 lines (141 loc) · 5.01 KB
/
Tight Trailing Stops(manual).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
//+------------------------------------------------------------------+
//| Tight Trailing Stops.mq4 |
//| Nicholishen |
//| www.tradingintl.com |
//+------------------------------------------------------------------+
#property copyright "Nicholishen"
#property link "www.tradingintl.com"
#define OrderID 1928378
extern bool UseTightStop=false;
extern int ScalpPips=3;
extern bool UseTrailing = false;
extern double TrailingAct = 6;
extern double TrailingStep = 3;
int TrailPrice;
void TrailingPositions() {
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol() ) {
if (OrderType()==OP_SELL) {
if (OrderOpenPrice()-Ask>TrailingAct*Point && TrailPrice ==0) {
TrailPrice=Ask+TrailingStep*Point;
Print("TRAIL PRICE SET: ",TrailPrice);
if(TrailingStep > 8){
ModifyStopLoss(TrailPrice);
}
}
if (TrailPrice!=0 && Ask+TrailingStep*Point < TrailPrice ){
TrailPrice=Ask-TrailingStep*Point;
Print("TRAIL PRICE MODIFIED: ",TrailPrice);
if(TrailingStep > 8){
ModifyStopLoss(TrailPrice);
}
}
if (TrailPrice != 0 && Ask >= TrailPrice ){
CloseOrder(2);
}
}
if (OrderType()==OP_BUY) {
if (Bid-OrderOpenPrice() > TrailingAct*Point && TrailPrice ==0) {
TrailPrice=Bid-TrailingStep*Point;
Print("TRAIL PRICE MODIFIED: ",TrailPrice);
if(TrailingStep > 8){
ModifyStopLoss(TrailPrice);
}
}
if (TrailPrice!= 0 && Bid-TrailingStep*Point > TrailPrice ){
TrailPrice=Bid-TrailingStep*Point;
Print("TRAIL PRICE MODIFIED: ",TrailPrice);
if(TrailingStep > 8){
ModifyStopLoss(TrailPrice);
}
}
if (TrailPrice != 0 && Bid <= TrailPrice ){
CloseOrder(1);
}
}
}
}
}
}
void CloseOrder(int ord){
for(int i=0;i<OrdersTotal();i++){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if (OrderType()==OP_BUY && OrderMagicNumber()==OrderID){
if (ord==1){
int res = OrderClose(OrderTicket(),OrderLots(),Bid,3,White); // close
TrailPrice=0;
if(res<0){
int error=GetLastError();
//Print("Error = ",ErrorDescription(error));
}
}}
if (OrderType()==OP_SELL && OrderMagicNumber()==OrderID ){
if (ord==2) { // MA BUY signals
res = OrderClose(OrderTicket(),OrderLots(),Ask,3,White); // close
TrailPrice=0;
if(res<0){
error=GetLastError();
// Print("Error = ",ErrorDescription(error));
}
}
}
}
}
void Scalp(){
double res;int error;
for(int i=0;i<OrdersTotal();i++){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol()){
if(OrderType()==OP_BUY){
if(Bid - OrderOpenPrice() >= ScalpPips*Point){
res = OrderClose(OrderTicket(),OrderLots(),Bid,3,White); // close
TrailPrice=0;
if(res<0){
error=GetLastError();
// Print("Error = ",ErrorDescription(error));
}
}
}
if(OrderType()==OP_SELL){
if(OrderOpenPrice() - Ask >= ScalpPips*Point){
res = OrderClose(OrderTicket(),OrderLots(),Ask,3,White); // close
TrailPrice=0;
if(res<0){
error=GetLastError();
// Print("Error = ",ErrorDescription(error));
}
}
}
}
}
}
//+------------------------------------------------------------------+
// Order Modify function
//+------------------------------------------------------------------+
int openpos(){
int orders=0;
for(int i=0;i<OrdersTotal();i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderSymbol()==Symbol() ){
orders++;
}
}
}
return(orders);
}
void ModifyStopLoss(double ldStop) {
bool fm;
double ldOpen=OrderOpenPrice();
double ldTake=OrderTakeProfit();
fm=OrderModify(OrderTicket(), ldOpen, ldStop, ldTake, 0, Pink);
}
int start()
{
if(openpos()>1){Comment("Cannot Track Multiple Orders with TTS");return;}
if(openpos()==0)TrailPrice=0;
if(UseTrailing)TrailingPositions();
if(UseTightStop)Scalp();
return(0);
}
//+------------------------------------------------------------------+