// // Testing manual trading in the Visual Backtester // #property copyright "Copyright 2009, Ron Thompson" #property link "http://www.ForexMT4.com/" // coming from user double Lots = 0.2 ; double TakeProfit = 22; double StopLoss = 22; int Slippage=22; string TradeComment="2222222222"; int MagicNumber=222; //Order control int maxloop=25; double myPoint; //File control int fh; // file handle string s; // comment control int dd; //drawdown tracker // ticket tracking int ticket; string tktfile="TktTrack"; // used for verbose error logging #include // items are communicated in this order // // Print #1, Lots.Text // Print #1, TakeProfit.Text // Print #1, StopLoss.Text // Print #1, Slippage.Text // Print #1, TradeComment.Text // Print #1, MagicNumber.Text int init() { // get the file count fh=FileOpen(tktfile, FILE_READ ); if(fh>0) { s=FileReadString(fh); FileClose(fh); } int tt=StrToDouble(s); for(int i=0; i<=tt; i++) { FileDelete(DoubleToStr(i,0)); } FileDelete(tktfile); }//init int start() { myPoint=SetPoint(); // check for BUY file, return ticket number //======================================== fh=FileOpen("BUY", FILE_READ ); if(fh>0) { Sleep(500); s=FileReadString(fh); Lots=StrToDouble(s); Print("B Lots="+s+" "+Lots); s=FileReadString(fh); TakeProfit=StrToDouble(s); Print("B TakeProfit="+s+" "+TakeProfit); s=FileReadString(fh); StopLoss=StrToDouble(s); Print("B StopLoss="+s+" "+StopLoss); s=FileReadString(fh); Slippage=StrToDouble(s); Print("B Slippage="+s+" "+Slippage); s=FileReadString(fh); TradeComment=s; Print("B TradeComment="+s+" "+TradeComment); s=FileReadString(fh); MagicNumber=StrToDouble(s); Print("B MagicNumber="+s+" "+MagicNumber); FileClose(fh); FileDelete("BUY"); OpenBuy(); fh=FileOpen(DoubleToStr(ticket,0), FILE_WRITE); FileClose(fh); } // check for SELL file, return ticket number //========================================== fh=FileOpen("SELL", FILE_READ ); if(fh>0) { Sleep(500); s=FileReadString(fh); Lots=StrToDouble(s); Print("S Lots="+s+" "+Lots); s=FileReadString(fh); TakeProfit=StrToDouble(s); Print("S TakeProfit="+s+" "+TakeProfit); s=FileReadString(fh); StopLoss=StrToDouble(s); Print("S StopLoss="+s+" "+StopLoss); s=FileReadString(fh); Slippage=StrToDouble(s); Print("S Slippage="+s+" "+Slippage); s=FileReadString(fh); TradeComment=s; Print("S TradeComment="+s+" "+TradeComment); s=FileReadString(fh); MagicNumber=StrToDouble(s); Print("S MagicNumber="+s+" "+MagicNumber); FileClose(fh); // the input file FileDelete("SELL"); OpenSell(); fh=FileOpen(DoubleToStr(ticket,0), FILE_WRITE); FileClose(fh); // the return file } // check for CLOSEALL, closeeverything and delete file //==================================================== fh=FileOpen("CLOSEALL", FILE_READ ); if(fh>0) { FileClose(fh); FileDelete("CLOSEALL"); CloseEverything(); } //track highest open ticket //========================= fh=FileOpen(tktfile, FILE_WRITE); FileWrite(fh,DoubleToStr(ticket,0)); FileClose(fh); // the ticket tracker // check for ClearDD //================== fh=FileOpen("ClearDD", FILE_READ ); if(fh>0) { FileClose(fh); FileDelete("ClearDD"); dd=0; } // Comments handler //================= int ae=AccountEquity(); int ab=AccountBalance(); int ot=OrdersTotal(); int pl=ae-ab; if(dd>pl) dd=pl; Comment("Equity="+ae+" OpenTrades="+ot+" Profit/Loss="+pl+" MaxDD="+dd); }//start //ENTRY LONG (buy, Ask) void OpenBuy() { int gle=0; //int ticket=0; double SL=0; double TP=0; int loopcount; // PLACE order is independent of MODIFY order. // This is mandatory for ECNs and acceptable for retail brokers loopcount=0; while(true) { // place order - NO TP OR SL ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,TradeComment,MagicNumber,White); gle=GetLastError(); if(gle==0) { Print("BUY PLACED Ticket="+ticket+" Ask="+Ask+" Lots="+Lots); break; } else { Print("-----ERROR----- Placing BUY order: Lots="+Lots+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle)); RefreshRates(); Sleep(500); // give up after loopcount tries loopcount++; if(loopcount>maxloop) { Print("-----ERROR----- Giving up on placing BUY order"); return(gle); } } }//while - place order // modify the order for users TP & SL loopcount=0; while(true) { // don't set TP and SL both to zero, they're already there if(StopLoss==0 && TakeProfit==0) break; if(StopLoss ==0) SL=0; if(TakeProfit ==0) TP=0; if(StopLoss >0) SL=Ask-(StopLoss*myPoint); if(TakeProfit >0) TP=Ask+(TakeProfit*myPoint); OrderModify(ticket,OrderOpenPrice(),SL,TP,0,White); gle=GetLastError(); if(gle==0) { Print("BUY MODIFIED Ticket="+ticket+" Ask="+Ask+" Lots="+Lots+" SL="+SL+" TP="+TP); break; } else { Print("-----ERROR----- Modifying BUY order: Lots="+Lots+" SL="+SL+" TP="+TP+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle)); RefreshRates(); Sleep(500); loopcount++; if(loopcount>maxloop) { Print("-----ERROR----- Giving up on modifying BUY order"); return(gle); } } }//while - modify order }//BUYme //ENTRY SHORT (sell, Bid) void OpenSell() { int gle=0; //int ticket=0; double SL=0; double TP=0; int loopcount; // PLACE order is independent of MODIFY order. // This is mandatory for ECNs and acceptable for retail brokers loopcount=0; while(true) { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,TradeComment,MagicNumber,Red); gle=GetLastError(); if(gle==0) { Print("SELL PLACED Ticket="+ticket+" Bid="+Bid+" Lots="+Lots); break; } else { Print("-----ERROR----- placing SELL order: Lots="+Lots+" SL="+SL+" TP="+TP+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle)); RefreshRates(); Sleep(500); loopcount++; if(loopcount>maxloop) { Print("-----ERROR----- Giving up on placing SELL order"); return(gle); } } }//while // modify the order for users TP & SL loopcount=0; while(true) { // don't set TP and SL both to zero, they're already there if(StopLoss==0 && TakeProfit==0) break; if(StopLoss ==0) SL=0; if(TakeProfit ==0) TP=0; if(StopLoss >0) SL=Bid+(StopLoss*myPoint); if(TakeProfit >0) TP=Bid-(TakeProfit*myPoint); OrderModify(ticket,OrderOpenPrice(),SL,TP,0,Red); gle=GetLastError(); if(gle==0) { Print("SELL MODIFIED Ticket="+ticket+" Bid="+Bid+" Lots="+Lots+" SL="+SL+" TP="+TP); break; } else { Print("-----ERROR----- modifying SELL order: Lots="+Lots+" SL="+SL+" TP="+TP+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle)); RefreshRates(); Sleep(500); loopcount++; if(loopcount>maxloop) { Print("-----ERROR----- Giving up on placing SELL order"); return(gle); } } }//while }//SELLme void CloseBuy (string myInfo) { int gle; int cnt; int OrdersPerSymbol; int loopcount=0; string bTK=" Ticket="+OrderTicket(); string bSL=" SL="+OrderStopLoss(); string bTP=" TP="+OrderTakeProfit(); string bPM; string bLL; string bER; bPM=" PM="+TakeProfit; bLL=" LL="+StopLoss; while(true) { OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,White); gle=GetLastError(); bER=" error="+gle+" "+ErrorDescription(gle); if(gle==0) { Print("CLOSE BUY "+myInfo+ bTK + bSL + bTP + bPM + bLL); break; } else { Print("-----ERROR----- CLOSE BUY "+myInfo+ bER +" Bid="+Bid+ bTK + bSL + bTP + bPM + bLL); RefreshRates(); Sleep(500); } loopcount++; if(loopcount>maxloop) { Print("-----ERROR----- Giving up on closing SELL order"); return(gle); } }//while } void CloseSell (string myInfo) { int gle; int cnt; int OrdersPerSymbol; int loopcount=0; string sTK=" Ticket="+OrderTicket(); string sSL=" SL="+OrderStopLoss(); string sTP=" TP="+OrderTakeProfit(); string sPM; string sLL; string sER; sPM=" PM="+TakeProfit; sLL=" LL="+StopLoss; while(true) { OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red); gle=GetLastError(); sER=" error="+gle+" "+ErrorDescription(gle); if(gle==0) { Print("CLOSE SELL "+myInfo + sTK + sSL + sTP + sPM + sLL); break; } else { Print("-----ERROR----- CLOSE SELL "+myInfo+ sER +" Ask="+Ask+ sTK + sSL + sTP + sPM + sLL); RefreshRates(); Sleep(500); } loopcount++; if(loopcount>maxloop) { Print("-----ERROR----- Giving up on closing SELL order"); return(gle); } }//while } //+-----------------+ //| 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 // Function to correct the value of Point // for brokers that add an extra digit to price // Courtesy of Robert Hill double SetPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); }