forked from KonzACDC/MQL5_MT5_EA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrailingParabolicSAR.mq5
187 lines (186 loc) · 7.08 KB
/
TrailingParabolicSAR.mq5
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
//+------------------------------------------------------------------+
//| TrailingParabolicSAR.mq5 |
//| 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"
//+------------------------------------------------------------------+
//| Include |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include <Expert\Expert.mqh>
#include <Expert\Trailing\TrailingParabolicSAR.mqh>
//---
CTrade m_trade; // trading object
CSymbolInfo m_symbol; // symbol info object
CPositionInfo m_position; // trade position object
//+------------------------------------------------------------------+
//| Inputs |
//+------------------------------------------------------------------+
//--- inputs for trailing
input double InpLots =0.01; // Lots
input double Trailing_ParabolicSAR_Step =0.02; // Speed increment
input double Trailing_ParabolicSAR_Maximum=0.2; // Maximum rate
//---
bool Expert_EveryTick=false;
//+------------------------------------------------------------------+
//| Global expert object |
//+------------------------------------------------------------------+
CExpert ExtExpert;
//+------------------------------------------------------------------+
//| Initialization function of the expert |
//+------------------------------------------------------------------+
int OnInit()
{
//--- Initializing expert
if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,0))
{
//--- failed
printf(__FUNCTION__+": error initializing expert");
ExtExpert.Deinit();
return(-1);
}
//--- Creation of trailing object
CTrailingPSAR *trailing=new CTrailingPSAR;
if(trailing==NULL)
{
//--- failed
printf(__FUNCTION__+": error creating trailing");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//--- Add trailing to expert (will be deleted automatically))
if(!ExtExpert.InitTrailing(trailing))
{
//--- failed
printf(__FUNCTION__+": error initializing trailing");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//--- Set trailing parameters
trailing.Step(Trailing_ParabolicSAR_Step);
trailing.Maximum(Trailing_ParabolicSAR_Maximum);
//--- Tuning of all necessary indicators
if(!ExtExpert.InitIndicators())
{
//--- failed
printf(__FUNCTION__+": error initializing indicators");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//---
bool res=false;
{
ObjectCreate(0,"BUY",OBJ_BUTTON,0,0,0);
ObjectSetInteger(0,"BUY",OBJPROP_XDISTANCE,ChartGetInteger(0,CHART_WIDTH_IN_PIXELS)-102);
ObjectSetInteger(0,"BUY",OBJPROP_YDISTANCE,37);
ObjectSetString(0,"BUY",OBJPROP_TEXT,"BUY");
ObjectSetInteger(0,"BUY",OBJPROP_BGCOLOR,clrMediumSeaGreen);
ObjectCreate(0,"SELL",OBJ_BUTTON,0,0,0);
ObjectSetInteger(0,"SELL",OBJPROP_XDISTANCE,ChartGetInteger(0,CHART_WIDTH_IN_PIXELS)-50);
ObjectSetInteger(0,"SELL",OBJPROP_YDISTANCE,37);
ObjectSetString(0,"SELL",OBJPROP_TEXT,"SELL");
ObjectSetInteger(0,"SELL",OBJPROP_BGCOLOR,clrDarkOrange);
}
res=true;
//--- ok
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Deinitialization function of the expert |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ExtExpert.Deinit();
}
//+------------------------------------------------------------------+
//| "Tick" event handler function |
//+------------------------------------------------------------------+
void OnTick()
{
ExtExpert.OnTick();
//---
if(ObjectGetInteger(0,"BUY",OBJPROP_STATE)!=0)
{
ObjectSetInteger(0,"BUY",OBJPROP_STATE,0);
//--- check for long position (BUY) possibility
if(LongOpened())
return;
}
if(ObjectGetInteger(0,"SELL",OBJPROP_STATE)!=0)
{
ObjectSetInteger(0,"SELL",OBJPROP_STATE,0);
//--- check for short position (SELL) possibility
if(ShortOpened())
return;
}
}
//+------------------------------------------------------------------+
//| "Trade" event handler function |
//+------------------------------------------------------------------+
void OnTrade()
{
ExtExpert.OnTrade();
}
//+------------------------------------------------------------------+
//| "Timer" event handler function |
//+------------------------------------------------------------------+
void OnTimer()
{
ExtExpert.OnTimer();
}
//+------------------------------------------------------------------+
//| Check for long position opening |
//+------------------------------------------------------------------+
bool LongOpened(void)
{
bool res=false;
//--- check for long position (BUY) possibility
{
double price=m_symbol.Ask();
{
//--- open position
if(m_trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,InpLots,price,0.0,0.0))
printf("Position by %s to be opened",Symbol());
else
{
printf("Error opening BUY position by %s : '%s'",Symbol(),m_trade.ResultComment());
printf("Open parameters : price=%f,TP=%f",price,0.0);
}
}
//--- in any case we must exit from expert
res=true;
}
//--- result
return(res);
}
//+------------------------------------------------------------------+
//| Check for short position opening |
//+------------------------------------------------------------------+
bool ShortOpened(void)
{
bool res=false;
//--- check for short position (SELL) possibility
{
double price=m_symbol.Bid();
{
//--- open position
if(m_trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,InpLots,price,0.0,0.0))
printf("Position by %s to be opened",Symbol());
else
{
printf("Error opening SELL position by %s : '%s'",Symbol(),m_trade.ResultComment());
printf("Open parameters : price=%f,TP=%f",price,0.0);
}
}
//--- in any case we must exit from expert
res=true;
}
//--- result
return(res);
}
//+------------------------------------------------------------------+