Orders Execution
Indicators Used
0
Views
0
Downloads
0
Favorites
BBExpert_v1
//+------------------------------------------------------------------+
//| BBExpert_v1.mq4 |
//| Copyright © 2006, Forex-TSD.com |
//| Written by IgorAD,igorad2003@yahoo.co.uk |
//| http://finance.groups.yahoo.com/group/TrendLaboratory |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Forex-TSD.com "
#property link "http://www.forex-tsd.com/"
#include <stdlib.mqh>
#include <Tracert.mqh>
//---- input parameters
extern string Expert_Name = "---- BBExpert_v1 ----";
extern int Magic = 10000;
extern int Slippage = 6;
extern bool Trace = false; // Trace Switch
extern string Main_data = " Trade Volume & Trade Method";
extern double Lots = 0.1;
extern double TakeProfit = 50; // Take Profit Value
extern double InitialStop = 30; // Initial Stop Value
extern bool TrailingStop = true; // Trailing Stop Switch
extern bool SwingTrade = false; // Swing Trade Switch
extern bool PendingOrder = false; // PendingOrder/InstantExecution Switch
extern double PendOrdGap = 10; // Gap from High/Low for Pending Orders
extern double BreakEven = 20; // BreakEven Level in pips
extern double BreakEvenGap = 1; // Pips when BreakEven will be reached
extern string Calc_data = " Bollinger Bands Parameters ";
extern int Length = 20; // Bollinger Bands Period
extern int Deviation = 2; // Deviation
extern double MoneyRisk = 1.00; // Offset Factor
extern double DeltaLong = 0.00;
extern double DeltaShort = 0.00;
extern string MM_data = " MoneyManagement by L.Williams ";
extern bool MM = false; // ÌÌ Switch
extern double MMRisk = 0.15; // Risk Factor
extern double MaxLoss = 1000; // Maximum Loss by 1 Lot
int Trend=0, PrevTrend=0, digit, Signal=0, b=0, cnt=0, Kz=0, ticket=0;
double smin=0,smax=0,smin1=0,smax1=0,bsmin=0,bsmax=0,bsmin1=0,bsmax1=0,
SellStop,BuyStop,prevStop=0, SellProfit,BuyProfit;
bool BuyInTrade = false, SellInTrade = false;
bool BuySignal = false, SellSignal = false;
bool BuyExit = false, SellExit = false;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
// ---- Scan Trades
int ScanTrades()
{
int total = OrdersTotal();
int numords = 0;
for(int cnt=0; cnt<total; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS);
if(OrderSymbol() == Symbol() && OrderType()<=OP_SELLSTOP && OrderMagicNumber() == Magic)
numords++;
}
return(numords);
}
void BBStops()
{
smax1 = smax;
smin1 = smin;
smax=iBands(NULL,0,Length,Deviation,0,PRICE_CLOSE,MODE_UPPER,1);
smin=iBands(NULL,0,Length,Deviation,0,PRICE_CLOSE,MODE_LOWER,1);
PrevTrend=Trend;
if (Close[1]>smax1) Trend=1;
if (Close[1]<smin1) Trend=-1;
if(Trend>0 && smin<smin1) smin = smin1;
if(Trend<0 && smax>smax1) smax = smax1;
bsmax1 = bsmax;
bsmin1 = bsmin;
bsmax = smax+0.5*(MoneyRisk-1)*(smax-smin);
bsmin = smin-0.5*(MoneyRisk-1)*(smax-smin);
if(Trend>0 && bsmin<bsmin1) bsmin=bsmin1;
if(Trend<0 && bsmax>bsmax1) bsmax=bsmax1;
}
void TradeSignal()
{
BuySignal = (
Close[1] - smax1 > DeltaLong*Point
&&
PrevTrend<0
);
SellSignal= (
smin1 - Close[1] > DeltaShort*Point
&&
PrevTrend>0
);
}
void ExitSignal()
{
BuyExit = (
Close[1] < smin1
&&
PrevTrend>0
);
SellExit= (
Close[1] > smax1
&&
PrevTrend<0
);
}
double MoneyManagement ( bool flag, double Lots, double risk, double maxloss)
{
double Lotsi=Lots;
if ( flag ) Lotsi=Lots*NormalizeDouble(Lots*AccountFreeMargin()*MMRisk/MaxLoss,1);
if (Lotsi<0.1) Lotsi=0.1;
return(Lotsi);
}
void CloseTrade()
{
for (cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
int mode=OrderType();
if ( OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
// - BUY Orders
if ( BuyExit )
{
if (mode==OP_BUY)
{
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Yellow);
}
}
else
// - SELL Orders
if ( SellExit )
{
if (mode==OP_SELL)
{
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,White);
}
}
}
}
}
void BreakEvenStop()
{
for (cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS);
int mode=OrderType();
if ( OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if ( mode==OP_BUY )
{
if ( Bid-OrderOpenPrice() > 0 )
{Kz = MathFloor((Bid-OrderOpenPrice())/(BreakEven*Point));
if (Kz < 1) Kz=1;} else Kz = 1;
if (Bid-OrderOpenPrice() > Kz*BreakEven*Point)
{
BuyStop=OrderOpenPrice()+((Kz-1)*BreakEven+BreakEvenGap)*Point;
OrderModify(OrderTicket(),OrderOpenPrice(),
NormalizeDouble(BuyStop, digit),
OrderTakeProfit(),0,LightBlue);
return(0);
}
}
if ( mode==OP_SELL )
{
if ( OrderOpenPrice()-Ask > 0 )
{Kz = MathFloor((OrderOpenPrice()-Ask)/(BreakEven*Point));
if (Kz < 1) Kz=1;} else Kz = 1;
if (OrderOpenPrice()-Ask > Kz*BreakEven*Point)
{
SellStop=OrderOpenPrice()-((Kz-1)*BreakEven+BreakEvenGap)*Point;
OrderModify(OrderTicket(),OrderOpenPrice(),
NormalizeDouble(SellStop, digit),
OrderTakeProfit(),0,Orange);
return(0);
}
}
}
}
}
void TrailStop()
{
for (cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS);
int mode=OrderType();
if ( OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if (mode==OP_BUY)
{
BuyStop = bsmin;
if (BuyStop>OrderStopLoss())
{
bool result = OrderModify(OrderTicket(),OrderOpenPrice(),
NormalizeDouble(BuyStop, digit),
OrderTakeProfit(),0,LightGreen);
//if( !result )
//{
//Print("BUY: OrderModify failed with error #",GetLastError());
//return(0);
//}
}
}
// - SELL Orders
if (mode==OP_SELL)
{
SellStop = bsmax;
if( SellStop<OrderStopLoss())
{
result = OrderModify(OrderTicket(),OrderOpenPrice(),
NormalizeDouble(SellStop, digit),
OrderTakeProfit(),0,Yellow);
//if( !result )
//{
//Print("SELL: OrderModify failed with error #",GetLastError());
//return(0);
//}
}
}
}
}
}
// ---- Open Sell Orders
void SellOrdOpen()
{
double SellStop,SellProfit;
double SellPrice=Bid;
double StopPrice = Ask;
int Mode = OP_SELL;
if ( PendingOrder ) {SellPrice=bsmin - PendOrdGap*Point; StopPrice = SellPrice; Mode=OP_SELLSTOP;}
if (InitialStop > 0) SellStop =StopPrice + InitialStop*Point;
else if (!TrailingStop) SellStop = 0; else SellStop = bsmax;
if (TakeProfit > 0) SellProfit=SellPrice - TakeProfit*Point;
else SellProfit=0;
ticket = OrderSend( Symbol(),Mode,MoneyManagement ( MM, Lots, MMRisk, MaxLoss),
NormalizeDouble(SellPrice, digit),
Slippage,
NormalizeDouble(SellStop , digit),
NormalizeDouble(SellProfit , digit),
"sell",Magic,0,Red);
if(ticket<0)
{
Print("SELL: OrderSend failed with error #",GetLastError());
}
if ( PendingOrder && GetLastError() == 130)
int ticket2=OrderSend(Symbol(),OP_SELL, MoneyManagement ( MM, Lots, MMRisk, MaxLoss),
Bid,
Slippage,
NormalizeDouble(SellStop , digit),
NormalizeDouble(SellProfit , digit),
"sell",Magic,0,Red);
SellInTrade=true; BuyInTrade=false; SellSignal=false;
return(0);
}
// ---- Open Buy Orders
void BuyOrdOpen()
{
double BuyStop,BuyProfit;
double BuyPrice = Ask;
double StopPrice = Bid;
int Mode = OP_BUY;
if ( PendingOrder ) {BuyPrice = smax1 + PendOrdGap*Point; StopPrice = BuyPrice; Mode = OP_BUYSTOP;}
if (InitialStop > 0) BuyStop = StopPrice - InitialStop*Point;
else if (!TrailingStop) BuyStop = 0; else BuyStop = bsmin;
if (TakeProfit > 0) BuyProfit= BuyPrice + TakeProfit*Point;
else BuyProfit=0;
ticket = OrderSend(Symbol(),Mode, MoneyManagement ( MM, Lots, MMRisk, MaxLoss),
NormalizeDouble(BuyPrice, digit),
Slippage,
NormalizeDouble(BuyStop , digit),
NormalizeDouble(BuyProfit , digit),
"buy",Magic,0,Blue);
if(ticket<0)
{
Print("BUY : OrderSend failed with error #",GetLastError());
//return(0);
}
if (PendingOrder && GetLastError() == 130)
int ticket2=OrderSend(Symbol(),OP_BUY, MoneyManagement ( MM, Lots, MMRisk, MaxLoss),
Ask,
Slippage,
NormalizeDouble(BuyStop , digit),
NormalizeDouble(BuyProfit , digit),
"buy",Magic,0,Blue);
BuyInTrade=true; SellInTrade = false; BuySignal = false;
return(0);
}
void PendOrdsDel()
{
if(Close[1]< 0.5*(bsmin+bsmax))int PendClose=1;
if(Close[1]> 0.5*(bsmin+bsmax)) PendClose=-1;
for (cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS);
int mode=OrderType();
if ( OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if (mode==OP_BUYSTOP && PendClose>0)
{
OrderDelete(OrderTicket());
return(0);
}
else
if (mode==OP_SELLSTOP && PendClose<0)
{
OrderDelete(OrderTicket());
return(0);
}
}
}
}
// -------------------------------------------------
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int total,ticket;
//----
digit = MarketInfo(Symbol(),MODE_DIGITS);
if ( Trace ) SetTrace();
BBStops();
PendOrdsDel();
TradeSignal();
ExitSignal();
CloseTrade();
if (BreakEven >0) BreakEvenStop();
if (TrailingStop) TrailStop();
if (SwingTrade)
{
if (ScanTrades()<1 && BuySignal && !BuyInTrade ) BuyOrdOpen() ;
if (ScanTrades()<1 && SellSignal && !SellInTrade) SellOrdOpen();
}
else
{
if (ScanTrades()<1 && BuySignal ) BuyOrdOpen() ;
if (ScanTrades()<1 && SellSignal) SellOrdOpen();
}
//----
return(0);
} //start()
//+------------------------------------------------------------------+
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
---