// *** DO NOT USE ON LIVE ACCOUNT *** // *** DO NOT USE ON LIVE ACCOUNT *** // *** DO NOT USE ON LIVE ACCOUNT *** // *** DO NOT USE ON LIVE ACCOUNT *** // // This EA opens a position at random, every 3-13 bars, random buy/sell, // at random times random % into the selected bar. // //+------------------------------------------------------------------+ //| Randomness EA v1.mq4 | //| Copyright © 2009, David Moser | //| dmoser71@gmail.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2009, David Moser" #property link "dmoser71@gmail.com" #define EAName "Randomness EA v1" extern string S1="---- Order and Broker Management"; extern double AccountRiskPercent=2; // account risk in percentage of free margin at time of trade opening extern int Slippage=3; // slippage in standard PIPs extern int Magic=23785614; // magic number extern bool Use_Original_Idea=true; // if true, use Shiva's original idea; if false, use my crazy idea extern string S2="---- Use_Original_Idea=true Parameters"; extern int TP=30; extern int SL=30; extern string S3="---- Use_Original_Idea=false Parameters"; extern double TPFactor=1.0; extern double SLFactor=2.0; //---- variables datetime OncePerBar=0; double pip2point=1, MinimumStopLevel=0, Stop_Price=0, Take_Profit=0, TimeFrameFactor=1; int ticket=0, precision=0; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- MathSrand(TimeLocal()); OncePerBar=Time[0]+(Period()*60)*(MathCeil(MathRand()/3333)+3); // First trade will be 3 to 13 bars from the opening of the EA if (Period()>1) TimeFrameFactor=MathLog(Period())/4; if (MarketInfo(Symbol(), MODE_DIGITS)==3 || MarketInfo(Symbol(), MODE_DIGITS)==5) pip2point = 10; precision = MarketInfo(Symbol(), MODE_DIGITS); if (MarketInfo(Symbol(), MODE_DIGITS)==4 || MarketInfo(Symbol(), MODE_DIGITS)==5) MinimumStopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL)*Point + 0.0002; if (MarketInfo(Symbol(), MODE_DIGITS)==2 || MarketInfo(Symbol(), MODE_DIGITS)==3) MinimumStopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL)*Point + 0.02; //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- if (OncePerBar <= Time[0]) // Begin Code Section that allows one open trade per 3-13 bars { if (MathMod(MathRand(),2)==1) // Go Long { CloseAllSellOrders(); Sleep(MathRand()); RefreshRates(); if (Use_Original_Idea) { Stop_Price = Bid - SL*pip2point*Point; Take_Profit= Ask + TP*pip2point*Point; } else { Stop_Price = Low[iLowest(Symbol(),0,MODE_LOW,3,1)] - (MathCeil(MathRand()/1000)+13)*pip2point*Point*SLFactor*TimeFrameFactor; if (Stop_Price >= Bid - MinimumStopLevel) Stop_Price = Bid - MinimumStopLevel * (MathCeil(MathRand()/1000)+1); Take_Profit= Ask + (MathCeil(MathRand()/900)+17)*pip2point*Point*TPFactor*TimeFrameFactor; } GoLong(); } else { CloseAllBuyOrders(); Sleep(MathRand()); RefreshRates(); if (Use_Original_Idea) { Stop_Price = Ask + SL*pip2point*Point; Take_Profit= Bid - TP*pip2point*Point; } else { Stop_Price = High[iHighest(Symbol(),0,MODE_HIGH,3,1)] + (MathCeil(MathRand()/1000)+13)*pip2point*Point*SLFactor*TimeFrameFactor; if (Stop_Price <= Ask + MinimumStopLevel) Stop_Price = Ask + MinimumStopLevel * (MathCeil(MathRand()/1000)+1); Take_Profit = Bid - (MathCeil(MathRand()/900)+17)*pip2point*Point*TPFactor*TimeFrameFactor; } GoShort(); } } //---- return(0); } //+------------------------------------------------------------------+ void GoLong() { Print("Opening BUY order: ",DoubleToStr(Ask,precision), ", ",DoubleToStr(Stop_Price,precision),", ",DoubleToStr(Take_Profit,precision)); ticket=OrderSend(Symbol(),OP_BUY,CalcLots(Ask),Ask,Slippage*pip2point,0,0,EAName,Magic,0,Blue); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { OrderModify(OrderTicket(), OrderOpenPrice(), Stop_Price, Take_Profit, 0, Blue); OncePerBar=Time[0]+(Period()*60)*(MathCeil(MathRand()/3333)+3); // Successful trade placed on this bar } } else Print("Error opening Market BUY order : ",GetLastError()); } void GoShort() { Print("Opening SELL order: ",DoubleToStr(Bid,precision), ", ",DoubleToStr(Stop_Price,precision),", ",DoubleToStr(Take_Profit,precision)); ticket=OrderSend(Symbol(),OP_SELL,CalcLots(Bid),Bid,Slippage*pip2point,0,0,EAName,Magic,0,Red); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { OrderModify(OrderTicket(), OrderOpenPrice(), Stop_Price, Take_Profit, 0, Red); OncePerBar=Time[0]+(Period()*60)*(MathCeil(MathRand()/3333)+3); // Successful trade placed on this bar } } else Print("Error opening Market SELL order : ",GetLastError()); } void CloseAllBuyOrders() { int total=OrdersTotal(); for (int count=total-1;count>=0;count--) { OrderSelect(count,SELECT_BY_POS,MODE_TRADES); if((OrderMagicNumber() == Magic) && (OrderSymbol()==Symbol())) { int ot=OrderType(); switch(ot) { case OP_BUY: { OrderClose(OrderTicket(),OrderLots(),Bid,Slippage); break; } case OP_BUYSTOP: { OrderDelete(OrderTicket()); break; } case OP_BUYLIMIT: { OrderDelete(OrderTicket()); break; } } } } } void CloseAllSellOrders() { int total=OrdersTotal(); for(int count=total-1;count>=0;count--) { OrderSelect(count,SELECT_BY_POS,MODE_TRADES); if((OrderMagicNumber() == Magic) && (OrderSymbol()==Symbol())) { int ot=OrderType(); switch(ot) { case OP_SELL: { OrderClose(OrderTicket(),OrderLots(),Ask,Slippage); break; } case OP_SELLSTOP: { OrderDelete(OrderTicket()); break; } case OP_SELLLIMIT: { OrderDelete(OrderTicket()); break; } } } } } double CalcLots(double CurrentPrice) { double Lots = (((AccountFreeMargin()*AccountRiskPercent/100)/(MathAbs(CurrentPrice - Stop_Price)/Point))/MarketInfo(Symbol(),MODE_TICKVALUE)) * (100000 / MarketInfo(Symbol(), MODE_LOTSIZE)); if (Lots < MarketInfo(Symbol(), MODE_MINLOT)) Lots = MarketInfo(Symbol(), MODE_MINLOT); Lots = MathFloor(Lots / MarketInfo(Symbol(), MODE_LOTSTEP)) * MarketInfo(Symbol(), MODE_LOTSTEP); return(Lots); }