//+--------------+ //|One(Buy/Sell) | //+--------------+ #property copyright "Ron Thompson" #property link "http://www.ForexMT4.com/" // This code is NEVER to be SOLD individually // This code is NEVER to be INCLUDED as part of a collection that is SOLD // This is really a script, so... #property show_inputs // EA SPECIFIC extern double Lots = 0.01 ; extern double ProfitMade = 0 ; extern double LossLimit = 0 ; extern int Slippage = 0 ; // Trade control int MagicNumber=142555; double myPoint; // support for 3/5 decimal places int loopcount; // count of order attempts int maxloop=3; // maximum number of attempts to handle errors // used for verbose error logging #include //+-------------+ //| Custom init | //|-------------+ // Called ONCE when EA is added to chart or recompiled int init() { // get normalized Point based on Broker decimal places myPoint = SetPoint(); OpenBuy(); OpenSell(); Print("Init Complete BUY-D"); Comment(" "); } //+----------------+ //| Custom DE-init | //+----------------+ // Called ONCE when EA is removed from chart int deinit() { } //+-----------+ //| Main | //+-----------+ // Called EACH TICK and each Bar[] int start() { } // start() //ENTRY LONG (buy, Ask) void OpenBuy() { int gle=0; int ticket=0; double SL=0; double TP=0; int loopcount; string mySymbol=Symbol(); // 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(mySymbol,OP_BUY,Lots,Ask,Slippage,0,0,"BUY-D",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(); // 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(LossLimit==0 && ProfitMade==0) break; if(LossLimit ==0) SL=0; if(ProfitMade ==0) TP=0; if(LossLimit >0) SL=Ask-((LossLimit)*myPoint ); if(ProfitMade >0) TP=Ask+((ProfitMade)*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(); 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; string mySymbol=Symbol(); // PLACE order is independent of MODIFY order. // This is mandatory for ECNs and acceptable for retail brokers loopcount=0; while(true) { ticket=OrderSend(mySymbol,OP_SELL,Lots,Bid,Slippage,0,0,"SELL-D",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(); 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(LossLimit==0 && ProfitMade==0) break; if(LossLimit ==0) SL=0; if(ProfitMade ==0) TP=0; if(LossLimit >0) SL=Bid+((LossLimit)*myPoint ); if(ProfitMade >0) TP=Bid-((ProfitMade)*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(); loopcount++; if(loopcount>maxloop) { Print("-----ERROR----- Giving up on placing SELL order"); return(gle); } } }//while }//SELLme // 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); }