Orders Execution
Miscellaneous
0
Views
0
Downloads
0
Favorites
Join the dots-v1.2.1
//+------------------------------------------------------------------+
//| Join the dots-v1.2.1.mq4|
//| Copyright © 2008, LEGRUPO |
//| http://www.legrupo.com |
//| Version: 1.2.1 |
//| History: |
//| 1.0 => Release version to the public |
//| 1.1 => StopLoss bug fixed |
//| 1.2 => fixes on this releases: |
//| 1) More StopLoss bug fixed; |
//| 2) Now the EA close on the next oposite dot |
//| Make sure that CloseOrdersOnNextDot is true |
//|1.2.1 => Stoploss Bug fixed by Saidas |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, LEGRUPO"
#property link "http://www.legrupo.com"
//+------------------------------------------------------------------+
//| EX4 imports |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
int MagicNumber = 0;
int ticket = 0;
double Price[2];
int giSlippage;
extern int ExpertID = 994488;
extern int TakeProfit = 300;
extern double StopLoss = 50;
extern double LotSize = 0.1;
extern bool useStopLoss = true;
extern bool CloseOrdersOnNextDot = true;
extern int Slippage = 5;
color ExitLongColor = CLR_NONE;
color ExitShortColor = CLR_NONE;
extern string BuyComment = "Join the Dots BUY";
extern string SellComment = "Join the Dots SELL";
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//--- by Saidas
double SL = StopLoss*Point;//by Saidas
//----
MagicNumber = MakeMagicNumber( ExpertID, false );
double MegaTrend_up = iCustom(NULL, 0, "Mega trend",144,3,0,0,0);
double MegaTrend_down = iCustom(NULL, 0, "Mega trend",144,3,0,1,0);
double MegaTrend_buffer = iCustom(NULL, 0, "Mega trend",144,3,0,2,0);
string megatrend_direction, trendhisto_direction, swingzz_direction;
//Comment("MegaTrend_up: ",MegaTrend_up,"\nMegaTrend_down: ", MegaTrend_down, "\nMegaTrend_buffer: ",MegaTrend_buffer);
if (MegaTrend_buffer == MegaTrend_up) {
//Comment("Megatrend up");
megatrend_direction = "up";
} else if (MegaTrend_buffer == MegaTrend_down) {
//Comment("Megatrend down");
megatrend_direction = "down";
} else {
//Comment("No trend from MegaTrend");
megatrend_direction = "flat";
}
double TrendHisto_up = iCustom(NULL, 0, "Trend Histo",1500,1,0);
double TrendHisto_down = iCustom(NULL, 0, "Trend Histo",1500,2,0);
double TrendHisto_buffer = iCustom(NULL, 0, "Trend Histo",1500,0,0);
//Comment("TrendHisto_up: ",TrendHisto_up,"\nTrendHisto_down: ", TrendHisto_down, "\nTrendHisto_buffer: ",TrendHisto_buffer);
if (TrendHisto_buffer == TrendHisto_up) {
//Comment("TrendHisto up");
trendhisto_direction = "up";
} else if (TrendHisto_buffer == TrendHisto_down) {
//Comment("TrendHisto down");
trendhisto_direction = "down";
} else {
//Comment("No trend from TrendHisto");
trendhisto_direction = "flat";
}
double swingzz_up = iCustom(NULL, 0, "Swing_ZZ",2,1,0);
double swingzz_down = iCustom(NULL, 0, "Swing_ZZ",2,2,0);
double swingzz_buffer = iCustom(NULL, 0, "Swing_ZZ",2,0,0);
//Comment("swingzz_up: ",swingzz_up,"\nswingzz_down: ", swingzz_down, "\nswingzz_buffer: ",swingzz_buffer);
if (swingzz_down != 0 && megatrend_direction == "up" && trendhisto_direction == "up") {
CloseShorts(MagicNumber);
//Alert("BUY");
if (CountLongs(MagicNumber)== 0 && CountShorts(MagicNumber)== 0) {
if (useStopLoss) {
SL = Bid-SL;//by Saidas
} else {
SL = 0;//by Saidas
}
ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,SL,Ask+TakeProfit*Point,BuyComment,MagicNumber,0,CLR_NONE);//by Saidas
if(ticket<0)
{
Alert("OrderSend failed with error #",ErrorDescription(GetLastError()));
return(0);
}
}
}
if (swingzz_up != 0 && megatrend_direction == "down" && trendhisto_direction == "down") {
CloseLongs(MagicNumber);
//Alert("SELL");
if (CountLongs(MagicNumber)== 0 && CountShorts(MagicNumber)== 0) {
if (useStopLoss) {
SL = Ask+SL;//by Saidas
} else {
SL = 0;//by Saidas
}
ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,SL,Bid-TakeProfit*Point,SellComment,MagicNumber,0,CLR_NONE);//by Saidas
if(ticket<0)
{
Alert("OrderSend failed with error #",ErrorDescription(GetLastError()));
return(0);
}
}
}
if (CloseOrdersOnNextDot) {
if (swingzz_up != 0) {
CloseLongs(MagicNumber);
}
if (swingzz_down != 0) {
CloseShorts(MagicNumber);
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Make Magic Number |
//+------------------------------------------------------------------+
int MakeMagicNumber( int ExpertID, bool TimeSpecific )
{
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; }
//---- Period Code
if( TimeSpecific )
{
if( Period() == 1 ) { PeriodCode = 10; }
else if( Period() == 5 ) { PeriodCode = 20; }
else if( Period() == 15 ) { PeriodCode = 30; }
else if( Period() == 30 ) { PeriodCode = 40; }
else if( Period() == 60 ) { PeriodCode = 50; }
else if( Period() == 240 ) { PeriodCode = 60; }
else if( Period() == 1440 ) { PeriodCode = 70; }
else if( Period() == 10080 ){ PeriodCode = 80; }
}
else
{
PeriodCode = 0;
}
//---- Calculate MagicNumber
MagicNumber = ExpertID+SymbolCode+PeriodCode;
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)
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)
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);
}
}
}
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
---