//+------+ //|BollTrade //+------+ #property copyright "Ron Thompson" #property link "http://www.lightpatch.com/forex" #include <LibNewsTimes_v02.mqh> // This EA is NEVER TO BE SOLD individually // This EA is NEVER TO BE INCLUDED as part of a collection that is SOLD // user input // "12345678901234567890123456789012345678901"; 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 = 8; extern string BasketProfit_is = "close all orders if this $ profit "; extern double BasketProfit = 14.5; extern string LossLimit_is = "PIPS LOSS you can afford per trade "; extern double LossLimit = 35; 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 VolumeMax_is = "Volume must be below this before trading "; extern double VolumeMax = 280; extern string Compounding_is = "Open more orders based on CompoundStep "; extern bool Compounding = false; extern string CompoundStep_is = "how many pips movement before compounding"; extern int CompoundStep = 1; 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 = 2.0; 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.1; 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 = 1 ; extern string TradeOnFriday_is = "trade on Friday, or not "; extern bool TradeOnFriday = true ; extern string ExtraComment_is = "appended to TradeComment for each order "; extern string ExtraComment = "" ; extern string KillLogging_is = "Turn off ALL logging "; extern bool KillLogging = true ; extern string logging_is = "Logging of data on order Open and Close "; extern bool logging = true ; extern string logerrs_is = "Logging of errors when they happen "; extern bool logerrs = true ; extern string logtick_is = "Log information on each tick "; extern bool logtick = false ; // non-external flag settings int Slippage=2; // how many pips of slippage can you tolorate double CompoundTrack=0; // used to track compounding and steps // naming and numbering int MagicNumber = 363641; // allows multiple experts to trade on same account string TradeComment = "_bolltrade_v03mD3.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 // Min/Max tracking and tick logging int maxOrders; // statistic for maximum numbers or orders open at one time double maxEquity=0; // statistic for maximum equity level double minEquity=999999; // statistic for minimum equity level double EquityPos=0; // statistic for number of ticks order was positive double EquityNeg=0; // statistic for number of ticks order was negative double EquityZer=0; // statistic for number of ticks order was zero // used for verbose error logging #include <stdlib.mqh> #include <LibNewsTimes_v02.mqh> // EA Specific double bup0; double bup1; double bdn0; double bdn1; //+-------------+ //| 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; logwrite(TradeComment,"LotIncrease ACTIVE Account balance="+AccountBalance()+" Lots="+Lots+" StartingBalance="+StartingBalance); } else { logwrite(TradeComment,"LotIncrease NOT ACTIVE Account balance="+AccountBalance()+" Lots="+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,"MAX equity "+maxEquity); logwrite(TradeComment,"MIN equity "+minEquity); // so you can see stats in journal Print("MAX number of orders "+maxOrders); Print("MAX equity "+maxEquity); Print("MIN equity "+minEquity); logwrite(TradeComment,"DE-Init Complete"); Comment(" "); } //+-----------+ //| Main | //+-----------+ // Called EACH TICK and each Bar[] int start() { int cnt=0; int gle=0; int ticket=0; int OrdersPerSymbol=0; int OrdersBUY=0; int OrdersSELL=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++; CompoundTrack=0; bup1=bup0; bdn1=bdn0; } // 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++; if(OrderType()==OP_BUY) {OrdersBUY++;} if(OrderType()==OP_SELL){OrdersSELL++;} } } if(OrdersPerSymbol==0) { // This allows the 1st order at BDistance after everything closes // others are handled in the indicator section TradeAllowed=true; // this makes a good-sized difference in profits // Once the orders all clear, reset the compound limiter // so that if price goes BACK UP AGAIN within same bar // you can take advantage of more profit. // Makes curve noiser tho, so some people may not like it // CompoundTrack=0; } // 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))+(CompoundTrack*Point); double bdx= (bdn0-(BDistance*Point))-(CompoundTrack*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); // < and NOT <= so tick won't match 0 in a non-tick bar bool volumeOK=false; if(Volume[1]<VolumeMax && Volume[0]<VolumeMax) volumeOK=true; if (VolumeMax==0) volumeOK=true; bool TradeDay=true; if(DayOfWeek()==5 && TradeOnFriday==false) TradeDay=false; // set false 1st, then we can check multiple related pairs NewsTime = false; if( IsNewsTime(MinutesBeforeNews, MinutesAfterNews, Symbol()) ) { NewsTime = true; Comment("TRUE IsNewsTime TRUE\n"); } else { Comment("FALSE IsNewsTime FALSE\n"); } // if close is above upper band + BDistance then SELL if(Close[0]>bux && volumeOK && TradeDay && !NewsTime) { SELLme=true; if(logging) logwrite(TradeComment,"---SELLme happened"); if(Compounding && CompoundTrack>0 ) { SELLme=true; TradeAllowed=true; if(logging) logwrite(TradeComment,"---SELLme happened again at "+CompoundTrack ); } } // if close is below lower band + BDistance then BUY if(Close[0]<bdx && volumeOK && TradeDay && !NewsTime) { BUYme=true; if(logging) logwrite(TradeComment,"----BUYme happened"); if(Compounding && CompoundTrack>0) { BUYme=true; TradeAllowed=true; if(logging) logwrite(TradeComment,"----BUYme happened again at "+CompoundTrack ); } } //+------------+ //| End Insert | //+------------+ //ENTRY LONG (buy, Ask) if( TradeAllowed && BUYme) { while(true) { if( AccountFreeMargin()< (AccountBalance()*(MinFreeMarginPct/100)) ) { if(logging) 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,Lots,Ask,Slippage,SL,TP,TradeComment,MagicNumber,White); gle=GetLastError(); if(gle==0) { if (Compounding && CompoundStep>0) CompoundTrack=CompoundTrack+CompoundStep; if(logging) logwrite(TradeComment," "); if(logging) logwrite(TradeComment,"BUY Ticket="+ticket+" bdn0="+bdn0+" Ask="+Ask+" Lots="+Lots+" SL="+SL+" TP="+TP); EquityPos=0; EquityNeg=0; EquityZer=0; TradeAllowed=false; break; } else { if(logerrs) logwrite(TradeComment,"-----ERROR----- opening BUY order: SL="+SL+" TP="+TP+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle)); Print("-----ERROR----- opening BUY order :"+gle+" ticket="+ticket+" "+ErrorDescription(gle)); RefreshRates(); Sleep(500); } }//while }//BUYme //ENTRY SHORT (sell, Bid) if( TradeAllowed && SELLme ) { while(true) { if( AccountFreeMargin()< (AccountBalance()*(MinFreeMarginPct/100)) ) { if(logging) 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,Lots,Bid,Slippage,SL,TP,TradeComment,MagicNumber,Red); gle=GetLastError(); if(gle==0) { if (Compounding && CompoundStep>0) CompoundTrack=CompoundTrack+CompoundStep; if(logging) logwrite(TradeComment," "); if(logging) logwrite(TradeComment,"SELL Ticket="+ticket+" bup0="+bup0+" Bid="+Bid+" Lots="+Lots+" SL="+SL+" TP="+TP); EquityPos=0; EquityNeg=0; EquityZer=0; TradeAllowed=false; break; } else { if(logerrs) logwrite(TradeComment,"-----ERROR----- opening SELL order: SL="+SL+" TP="+TP+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle)); Print("-----ERROR----- opening SELL order :"+gle+" ticket="+ticket+" "+ErrorDescription(gle)); RefreshRates(); Sleep(500); } }//while }//SELLme //Basket profit or loss CurrentBasket=AccountEquity()-AccountBalance(); if( BasketProfit>0 && CurrentBasket>=BasketProfit ) CloseEverything(); if( BasketLoss >0 && CurrentBasket<=(BasketLoss*(-1)) ) CloseEverything(); // accumulate statistics CurrentBasket=AccountEquity()-AccountBalance(); if(CurrentBasket>maxEquity) { maxEquity=CurrentBasket; } if(CurrentBasket<minEquity) { minEquity=CurrentBasket; } if(CurrentBasket>0) EquityPos++; if(CurrentBasket<0) EquityNeg++; if(CurrentBasket==0) EquityZer++; // // 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()) ; if(logtick) 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) { if(logging) logwrite(TradeComment,"MODIFY BUY BE Ticket="+OrderTicket()+" SL="+SL+" TP="+TP); } else { if(logerrs) 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) { if(logging) logwrite(TradeComment,"MODIFY BUY TS Ticket="+OrderTicket()+" SL="+SL+" TP="+TP); } else { if(logerrs) logwrite(TradeComment,"-----ERROR----- MODIFY BUY TS Bid="+Bid+" error="+gle+" "+ErrorDescription(gle)+" "); } } } // Did we make a profit //====================== if(ProfitMade>0 && CurrentProfit>=(ProfitMade*Point)) CloseBuy("PROFIT"); // Did we take a loss //==================== if(LossLimit>0 && CurrentProfit<=(LossLimit*(-1)*Point)) CloseBuy("PROFIT"); } // if BUY if(OrderType()==OP_SELL) { CurrentProfit=(OrderOpenPrice()-Ask); if(logtick) 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) { if(logging) logwrite(TradeComment,"MODIFY SELL BE Ticket="+OrderTicket()+" SL="+SL+" TP="+TP); } else { if(logerrs) 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) { if(logging) logwrite(TradeComment,"MODIFY SELL TS Ticket="+OrderTicket()+" SL="+SL+" TP="+TP); } else { if(logerrs) logwrite(TradeComment,"-----ERROR----- MODIFY SELL TS Ask="+Ask+" error="+gle+" "+ErrorDescription(gle)); } } } // Did we make a profit //====================== if( ProfitMade>0 && CurrentProfit>=(ProfitMade*Point) ) CloseSell("PROFIT"); // Did we take a loss //==================== if( LossLimit>0 && CurrentProfit<=(LossLimit*(-1)*Point) ) CloseSell("LOSS"); } //if SELL } // if(OrderSymbol) } // for } // 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); // 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) { if(logging) { logwrite(TradeComment,"CLOSE BUY "+myInfo+" Ticket="+OrderTicket()+" SL="+OrderStopLoss()+" TP="+OrderTakeProfit()+" PM="+ProfitMade+" LL="+LossLimit); logwrite(TradeComment,"order equity positive ticks ="+EquityPos); logwrite(TradeComment,"order equity negative ticks ="+EquityNeg); logwrite(TradeComment,"order equity zero ticks ="+EquityZer); } break; } else { if(logerrs) 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) { if(logging) { logwrite(TradeComment,"CLOSE SELL "+myInfo+" Ticket="+OrderTicket()+" SL="+OrderStopLoss()+" TP="+OrderTakeProfit()+" PM="+ProfitMade+" LL="+LossLimit); logwrite(TradeComment,"order equity positive ticks ="+EquityPos); logwrite(TradeComment,"order equity negative ticks ="+EquityNeg); logwrite(TradeComment,"order equity zero ticks ="+EquityZer); } break; } else { if(logerrs) 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 }
Sample
Analysis
Market Information Used:
Series array that contains open time of each bar
Series array that contains close prices for each bar
Series array that contains tick volumes of each bar
Indicator Curves created:
Indicators Used:
Moving average indicator
Standard Deviation indicator
Custom Indicators Used:
Order Management characteristics:
Checks for the total of open orders
It automatically opens orders when conditions are reached
It can change open orders parameters, due to possible stepping strategy
It Closes Orders by itself
Other Features:
Uses files from the file system
It writes information to file