//+------------------------------------------------------------------| //| PRD Trader.mq4 | //| by William R Garrison | //| 8 June 2009 | //| | //| Based on PRD Signal2 indicator by BigBawls | //| which, in turn, is based on the trading system by Robert Hill | //| which uses the past regression deviated indicator with a | //| three-order scale-out order management system | //+------------------------------------------------------------------+ #include extern double lot = 0.1; extern int UseMM = true; extern double Risk = 1.0; int Shift = 1; extern int UseTrailingStop = false; extern int TrailingStop=160.0; extern int buyzone=-100; extern int sellzone=100; extern bool EmailAlert=false; extern bool AudioAlert=false; extern bool useCCI=true; extern int AdjustTP = 0; extern int TradeStartHour = 0; extern int TradeStopHour = 0; extern bool RecordTicks = false; extern string EAVersion = "PRD 1.0"; extern int MagicNumber1 = 090608; extern int MagicNumber2 = 090609; extern int MagicNumber3 = 090610; int lotdigits = 0; int pip_adjust = 0; double minlot, maxlot; string tick_file_name; double BuyEntry, SellEntry; int BuySellTimeFrame; datetime last_tick = -1; int init() { double foo = MarketInfo(Symbol(),MODE_LOTSTEP); // Print("lotstep ", foo); if (foo == 0.01) lotdigits = 2; else if (foo == 0.1) lotdigits = 1; else lotdigits = 0; if ((Digits == 3) || (Digits == 5)) pip_adjust = 10; else pip_adjust = 1; Print("digits ", Digits, " lotdigits ", lotdigits, " pip_adjust ", pip_adjust, " TrailingStop ", TrailingStop*pip_adjust); // If the tick file doesn't exist, write the column header tick_file_name = Symbol()+"_ticks.csv"; init_tick_file(); } int start() { double locallots = 0; double localstop = 0; if (RecordTicks) Write_Tick_To_File(); // determine what orders are still open int total = OrdersTotal(); int ticket1 = -1; int ticket2 = -1; int ticket3 = -1; for(int cnt = 0; cnt < total; cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderType() <= OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber1) ticket1 = OrderTicket(); if(OrderType() <= OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber2) ticket2 = OrderTicket(); if(OrderType() <= OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber3) ticket3 = OrderTicket(); } // establish indicator values double MeanYellow = iCustom(NULL, 0, "Past Regression Deviated",0, 55,0.809, 1.618,2.618, 0,0); double High1Lime = iCustom(NULL, 0, "Past Regression Deviated",0, 55,0.809, 1.618,2.618, 1,0); double Low1Lime = iCustom(NULL, 0, "Past Regression Deviated",0, 55,0.809, 1.618,2.618, 2,0); double High2Orange = iCustom(NULL, 0, "Past Regression Deviated",0, 55,0.809, 1.618,2.618, 3,0); double Low2Orange = iCustom(NULL, 0, "Past Regression Deviated",0, 55,0.809, 1.618,2.618, 4,0); double High3Red = iCustom(NULL, 0, "Past Regression Deviated",0, 55,0.809, 1.618,2.618, 5,0); double Low3Red = iCustom(NULL, 0, "Past Regression Deviated",0, 55,0.809, 1.618,2.618, 6,0); // adjust targets if((AdjustTP > 0) && (ticket1 > 0)) { bool foo = OrderSelect(ticket1, SELECT_BY_TICKET, MODE_TRADES); if (!foo) { Print("Error during target1 adjust order select ", ErrorDescription(GetLastError())); return(0); } if(OrderTakeProfit() != NormalizeDouble(MeanYellow, Digits)) { if(!OrderModify(ticket1, OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(MeanYellow,Digits), 0, Blue)) { Print("Error during ticket1 target adjust ", ErrorDescription(GetLastError())); // Print("TS buy open ", OrderOpenPrice(), " stop ", OrderStopLoss(), " loc ", localstop, " BS ", buy_stop, " TP ", OrderTakeProfit()); } } } // end of ticket 1 if(ticket2 > 0) { foo = OrderSelect(ticket2, SELECT_BY_TICKET, MODE_TRADES); if (!foo) { Print("Error during target2 adjust order select ", ErrorDescription(GetLastError())); return(0); } if (OrderType() == OP_BUY) { // bring stop to breakeven if first order closed if (ticket1 < 0) { if(!OrderModify(ticket2, OrderOpenPrice(), NormalizeDouble(OrderOpenPrice()+10*Point*pip_adjust,Digits), OrderTakeProfit(), 0, Blue)) { Print("Error during ticket2 stop adjust ", ErrorDescription(GetLastError())); // Print("TS buy open ", OrderOpenPrice(), " BS ", OrderStopLoss(), " TP ", OrderTakeProfit()); } } if (AdjustTP > 0) { if(OrderTakeProfit() != NormalizeDouble(High2Orange, Digits)) { if(!OrderModify(ticket2, OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(High2Orange,Digits), 0, Blue)) { Print("Error during ticket2 target adjust ", ErrorDescription(GetLastError())); // Print("TS buy open ", OrderOpenPrice(), " stop ", OrderStopLoss(), " loc ", localstop, " BS ", buy_stop, " TP ", OrderTakeProfit()); } } } //end of adjust TP } // end of OP_BUY else { // OrderType() == OP_SELL // bring stop to breakeven if first order closed if (ticket1 < 0) { if(!OrderModify(ticket2, OrderOpenPrice(), NormalizeDouble(OrderOpenPrice()-10*Point*pip_adjust,Digits), OrderTakeProfit(), 0, Red)) { Print("Error during ticket2 stop adjust ", ErrorDescription(GetLastError())); // Print("TS buy open ", OrderOpenPrice(), " BS ", OrderStopLoss(), " TP ", OrderTakeProfit()); } } if (AdjustTP > 0) { if(OrderTakeProfit() != NormalizeDouble(Low2Orange, Digits)) { if(!OrderModify(ticket2, OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(Low2Orange,Digits), 0, Red)) { Print("Error during ticket2 target adjust ", ErrorDescription(GetLastError())); // Print("TS buy open ", OrderOpenPrice(), " stop ", OrderStopLoss(), " loc ", localstop, " BS ", buy_stop, " TP ", OrderTakeProfit()); } } } // end of adjust TP } // end of OP_SELL } // end of ticket 2 if(ticket3 > 0) { foo = OrderSelect(ticket3, SELECT_BY_TICKET, MODE_TRADES); if (!foo) { Print("Error during target3 adjust order select ", ErrorDescription(GetLastError())); return(0); } if (OrderType() == OP_BUY) { // special for the last ticket, bring stop to breakeven if (ticket1 < 0) { if(!OrderModify(ticket3, OrderOpenPrice(), NormalizeDouble(OrderOpenPrice()+10*Point*pip_adjust,Digits), OrderTakeProfit(), 0, Blue)) { Print("Error during ticket3 target adjust ", ErrorDescription(GetLastError())); // Print("TS buy open ", OrderOpenPrice(), " BS ", OrderStopLoss(), " TP ", OrderTakeProfit()); } } if(AdjustTP > 0) { if(OrderTakeProfit() != NormalizeDouble(High3Red, Digits)) { if(!OrderModify(ticket3, OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(High2Orange,Digits), 0, Blue)) { Print("Error during ticket3 target adjust ", ErrorDescription(GetLastError())); // Print("TS buy open ", OrderOpenPrice(), " BS ", OrderStopLoss(), " TP ", OrderTakeProfit()); } } } if (UseTrailingStop == 1) { localstop = NormalizeDouble(Bid-TrailingStop*Point*pip_adjust, Digits); if(OrderStopLoss() > localstop-(5*Point*pip_adjust)) { if(!OrderModify(ticket3, OrderOpenPrice(), NormalizeDouble(localstop, Digits), OrderTakeProfit(), 0, Red)) { Print("Error during ticket3 trailing stop ", ErrorDescription(GetLastError())); // Print("TS3 Buy open ", OrderOpenPrice(), " BS ", OrderStopLoss(), " TP ", OrderTakeProfit()); } } } // end of trailing stop } // end of OP_BUY else { // OrderType() == OP_SELL // special for the last ticket, bring stop to breakeven if (ticket1 < 0) { if(!OrderModify(ticket3, OrderOpenPrice(), NormalizeDouble(OrderOpenPrice()-10*Point*pip_adjust,Digits), OrderTakeProfit(), 0, Red)) { Print("Error during ticket3 target adjust ", ErrorDescription(GetLastError())); // Print("TS buy open ", OrderOpenPrice(), " BS ", OrderStopLoss(), " TP ", OrderTakeProfit()); } } if(AdjustTP > 0) { if(OrderTakeProfit() != NormalizeDouble(Low3Red, Digits)) { if(!OrderModify(ticket2, OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(Low2Orange,Digits), 0, Red)) { Print("Error during ticket3 target adjust ", ErrorDescription(GetLastError())); // Print("TS buy open ", OrderOpenPrice(), " stop ", OrderStopLoss(), " loc ", localstop, " BS ", buy_stop, " TP ", OrderTakeProfit()); } } } if (UseTrailingStop == 1) { localstop = NormalizeDouble(Ask+TrailingStop*Point*pip_adjust, Digits); if(OrderStopLoss() > localstop-(5*Point*pip_adjust)) { if(!OrderModify(ticket3, OrderOpenPrice(), NormalizeDouble(localstop, Digits), OrderTakeProfit(), 0, Red)) { Print("Error during trailing stop ", ErrorDescription(GetLastError())); // Print("TS3 sell open ", OrderOpenPrice(), " stop ", OrderStopLoss(), " loc ", localstop, " BS ", sell_stop, " TP ", OrderTakeProfit()); } } } // end of trailing stop } // end of OP_SELL } //end of ticket3 // open orders only at the start of a bar if (Time[0] == last_tick) return(0); last_tick = Time[0]; double buy_stop = iCustom(Symbol(), 0, "PRDSignal2", buyzone, sellzone, EmailAlert, AudioAlert, useCCI, 0, Shift); double sell_stop = iCustom(Symbol(), 0, "PRDSignal2", buyzone, sellzone, EmailAlert, AudioAlert, useCCI, 1, Shift); if (buy_stop == EMPTY_VALUE) buy_stop = 0; if (sell_stop == EMPTY_VALUE) sell_stop = 0; // Buy entry if (buy_stop > 0) BuyEntry = 1; else BuyEntry = 0; // Sell entry if (sell_stop > 0) SellEntry = 1; else SellEntry = 0; // if there is no open order, we can open one here if ((ticket1 < 0) && (ticket2 < 0) && (ticket3 < 0)) { // check hours that trade is not disqualified if(((TradeStartHour < TradeStopHour) && ((Hour() < TradeStartHour) || (Hour() >= TradeStopHour))) || ((TradeStartHour > TradeStopHour) && (Hour() < TradeStartHour) && (Hour() >= TradeStopHour))) { BuyEntry = 0; SellEntry = 0; } locallots = LotsOptimized(); if (BuyEntry > 0) { if (!IsTradeAllowed()) Print("Buy order at ", Ask); else { // First third of order ticket1=OrderSend(Symbol(),OP_BUY, locallots, Ask, 3, 0, 0, EAVersion, MagicNumber1, 0, Blue); if(ticket1 > 0) { OrderModify(ticket1, OrderOpenPrice(), NormalizeDouble(Bid-TrailingStop*Point*pip_adjust, Digits), NormalizeDouble(MeanYellow,Digits), 0, Blue); if (OrderSelect(ticket1, SELECT_BY_TICKET, MODE_TRADES)) Print("BE1 buy open ", OrderOpenPrice(), " BS ", OrderStopLoss(), " TP ", OrderTakeProfit()); } else Print("BE1 ", ErrorDescription(GetLastError())); // Second third of order ticket2=OrderSend(Symbol(),OP_BUY, locallots, Ask, 3, 0, 0, EAVersion, MagicNumber2, 0, Blue); if(ticket2 > 0) { OrderModify(ticket2, OrderOpenPrice(), NormalizeDouble(Bid-TrailingStop*Point*pip_adjust, Digits), NormalizeDouble(High1Lime,Digits), 0, Blue); if (OrderSelect(ticket2, SELECT_BY_TICKET, MODE_TRADES)) Print("BE2 buy open ", OrderOpenPrice(), " BS ", OrderStopLoss(), " TP ", OrderTakeProfit()); } else Print("BE2 ", ErrorDescription(GetLastError())); // Last third of order ticket3=OrderSend(Symbol(),OP_BUY, locallots, Ask, 3, 0, 0, EAVersion, MagicNumber3, 0, Blue); if(ticket3 > 0) { OrderModify(ticket3, OrderOpenPrice(), NormalizeDouble(Bid-TrailingStop*Point*pip_adjust, Digits), NormalizeDouble(High2Orange,Digits), 0, Blue); if (OrderSelect(ticket3, SELECT_BY_TICKET, MODE_TRADES)) Print("BE3 buy open ", OrderOpenPrice(), " BS ", OrderStopLoss(), " TP ", OrderTakeProfit()); } else Print("BE3 ", ErrorDescription(GetLastError())); } } // end of buy entry if (SellEntry > 0) { if (!IsTradeAllowed()) Print("Sell order at ", Bid); else { // First third of order ticket1=OrderSend(Symbol(), OP_SELL, locallots, Bid, 3, 0, 0, EAVersion, MagicNumber1, 0,Red); if(ticket1 > 0) { OrderModify(ticket1, OrderOpenPrice(), NormalizeDouble(Ask+TrailingStop*Point*pip_adjust, Digits), NormalizeDouble(MeanYellow,Digits), 0, Red); if(OrderSelect(ticket1, SELECT_BY_TICKET, MODE_TRADES)) Print("SE1 sell open ", OrderOpenPrice(), " SS ", OrderStopLoss(), " TP ", OrderTakeProfit()); } else Print("SE1 ", ErrorDescription(GetLastError())); // Second third of order ticket2=OrderSend(Symbol(), OP_SELL, locallots, Bid, 3, 0, 0, EAVersion, MagicNumber2, 0,Red); if(ticket2 > 0) { OrderModify(ticket2, OrderOpenPrice(), NormalizeDouble(Ask+TrailingStop*Point*pip_adjust, Digits), NormalizeDouble(Low1Lime,Digits), 0, Red); if(OrderSelect(ticket2, SELECT_BY_TICKET, MODE_TRADES)) Print("SE2 sell open ", OrderOpenPrice(), " SS ", OrderStopLoss(), " TP ", OrderTakeProfit()); } else Print("SE2 ", ErrorDescription(GetLastError())); // Last third of order ticket3=OrderSend(Symbol(), OP_SELL, locallots, Bid, 3, 0, 0, EAVersion, MagicNumber3, 0,Red); if(ticket3 > 0) { OrderModify(ticket3, OrderOpenPrice(), NormalizeDouble(Ask+TrailingStop*Point*pip_adjust, Digits), NormalizeDouble(Low2Orange,Digits), 0, Red); if(OrderSelect(ticket3, SELECT_BY_TICKET, MODE_TRADES)) Print("SE3 sell open ", OrderOpenPrice(), " SS ", OrderStopLoss(), " TP ", OrderTakeProfit()); } else Print("SE3 ", ErrorDescription(GetLastError())); } } return(0); } // end of open new orders //---- return(0); } //+------------------------------------------------------------------+ //| Money Management | //+------------------------------------------------------------------+ double LotsOptimized() { double oLots; //---- select lot size if(UseMM == 1) oLots=NormalizeDouble(AccountFreeMargin()*Risk/100/MarketInfo(Symbol(),MODE_MARGINREQUIRED),lotdigits); else oLots = lot; // Print("Margin ", AccountFreeMargin(), " Risk/100 ", Risk/100, " reqd ", MarketInfo(Symbol(),MODE_MARGINREQUIRED), " digits ", lotdigits); //---- return lot size minlot = MarketInfo(Symbol(),MODE_MINLOT); maxlot = MarketInfo(Symbol(),MODE_MAXLOT); // Print("oLots ", oLots, " minlot ", minlot, " maxlot ", maxlot); if (oLots > maxlot) oLots = maxlot; if (oLots < minlot) oLots = minlot; return(oLots); } void init_tick_file() { int fhandle = 0; int fcount; int r; fhandle = FileOpen(tick_file_name, FILE_CSV|FILE_READ, ","); // Print("tick file init open ", fhandle); if (fhandle < 1) { // tick file does not exist - write header Print ("Tick file does not exist - write header"); fhandle = FileOpen(tick_file_name, FILE_CSV|FILE_WRITE, ","); fcount = FileWrite(fhandle, "Date/Time", "Bid", "Ask", "Spread", "Buy entry", "Sell entry"); if (fcount < 0) { Print("Error during tick header write ", GetLastError()); return; } FileClose(fhandle); Print("Tick file header written for ", Symbol()); } } void Write_Tick_To_File() { int fhandle; int fcount; string currentTime; fhandle = FileOpen(tick_file_name, FILE_CSV|FILE_READ|FILE_WRITE, ","); if (fhandle == -1) { Print("Tick file cannot be opened"); return; } fcount = FileSeek(fhandle, 0, SEEK_END); if (fcount < 0) { Print("Error during seek end of tick file ", GetLastError()); } currentTime = TimeToStr(TimeCurrent(), TIME_SECONDS); fcount = FileWrite(fhandle, currentTime, NormalizeDouble(Bid, Digits), NormalizeDouble(Ask, Digits), (Ask/Point)-(Bid/Point), BuyEntry, SellEntry); if (fcount < 0) { Print("Error during tick write ", GetLastError()); } FileClose(fhandle); }