bleh
[mqlkit.git] / indicators / SWB_SuperTrend_V1.01_Man.mq4
blob36e221c28cdcefe8912f233359badbd2af857d10
1 //+------------------------------------------------------------------+\r
2 //|                                         SWB_SuperTrend_V1.01.mq4 |\r
3 //|                                Copyright © 2010, Dennis Hamilton |\r
4 //|                                              ramble_32@yahoo.com |\r
5 //+------------------------------------------------------------------+\r
6 #property copyright "Copyright © 2010, Dennis Hamilton"\r
7 #property link      "ramble_32@yahoo.com"\r
8 //----\r
9 extern string  Separator_01="----  Input  Settings  ----";\r
10 extern double   lot=0.05;\r
11 extern double   tp_1=25;\r
12 extern double   tp_2=25;\r
13 extern int      magic=123;\r
14 extern int      bars_closed=0;\r
15 extern string  Separator_02="----  Range  &  Offsets  ----";\r
16 extern double   range=40;\r
17 extern int      max_level=25;\r
18 extern int      curb_level=4;\r
19 extern int      cp_level=3;\r
20 extern double   p1_offset=0;\r
21 extern double   p2_offset=0;\r
22 extern int      pend_delete=0;\r
23 extern double   profit_target=2.5;\r
24 extern double   curb_target=2.5;\r
25 extern bool     stealth=true;\r
26 extern bool     tier2_sig=false;\r
27 extern string  Separator_03="----  Lot  Settings  ----";\r
28 extern bool     lot_multiplier=false;\r
29 extern double   multiplier=2.0;\r
30 extern double   increment=0.0;\r
31 extern bool     power_lots=true;\r
32 extern string  Separator_04="----  Manual  Trading  ----";\r
33 extern bool     auto_trading=false;\r
34 extern bool     manual_start=false;\r
35 extern bool     manual_buy=false;\r
36 extern bool     manual_sell=false;\r
37 extern bool     limit_order=false;\r
38 extern int      manual_tp=25;\r
39 extern double   manual_offset=10;\r
40 extern string  Separator_05="----  Additional  ----";\r
41 extern double   deviation_reset=0;\r
42 extern int      reset_level=3;\r
43 extern bool     closeall=false;\r
44 //----\r
45 double  pt;\r
46 double  p_off;\r
47 double  p1_off;\r
48 double  p2_off;\r
49 double  m_tp;\r
50 double  m_off;\r
51 double  std=0.1;\r
52 double  AE1;\r
53 double  p_lot;\r
54 double  lot2;\r
55 double  bal_2;\r
56 double  incr_1;\r
57 double  b_price;\r
58 double  s_price;\r
59 double  L;\r
60 double  d_reset;\r
61 int     t_cnt;\r
62 int     OT;\r
63 bool    deleteall;\r
64 bool    hedge=false;\r
65 bool    m2_set;\r
66 bool    tp_avg;\r
67 string  ID="L";\r
68 string  f_trade="NULL";\r
69 //+------------------------------------------------------------------+\r
70 //| expert initialization function                                   |\r
71 //+------------------------------------------------------------------+\r
72 int init()\r
73   {\r
74 //----\r
75    pt=Point; if(Digits==3 || Digits==5) pt=10*Point;\r
76    tp_1*=pt;\r
77    tp_2*=pt;\r
78    range*=pt;\r
79    if(p1_offset<5) p1_offset=0;\r
80    if(p2_offset<5) p2_offset=0;\r
81    if(manual_offset<5) manual_offset=0;\r
82    p1_off=p1_offset*pt;\r
83    p2_off=p2_offset*pt;\r
84    m_off=manual_offset*pt;\r
85    m_tp=manual_tp*pt;\r
86 //----\r
87    d_reset=deviation_reset;\r
88    if(curb_level<4) curb_level=0;\r
89    cp_level-=1;\r
90 //----\r
91    AE1=AccountEquity(); p_lot=lot/AE1;\r
92    if(StringLen(Symbol())>6) std=1.0;\r
93    profit_target*=std;\r
94 //----\r
95    return(0);\r
96   }\r
97 //+------------------------------------------------------------------+\r
98 //| expert start function                                            |\r
99 //+------------------------------------------------------------------+\r
100 int start()\r
101   {\r
102 //----\r
103    int total=0, p_cnt=0, b_cnt=0, s_cnt=0, b2_cnt=0, s2_cnt=0; t_cnt=0;\r
104    int b3_cnt=0, s3_cnt=0;  double L2[], B_LOOP, S_LOOP;\r
105    for(int i=0; i<OrdersTotal(); i++)\r
106    {\r
107       OrderSelect(i,SELECT_BY_POS,MODE_TRADES);\r
108       if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;\r
109       total++;\r
110       if(OT2()!="H") L2[LCT()]=OOP();\r
111 //----\r
112       if(cmd()< 2) t_cnt++;\r
113       if(cmd()> 1) p_cnt++;\r
114       if(cmd()==0){ b_cnt++; b3_cnt++; } if(cmd()==4){ b2_cnt++; b3_cnt++; }\r
115       if(cmd()==1){ s_cnt++; s3_cnt++; } if(cmd()==5){ s2_cnt++; s3_cnt++; }\r
116       if(b3_cnt==1 || (b3_cnt>1 && OOP()<B_LOOP)) B_LOOP=OOP();\r
117       if(s3_cnt==1 || (s3_cnt>1 && OOP()>S_LOOP)) S_LOOP=OOP();\r
118    }\r
119 //----\r
120    string H=""; if(stealth) H="_S"; if(hedge) H="_H"; if(stealth && hedge) H="_SH";\r
121    if(total==0) f_trade="Null"+H;\r
122    if(t_cnt==0 && p_cnt==1)  f_trade="Pend"+H;\r
123    if(t_cnt==1 && p_cnt==0){ f_trade="Buy"+H; if(s_cnt==1) f_trade="Sell"+H; }\r
124 //+------------------------------------------------------------------+\r
125    double o_charts=GlobalVariableGet("open_charts"); o_charts=1; // Global o_charts by-passed\r
126    double AB=AccountBalance(), lot2=lot/o_charts, plf=1/o_charts;\r
127 //----\r
128    if(power_lots)\r
129    {\r
130       if(AB>AE1) lot2=NormalizeDouble((p_lot*AB)/o_charts,Digits);\r
131       plf=(lot2/lot)/o_charts; double lot3=lot2;\r
132    }\r
133 //----\r
134    double p_targ=profit_target*plf, incr_1=increment*plf, b_lot, s_lot;\r
135 //+------------------------------------------------------------------+\r
136    bool t_buy=false, t_sell=false, t2_buy=false, t2_sell=false;\r
137    if(auto_trading && !manual_start)\r
138    {\r
139       if((p1_off==0 && t_cnt==0) || (p1_off>0 && b3_cnt==0 && (s_cnt==0 || f_trade=="Buy"+H)))  t_buy=true;\r
140       if((p1_off==0 && t_cnt==0) || (p1_off>0 && s3_cnt==0 && (b_cnt==0 || f_trade=="Sell"+H))) t_sell=true;\r
141    }\r
142    if(auto_trading || manual_start)\r
143    {\r
144       if(f_trade=="Buy"+H  && b3_cnt>0 && b_cnt<max_level) t2_buy=true;\r
145       if(f_trade=="Sell"+H && s3_cnt>0 && s_cnt<max_level) t2_sell=true;\r
146    }\r
147 //----\r
148    if(total==0){ closeall=false; }\r
149    if(b2_cnt==0 && s2_cnt==0) deleteall=false;\r
150    if(!closeall && !deleteall)\r
151    {\r
152       if(t_buy || (t2_buy && Ask<=B_LOOP-range-p2_off) || manual_buy) // BUY\r
153       {\r
154          if(signal()==0 || (b_cnt>0 && !tier2_sig) || manual_buy)\r
155          {\r
156             if(t_cnt>=8) bal_2+=1.0*plf;\r
157             if(lot_multiplier)  b_lot=lot2*MathPow(multiplier,b_cnt);\r
158             if(!lot_multiplier) b_lot=lot2+(incr_1*b_cnt);\r
159             OT=0; p_off=p1_off;\r
160             if(b_cnt>0) p_off=p2_off;\r
161             if(manual_buy){ p_off=m_off; m2_set=true; manual_buy=false; }\r
162             if(p_off>0){ OT=4; if(limit_order){ OT=2; p_off*=-1; limit_order=false; } }\r
163             //----\r
164             OrderSend(Symbol(),OT,b_lot,Ask+p_off,3,0,0,ID+(b_cnt+1),magic,0,Blue);\r
165          }\r
166       }\r
167 //----\r
168       if(t_sell || (t2_sell && Bid>=S_LOOP+range+p2_off) || manual_sell) // SELL\r
169       {\r
170          if(signal()==1 || (s_cnt>0 && !tier2_sig) || manual_sell)\r
171          {\r
172             if(t_cnt>=8) bal_2+=1.0*plf;\r
173             if(lot_multiplier)  s_lot=lot2*MathPow(multiplier,s_cnt);\r
174             if(!lot_multiplier) s_lot=lot2+(incr_1*s_cnt);\r
175             OT=1; p_off=p1_off;\r
176             if(s_cnt>0) p_off=p2_off;\r
177             if(manual_sell){ p_off=m_off; m2_set=true; manual_sell=false; }\r
178             if(p_off>0){ OT=5; if(limit_order){ p_off*=-1; OT=3; limit_order=false; } }\r
179             //----\r
180             OrderSend(Symbol(),OT,s_lot,Bid-p_off,3,0,0,ID+(s_cnt+1),magic,0,Red);\r
181          }\r
182       }\r
183    }\r
184 //+------------------------------------------------------------------+\r
185    double profit=0, c_prof[10], p_curb, t_lot=0, b2_lot=0, s2_lot=0;\r
186    for(i=0; i<OrdersTotal(); i++)\r
187    {\r
188       OrderSelect(i,SELECT_BY_POS,MODE_TRADES);\r
189       if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;\r
190       profit+=OrderProfit();\r
191       if(t_cnt>=curb_level && curb_level>0 && OT2()!="H")\r
192       {\r
193          int p=t_cnt-LCT()+1;\r
194          c_prof[p]=OrderProfit();\r
195          p_curb=c_prof[1]+c_prof[2]+c_prof[3];\r
196       }\r
197       if(cmd()< 2){ t_lot+=OrderLots();  if(LCT()==t_cnt-1) L=OOP(); }\r
198       if(cmd()==0){ b2_lot+=OrderLots(); if(LCT()==1) b_price=OOP(); }\r
199       if(cmd()==1){ s2_lot+=OrderLots(); if(LCT()==1) s_price=OOP(); }\r
200    }\r
201 //----\r
202    double dev_b, dev_s, deviate=0;\r
203    if(b_cnt>0){ dev_b=b_price-Ask; if(dev_b>0) deviate=dev_b/pt; }\r
204    if(s_cnt>0){ dev_s=Bid-s_price; if(dev_s>0) deviate=dev_s/pt; }\r
205 //+------------------------------------------------------------------+\r
206    int d_cnt=0; double d_OOP=0;\r
207    for(i=0; i<OrdersTotal(); i++)\r
208    {\r
209       OrderSelect(i,SELECT_BY_POS,MODE_TRADES);\r
210       if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;\r
211 //----\r
212       double otp=OTP(), tp_3=tp_1; bool curb2=false;\r
213       if(cmd()==0 && LCT()==t_cnt && L-OOP()>range*1.5) curb2=true;\r
214       if(cmd()==1 && LCT()==t_cnt && OOP()-L>range*1.5) curb2=true;\r
215       if(otp!=0 && LCT()>1 && LCT()<t_cnt){ d_cnt=LCT(); d_OOP=OOP(); }\r
216       if(stealth && t_cnt==0) tp_avg=false;\r
217       if(stealth && t_cnt>1){ tp_avg=true; if(LCT()==1) otp=0; }\r
218 //----\r
219       if(cmd() <2 && otp==0) // Order Modify\r
220       {\r
221          if(t_cnt>1) tp_3=tp_2;\r
222          if(m2_set){ tp_3=m_tp; m2_set=false; } if(cmd()==1) tp_3*=-1;\r
223          if(!stealth || (stealth&& LCT()==1 && !tp_avg ) || curb2) otp=OOP()+tp_3;\r
224 //----\r
225          if(otp!=0 || (LCT()==1 && tp_avg))\r
226          {\r
227             OrderModify(OrderTicket(),0,0,otp,0,CLR_NONE);\r
228             if(LCT()==1) deleteall=true;\r
229          }\r
230       }\r
231 //----\r
232       if(t_cnt==0 && pend_delete>0) // Pending Timeout\r
233       {\r
234          datetime OOT=OrderOpenTime(), get_time=TimeCurrent();\r
235          if(cmd()>1 && get_time-OOT>pend_delete*60) OrderDelete(OrderTicket());\r
236       }\r
237    }\r
238 //+------------------------------------------------------------------+\r
239    for(i=OrdersTotal()-1; i>=0; i--)\r
240    {\r
241       OrderSelect(i,SELECT_BY_POS,MODE_TRADES);\r
242       if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;\r
243       if(cmd()<2)\r
244       {\r
245          if(deviate>=d_reset && d_reset>0 && t_cnt>=reset_level)\r
246          {\r
247             if(LCT()==1)\r
248             OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,CLR_NONE);\r
249          }\r
250          if(d_cnt>0 && LCT()>d_cnt) // Secondary Curb\r
251          {\r
252             if(!Ask && (cmd()==0 && Ask>=d_OOP) || (cmd()==1 && Bid<=d_OOP))\r
253             OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,CLR_NONE);\r
254             if(!Ask && LCT()==t_cnt)\r
255             {\r
256                if((cmd()==0 && Ask>=OOP()+(range*1.5)) || (cmd()==1 && Bid<=OOP()-(range*1.5)))\r
257                OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,CLR_NONE);\r
258             }\r
259          }\r
260          if(t_cnt>=8 && curb_level>0)\r
261          {\r
262             if(LCT()==t_cnt)\r
263             {\r
264                if((cmd()==0 && Ask>=OOP()+(range*1)) || (cmd()==1 && Bid<=OOP()-(range*1)))\r
265                OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,CLR_NONE);\r
266             }\r
267          }\r
268       }\r
269    }\r
270 //+------------------------------------------------------------------+\r
271    double balance=0;\r
272    for(i=0; i<OrdersHistoryTotal(); i++)\r
273    {\r
274       OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);\r
275       if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;\r
276       balance+=OrderProfit();\r
277    }\r
278 //----\r
279    if(t_cnt==0) GlobalVariableSet("bal_2"+Symbol()+magic,balance);\r
280    bal_2=GlobalVariableGet("bal_2"+Symbol()+magic);\r
281 //+------------------------------------------------------------------+\r
282    bool curb=false;\r
283    if(t_cnt>=curb_level && p_curb>=curb_target*plf){ curb=true; } // Primary Curb\r
284    if(t_cnt>1) p_targ=p_targ+(p_targ*(t_cnt-1)*0.5);\r
285    if(balance+profit>=bal_2+p_targ){ closeall=true; get_time=TimeCurrent(); }\r
286 //----\r
287    if(closeall || deleteall || curb)\r
288    {\r
289       for(i=OrdersTotal()-1; i>=0; i--)\r
290       {\r
291          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);\r
292          if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;\r
293          if(closeall) deleteall=true;\r
294          if(cmd()>1)\r
295          {\r
296             if(deleteall || (curb && LCT()>=t_cnt-cp_level))\r
297             OrderDelete(OrderTicket());\r
298          }\r
299          if(cmd()<2)\r
300          {\r
301             if(closeall  || (curb && LCT()>=t_cnt-cp_level))\r
302             OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,CLR_NONE);\r
303          }\r
304       }\r
305    }\r
306 //+------------------------------------------------------------------+\r
307    string multi="Multiplier = "+DoubleToStr(multiplier,2), tier2="Yes", dev_r="";\r
308    if(!lot_multiplier) multi="Increment = "+DoubleToStr(incr_1,2);\r
309    if(!tier2_sig) tier2="No"; if(d_reset>0) dev_r=" / "+DoubleToStr(d_reset,0)+" / "+t_cnt+" / "+reset_level;\r
310 //----\r
311    string line_1="Balance = "+DoubleToStr(bal_2,2)+"  |  Profit = "+DoubleToStr(bal_2+profit,2)+"  |  Equity = "+DoubleToStr(AccountEquity(),2)+"  |  Target = "+DoubleToStr(profit_target,2)+" / "+DoubleToStr(p_targ,2)+" / "+DoubleToStr(profit,2)+"\n";\r
312    string line_2="Start Lot = "+DoubleToStr(lot,2)+"  |  Power lots = "+DoubleToStr(lot3,2)+"  |  Open = "+b_cnt+" / "+s_cnt+"  |  Pending = "+b2_cnt+" / "+s2_cnt+"  |  Deviate = "+DoubleToStr(deviate,0)+dev_r+"\n";\r
313    string line_3="OPT = "+f_trade+"  |  "+multi+"  |  Spread = "+DoubleToStr((Ask-Bid)/pt,2)+"  |  Total Lots = "+DoubleToStr(t_lot,2)+"  |  Tier 2 = "+tier2+"\n";\r
314 //----\r
315    Comment(line_1, line_2, line_3);\r
316 //----\r
317    return(0);\r
318   }\r
319 //+------------------------------------------------------------------+\r
320 int signal()\r
321   {\r
322    int signal=2;\r
323    if(iCustom(NULL,0,"SuperTrend",10,3.0,0,bars_closed)!=EMPTY_VALUE) signal=0;\r
324    if(iCustom(NULL,0,"SuperTrend",10,3.0,1,bars_closed)!=EMPTY_VALUE) signal=1;\r
325    return(signal);\r
326   }\r
327 //+------------------------------------------------------------------+\r
328 //----\r
329 int cmd(){ int cmd=OrderType(); return(cmd); }\r
330 //----\r
331 double OOP(){ double OOP=OrderOpenPrice(); return(OOP); }\r
332 //----\r
333 double OTP(){ double OOP=OrderTakeProfit(); return(OOP); }\r
334 //----\r
335 string OT2(){ string OT2=StringSubstr(OrderComment(),0,1); return(OT2); }\r
336 //----\r
337 int LCT(){ int LCT=StrToInteger(StringSubstr(OrderComment(),1,3)); return(LCT); }\r
338 //----\r
339 //+------------------------------------------------------------------+\r