//+------------------------------------------------------------------+ //| FBCI EA Beta.mq4 | //| Copyright © 2008, MyJunkBiotch | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, MetaQuotes Software Corp." #property link "http://www.metaquotes.net/" // magic number extern int Magic = 12345; extern double TrailingStop = 15; extern double Lots = 0; extern int verboselogging = 0; #include #include string logstuff(string sometext) { if(verboselogging==1) { string textfile=StringConcatenate(Symbol(),"_logging.txt"); int logging=FileOpen(textfile,FILE_READ|FILE_WRITE); if(logging>0) { FileSeek(logging,0,SEEK_END); FileWrite(logging,sometext); FileClose(logging); logging=0; } else { Print("Error opening file..",GetLastError()); return(false); } } } int StripExtras(string text) { int length=StringLen(text) - 1; string search1="0"; string search2="."; while(length>=0) { int index=StringFind(text,search1,0); int index2=StringFind(text,search2,0); text=StringSetChar(text,index,32); text=StringSetChar(text,index2,32); length--; } return(StrToInteger(text)); } double CalculateLots(int Lots) { double Margin; Margin = AccountFreeMargin( ) ; double Hasil; double MLots ; Hasil = Margin/10000; MLots = MathFloor(Hasil); //Seeing Max Lots of 50 for IBFX Mini if(Lots==0) { return(MLots); } if(Lots!=0) { return(Lots); } } int GetOrdersCount(int Type = -1) { int count = 0; for(int i = 0; i < OrdersTotal(); i++) { // already closed if(OrderSelect(i, SELECT_BY_POS) == false) continue; // not current symbol if(OrderSymbol() != Symbol()) continue; if(OrderType() == Type || Type == -1) { count++; } } logstuff("---------------------------------------"); logstuff("Iteration beginning.."); logstuff("---------------------------------------"); if(count==0) { logstuff("No open orders for pair.."); logstuff("Why not?.."); logstuff(" "); } else { logstuff("Order in progress.."); logstuff("What are we doing with this order?.."); logstuff(" "); } return (count); } int ReturnFirstTicket(int Magic) { // Takes an input of MagicNumber int icnt, itotal, iTicket; itotal=OrdersTotal(); for(icnt=0;icntSMA50normal) { logstuff("SMA50 line is below the current price level.."); return(1); } else { logstuff("SMA50 line is above the current price level.."); logstuff(" "); return(2); } } int CheckBars() { // string textfile=StringConcatenate(Symbol(),"_logging.txt"); // int logging=FileOpen(textfile,FILE_READ|FILE_WRITE); double bar0=Close[1]-Open[1]; double bar1=Close[2]-Open[2]; if(bar0>0 && bar1>0) { logstuff("Two consecutive green bars...Pending close"); logstuff(" "); return(1); } if(bar0<0 && bar1<0) { logstuff("Two consecutive red bars...Pending close"); logstuff(" "); return(0); } } int FibRetracement(int trend) { //Check current price //Go back max of 15 bars for min/max double sellmin, // Minimal price buymax; // Maximal price int Quant_Bars=15; string numberofbarstotest=StringConcatenate("Number of bars to test ",Quant_Bars); int Ind_max =ArrayMaximum(High,Quant_Bars,1);// Bar index of max. price int Ind_min =ArrayMinimum(Low, Quant_Bars,1);// Bar index of min. price buymax=High[Ind_max]; // Desired max. price sellmin=Low[Ind_min]; // Desired min. price if(trend==1) { //subtract bar price from current price and get pips int bdifference=StripExtras(buymax - Ask); if(Ask>=buymax) { logstuff(numberofbarstotest); logstuff("Invalid retracement..defaulting to trend TP of 50 pips"); logstuff(" "); return (50); } else { logstuff(numberofbarstotest); string bretracement=StringConcatenate("Valid retracement..set to TP",bdifference); logstuff(bretracement); logstuff(" "); return (bdifference); } } if(trend==2) { int sdifference=StripExtras(Bid - sellmin); if(Bid<=sellmin) { logstuff(numberofbarstotest); logstuff("Invalid retracement..defaulting to trend TP of 50 pips"); logstuff(" "); return (50); } else { logstuff(numberofbarstotest); string sretracement=StringConcatenate("Valid retracement..set to TP",sdifference); logstuff(sretracement); logstuff(" "); return (sdifference); } } return (0); } int CheckTime() { //Have to go through this junk because strtime uses local time and not server time int y=Year(); int M=Month(); int d=Day(); int h=Hour(); int m=Minute(); if(M<10) { string N="0"+M; M=StrToInteger(N); } if(d<10) { string e="0"+d; d=StrToInteger(e); } if(m<10) { string n="0"+m; m=StrToInteger(n); } //Convert Time to Seconds since 1970 - 2009.04.06 1:05 string times=StringConcatenate(y,".",M,".",d," ",h,":",m); int brokerseconds=StrToTime(times); //Compensate for FBCI Time Difference int fbcithreshold=brokerseconds - 480; logstuff("Getting Broker Threshold.."); string logthreshold=StringConcatenate("FBCI Threshold time is ",TimeToStr(fbcithreshold)); logstuff(logthreshold); logstuff(" "); return (fbcithreshold); } int CheckComment(string checkComment) { //For logging if enabled // string textfile=StringConcatenate(Symbol(),"_logging.txt"); // int logging=FileOpen(textfile,FILE_READ|FILE_WRITE); //Comment is written with current symbol, checking here string fbcibuild=StringConcatenate(Symbol(),"_SFBInd_FractalComment2"); string fbciComment=ObjectDescription(fbcibuild); int fbcilength=StringLen(fbciComment); //Extracting Time Element from Comment string fbcitimesub=StringSubstr(fbciComment,fbcilength - 16,0); //Extracting Order Type Element from Comment string fbciordersub=StringSubstr(fbciComment, 0, 4); //Need to Trim both Order and Time Elements string fbcitimetxt=StringTrimLeft(fbcitimesub); string fbciorder=StringTrimRight(fbciordersub); //string fbcitimetxt="2009.04.08 12:25"; //Convert Time to Seconds since 1970 int fbcitime=StrToTime(fbcitimetxt); logstuff("Checking Comment.."); logstuff("Getting Time From FBCI Indicator.."); string logfbcitime=StringConcatenate("FBCI Time is ",TimeToStr(fbcitime)); logstuff(logfbcitime); logstuff(" "); string ordercomment=StringConcatenate("OrderType from Comment is ",fbciorder); logstuff(ordercomment); logstuff(" "); //Checking for OrderType, buy or sell if(fbciorder==checkComment) { logstuff("Matched OrderType.."); logstuff(" "); //compare to broker time return (fbcitime); } return(0); } void fbuy (double MLots, int TP) { string lotsconfirm=StringConcatenate("Lots= ",MLots); logstuff(lotsconfirm); if(MLots>50) { logstuff("Lots out of threshold"); } string fbcibuild=StringConcatenate(Symbol(),"_SFBInd_FractalComment2"); string fbciComment=ObjectDescription(fbcibuild); int bticket=OrderSend(Symbol(),OP_BUY,MLots,Ask,3,0,Ask+TP*Point,fbciComment,Magic,0,Green); if(bticket<1) { int error=GetLastError(); Print("Error = ",ErrorDescription(error)); return; } OrderPrint(); return(0); } void fsell (double MLots, int TP) { string lotsconfirm=StringConcatenate("Lots= ",MLots); logstuff(lotsconfirm); if(MLots>50) { logstuff("Lots out of threshold"); } string fbcibuild=StringConcatenate(Symbol(),"_SFBInd_FractalComment2"); string fbciComment=ObjectDescription(fbcibuild); int sticket=OrderSend(Symbol(),OP_SELL,MLots,Bid,3,0,Bid-TP*Point,fbciComment,Magic,0,Red); if(sticket<1) { int error=GetLastError(); Print("Error = ",ErrorDescription(error)); return; } OrderPrint(); return(0); } //+------------------------------------------------------------------+ //| script program start function | //+------------------------------------------------------------------+ int start() { //---- string logbuytime=StringConcatenate("FBCI Time from BUY Request: ",CheckComment("BUY")); string logselltime=StringConcatenate("FBCI Time from SELL Request: ",CheckComment("SELL")); string logbrokertime=StringConcatenate("Broker Threshold: ",CheckTime()); string logsma50=StringConcatenate("SMA50 position: ",wherami(GetSL())); logstuff(logbuytime); logstuff(logselltime); logstuff(logbrokertime); logstuff(logsma50); logstuff(" "); //TODO: Work on comparing grouped orders for HighA,LowD if (GetOrdersCount() == 0) { if (CheckComment("BUY") > CheckTime()) { logstuff("Buy Order Set.."); logstuff(" "); if(wherami(GetSL())==1) { logstuff("Buy Order Placed"); logstuff(" "); fbuy(CalculateLots(Lots),FibRetracement(1)); } } else { if(CheckComment("BUY")!=0) { logstuff("Buy Order Revoked"); logstuff("No new signals"); logstuff(" "); //Print("Buy Order Revoked"); } } if (CheckComment("SELL") > CheckTime()) { logstuff("Sell Order Set.."); logstuff(" "); if(wherami(GetSL())==2) { logstuff("Sell Order Placed"); logstuff(" "); fsell(CalculateLots(Lots),FibRetracement(2)); } } else { if(CheckComment("SELL")!=0) { logstuff("Sell Order Revoked"); logstuff("No new signals"); logstuff(" "); //Print("Sell Order Revoked"); } } } if (GetOrdersCount()>0) { int ticket=ReturnFirstTicket(Magic); OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES); if(OrderType()==OP_BUY) { if(Bid0) { if(Bid-OrderOpenPrice()>Point*TrailingStop) { if(OrderStopLoss()GetSL() && Close[1]>GetSL()) { if(CheckBars()==1) { OrderClose(ticket,CalculateLots(Lots),Bid,3,Blue); } } if(TrailingStop>0) { if((OrderOpenPrice()-Ask)>(Point*TrailingStop)) { if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0)) { OrderModify(ticket,OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red); return(0); } } } } } return(0); } //+------------------------------------------------------------------+