-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSuperMao.mq4
135 lines (127 loc) · 6.39 KB
/
SuperMao.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
//+------------------------------------------------------------------+
//| SuperMao.mq4 |
//| Copyright 2020, khantanvir |
//| https://github.com/tanvird3 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, khantanvir"
#property link "https://github.com/tanvird3"
#property version "2.00"
#property strict
#include <openordercheck.mqh>
input string MySymbol = NULL; // provide the symbol
input ENUM_TIMEFRAMES MyTimeFrame = PERIOD_CURRENT; // For per minute, hour or monthly
input int MyAvgPeriod = 50; // time frame for calculating moving average
input ENUM_APPLIED_PRICE MyPrice = PRICE_CLOSE; // what price do we want to consider
input double LotSize = 0.01; // the lot size we wish to trade
input int FirstBBand = 1;
input int SecondBBand = 2;
input int ThirdBBand = 6;
input int MacdFast = 24;
input int MacdSlow = 52;
input int MacdSignal = 18;
input string ContUpdate = "NO"; // Should TP & SL Keep Updating Continuously
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int MagicNumber = 9228+Period()+StringGetChar(MySymbol, 0)+StringGetChar(MySymbol, 3); //set the unique number for each comb of currency pair and timeframe
int OrderId;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
Alert("You Have Just Awakened SuperMao");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
Alert("SuperMao Is Going Into Hybernation");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
// first band is at 1 std
double UBBAND1 = iBands(MySymbol, MyTimeFrame, MyAvgPeriod, FirstBBand, 0, MyPrice, MODE_UPPER, 0);
double LBBAND1 = iBands(MySymbol, MyTimeFrame, MyAvgPeriod, FirstBBand, 0, MyPrice, MODE_LOWER, 0);
// second band is at 2 std
double UBBAND2 = iBands(MySymbol, MyTimeFrame, MyAvgPeriod, SecondBBand, 0, MyPrice, MODE_UPPER, 0);
double LBBAND2 = iBands(MySymbol, MyTimeFrame, MyAvgPeriod, SecondBBand, 0, MyPrice, MODE_LOWER, 0);
double MBBAND = iBands(MySymbol, MyTimeFrame, MyAvgPeriod, SecondBBand, 0, MyPrice, MODE_MAIN, 0);
// third band is at 6 std
double UBBAND6 = iBands(MySymbol, MyTimeFrame, MyAvgPeriod, ThirdBBand, 0, MyPrice, MODE_UPPER, 0);
double LBBAND6 = iBands(MySymbol, MyTimeFrame, MyAvgPeriod, ThirdBBand, 0, MyPrice, MODE_LOWER, 0);
// macd histogram value for confirmation
double MACDMAIN = iMACD(MySymbol, MyTimeFrame, MacdFast, MacdSlow, MacdSignal, MyPrice, MODE_MAIN, 0);
double MACDSIGNAL = iMACD(MySymbol, MyTimeFrame, MacdFast, MacdSlow, MacdSignal, MyPrice, MODE_SIGNAL, 0);
// if there is no order only then a new order would be sent
if(!CheckOpenOrder(MagicNumber))
{
if(Ask >= LBBAND2 && Ask < UBBAND1 && MACDMAIN > MACDSIGNAL) //if price is between lower band 2 and upper band 1 and macd line above macd signal go long
{
Alert("Buy Now at "+ NormalizeDouble(Ask, Digits));
Alert("Take Proft at "+ NormalizeDouble(UBBAND1, Digits));
Alert("Stop Loss at "+ NormalizeDouble(LBBAND6, Digits));
OrderId = OrderSend(MySymbol, OP_BUYLIMIT, LotSize, Ask, 10, NormalizeDouble(LBBAND6, Digits), NormalizeDouble(UBBAND1, Digits), NULL, MagicNumber);
if(OrderId < 0)
Alert("The Order Could Not Be Sent, ErrorCode: " + GetLastError());
}
else
if(Bid <= UBBAND2 && Bid > LBBAND1 && MACDMAIN < MACDSIGNAL) //if price is between upper band 2 and lower band 1 and macd below signal go short
{
Alert("Sell Now at "+ NormalizeDouble(Bid, Digits));
Alert("Take Proft at "+ NormalizeDouble(LBBAND1, Digits));
Alert("Stop Loss at "+ NormalizeDouble(UBBAND6, Digits));
OrderId = OrderSend(MySymbol, OP_SELLLIMIT, LotSize, Bid, 10, NormalizeDouble(UBBAND6, Digits), NormalizeDouble(LBBAND1, Digits), NULL, MagicNumber);
if(OrderId < 0)
Alert("The Order Could Not Be Sent, ErrorCode: " + GetLastError());
}
/*else
{
Alert("Hold Your Nerve");
}*/
}
else // if an order has already been placed
{
if(ContUpdate=="Yes")
{
if(OrderSelect(OrderId, SELECT_BY_TICKET)==true) // check if extracting data is possible
{
int orderType = OrderType(); // check the ordertype, 0 = long, 1 = short
double UpdatedTakeProfit;
if(orderType == 0)
{
UpdatedTakeProfit = NormalizeDouble(UBBAND1, Digits); // modify the take profit for long position
}
else
{
UpdatedTakeProfit = NormalizeDouble(LBBAND1, Digits); // modify the take profit for short position
}
double TP = OrderTakeProfit();
double OpenPrice = OrderOpenPrice();
double TPdistance = MathAbs(TP - UpdatedTakeProfit);
if((orderType ==0 && TP!=UpdatedTakeProfit && UpdatedTakeProfit > OpenPrice && TPdistance >= .0001) || (orderType ==1 && TP!=UpdatedTakeProfit && UpdatedTakeProfit < OpenPrice && TPdistance >= .0001)) // if the currect take profit is not equal to the updated take profit by at least a pip
{
bool ModifySuccess = OrderModify(OrderId, OrderOpenPrice(), OrderStopLoss(), UpdatedTakeProfit, 0);
if(ModifySuccess == true)
{
Print("Order modified: ",OrderId);
}
else
{
Print("Unable to modify order: ",OrderId);
}
}
}
}
}
}
//+------------------------------------------------------------------+