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
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=10;
\r
18 extern int curb_level=3;
\r
19 extern double p1_offset=10;
\r
20 extern double p2_offset=15;
\r
21 extern int pend_delete=30;
\r
22 extern double profit_target=3.0;
\r
23 extern double curb_factor=0.5;
\r
24 extern double safety_coef = 0.5;
\r
25 extern bool stealth=false;
\r
26 extern bool tier2_sig=true;
\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.02;
\r
31 extern bool power_lots=true;
\r
32 extern string Separator_04="---- Additional ----";
\r
33 extern double deviation_reset=0;
\r
34 extern int reset_level=3;
\r
35 extern bool closeall=false;
\r
36 extern bool stop_ea=false;
\r
56 string f_trade="NULL";
\r
57 //+------------------------------------------------------------------+
\r
58 //| expert initialization function |
\r
59 //+------------------------------------------------------------------+
\r
63 pt=Point; if(Digits==3 || Digits==5) pt=10*Point;
\r
67 if(p1_offset<5.0) p1_offset=0.0;
\r
68 if(p2_offset<5.0) p2_offset=0.0;
\r
69 p1_off=p1_offset*pt;
\r
70 p2_off=p2_offset*pt;
\r
71 d_reset=deviation_reset;
\r
72 if(curb_level<3) curb_level=0;
\r
74 AE1=AccountEquity(); p_lot=lot/AE1;
\r
75 if(StringLen(Symbol())>6) std=1.0;
\r
80 //+------------------------------------------------------------------+
\r
81 //| expert start function |
\r
82 //+------------------------------------------------------------------+
\r
86 int total=0, t_cnt=0, p_cnt=0, b_cnt=0, s_cnt=0, b2_cnt=0, s2_cnt=0;
\r
87 int b3_cnt=0, s3_cnt=0; double FOOP, B_LOOP, S_LOOP;
\r
88 for(int i=0; i<OrdersTotal(); i++)
\r
90 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
\r
91 if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
\r
94 if(cmd()< 2) t_cnt++;
\r
95 if(cmd()> 1) p_cnt++;
\r
96 if(cmd()==0){ b_cnt++; b3_cnt++; } if(cmd()==4){ b2_cnt++; b3_cnt++; }
\r
97 if(cmd()==1){ s_cnt++; s3_cnt++; } if(cmd()==5){ s2_cnt++; s3_cnt++; }
\r
98 if(b3_cnt==1 || (b3_cnt>1 && OOP()<B_LOOP)) B_LOOP=OOP();
\r
99 if(s3_cnt==1 || (s3_cnt>1 && OOP()>S_LOOP)) S_LOOP=OOP();
\r
100 if(b_cnt ==1 || (b_cnt >1 && OOP()>FOOP)) FOOP=OOP();
\r
101 if(s_cnt ==1 || (s_cnt >1 && OOP()<FOOP)) FOOP=OOP();
\r
104 string H=""; if(stealth) H="_S"; if(hedge) H="_H"; if(stealth && hedge) H="_SH";
\r
105 if(total==0) f_trade="Null"+H;
\r
106 if(t_cnt==0 && p_cnt==1) f_trade="Pend"+H;
\r
107 if(t_cnt==1 && p_cnt==0){ f_trade="Buy"+H; if(s_cnt==1) f_trade="Sell"+H; }
\r
108 //+------------------------------------------------------------------+
\r
109 for(i=0; i<OrdersTotal(); i++)
\r
111 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
\r
112 if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
\r
113 double OTP=OrderTakeProfit(), tp_3=tp_1;
\r
114 if(cmd()<2 && OTP==0)
\r
116 if(t_cnt>1) tp_3=tp_2; if(cmd()==1) tp_3*=-1;
\r
117 if(!stealth || (stealth && OrderComment()=="L1")) OTP=OOP()+tp_3;
\r
118 if(OTP!=0){ OrderModify(OrderTicket(),0,0,OTP,0,CLR_NONE); deleteall=true; }
\r
121 if(t_cnt==0 && pend_delete>0)
\r
123 datetime OOT=OrderOpenTime(), get_time=TimeCurrent();
\r
124 if(cmd()>1 && get_time-OOT>pend_delete*60) OrderDelete(OrderTicket());
\r
127 //+------------------------------------------------------------------+
\r
128 double o_charts=GlobalVariableGet("open_charts"); o_charts=1; // Global currently by-passed
\r
129 double AB=AccountBalance(), lot2=lot/o_charts, pl_factor=1/o_charts;
\r
133 if(AB>AE1) lot2=NormalizeDouble((p_lot*AB)/o_charts,Digits);
\r
134 pl_factor=(lot2/lot)/o_charts; double lot3=lot2;
\r
137 double p_targ=profit_target*pl_factor, incr_1=increment*pl_factor, b_lot=lot2, s_lot=lot2;
\r
138 //+------------------------------------------------------------------+
\r
139 bool t_buy=false, t_sell=false, t2_buy=false, t2_sell=false;
\r
140 bool isfriday = false; /* aseza put something */
\r
141 if(DayOfWeek()==5) isfriday = true;
\r
142 if((p1_off==0 && t_cnt==0) || (p1_off>0 && b3_cnt==0 && (s_cnt==0 || f_trade=="Buy"+H))) t_buy=true;
\r
143 if((p1_off==0 && t_cnt==0) || (p1_off>0 && s3_cnt==0 && (b_cnt==0 || f_trade=="Sell"+H))) t_sell=true;
\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
147 if(total==0){ closeall=false; b_send=false; s_send=false; }
\r
148 if(b2_cnt==0 && s2_cnt==0) deleteall=false;
\r
149 if(!stop_ea && !closeall && !deleteall && !isfriday)
\r
151 if(t_buy || (t2_buy && Ask<=B_LOOP-range-p2_off)) // BUY
\r
153 if(signal()==0 || (b_cnt>0 && !tier2_sig))
\r
155 if(lot_multiplier) b_lot=lot2*MathPow(multiplier,b_cnt);
\r
156 if(!lot_multiplier) b_lot=lot2+(incr_1*b_cnt);
\r
157 OT=0; p_off=p1_off;
\r
158 if(b_cnt>0) p_off=p2_off; if(p_off>0) OT=4;
\r
160 OrderSend(Symbol(),OT,b_lot,Ask+p_off,3,0,0,"L"+(b_cnt+1),magic,0,Blue);
\r
164 if(t_sell || (t2_sell && Bid>=S_LOOP+range+p2_off)) // SELL
\r
166 if(signal()==1 || (s_cnt>0 && !tier2_sig))
\r
168 if(lot_multiplier) s_lot=lot2*MathPow(multiplier,s_cnt);
\r
169 if(!lot_multiplier) s_lot=lot2+(incr_1*s_cnt);
\r
170 OT=1; p_off=p1_off;
\r
171 if(s_cnt>0) p_off=p2_off; if(p_off>0) OT=5;
\r
173 OrderSend(Symbol(),OT,s_lot,Bid-p_off,3,0,0,"L"+(s_cnt+1),magic,0,Red);
\r
177 //+------------------------------------------------------------------+
\r
178 double profit=0, profit_1=0, profit_2=0, profit_3=0, t_lot=0, b2_lot=0, s2_lot=0;
\r
179 for(i=0; i<OrdersTotal(); i++)
\r
181 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
\r
182 if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
\r
183 profit+=OrderProfit();
\r
184 if(t_cnt>2 && OT2()=="L")
\r
186 if(OrderComment()=="L"+t_cnt) profit_1=OrderProfit();
\r
187 if(OrderComment()=="L"+(t_cnt-1)) profit_2=OrderProfit();
\r
188 profit_3=profit_1+profit_2;
\r
190 if(cmd()< 2) t_lot+=OrderLots();
\r
191 if(cmd()==0){ b2_lot+=OrderLots(); if(OOP()==FOOP) b_price=OOP(); }
\r
192 if(cmd()==1){ s2_lot+=OrderLots(); if(OOP()==FOOP) s_price=OOP(); }
\r
195 double dev_b, dev_s, deviate=0;
\r
196 if(b_cnt>0){ dev_b=b_price-Ask; if(dev_b>0) deviate=dev_b/pt; }
\r
197 if(s_cnt>0){ dev_s=Bid-s_price; if(dev_s>0) deviate=dev_s/pt; }
\r
198 //+------------------------------------------------------------------+
\r
199 for(i=OrdersTotal()-1; i>=0; i--)
\r
201 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
\r
202 if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
\r
205 if(deviate>=d_reset && d_reset>0 && t_cnt>=reset_level)
\r
208 OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,CLR_NONE);
\r
210 if(t_cnt>=curb_level && profit_3>=p_targ*curb_factor)
\r
212 if(OCM()=="L"+t_cnt || OCM()=="L"+(t_cnt-1))
\r
213 OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,CLR_NONE);
\r
217 //+------------------------------------------------------------------+
\r
219 for(i=0; i<OrdersHistoryTotal(); i++)
\r
221 OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
\r
222 if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
\r
223 balance+=OrderProfit();
\r
226 if(t_cnt==0) GlobalVariableSet("bal_2"+Symbol()+magic,balance);
\r
227 bal_2=GlobalVariableGet("bal_2"+Symbol()+magic);
\r
228 //+------------------------------------------------------------------+
\r
229 if(balance+profit>=bal_2+p_targ || AB+profit<AB*safety_coef) closeall=true;
\r
230 if(closeall || deleteall)
\r
232 for(i=OrdersTotal()-1; i>=0; i--)
\r
234 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
\r
235 if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;
\r
236 if(closeall) deleteall=true;
\r
237 if(cmd()>1 && deleteall) OrderDelete(OrderTicket());
\r
238 if(cmd()<2 && closeall) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,CLR_NONE);
\r
241 //+------------------------------------------------------------------+
\r
242 string multi="Multiplier = "+DoubleToStr(multiplier,2), tier2="Yes", dev_r="";
\r
243 if(!lot_multiplier) multi="Increment = "+DoubleToStr(incr_1,2);
\r
244 if(!tier2_sig) tier2="No"; if(d_reset>0) dev_r=" / "+DoubleToStr(d_reset,0)+" / "+t_cnt+" / "+reset_level;
\r
246 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
247 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
248 string line_3="OPT = "+f_trade+" | "+multi+" | Spread = "+DoubleToStr((Ask-Bid)/pt,2)+" | Total Lots = "+DoubleToStr(t_lot,2)+" | Tier 2 = "+tier2+"\n";
\r
250 Comment(line_1, line_2, line_3);
\r
254 //+------------------------------------------------------------------+
\r
256 { int cmd=OrderType();
\r
260 { double OOP=OrderOpenPrice();
\r
264 { string OCM=OrderComment();
\r
268 { string OT2=StringSubstr(OrderComment(),0,1);
\r
270 //+------------------------------------------------------------------+
\r
274 if(iCustom(NULL,0,"SuperTrend",10,3.0,0,bars_closed)!=EMPTY_VALUE) signal=0;
\r
275 if(iCustom(NULL,0,"SuperTrend",10,3.0,1,bars_closed)!=EMPTY_VALUE) signal=1;
\r
278 //+------------------------------------------------------------------+