//+------------------------------------------------------------------+ //| BarFly.mq4 | //| Copyright © 2011, Markdshark, Cave Ceteris Investments, LLC | //| http://www.caveceteris.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2011, Markdshark, Cave Ceteris Investments, LLC" #property link "http://www.caveceteris.com" ////////////////////////////////////////////////////////////////////////////// /* Revision History Date Author Version Changes 8/6/11 markdshark 1 Coding start */ ////////////////////////////////////////////////////////////////////////////// #import "stdlib.ex4" string ErrorDescription(int error_code); #import //--------- External Variables extern string GeneralSettings = "---------------------------"; extern double externLot=1; extern double maxLots=2; extern double accountFactor=0.17; //%age of acct value extern string s6Section = "--------Barfly settings--------"; extern double flyFactor=3; extern double capturePips = 100; //- extern string comment="barFly"; //-----------Internal Variables bool newBar=false, weekendAlert; bool s6FirstTime=true; bool tradable=true; double lot; double stopLoss, sarStopLoss, lastSarStopLoss, sandLineTest; double myPoint,tickValue; double pipsLostThisBar; int trend, ticket, s6LastTrade=0; int arrayStrat6Tickets[10]; string s6Comment="barFly"; static datetime currBar; /////////////////////////////////////////////// // init /////////////////////////////////////////////// void init(){ myPoint = setPoint(); tickValue = MarketInfo(Symbol(),MODE_TICKVALUE); ArrayResize(arrayStrat6Tickets, externLot); ///This will get screwed if you use a fractional value for externLots ArrayInitialize(arrayStrat6Tickets,-1); } /////////////////////////////////////////////// // initializeTradeFlags /////////////////////////////////////////////// void initializeStrat6Flags(){ pipsLostThisBar = 0; } /////////////////////////////////////////////// // setVariables /////////////////////////////////////////////// void setVariables(){ } ////////////////////////////////////// // Start ////////////////////////////////////// int start(){ if(currBar != Time[0] ){ currBar = Time[0]; newBar=true; } else newBar=false; setVariables(); barFly(); } ////////////////////////////////////// // Barfly ////////////////////////////////////// void barFly(){ int openPositions=getOpenPositions(arrayStrat6Tickets, s6Comment); if(newBar) { tradable=true; initializeStrat6Flags(); } //Trade int trend=trend(); if (openPositions < 1 ) { if ( (trend==1 || trend==2) && tradable ){ trade(trend, s6LastTrade, arrayStrat6Tickets, externLot, s6Comment); } } //And Manage for (int l=0; l0 && Bid-OrderOpenPrice()> flyFactor*pipsLostThisBar || Bid-OrderOpenPrice() > capturePips ){ Alert("FLYFACTOR HIT for ",Bid-OrderOpenPrice()," PIPS"); closeAll(arrayStrat6Tickets); tradable=false; } } else if(OrderType()==OP_SELL){ if (pipsLostThisBar>0 && OrderOpenPrice()-Ask> flyFactor*pipsLostThisBar || OrderOpenPrice()-Ask > capturePips ){ Alert("FLYFACTOR HIT FOR",OrderOpenPrice()-Ask," PIPS" ); closeAll(arrayStrat6Tickets); tradable=false; } } closeTradeIfNeeded(arrayStrat6Tickets); } } } } ////////////////////////////////////////////////////////////////////////// // TRADE SECTION ////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////// // trade /////////////////////////////////////////////// bool trade(int trend, int& lastTrade, int& ticketArray[], int lotFactor, string comment){ bool traded=false; if (trend==1){ if (buy(ticketArray, lotFactor, comment)){ lastTrade=1; traded=true; } } else if (trend==2){ if (sell(ticketArray, lotFactor, comment)){ lastTrade=2; traded=true; } } if (traded) { Sleep(500); } return(traded); } /////////////////////////////////////////////// // trend /////////////////////////////////////////////// int trend() { if ( Bid>Open[0]+flyFactor*(Ask-Bid) ){ return (1); } else if ( Ask Open[0]+(Ask-Bid) || newBar ){ Alert("SELL CLOSE CONDITION for ",Symbol()); return(true); } else return (false); } //////////////////////////////////////////////////// // getOpenPositions /////////////////////////////////////////////////// int getOpenPositions(int& ticketArray[], string comment) { ArrayInitialize(ticketArray,0); int ticketIndex=0; for(int j=0; j 0) lot=externLot; else lot=getMaxLots(accountFactor, lotFactor, maxLots); for (int tradeNumber=0; tradeNumber 0) lot=externLot; else lot=getMaxLots(accountFactor, lotFactor, maxLots); for (int tradeNumber=0; tradeNumber maxLots) actualLots = maxLots; if (MINLOT<0.1) return(NormalizeDouble(actualLots,2)); else return(NormalizeDouble(actualLots,1)); } /////////////////////////////////////////////// // setPoint /////////////////////////////////////////////// double setPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } /////////////////////////////////////////////// // tradeErrorAlert /////////////////////////////////////////////// void tradeErrorAlert(int errorCode){ Alert("TRADE ERROR! ",Symbol()," >>>>>>>",ErrorDescription(errorCode)," ",errorCode); }