The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by World Reader Hub, 2022-12-10 00:32:35

EA Programming(212)

EA Programming(212)

Appendix D

while(IsTradeContextBusy()) Sleep(10);
bool Closed = OrderDelete(CloseTicket,Red);

// Error Handling
if(Closed == false)

{
int ErrorCode = GetLastError();
string ErrDesc = ErrorDescription(ErrorCode);

string ErrAlert = StringConcatenate("Close All Buy Stop Orders - ",
"Error",ErrorCode,": ",ErrDesc);

Alert(ErrAlert);

string ErrLog = StringConcatenate("Bid: ",
MarketInfo(argSymbol,MODE_BID), " Ask: ",
MarketInfo(argSymbol,MODE_ASK)," Ticket: ",CloseTicket);

Print(ErrLog);
}
else Counter--;
}
}
}

void CloseAllSellStopOrders(string argSymbol, int argMagicNumber)
{
for(int Counter = 0; Counter <= OrdersTotal()-1; Counter++)
{
OrderSelect(Counter,SELECT_BY_POS);

if(OrderMagicNumber() == argMagicNumber && OrderSymbol() == argSymbol
&& OrderType() == OP_SELLSTOP)
{
// Delete Order
int CloseTicket = OrderTicket();

while(IsTradeContextBusy()) Sleep(10);

bool Closed = OrderDelete(CloseTicket,Red);

// Error Handling
if(Closed == false)

{
int ErrorCode = GetLastError();
string ErrDesc = ErrorDescription(ErrorCode);

string ErrAlert = StringConcatenate("Close All Sell Stop Orders - ",
"Error ", ErrorCode,": ",ErrDesc);

Alert(ErrAlert);

193


EXPERT ADVISOR PROGRAMMING

string ErrLog = StringConcatenate("Bid: ",
MarketInfo(argSymbol,MODE_BID), " Ask: ",
MarketInfo(argSymbol,MODE_ASK)," Ticket: ",CloseTicket);

Print(ErrLog);
}
else Counter--;
}
}
}

void CloseAllBuyLimitOrders(string argSymbol, int argMagicNumber)
{
for(int Counter = 0; Counter <= OrdersTotal()-1; Counter++)
{
OrderSelect(Counter,SELECT_BY_POS);

if(OrderMagicNumber() == argMagicNumber && OrderSymbol() == argSymbol
&& OrderType() == OP_BUYLIMIT)
{
// Delete Order
int CloseTicket = OrderTicket();

while(IsTradeContextBusy()) Sleep(10);

bool Closed = OrderDelete(CloseTicket,Red);

// Error Handling
if(Closed == false)

{
int ErrorCode = GetLastError();
string ErrDesc = ErrorDescription(ErrorCode);

string ErrAlert = StringConcatenate("Close All Buy Limit Orders - ",
"Error ", ErrorCode,": ",ErrDesc);

Alert(ErrAlert);

string ErrLog = StringConcatenate("Bid: ",
MarketInfo(argSymbol,MODE_BID), " Ask: ",
MarketInfo(argSymbol,MODE_ASK)," Ticket: ",CloseTicket);

Print(ErrLog);
}
else Counter--;
}
}
}

194


Appendix D

void CloseAllSellLimitOrders(string argSymbol, int argMagicNumber)
{
for(int Counter = 0; Counter <= OrdersTotal()-1; Counter++)
{
OrderSelect(Counter,SELECT_BY_POS);

if(OrderMagicNumber() == argMagicNumber && OrderSymbol() == argSymbol
&& OrderType() == OP_SELLLIMIT)
{
// Delete Order
int CloseTicket = OrderTicket();

while(IsTradeContextBusy()) Sleep(10);

bool Closed = OrderDelete(CloseTicket,Red);

// Error Handling
if(Closed == false)

{
int ErrorCode = GetLastError();
string ErrDesc = ErrorDescription(ErrorCode);

string ErrAlert = StringConcatenate("Close All Sell Limit Orders - ",
"Error ", ErrorCode,": ",ErrDesc);

Alert(ErrAlert);

string ErrLog = StringConcatenate("Bid: ",
MarketInfo(argSymbol,MODE_BID), " Ask: ",
MarketInfo(argSymbol,MODE_ASK)," Ticket: ",CloseTicket);

Print(ErrLog);
}
else Counter--;
}
}
}

