Price Data Components
Orders Execution
Indicators Used
Miscellaneous
1
Views
0
Downloads
0
Favorites
[ea]BollTrade_Ron_MT4_v03mD3_GridExit v02 EUSD
//+------+
//|BollTrade
//+------+
#property copyright "Ron Thompson"
#property link "http://www.lightpatch.com/forex"
// This EA is NEVER TO BE SOLD individually
// This EA is NEVER TO BE INCLUDED as part of a collection that is SOLD
// v02
//
// removed all the "if(logging)" stuff and handled it all in the logwrite function
// removed all the equity tracking statistics, didn't work with grid exit
// removed compounding - bad idea
// removed TradeOnFriday
// removed volume checking
// added framework for Martingale (need to frame orders so each individual 0.01 loss
// doesn't multiply the lot size!!!)
// user input
// "12345678901234567890123456789012345678901";
extern string OpenOrders_is = "Numbers of orders for grid exit ";
extern int OpenOrders = 17;
extern string StartProfit_is = "ProfitMade level to take out 1st order ";
extern int StartProfit = 8;
extern string StepProfit_is = "ProfitMade steps for each other order ";
extern int StepProfit = 1;
extern string BEOrders_is = "Move all BreakEven after closing orders ";
extern int BEOrders = 1;
string MStep_is = "Increase next order this much if a loss ";
double MStep = 1.0;
extern string MinutesBeforeNews_is= "Minutes BEFORE news event to avoid trades ";
extern int MinutesBeforeNews = 16;
extern string MinutesAfterNews_is = "Minutes AFTER news event to avoid trades ";
extern int MinutesAfterNews = 16;
extern string MinFreeMarginPct_is = "percent of freemargin necessary to trade ";
extern double MinFreeMarginPct = 25.0;
extern string ProfitMade_is = "PIPS PROFIT you want to make per trade ";
extern double ProfitMade = 0;
extern string BasketProfit_is = "close all orders if this $ profit ";
extern double BasketProfit = 0;
extern string LossLimit_is = "PIPS LOSS you can afford per trade ";
extern double LossLimit = 9;
extern string BasketLoss_is = "close all orders if this $ loss ";
extern double BasketLoss = 0;
extern string BreakEven_is = "set StopLoss to OpenPrice at this profit ";
extern double BreakEven = 0;
extern string TrailStop_is = "Trail OrderPrice starting from 1st tick ";
extern double TrailStop = 0;
extern string BDistance_is = "pips outside bollinger before trading ";
extern double BDistance = 14;
extern string BPeriod_is = "Bollinger period ";
extern int BPeriod = 15;
extern string Deviation_is = "Bollinger deviation ";
extern double Deviation = 1.8;
extern string Lots_is1 = "How many lots or partial lots per trade ";
extern string Lots_is2 = "Lots affects BasketProfit and Loss ";
extern double Lots = 0.01;
extern string LotIncrease_is = "grow Lots based on account balance ";
extern bool LotIncrease = true ;
extern string LotResolution_is = "for LotIncrease: 0=FullLot 1=0.1 2=0.01 ";
extern int LotResolution = 2 ;
extern string ExtraComment_is = "appended to TradeComment for each order ";
extern string ExtraComment = "" ;
extern string KillLogging_is = "Turn off ALL logging ";
extern bool KillLogging = false ;
// non-external flag settings
int Slippage=2; // how many pips of slippage can you tolorate
// naming and numbering
int MagicNumber = 363655; // allows multiple experts to trade on same account
string TradeComment = "_bolltrade_v03mD3_GridExit.txt";
double StartingBalance=0; // lot size control if LotIncrease == true
int LL2SL=10; // LossLimit to StopLoss server spread
int maxloop=50; // no more than 50 tries/25 seconds to close an order
// Bar handling
datetime bartime=0; // used to determine when a bar has moved
int bartick=0; // number of times bars have moved
// Trade control
bool TradeAllowed=true; // used to manage trades
// Lot Recovery
double lotsi;
// Min/Max tracking and tick logging
int maxOrders; // statistic for maximum numbers or orders open at one time
int maxLots; // statistic for maximum numbers or orders open at one time
// used for verbose error logging
#include <stdlib.mqh>
#include <LibNewsTimes_v02.mqh>
// EA Specific
double bup0;
double bdn0;
// Grid specific
double tpx[20];
int profitclosed;
//+-------------+
//| Custom init |
//|-------------+
// Called ONCE when EA is added to chart or recompiled
int init()
{
TradeComment=TradeComment+" "+ExtraComment;
if(MinFreeMarginPct==0) MinFreeMarginPct=1;
if(LotIncrease)
{
StartingBalance=AccountBalance()/Lots;
//StartingBalance=5000.00;
logwrite(TradeComment,"LotIncrease ACTIVE Account balance="+AccountBalance()+" Lots="+Lots+" StartingBalance="+StartingBalance);
}
else
{
logwrite(TradeComment,"LotIncrease NOT ACTIVE Account balance="+AccountBalance()+" Lots="+Lots);
}
lotsi=Lots;
logwrite(TradeComment,"Init Complete");
Comment(" ");
}
//+----------------+
//| Custom DE-init |
//+----------------+
// Called ONCE when EA is removed from chart
int deinit()
{
// always indicate deinit statistics
logwrite(TradeComment,"MAX number of orders "+maxOrders);
logwrite(TradeComment,"DE-Init Complete");
Comment(" ");
}
//+-----------+
//| Main |
//+-----------+
// Called EACH TICK and each Bar[]
int start()
{
int cnt=0;
int ptr=0;
int clo=0;
int gle=0;
int ticket=0;
int OrdersPerSymbol=0;
// stoploss and takeprofit and close control
double SL=0;
double TP=0;
double CurrentProfit=0;
double CurrentBasket=0;
// direction control
bool BUYme=false;
bool SELLme=false;
//safety counter
int loopcount=0;
// bar counting
if(bartime!=Time[0])
{
bartime=Time[0];
bartick++;
// this limits trade to once per bar
OrdersPerSymbol=0;
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) OrdersPerSymbol++;
}
if(OrdersPerSymbol==0) TradeAllowed=true;
}
// Lot increasement based on AccountBalance when expert is started
// this will trade 1.0, then 1.1, then 1.2 etc as account balance grows
// or 0.9 then 0.8 then 0.7 as account balance shrinks
if(LotIncrease)
{
Lots=NormalizeDouble(AccountBalance()/StartingBalance,LotResolution);
if( Lots>MarketInfo(Symbol(), MODE_MAXLOT) ) Lots=MarketInfo(Symbol(), MODE_MAXLOT);
}
OrdersPerSymbol=0;
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) OrdersPerSymbol++;
}
// keep some statistics
if(OrdersPerSymbol>maxOrders) maxOrders=OrdersPerSymbol;
//+-----------------------------+
//| Insert your indicator here |
//| And set either BUYme or |
//| SELLme true to place orders |
//+-----------------------------+
bool NewsTime=false;
double ma = iMA(Symbol(),0,BPeriod,0,MODE_SMA,PRICE_OPEN,0);
double stddev = iStdDev(Symbol(),0,BPeriod,0,MODE_SMA,PRICE_OPEN,0);
bup0 = ma+(Deviation*stddev);
bdn0 = ma-(Deviation*stddev);
// if NOT compounding, CompoundTrack will always be 0
double bux= (bup0+(BDistance*Point));
double bdx= (bdn0-(BDistance*Point));
string myCmt;
if(Close[0]>bup0) myCmt="S Up="+bup0+" Dn="+bdn0+" Close="+Close[0]+" Vol_1="+Volume[1]+" Vol_0="+Volume[0];
if(Close[0]<bdn0) myCmt="B Up="+bup0+" Dn="+bdn0+" Close="+Close[0]+" Vol_1="+Volume[1]+" Vol_0="+Volume[0];
if(Close[0]<bup0 && Close[0]>bdn0) myCmt="X Up="+bup0+" Dn="+bdn0+" Close="+Close[0]+" Vol_1="+Volume[1]+" Vol_0="+Volume[0];
Comment(myCmt);
// set false 1st, then we can check multiple related pairs
NewsTime = false;
if( IsNewsTime(MinutesBeforeNews, MinutesAfterNews, Symbol()) )
{
NewsTime = true;
Comment("TRUE IsNewsTime TRUE\n");
logwrite(TradeComment,"Its NewsTime CurTime="+CurTime()+" Local="+LocalTime() );
}
else
{
Comment("FALSE IsNewsTime FALSE\n");
}
// if close is above upper band + BDistance then SELL
if(Close[0]>bux && !NewsTime)
{
SELLme=true;
logwrite(TradeComment,"---SELLme happened");
}
// if close is below lower band + BDistance then BUY
if(Close[0]<bdx && !NewsTime)
{
BUYme=true;
logwrite(TradeComment,"----BUYme happened");
}
//+------------+
//| End Insert |
//+------------+
//ENTRY LONG (buy, Ask)
if( TradeAllowed && BUYme)
{
ptr=StartProfit;
for(cnt=1; cnt<=OpenOrders; cnt++)
{
OpenBuy("BUY");
tpx[cnt]=ptr;
ptr=ptr+StepProfit;
}
// used for break even
profitclosed=OpenOrders;
}//BUYme
//ENTRY SHORT (sell, Bid)
if( TradeAllowed && SELLme )
{
ptr=StartProfit;
for(cnt=1; cnt<=OpenOrders; cnt++)
{
OpenSell("SELL");
tpx[cnt]=ptr;
ptr=ptr+StepProfit;
}
// used for break even
profitclosed=OpenOrders;
}//SELLme
//Basket profit or loss
CurrentBasket=AccountEquity()-AccountBalance();
if( BasketProfit>0 && CurrentBasket>=BasketProfit ) CloseEverything();
if( BasketLoss >0 && CurrentBasket<=(BasketLoss*(-1)) ) CloseEverything();
//
// Order Management
//
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber )
{
if(OrderType()==OP_BUY)
{
CurrentProfit=(Bid-OrderOpenPrice()) ;
//logwrite(TradeComment,"BUY CurrentProfit="+CurrentProfit/Point+" CurrentBasket="+CurrentBasket/Point);
//
// Modify for break even
//=======================
//
// OrderStopLoss will be equal to OrderOpenPrice if this event happens
// thus it will only ever get executed one time per ticket
if( BreakEven>0 )
{
if (CurrentProfit >= BreakEven*Point && OrderOpenPrice()>OrderStopLoss())
{
SL=OrderOpenPrice()+(Ask-Bid);
TP=OrderTakeProfit();
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP, White);
gle=GetLastError();
if(gle==0)
{
logwrite(TradeComment,"MODIFY BUY BE Ticket="+OrderTicket()+" SL="+SL+" TP="+TP);
}
else
{
logwrite(TradeComment,"-----ERROR----- MODIFY BUY BE Bid="+Bid+" error="+gle+" "+ErrorDescription(gle));
}
}
}
//
// check for trailing stop
//=========================
//
if( TrailStop>0 )
{
if( Bid-OrderStopLoss()>(TrailStop*Point) )
{
SL=Bid-(TrailStop*Point);
TP=OrderTakeProfit();
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,White);
gle=GetLastError();
if(gle==0)
{
logwrite(TradeComment,"MODIFY BUY TS Ticket="+OrderTicket()+" SL="+SL+" TP="+TP);
}
else
{
logwrite(TradeComment,"-----ERROR----- MODIFY BUY TS Bid="+Bid+" error="+gle+" "+ErrorDescription(gle)+" ");
}
}
}
// Did we make a profit
//======================
for(clo=1; clo<=OpenOrders; clo++)
{
if(tpx[clo]>0 && CurrentProfit>=(tpx[clo]*Point))
{
CloseBuy("PROFIT");
tpx[clo]=0;
// indicate that we closed a profit order
profitclosed--;
lotsi=Lots;
}
}
// Did we take a loss
//====================
if(LossLimit>0 && CurrentProfit<=(LossLimit*(-1)*Point))
{
CloseBuy("LOSS");
lotsi=NormalizeDouble(lotsi*MStep,LotResolution);
}
} // if BUY
if(OrderType()==OP_SELL)
{
CurrentProfit=(OrderOpenPrice()-Ask);
//logwrite(TradeComment,"SELL CurrentProfit="+CurrentProfit/Point+" CurrentBasket="+CurrentBasket/Point);
//
// Modify for break even
//=======================
//
// OrderStopLoss will be equal to OrderOpenPrice if this event happens
// thus it will only ever get executed one time per ticket
if( BreakEven>0 )
{
if (CurrentProfit >= BreakEven*Point && OrderOpenPrice()<OrderStopLoss())
{
SL=OrderOpenPrice()-(Ask-Bid);
TP=OrderTakeProfit();
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP, Red);
gle=GetLastError();
if(gle==0)
{
logwrite(TradeComment,"MODIFY SELL BE Ticket="+OrderTicket()+" SL="+SL+" TP="+TP);
}
else
{
logwrite(TradeComment,"-----ERROR----- MODIFY SELL BE Ask="+Ask+" error="+gle+" "+ErrorDescription(gle));
}
}
}
//
// check for trailing stop
//=========================
//
if(TrailStop>0)
{
if( (OrderStopLoss()-Ask)>(TrailStop*Point) )
{
SL=Ask+(TrailStop*Point);
TP=OrderTakeProfit();
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Red);
gle=GetLastError();
if(gle==0)
{
logwrite(TradeComment,"MODIFY SELL TS Ticket="+OrderTicket()+" SL="+SL+" TP="+TP);
}
else
{
logwrite(TradeComment,"-----ERROR----- MODIFY SELL TS Ask="+Ask+" error="+gle+" "+ErrorDescription(gle));
}
}
}
// Did we make a profit
//======================
for(clo=1; clo<=OpenOrders; clo++)
{
if(tpx[clo]>0 && CurrentProfit>=(tpx[clo]*Point))
{
CloseSell("PROFIT");
tpx[clo]=0;
// indicate that we closed a profit order
profitclosed--;
lotsi=Lots;
}
}
// Did we take a loss
//====================
if( LossLimit>0 && CurrentProfit<=(LossLimit*(-1)*Point) )
{
CloseSell("LOSS");
lotsi=NormalizeDouble(lotsi*MStep,LotResolution);
}
} //if SELL
} // if(OrderSymbol)
} // for
// now that all orders have been opened or closed in steps
// lets check to see if we've exceeded the BEOrders count
// of closed orders, and set everything remaining to BreakEven
// if we've closed more than BEOrders,
// then set everything to Break even
// use = then ser profitclosed to zero
// so this only ever happens once per frame
// because gaps can close more than one order in the
// trade management loop above, and skip the BE
if(profitclosed!=0 && OpenOrders-profitclosed>=BEOrders)
{
profitclosed=0;
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber )
{
if(OrderType()==OP_BUY)
{
SL=OrderOpenPrice()+(Ask-Bid);
TP=OrderTakeProfit();
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP, White);
gle=GetLastError();
if(gle==0)
{
logwrite(TradeComment,"BEORDERS BUY BE Ticket="+OrderTicket()+" SL="+SL+" TP="+TP);
}
else
{
logwrite(TradeComment,"-----ERROR----- BEORDERS BUY BE Bid="+Bid+" error="+gle+" "+ErrorDescription(gle));
}
}
if(OrderType()==OP_SELL)
{
SL=OrderOpenPrice()-(Ask-Bid);
TP=OrderTakeProfit();
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP, Red);
gle=GetLastError();
if(gle==0)
{
logwrite(TradeComment,"BEORDERS SELL BE Ticket="+OrderTicket()+" SL="+SL+" TP="+TP);
}
else
{
logwrite(TradeComment,"-----ERROR----- BEORDERS SELL BE Ask="+Ask+" error="+gle+" "+ErrorDescription(gle));
}
}
}//magic
}//for
}//if
} // start()
//+-----------------+
//| CloseEverything |
//+-----------------+
// Closes all OPEN and PENDING orders
int CloseEverything()
{
int i;
for(i=OrdersTotal();i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
if(OrderType()==OP_BUY) CloseBuy ("BASKET");
if(OrderType()==OP_SELL) CloseSell("BASKET");
if(OrderType()==OP_BUYLIMIT) OrderDelete( OrderTicket() );
if(OrderType()==OP_SELLLIMIT) OrderDelete( OrderTicket() );
if(OrderType()==OP_BUYSTOP) OrderDelete( OrderTicket() );
if(OrderType()==OP_SELLSTOP) OrderDelete( OrderTicket() );
}
Sleep(1000);
} //for
} // closeeverything
void logwrite (string filename, string mydata)
{
int myhandle;
string gregorian=TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS);
// everything goes to the journal
Print(mydata+" "+gregorian);
// don't log anything if testing or if user doesn't want it
if(IsTesting()) return(0);
if(KillLogging) return(0);
myhandle=FileOpen(Symbol()+"_"+filename, FILE_CSV|FILE_WRITE|FILE_READ, ";");
if(myhandle>0)
{
FileSeek(myhandle,0,SEEK_END);
FileWrite(myhandle, mydata+" "+gregorian);
FileClose(myhandle);
}
}
void CloseBuy (string myInfo)
{
int gle;
int cnt;
int OrdersPerSymbol;
int loopcount=0;
while(true)
{
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,White);
gle=GetLastError();
if(gle==0)
{
logwrite(TradeComment,"CLOSE BUY "+myInfo+" Ticket="+OrderTicket()+" SL="+OrderStopLoss()+" TP="+OrderTakeProfit()+" PM="+ProfitMade+" LL="+LossLimit);
break;
}
else
{
logwrite(TradeComment,"-----ERROR----- CLOSE BUY PROFIT Bid="+Bid+" error="+gle+" "+ErrorDescription(gle));
RefreshRates();
Sleep(500);
}
OrdersPerSymbol=0;
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) break;
}
loopcount++;
if(loopcount>maxloop) break;
}//while
}
void CloseSell (string myInfo)
{
int gle;
int cnt;
int OrdersPerSymbol;
int loopcount=0;
while(true)
{
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);
gle=GetLastError();
if(gle==0)
{
logwrite(TradeComment,"CLOSE SELL "+myInfo+" Ticket="+OrderTicket()+" SL="+OrderStopLoss()+" TP="+OrderTakeProfit()+" PM="+ProfitMade+" LL="+LossLimit);
break;
}
else
{
logwrite(TradeComment,"-----ERROR----- CLOSE SELL PROFIT Ask="+Ask+" error="+gle+" "+ErrorDescription(gle));
RefreshRates();
Sleep(500);
}
OrdersPerSymbol=0;
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) break;
}
loopcount++;
if(loopcount>maxloop) break;
}//while
}
//ENTRY LONG (buy, Ask)
void OpenBuy (string myInfo)
{
int loopcount=0;
double SL,TP;
int ticket;
int gle;
while(true)
{
if( AccountFreeMargin()< (AccountBalance()*(MinFreeMarginPct/100)) )
{
logwrite(TradeComment,"Your BUY equity is too low to trade");
break;
}
if(LossLimit ==0) SL=0; else SL=Ask-((LossLimit+LL2SL)*Point );
if(ProfitMade==0) TP=0; else TP=Ask+((ProfitMade+LL2SL)*Point );
ticket=OrderSend(Symbol(),OP_BUY,lotsi,Ask,Slippage,SL,TP,TradeComment,MagicNumber,White);
gle=GetLastError();
if(gle==0)
{
logwrite(TradeComment,"BUY Ticket="+ticket+" bdn0="+bdn0+" Ask="+Ask+" Lots="+lotsi+" SL="+SL+" TP="+TP);
TradeAllowed=false;
break;
}
else
{
logwrite(TradeComment,"-----ERROR----- opening BUY order: SL="+SL+" TP="+TP+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle));
RefreshRates();
Sleep(500);
}
}//while
}//OpenBuy
//ENTRY SHORT (sell, Bid)
void OpenSell (string myInfo)
{
int loopcount=0;
double SL,TP;
int ticket;
int gle;
while(true)
{
if( AccountFreeMargin()< (AccountBalance()*(MinFreeMarginPct/100)) )
{
logwrite(TradeComment,"Your SELL equity is too low to trade");
break;
}
if(LossLimit ==0) SL=0; else SL=Bid+((LossLimit+LL2SL)*Point );
if(ProfitMade==0) TP=0; else TP=Bid-((ProfitMade+LL2SL)*Point );
ticket=OrderSend(Symbol(),OP_SELL,lotsi,Bid,Slippage,SL,TP,TradeComment,MagicNumber,Red);
gle=GetLastError();
if(gle==0)
{
logwrite(TradeComment,"SELL Ticket="+ticket+" bup0="+bup0+" Bid="+Bid+" Lots="+lotsi+" SL="+SL+" TP="+TP);
TradeAllowed=false;
break;
}
else
{
logwrite(TradeComment,"-----ERROR----- opening SELL order: SL="+SL+" TP="+TP+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle));
RefreshRates();
Sleep(500);
}
}//while
}//SELLme
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
---