//+---------------------------------------------------------------------------| //| PRD Trader v1.6.mq4 | //| by William R Garrison | //| 10 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 | //| | //| 9 June 2009 - Added low and high range for TP targets | //| 10 June 2009 - Adjust slippage for 5-digit pricing, | //| add RefreshRates() between orders | //| 11 June 2009 - Add option to record channel values | //| 11 June 2009 - Fix stop adjust for orders 2 and 3 when price is too close | //| 11 June 2009 - Move signal logic into EA | //| 12 June 2009 - Add closeout for reversal signal | //| Add "No TP" and channel line SL option for third order | //| 12 June 2009 - Add PermitTpAdjustEveryTick | //| Add cutoff time on Fridays | //+---------------------------------------------------------------------------+ #property copyright "Copyright 2009, William Garrison" #include extern double lot = 0.1; extern int UseMM = true; extern double Risk = 3.0; int Shift = 1; extern string target_range_desc = "1 for low targets, 2 for high targets"; extern int TargetRange = 2; extern bool DynamicTP = false; extern bool Order3OpenTP = true; extern int InitialStop = 150; extern bool UseTrailingStop = false; extern int TrailingStop=30.0; extern bool UseChannelStop = true; extern int ChannelStopLinesBack = 2; extern bool EnterWithCCI=true; extern int CCIPeriod = 14; extern int CCIEntryLevel = 100; extern int PRD.period=0; /*default 0 means the channel will use the open time from "x" bars back on which ever time period the indicator is attached to. one can change to 1,5,15,30,60...etc to "lock" the start time to a specific period, and then view the "locked" channels on a different time period...*/ extern int PRD.LR.length=55; // bars back regression begins extern double PRD.std.channel.1 = 0.809; // 1st channel extern double PRD.std.channel.2 = 1.618; // 2nd channel extern double PRD.std.channel.3 = 2.618; // 3nd channel extern int TradeStartHour = 0; extern int TradeStopHour = 0; extern int FridayCutoffHour = 12; extern bool EmailAlert=false; extern bool RecordTicks = false; extern bool RecordChannel = false; extern string EAVersion = "PRD 1.6"; extern int MagicNumber1 = 090608; extern int MagicNumber2 = 090609; extern int MagicNumber3 = 090610; int slippage = 3; int lotdigits = 0; int pip_adjust = 0; double minlot, maxlot, stops_level; string tick_file_name; int buyzone, sellzone; double BuyEntry, SellEntry; int BuySellTimeFrame; datetime last_tick = -1; bool order2mod = false; bool order3mod = false; bool new_bar = false; 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; stops_level = MarketInfo(Symbol(), MODE_STOPLEVEL); buyzone =-CCIEntryLevel; sellzone= CCIEntryLevel; Print("digits ", Digits, " lotdigits ", lotdigits, "buyzone ", buyzone, " sellzone ", sellzone); Print("pip_adjust ", pip_adjust, " InitialStop ", InitialStop*pip_adjust, " TrailingStop ", TrailingStop*pip_adjust, " slippage ", slippage*pip_adjust, " Stops Level ", stops_level*pip_adjust); // range check for the TP target range selection if (TargetRange < 1) TargetRange = 1; if (TargetRange > 2) TargetRange = 2; // range check for channel trailing stop value if (ChannelStopLinesBack < 1) ChannelStopLinesBack = 1; if (ChannelStopLinesBack > 2) ChannelStopLinesBack = 2; new_bar = false; // If the tick file doesn't exist, write the column header tick_file_name = Symbol()+"_ticks.csv"; if (RecordTicks) init_tick_file(); } int start() { double locallots = 0; double localstop = 0; double localtarget = 0; double MeanYellow; double High1Lime; double Low1Lime; double High2Orange; double Low2Orange; double High3Red; double Low3Red; if (RecordTicks) Write_Tick_To_File(); // there are function that only happen at the start of a bar if (Time[0] == last_tick) new_bar = false; else new_bar = true; // determine what orders are still open int total = OrdersTotal(); int ticket1 = -1; int ticket1type = -1; int ticket2 = -1; int ticket2type = -1; int ticket3 = -1; int ticket3type = -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(); ticket1type = OrderType(); } if(OrderType() <= OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber2) { ticket2 = OrderTicket(); ticket2type = OrderType(); } if(OrderType() <= OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber3) { ticket3 = OrderTicket(); ticket3type = OrderType(); } } // what if the first target was so close to the open that ticket1 has no TP value? // go ahead and close ticket 1 whan ticket 2 closes if((ticket1 > 0) && (ticket2 < 0)) { OrderSelect(ticket1, SELECT_BY_TICKET, MODE_TRADES); OrderClose(ticket1, OrderLots(), OrderClosePrice(), 3*pip_adjust, 0); Print("Ticket 1 closed by ticket 2 closing"); } // adjust profit targets based on current value of channel if((DynamicTP > 0) && (ticket1 > 0) && (new_bar)) { bool foo = OrderSelect(ticket1, SELECT_BY_TICKET, MODE_TRADES); if (!foo) { Print("Error during target1 adjust order select ", ErrorDescription(GetLastError())); return(0); } if (TargetRange == 1) { if (OrderType() == OP_BUY) { Low1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 2,0); localtarget = Low1Lime; } else { // OrderType() == OP_SELL High1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 1,0); localtarget = High1Lime; } } else { // targets in high range MeanYellow = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 0,0); localtarget = MeanYellow; } if(((OrderType() == OP_BUY) && (OrderTakeProfit() < localtarget)) || ((OrderType() == OP_SELL) && (OrderTakeProfit() > localtarget))) { if(!OrderModify(ticket1, OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(localtarget,Digits), 0, Blue)) { Print("Error during ticket1 target adjust ", ErrorDescription(GetLastError())); Print("TP1 buy/sell open ", OrderOpenPrice(), " SL ", OrderStopLoss(), " localtarget ", localtarget, " 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) && (!order2mod) && (Bid>=OrderOpenPrice()+25*Point*pip_adjust)) { localstop = OrderOpenPrice()+10*Point*pip_adjust; if(!OrderModify(ticket2, OrderOpenPrice(), NormalizeDouble(localstop,Digits), OrderTakeProfit(), 0, Blue)) { Print("Error during ticket2 stop adjust ", ErrorDescription(GetLastError())); Print("TS2 BE buy open ", OrderOpenPrice(), " BS ", OrderStopLoss(), " localstop ", localstop, " TP ", OrderTakeProfit()); } else order2mod = true; } if ((DynamicTP > 0) && (new_bar)) { if (TargetRange == 1) { // low range targets MeanYellow = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 0,0); localtarget = MeanYellow; } else { // high range targets High1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 1,0); localtarget = High1Lime; } if(OrderTakeProfit() != NormalizeDouble(localtarget, Digits)) { if(!OrderModify(ticket2, OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(localtarget,Digits), 0, Blue)) { Print("Error during ticket2 target adjust ", ErrorDescription(GetLastError())); Print("TP2 buy open ", OrderOpenPrice(), " SL ", OrderStopLoss(), " localtarget ", localtarget, " 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) && (!order2mod) && (Ask<=OrderOpenPrice()-25*Point*pip_adjust)) { localstop = OrderOpenPrice()-10*Point*pip_adjust; if(!OrderModify(ticket2, OrderOpenPrice(), NormalizeDouble(localstop,Digits), OrderTakeProfit(), 0, Red)) { Print("Error during ticket2 stop adjust ", ErrorDescription(GetLastError())); Print("TS2 BE sell open ", OrderOpenPrice(), " SL ", OrderStopLoss(), " localstop ", localstop, " TP ", OrderTakeProfit()); } else order2mod = true; } if ((DynamicTP > 0) && (new_bar)) { if (TargetRange == 1) { MeanYellow = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 0,0); localtarget = MeanYellow; } else { Low1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 2,0); localtarget = Low1Lime; } if(OrderTakeProfit() != NormalizeDouble(localtarget, Digits)) { if(!OrderModify(ticket2, OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(localtarget,Digits), 0, Red)) { Print("Error during ticket2 target adjust ", ErrorDescription(GetLastError())); Print("TP2 sell open ", OrderOpenPrice(), " SL ", OrderStopLoss(), " localtarget ", localtarget, " 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 it isn't already better if ((ticket1 < 0) && (!order3mod) && (Bid>=OrderOpenPrice()+25*Point*pip_adjust) && (OrderStopLoss() < OrderOpenPrice()+10*Point*pip_adjust)) { localstop = OrderOpenPrice()+10*Point*pip_adjust; if(!OrderModify(ticket3, OrderOpenPrice(), NormalizeDouble(localstop,Digits), OrderTakeProfit(), 0, Blue)) { Print("Error during ticket3 stop adjust ", ErrorDescription(GetLastError())); Print("TS3 BE buy open ", OrderOpenPrice(), " SL ", OrderStopLoss(), " localstop ", localstop, " TP ", OrderTakeProfit()); } else order3mod = true; } if((DynamicTP > 0) && (!Order3OpenTP) && (new_bar)) { if (TargetRange == 1) { High1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 1,0); localtarget = High1Lime; } else { High2Orange = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 3,0); localtarget = High2Orange; } if(OrderTakeProfit() != NormalizeDouble(localtarget, Digits)) { if(!OrderModify(ticket3, OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(localtarget,Digits), 0, Blue)) { Print("Error during ticket3 target adjust ", ErrorDescription(GetLastError())); Print("TP3 buy open ", OrderOpenPrice(), " SL ", OrderStopLoss(), " localtarget ", localtarget, " TP ", OrderTakeProfit()); } } } if (((UseTrailingStop) || (UseChannelStop) || (Order3OpenTP)) && (new_bar)) { if (UseChannelStop) { if (ChannelStopLinesBack == 1) { High3Red = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 5,0); High2Orange = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 3,0); High1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 1,0); MeanYellow = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 0,0); if (OrderClosePrice() > High3Red) localstop = High2Orange; else if (OrderClosePrice() > High2Orange) localstop = High1Lime; else if (OrderClosePrice() > High1Lime) localstop = MeanYellow; } else { High3Red = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 5,0); High2Orange = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 3,0); High1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 1,0); MeanYellow = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 0,0); Low1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 2,0); if (OrderClosePrice() > High3Red) localstop = High1Lime; else if (OrderClosePrice() > High2Orange) localstop = MeanYellow; else if (OrderClosePrice() > High1Lime) localstop = Low1Lime; } } else localstop = NormalizeDouble(Bid-TrailingStop*Point*pip_adjust, Digits); if((localstop > OrderStopLoss()+(5*Point*pip_adjust)) && (Bid > localstop+stops_level*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(), " SL ", OrderStopLoss(), " localstop ", localstop, " TP ", OrderTakeProfit(), " Channel ", UseChannelStop, " Bid ", Bid); } } } // end of trailing stop } // end of OP_BUY else { // OrderType() == OP_SELL // special for the last ticket, bring stop to breakeven if ((ticket1 < 0) && (!order3mod) && (Ask<=OrderOpenPrice()-25*Point*pip_adjust) && (OrderStopLoss() > OrderOpenPrice()-10*Point*pip_adjust)) { localstop = OrderOpenPrice()-10*Point*pip_adjust; if(!OrderModify(ticket3, OrderOpenPrice(), NormalizeDouble(localstop,Digits), OrderTakeProfit(), 0, Red)) { Print("Error during ticket3 target adjust ", ErrorDescription(GetLastError())); Print("TS3 BE sell open ", OrderOpenPrice(), " SL ", OrderStopLoss(), " localstop ", localstop, " TP ", OrderTakeProfit()); } else order3mod = true; } if((DynamicTP > 0) && (!Order3OpenTP) && (new_bar)) { if (TargetRange == 1) { Low1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 2,0); localtarget = Low1Lime; } else { Low2Orange = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 4,0); localtarget = Low2Orange; } if(OrderTakeProfit() != NormalizeDouble(localtarget, Digits)) { if(!OrderModify(ticket3, OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(localtarget,Digits), 0, Red)) { Print("Error during ticket3 target adjust ", ErrorDescription(GetLastError())); Print("TP3 buy open ", OrderOpenPrice(), " SL ", OrderStopLoss(), " localtarget ", localtarget, " TP ", OrderTakeProfit()); } } } if (((UseTrailingStop) || (UseChannelStop) || (Order3OpenTP)) && (new_bar)) { if (UseChannelStop) { if (ChannelStopLinesBack == 1) { MeanYellow = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 0,0); Low1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 2,0); Low2Orange = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 4,0); Low3Red = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 6,0); if (OrderClosePrice() < Low3Red) localstop = Low2Orange; else if (OrderClosePrice() < Low2Orange) localstop = Low1Lime; else if (OrderClosePrice() > Low1Lime) localstop = MeanYellow; } else { High1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 1,0); MeanYellow = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 0,0); Low1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 2,0); Low2Orange = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 4,0); Low3Red = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 6,0); if (OrderClosePrice() < Low3Red) localstop = Low1Lime; else if (OrderClosePrice() < Low2Orange) localstop = MeanYellow; else if (OrderClosePrice() > Low1Lime) localstop = High1Lime; } } else localstop = NormalizeDouble(Ask+TrailingStop*Point*pip_adjust, Digits); if((localstop < OrderStopLoss()-(5*Point*pip_adjust)) && (Ask < localstop-stops_level*pip_adjust)) { if(!OrderModify(ticket3, OrderOpenPrice(), NormalizeDouble(localstop, Digits), OrderTakeProfit(), 0, Red)) { Print("Error during trailing stop ", ErrorDescription(GetLastError())); Print("TS3 sell open ", OrderOpenPrice(), " SL ", OrderStopLoss(), " localstop ", localstop, " TP ", OrderTakeProfit(), " Channel ", UseChannelStop, " Ask ", Ask); } } } // 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]; BuyEntry = BuySignal(); SellEntry = SellSignal(); if (RecordChannel) { Print("New Bar ", TimeToStr(last_tick, TIME_MINUTES), " Buy Signal ", BuyEntry, " Sell Signal ", SellEntry); High3Red = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 5,0); High2Orange = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 3,0); High1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 1,0); MeanYellow = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 0,0); Low1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 2,0); Low2Orange = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 4,0); Low3Red = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 6,0); Print("Channels ", High3Red, " ", High2Orange, " ", High1Lime, " ", MeanYellow, " ", Low1Lime, " ", Low2Orange, " ", Low3Red); } // if any order is open, Check for reversal signal if (CheckForReversal(ticket1, ticket1type)) ticket1 = -1; if (CheckForReversal(ticket2, ticket2type)) ticket2 = -1; if (CheckForReversal(ticket3, ticket3type)) ticket3 = -1; // 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; } // Check that we don't open new orders late on Fridays if((Day() == 5) && (FridayCutoffHour > 0) && (Hour() >= FridayCutoffHour)) { BuyEntry = 0; SellEntry = 0; } locallots = LotsOptimized(); if (BuyEntry > 0) { if (!IsTradeAllowed()) Print("Buy order at ", Ask); else { // establish indicator values Low1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 2,0); MeanYellow = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 0,0); High1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 1,0); High2Orange = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 3,0); // High3Red = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 5,0); // First third of order RefreshRates(); ticket1=OrderSend(Symbol(),OP_BUY, locallots, Ask, slippage*pip_adjust, 0, 0, EAVersion, MagicNumber1, 0, Blue); if(ticket1 > 0) { if (TargetRange == 1) localtarget = Low1Lime; else localtarget = MeanYellow; OrderModify(ticket1, OrderOpenPrice(), NormalizeDouble(Bid-InitialStop*Point*pip_adjust, Digits), NormalizeDouble(localtarget,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 RefreshRates(); ticket2=OrderSend(Symbol(),OP_BUY, locallots, Ask, slippage*pip_adjust, 0, 0, EAVersion, MagicNumber2, 0, Blue); if(ticket2 > 0) { if (TargetRange == 1) localtarget = MeanYellow; else localtarget = High1Lime; OrderModify(ticket2, OrderOpenPrice(), NormalizeDouble(Bid-InitialStop*Point*pip_adjust, Digits), NormalizeDouble(localtarget,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 RefreshRates(); ticket3=OrderSend(Symbol(),OP_BUY, locallots, Ask, slippage*pip_adjust, 0, 0, EAVersion, MagicNumber3, 0, Blue); if(ticket3 > 0) { if (TargetRange == 1) localtarget = High1Lime; else localtarget = High2Orange; if (Order3OpenTP) localtarget = 0; OrderModify(ticket3, OrderOpenPrice(), NormalizeDouble(Bid-InitialStop*Point*pip_adjust, Digits), NormalizeDouble(localtarget,Digits), 0, Blue); if (OrderSelect(ticket3, SELECT_BY_TICKET, MODE_TRADES)) { Print("BE3 buy open ", OrderOpenPrice(), " BS ", OrderStopLoss(), " TP ", OrderTakeProfit()); if (EmailAlert) SendMail(EAVersion+"Buy", "Buy orders open on "+Symbol()+" at price "+DoubleToStr(OrderOpenPrice(),Digits)); } } else Print("BE3 ", ErrorDescription(GetLastError())); order2mod = false; order3mod = false; } } // end of buy entry if (SellEntry > 0) { if (!IsTradeAllowed()) Print("Sell order at ", Bid); else { // establish indicator values High1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 1,0); MeanYellow = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 0,0); Low1Lime = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 2,0); Low2Orange = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 4,0); // Low3Red = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 6,0); // First third of order RefreshRates(); ticket1=OrderSend(Symbol(), OP_SELL, locallots, Bid, slippage*pip_adjust, 0, 0, EAVersion, MagicNumber1, 0,Red); if(ticket1 > 0) { if (TargetRange == 1) localtarget = High1Lime; else localtarget = MeanYellow; OrderModify(ticket1, OrderOpenPrice(), NormalizeDouble(Ask+InitialStop*Point*pip_adjust, Digits), NormalizeDouble(localtarget,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 RefreshRates(); ticket2=OrderSend(Symbol(), OP_SELL, locallots, Bid, slippage*pip_adjust, 0, 0, EAVersion, MagicNumber2, 0,Red); if(ticket2 > 0) { if (TargetRange == 1) localtarget = MeanYellow; else localtarget = Low1Lime; OrderModify(ticket2, OrderOpenPrice(), NormalizeDouble(Ask+InitialStop*Point*pip_adjust, Digits), NormalizeDouble(localtarget,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 RefreshRates(); ticket3=OrderSend(Symbol(), OP_SELL, locallots, Bid, slippage*pip_adjust, 0, 0, EAVersion, MagicNumber3, 0,Red); if(ticket3 > 0) { if (TargetRange == 1) localtarget = Low1Lime; else localtarget = Low2Orange; if (Order3OpenTP) localtarget = 0; OrderModify(ticket3, OrderOpenPrice(), NormalizeDouble(Ask+InitialStop*Point*pip_adjust, Digits), NormalizeDouble(localtarget,Digits), 0, Red); if(OrderSelect(ticket3, SELECT_BY_TICKET, MODE_TRADES)) { Print("SE3 sell open ", OrderOpenPrice(), " SS ", OrderStopLoss(), " TP ", OrderTakeProfit()); if (EmailAlert) SendMail(EAVersion+"Sell", "Sell orders open on "+Symbol()+" at price "+DoubleToStr(OrderOpenPrice(),Digits)); } } else Print("SE3 ", ErrorDescription(GetLastError())); order2mod = false; order3mod = false; } } 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); } // ---------------------------------------------------------------------- // The signal check functions look for Bar-2 and Bar-1 to have crossed // the orange line. // ---------------------------------------------------------------------- int BuySignal() { double Low2Orange = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 4,Shift); double Low2Orange2 = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 4,Shift+1); if ((Close[Shift]>Low2Orange) && (Close[Shift+1] buyzone) return(1); else return(0); } return(1); } return(0); } int SellSignal() { double High2Orange = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 3,Shift); double High2Orange2 = iCustom(NULL, 0, "Past Regression Deviated", PRD.period, PRD.LR.length, PRD.std.channel.1, PRD.std.channel.2, PRD.std.channel.3, 3,Shift+1); if ((Close[Shift]High2Orange2)) { if (EnterWithCCI) { double cci=iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,Shift); if (cci < sellzone) return(1); else return(0); } return(1); } return(0); } // ---------------------------------------------------------------------- // CheckForReversal will close an order if a opposite signal appears // returns true if order is now closed, false if no action taken // ---------------------------------------------------------------------- bool CheckForReversal(int l_ticket, int l_type) { if (l_ticket < 0) return(true); if (((l_type == OP_BUY) && (SellEntry > 0)) || ((l_type == OP_SELL) && (BuyEntry > 0))) { if (!OrderSelect(l_ticket, SELECT_BY_TICKET, MODE_TRADES)) { Print("Error during reversal ticket ", l_ticket, " select ", ErrorDescription(GetLastError())); return(false); } if (!OrderClose(l_ticket, OrderLots(), OrderClosePrice(), 3*pip_adjust, 0)) { Print("Error during reversal ticket ", l_ticket, " close ", ErrorDescription(GetLastError())); return(false); } return(true); } return(false); }