void BuyTrailingStop(string argSymbol, int argTrailingStop, int argMinProfit,
int argMagicNumber)
{
for(int Counter = 0; Counter <= OrdersTotal()-1; Counter++)
{
OrderSelect(Counter,SELECT_BY_POS);

// Calculate Max Stop and Min Profit
double MaxStopLoss = MarketInfo(argSymbol,MODE_BID) -

(argTrailingStop * PipPoint(argSymbol));

MaxStopLoss = NormalizeDouble(MaxStopLoss,
MarketInfo(OrderSymbol(),MODE_DIGITS));

double CurrentStop = NormalizeDouble(OrderStopLoss(),
MarketInfo(OrderSymbol(),MODE_DIGITS));

195


EXPERT ADVISOR PROGRAMMING

double PipsProfit = MarketInfo(argSymbol,MODE_BID) - OrderOpenPrice();
double MinProfit = argMinProfit * PipPoint(argSymbol);

// Modify Stop
if(OrderMagicNumber() == argMagicNumber && OrderSymbol() == argSymbol

&& OrderType() == OP_BUY && CurrentStop < MaxStopLoss
&& PipsProfit >= MinProfit)
{

bool Trailed = OrderModify(OrderTicket(),OrderOpenPrice(),MaxStopLoss,
OrderTakeProfit(),0);

// Error Handling
if(Trailed == false)

{
int ErrorCode = GetLastError();
string ErrDesc = ErrorDescription(ErrorCode);

string ErrAlert = StringConcatenate("Buy Trailing Stop – Error ",
",ErrorCode,": ",ErrDesc);

Alert(ErrAlert);

string ErrLog = StringConcatenate("Bid: ",
MarketInfo(argSymbol,MODE_BID), " Ticket: ",OrderTicket()," Stop: ",
OrderStopLoss()," Trail: ",MaxStopLoss);

Print(ErrLog);
}
}
}
}

void SellTrailingStop(string argSymbol, int argTrailingStop, int argMinProfit,
int argMagicNumber)
{
for(int Counter = 0; Counter <= OrdersTotal()-1; Counter++)
{
OrderSelect(Counter,SELECT_BY_POS);

// Calculate Max Stop and Min Profit
double MaxStopLoss = MarketInfo(argSymbol,MODE_ASK) +

(argTrailingStop * PipPoint(argSymbol));

MaxStopLoss = NormalizeDouble(MaxStopLoss,
MarketInfo(OrderSymbol(),MODE_DIGITS));

double CurrentStop = NormalizeDouble(OrderStopLoss(),
MarketInfo(OrderSymbol(),MODE_DIGITS));

double PipsProfit = OrderOpenPrice() - MarketInfo(argSymbol,MODE_ASK);
double MinProfit = argMinProfit * PipPoint(argSymbol);

196


Appendix D
// Modify Stop
if(OrderMagicNumber() == argMagicNumber && OrderSymbol() == argSymbol

&& OrderType() == OP_SELL && (CurrentStop > MaxStopLoss || CurrentStop == 0)
&& PipsProfit >= MinProfit)
{

bool Trailed = OrderModify(OrderTicket(),OrderOpenPrice(),MaxStopLoss,
OrderTakeProfit(),0);

// Error Handling
if(Trailed == false)

{
int ErrorCode = GetLastError();
string ErrDesc = ErrorDescription(ErrorCode);
string ErrAlert = StringConcatenate("Sell Trailing Stop - Error ",
ErrorCode,": ",ErrDesc);
Alert(ErrAlert);
string ErrLog = StringConcatenate("Ask: ",
MarketInfo(argSymbol,MODE_ASK), " Ticket: ",OrderTicket()," Stop: ",
OrderStopLoss()," Trail: ",MaxStopLoss);
Print(ErrLog);

}
}
}
}

197


EXPERT ADVISOR PROGRAMMING

Appendix E

Custom Indicator

Here is the code for the custom indicator from chapter 9:

#property copyright "Andrew Young"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 DeepSkyBlue
#property indicator_color2 DeepSkyBlue
#property indicator_color3 DeepSkyBlue

// External variables
extern int BandsPeriod = 20;
extern int BandsShift = 0;
extern int BandsMethod = 1;
extern int BandsPrice = 0;
extern int Deviations = 1;

// Buffers
double EMA[];
double UpperBand[];
double LowerBand[];

// Init
int init()

{
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,EMA);
SetIndexLabel(0,"EMA");

SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,UpperBand);
SetIndexLabel(1,"UpperBand");

SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,LowerBand);
SetIndexLabel(2,"LowerBand");

return(0);
}

198


Appendix E
// Start
int start()

{
int counted_bars = IndicatorCounted();
int CalculateBars = Bars - counted_bars;
for(int Count = CalculateBars; Count >= 0; Count--)
{
EMA[Count] = iMA(NULL,0,BandsPeriod,BandsShift,BandsMethod,BandsPrice,Count);
double StdDev = iStdDev(NULL,0,BandsPeriod,BandsShift,BandsMethod,BandsPrice,
Count);
UpperBand[Count] = EMA[Count] + (StdDev * Deviations);
LowerBand[Count] = EMA[Count] - (StdDev * Deviations);
}
return(0);

}

199


Index

A DayOfWeek() .......................................... 114
Debugging .............................................. 141
AccountBroker() ................................... 125 default operator .................................... 130
AccountEquity() ............................. 50, 132 Default values ........................................... 12
AccountName() ....................................... 125 deinit() .................................................. 17
AccountNumber() ................................... 125 DLLs ......................................................... 75
Alert() ................................................... 54 double data type ........................................ 9
Applied price constants ............................ 102
Arrays ..................................................... 134 E
Ask ........................................................... 20
ECN/STP ............................................. 20, 42
B else operator ......................................... 104
EMPTY_VALUE .......................................... 102
Bars ....................................................... 150 Error codes ............................................. 143
Bid ........................................................... 20 Error handling ........................................... 53
boolean data type ...................................... 9 ErrorDescription() ............................... 54
Boolean operators ................................... 105 Escape characters .................................... 122
Break even stop ........................................ 90 Expert Advisor Wizard ................................ 14
break operator ........................................ 130 Expiration ................................................. 21
Buffers .................................................... 146 extern variables ....................................... 16

C F

case operator ......................................... 130 FIFO ......................................................... 85
Close[] ................................................... 94 File formats ................................................. 4
color data type .......................................... 9 File locations ............................................... 5
Comment() .............................................. 122 for operator ............................................. 80
Comments .................................................. 8 Function arguments ................................... 12
Compilation errors ................................... 144 Functions .................................................. 10
Compound operators ................................... 8
Constants ................................................. 10 G
Custom indicators ...................................... 98
GetLastError() ....................................... 54
D Global variables ....................................... 136
Globally scoped variable ....................... 13, 17
Data types .................................................. 9 GlobalVariableDel() ............................ 137
Data window ........................................... 100 GlobalVariableDeleteAll() ................. 137
Datetime constants .................................. 112 GlobalVariableGet() ............................ 137
datetime data type .................................... 9 GlobalVariableSet() ............................ 136
datetime variables .................................. 112
Day() ..................................................... 114


H Low[] ....................................................... 94

