bleh
[mqlkit.git] / Close_on_Profit.mq4
blob084b81c9fd94d18d13a64e278a1e3a114e322d8a
1 //+------------------------------------------------------------------+\r
2 //|                                              Close_on_Profit.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 double  p_target=10.0;\r
10 extern double  targ_base=0;\r
11 extern bool    recurring=true;\r
12 extern bool    use_magic=true;\r
13 extern int     magic=123;\r
14 extern bool    closeall=false;\r
16 double AE;\r
17 int magic2, t_cnt, oneshot;\r
18 datetime get_time;\r
20 //+------------------------------------------------------------------+\r
21 //| expert initialization function                                   |\r
22 //+------------------------------------------------------------------+\r
23 int init()\r
24   {\r
25 //----\r
26    t_cnt=0;\r
27    oneshot=0;\r
28    AE=AccountEquity();\r
29    GlobalVariableSet("inhibit",0);\r
30    if(!GlobalVariableGet("targ_base") && targ_base==0) GlobalVariableSet("targ_base",AE);\r
31    if(targ_base>0) GlobalVariableSet("targ_base",targ_base);\r
32 //----\r
33    return(0);\r
34   }\r
35 //+------------------------------------------------------------------+\r
36 //| expert start function                                            |\r
37 //+------------------------------------------------------------------+\r
38 int start()\r
39   {\r
40 //----\r
41    AE=AccountEquity();\r
42    targ_base=GlobalVariableGet("targ_base");\r
43    if(AE>=targ_base+p_target || closeall){\r
44    get_time=TimeCurrent(); GlobalVariableSet("targ_base",AE); t_cnt++; }\r
45    if(!recurring){ oneshot=t_cnt; GlobalVariableSet("inhibit",t_cnt); }\r
46    if(!use_magic){ magic=0; magic2=0; }\r
47    closeall=false;\r
48 //----\r
49    if(recurring || oneshot==1)\r
50    {\r
51       for(int i=OrdersTotal()-1; i>=0; i--)\r
52       {\r
53          if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)\r
54          {\r
55             if(use_magic) magic2=OrderMagicNumber();\r
56             if(OrderOpenTime()<get_time && magic2==magic)\r
57             {\r
58                if(OrderType()>1) OrderDelete(OrderTicket());\r
59                if(OrderType()<2) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,CLR_NONE);\r
60             }\r
61          }\r
62       }\r
63    }\r
64 //----\r
65    int gt_cnt=0;\r
66    for(i=0; i<OrdersTotal(); i++)\r
67    {\r
68       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)\r
69       {\r
70          if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;\r
71          if(OrderOpenTime()<get_time && magic2==magic) gt_cnt++;\r
72       }\r
73    }\r
74 //----\r
75    Comment("Equity = "+DoubleToStr(AE,2)+"  |  Target = "+DoubleToStr(targ_base+p_target,2)+"  |  Magic = "+magic+"  |  Close Time = "+get_time+"  |  Orders = "+gt_cnt);\r
76 //----\r
77    return(0);\r
78   }\r
79 //+------------------------------------------------------------------+