Orders Execution
Indicators Used
Miscellaneous
0
Views
0
Downloads
0
Favorites
3 Ducks
//+------------------------------------------------------------------+
//| 3 Ducks.mq4 |
//| Copyright © 2008, LEGRUPO |
//| http://www.legrupo.com |
//| Version: 1.0 |
//| History: |
//| 1.0 => Release version to the public |
//+------------------------------------------------------------------+
#include <WinUser32.mqh>
#include <stderror.mqh>
#include <stdlib.mqh>
#property copyright "Copyright © 2008, LEGRUPO"
#property link "http://www.legrupo.com"
int ExpertID = 88888888;
extern double LotSize = 0.01;
extern double TakeProfit = 100.0;
extern double StopLoss = 100.0;
extern bool useStopLoss = true;
extern int Slippage = 1;
extern bool UseMoneyManagement = true;
extern bool AccountIsMicro = true;
extern int Risk = 10; // risk in percentage % (10% of risk in this case)
extern string BuyComment = "3 Ducks BUY";
extern string SellComment = "3 Ducks SELL";
color ExitLongColor = CLR_NONE;
color ExitShortColor = CLR_NONE;
bool isBuy, isSell, isCloseBuy, isCloseSell, isSellArea, isBuyArea = false;
double sell_sl, buy_sl;
int ticket_sell, ticket_buy;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
double h4_sma60 = iMA(NULL, PERIOD_H4, 60, 0, MODE_SMA, PRICE_CLOSE,0);
double h1_sma60 = iMA(NULL, PERIOD_H1, 60, 0, MODE_SMA, PRICE_CLOSE,0);
double m5_sma60 = iMA(NULL, PERIOD_M5, 60, 0, MODE_SMA, PRICE_CLOSE,0);
int MagicNumber = MakeMagicNumber();
if(UseMoneyManagement==true) {
LotSize = DefineLotSize(); //Adjust the lot size total
}
double ddiff_h4 = h4_sma60 - Ask;
double ddiff_h1 = h1_sma60 - Ask;
double ddiff_m5 = m5_sma60 - Ask;
int diff_h4,diff_h1,diff_m5;
if (Digits < 4) {
diff_h4 = (ddiff_h4) * 100;
diff_h1 = (ddiff_h1) * 100;
diff_m5 = (ddiff_m5) * 100;
} else {
diff_h4 = (ddiff_h4) * 10000;
diff_h1 = (ddiff_h1) * 10000;
diff_m5 = (ddiff_m5) * 10000;
}
int total_diff = diff_h4+diff_h1+diff_m5;
Comment("Diff-H4: ", diff_h4, "\nDiff-H1: ",diff_h1,"\nDiff-M5: ",diff_m5,"\n\nTotal diffs: ",total_diff);
//return(0);
//Comment("H4: ", h4_sma60, "\nH1: ",h1_sma60,"\nM5: ",m5_sma60);
if (Bid < h4_sma60 && Bid < h1_sma60 && Bid < m5_sma60) {
Comment("Price bellow SMA(60)");
if (CountShorts(MagicNumber) == 0 && total_diff < 40) {
CloseLongs(MagicNumber);
double sell_price = Bid;
if (useStopLoss) {
sell_sl = sell_price+StopLoss*Point;
} else {
sell_sl = 0;
}
ticket_sell=OrderSend(Symbol(),OP_SELL,LotSize,sell_price,Slippage,sell_sl,sell_price-TakeProfit*Point,SellComment,MagicNumber,0,CLR_NONE);
if(ticket_sell<0)
{
Alert(Symbol()," 3 Ducks SELL OrderSend failed with error #",ErrorDescription(GetLastError()));
//return(0);
}
}
} else {
//CloseShorts(MagicNumber);
}
if (Ask > h4_sma60 && Ask > h1_sma60 && Ask > m5_sma60) {
Comment("Price above SMA(60)");
if (CountLongs(MagicNumber) == 0 && total_diff < 40) {
CloseShorts(MagicNumber);
double buy_price = Ask;
if (useStopLoss) {
buy_sl = buy_price-StopLoss*Point;
} else {
buy_sl = 0;
}
ticket_buy=OrderSend(Symbol(),OP_BUY,LotSize,buy_price,Slippage,buy_sl,buy_price+TakeProfit*Point,BuyComment,MagicNumber,0,CLR_NONE);
if(ticket_buy<0)
{
Alert(Symbol()," 3 Ducks BUY OrderSend failed with error #",ErrorDescription(GetLastError()));
//return(0);
}
}
} else {
//CloseLongs(MagicNumber);
}
//----
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Make Magic Number |
//+------------------------------------------------------------------+
int MakeMagicNumber()
{
int SymbolCode = 0;
int PeriodCode = 0;
int MagicNumber = 0;
//---- Symbol Code
if( Symbol() == "AUDCAD" || Symbol() == "AUDCADm" ) { SymbolCode = 1000; }
else if( Symbol() == "AUDJPY" || Symbol() == "AUDJPYm" ) { SymbolCode = 2000; }
else if( Symbol() == "AUDNZD" || Symbol() == "AUDNZDm" ) { SymbolCode = 3000; }
else if( Symbol() == "AUDUSD" || Symbol() == "AUDUSDm" ) { SymbolCode = 4000; }
else if( Symbol() == "CHFJPY" || Symbol() == "CHFJPYm" ) { SymbolCode = 5000; }
else if( Symbol() == "EURAUD" || Symbol() == "EURAUDm" ) { SymbolCode = 6000; }
else if( Symbol() == "EURCAD" || Symbol() == "EURCADm" ) { SymbolCode = 7000; }
else if( Symbol() == "EURCHF" || Symbol() == "EURCHFm" ) { SymbolCode = 8000; }
else if( Symbol() == "EURGBP" || Symbol() == "EURGBPm" ) { SymbolCode = 9000; }
else if( Symbol() == "EURJPY" || Symbol() == "EURJPYm" ) { SymbolCode = 1000; }
else if( Symbol() == "EURUSD" || Symbol() == "EURUSDm" ) { SymbolCode = 1100; }
else if( Symbol() == "GBPCHF" || Symbol() == "GBPCHFm" ) { SymbolCode = 1200; }
else if( Symbol() == "GBPJPY" || Symbol() == "GBPJPYm" ) { SymbolCode = 1300; }
else if( Symbol() == "GBPUSD" || Symbol() == "GBPUSDm" ) { SymbolCode = 1400; }
else if( Symbol() == "NZDJPY" || Symbol() == "NZDJPYm" ) { SymbolCode = 1500; }
else if( Symbol() == "NZDUSD" || Symbol() == "NZDUSDm" ) { SymbolCode = 1600; }
else if( Symbol() == "USDCAD" || Symbol() == "USDCADm" ) { SymbolCode = 1700; }
else if( Symbol() == "USDCHF" || Symbol() == "USDCHFm" ) { SymbolCode = 1800; }
else if( Symbol() == "USDJPY" || Symbol() == "USDJPYm" ) { SymbolCode = 1900; }
//---- Calculate MagicNumber
MagicNumber = ExpertID+SymbolCode;
return(MagicNumber);
}
//+------------------------------------------------------------------+
//| Calculate concurrent Long position |
//+------------------------------------------------------------------+
int CountLongs(int MagicNumber)
{
int count=0;
int trade;
int trades=OrdersTotal();
for(trade=0;trade<trades;trade++)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if( OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber )
continue;
if(OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)
count++;
}//for
return(count);
}
//+------------------------------------------------------------------+
//| Calculate concurrent short position |
//+------------------------------------------------------------------+
int CountShorts(int MagicNumber)
{
int count=0;
int trade;
int trades=OrdersTotal();
for(trade=0;trade<trades;trade++)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber )
continue;
if(OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT)
count++;
}//for
return(count);
}
//+------------------------------------------------------------------+
//| Close Long Position |
//+------------------------------------------------------------------+
void CloseLongs(int MagicNumber)
{
int i = OrdersTotal();
while( CountLongs(MagicNumber) != 0 && i >= 0 )
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if( OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber )
{
i--;
continue;
}
else if(OrderType()==OP_BUY || OrderType()== OP_BUYLIMIT)
{
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,ExitLongColor);
i--;
}
}
}
//+------------------------------------------------------------------+
//| Close Short Position |
//+------------------------------------------------------------------+
void CloseShorts(int MagicNumber)
{
int i = OrdersTotal();
while( CountShorts(MagicNumber) != 0 && i >= 0 )
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if( OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber)
{
i--;
continue;
}
else if(OrderType()== OP_SELL || OrderType()==OP_SELLLIMIT )
{
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,ExitShortColor);
}
}
}
double DefineLotSize() {
double lotMM = MathCeil(AccountFreeMargin() * Risk / 1000) / 100;
if(AccountIsMicro==false) { //normal account
if (lotMM < 0.1) lotMM = LotSize;
if ((lotMM > 0.5) && (lotMM < 1)) lotMM=0.5;
if (lotMM > 1.0) lotMM = MathCeil(lotMM);
if (lotMM > 100) lotMM = 100;
} else { //micro account
if (lotMM < 0.01) lotMM = LotSize;
if (lotMM > 1.0) lotMM = MathCeil(lotMM);
if (lotMM > 100) lotMM = 100;
}
return (lotMM);
}
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---