High[] ..................................................... 94 M
Hour() ................................................... 114
Magic number ........................................... 23
I Market order ............................................. 20
MarketInfo() .......................................... 29
iClose() .................................................. 94 Martingale ............................................... 138
iCustom() ................................................ 98 MathPow() .............................................. 141
Identifiers ................................................... 8 Maximum lot size ....................................... 51
if operator ............................................. 103 MessageBox() ........................................ 125
iHigh() ................................................... 94 MetaEditor .................................................. 6
iHighest() ............................................... 32 Minimum lot size ....................................... 51
iLow() ..................................................... 94 Minute() ................................................ 114
iLowest() ................................................ 31 Mode parameter ......................................... 98
iMA() ....................................................... 96 Month() ................................................. 114
Include files .............................................. 74 Moving average methods ......................... 103
indicator_buffers ............................... 147
indicator_chart_window ...................... 147 N
indicator_color ................................... 147
indicator_separate_window ................. 147 Navigator window ....................................... 6
IndicatorCounted() .............................. 150 Newline character .................................... 122
Indicators ................................................. 95 NormalizeDouble() ................................. 52
IndicatorShortName() .......................... 149 NULL ......................................................... 31
init() ..................................................... 17
int data type .............................................. 9 O
iOpen() ................................................... 94
IsDemo() ................................................ 124 Open[] ..................................................... 94
IsDllsAllowed() ................................... 123 Order type constants ................................. 22
IsLibrariesAllowed() .......................... 123 OrderClose() .......................................... 34
iStdDev() .............................................. 151 OrderClosePrice() .......................... 33, 138
iStochastic() ......................................... 98 OrderCloseTime() ............................. 33, 35
IsTradeAllowed() ................................. 123 OrderComment() ............................... 33, 131
IsTradeContextBusy() ............................ 52 OrderDelete() ......................................... 35
OrderLots() ............................................ 33
L OrderMagicNumber() ............................... 33
OrderModify() ......................................... 42
Libraries .................................................... 74 OrderOpenPrice() ............................ 33, 138
Limit order ................................................ 21 OrderOpenTime() ..................................... 33
Local variable ............................................ 13 OrderProfit() ................................. 34, 137
Logs ....................................................... 141 OrderSelect() ......................................... 32
Lot size ..................................................... 49 OrderSend() ............................................ 22
Lot step value ........................................... 52 OrdersHistoryTotal() .......................... 139
OrderStopLoss() ..................................... 33


OrdersTotal() ......................................... 82 Stop out level .......................................... 132
OrderSymbol() ......................................... 33 string data type ........................................ 9
OrderTakeProfit() ................................. 33 StringConcatenate() .............................. 55
OrderTicket() ......................................... 33 StrToTime() .......................................... 113
OrderType() ............................................ 33 switch operator ...................................... 130
Oscillators ................................................. 97 Symbol() .................................................. 23

P T

Pending orders .......................................... 21 Take profit ................................................ 30
Point ....................................................... 26 Templates ................................................... 4
Preprocessor ............................................. 15 Tick value ................................................. 50
Price series arrays ..................................... 94 Time frame constants .............................. 102
Print() ................................................... 54 Time[] ................................................... 119
TimeCurrent() ....................................... 114
R TimeDay() .............................................. 115
TimeDayOfWeek() ................................... 115
RefreshRates() ....................................... 53 TimeHour() ............................................ 115
Relation operators ................................... 104 TimeLocal() .......................................... 114
return operator ........................................ 11 TimeMinute() ........................................ 115
TimeMonth() .......................................... 115
S TimeToStr() .......................................... 113
TimeYear() ............................................ 115
Scripts .................................................... 152 Toolbox window .......................................... 6
Semicolon as expression terminator .............. 7 Trailing stops ............................................. 87
SendMail() ............................................ 127
SetArrow() ............................................ 148 V
SetIndexBuffer() .......................... 101, 148
SetIndexLabel() ................................... 148 Variable assignment ................................... 10
SetIndexStyle() ............................ 101, 148 Variable scope ........................................... 13
SetLevelStyle() ................................... 149 Variables ..................................................... 9
SetLevelValue() ................................... 149 void data type .................................... 11, 85
Shift parameter ....................................... 96
show_confirm ........................................ 152 W
show_inputs .......................................... 152
Sleep() ................................................... 53 while operator ......................................... 81
Slippage .............................................. 20, 28
Spread .............................................. 20, 132 Y
start() ................................................... 17
static variable ......................................... 13 Year() ................................................... 114
stdlib.mqh include file ....................... 15, 54
Stop levels ................................................ 46 #
Stop loss ................................................... 30
Stop orders ............................................... 21 #define directive ...................................... 15
#import directive ...................................... 16
#include directive .................................... 15


#property copyright .............................. 15 #property library ................................. 75
#property directives ................................. 15 #property link ....................................... 15


Click to View FlipBook Version