/////////////////////////////////// #property copyright "markdshark" /////////////////////////////////// // DIBA - An Arrow pattern trader /////////////////////////////////// // Externally configurable variables extern double lot = 1; extern int sl = 50; extern int tp = 50; extern int Slippage = 3; extern int MagicNumber = 12345; //internal variables datetime prevBar; static double upperSlope, lowerSlope, hi[4], lo[4]; double myPoint; double STprice, TPprice; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { myPoint = SetPoint(); //---- return(0); } ///////////////////////////////////////// // Buy Condition //////////////////////////////////////// bool buyCondition(){ if(Close[0] > Close[2] + upperSlope && OrdersTotal()<1) return (true); else return (false); } ///////////////////////////////////////// // Sell Condition //////////////////////////////////////// bool sellCondition(){ if(Close[0] < Close[2] + lowerSlope && OrdersTotal()<1) return (true); else return (false); } ///////////////////////////////////////// // Start //////////////////////////////////////// int start(){ // We need at least 5 bars to run if (Bars < 5) return; // Everything happens when we find a new bar if (prevBar!=Time[0]){ prevBar = Time[0]; if(arrowCondition()){ loadArraysAndGetSlopes(); if (buyCondition()){ OpenBuyOrder(lot,sl,tp,"DIBA"); } else if (sellCondition()){ OpenSellOrder(lot,sl,tp,"DIBA"); } } } for (int i=0; ihighest){ high2=highest; highest=hi[j]; upX2=upX1; upX1=j; } if(lo[j] Low[2]) { //bar 2 is inside relative to bars 3 & 4 if (High[2] < High[ArrayMaximum(High,4,1)] && Low[2] > Low[ArrayMinimum(Low,4,1)]){ //high of bar 2 is below that of bar 3 && low of bar 2 is above low of bar 4 if (High[2] < High[3] && Low[2] > Low[4]) return (true); } } return (false); } int OpenBuyOrder(double mLots, int StopLoss, int TakeProfit, string mComment) { int err,ticket; RefreshRates(); ticket=OrderSend(Symbol(),OP_BUY,mLots,Ask,Slippage,0,0,mComment,MagicNumber,0,Green); if (ticket > 0) { if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) ) { Print("BUY order opened : ", OrderOpenPrice( )); if (StopLoss != 0 || TakeProfit != 0) { TPprice = 0; if (TakeProfit > 0) { TPprice=TakeLong(OrderOpenPrice(), TakeProfit); TPprice = ValidTakeProfit(OP_BUY,Ask, TPprice); } STprice = 0; if (StopLoss > 0) { STprice=StopLong(OrderOpenPrice(), StopLoss); STprice = ValidStopLoss(OP_BUY,Bid, STprice); } // Normalize stoploss / takeprofit to the proper # of digits. if (Digits > 0) { STprice = NormalizeDouble( STprice, Digits); TPprice = NormalizeDouble( TPprice, Digits); } OrderModify(ticket, OrderOpenPrice(), STprice, TPprice, 0, LightGreen); } } } return(ticket); } //+------------------------------------------------------------------+ //| OpenSellOrder | //| If Stop Loss or TakeProfit are used the values are calculated | //| for each trade | //+------------------------------------------------------------------+ void OpenSellOrder(double mLots, int StopLoss, int TakeProfit, string mComment) { int err, ticket; RefreshRates(); ticket=OrderSend(Symbol(),OP_SELL,mLots,Bid,Slippage,0,0,mComment,MagicNumber,0,Red); if (ticket > 0) { if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) ) { Print("Sell order opened : ", OrderOpenPrice()); if (StopLoss != 0 || TakeProfit != 0) { TPprice = 0; if (TakeProfit > 0) { TPprice=TakeShort(OrderOpenPrice(),TakeProfit); TPprice = ValidTakeProfit(OP_SELL,Bid, TPprice); } STprice = 0; if (StopLoss > 0) { STprice=StopShort(OrderOpenPrice() ,StopLoss); STprice = ValidStopLoss(OP_SELL,Ask, STprice); } // Normalize stoploss / takeprofit to the proper # of digits. if (Digits > 0) { STprice = NormalizeDouble( STprice, Digits); TPprice = NormalizeDouble( TPprice, Digits); } OrderModify(ticket, OrderOpenPrice(), STprice, TPprice, 0, LightGreen); } } } return(ticket); } double ValidStopLoss(int type, double price, double SL) { double minstop; double newSL; if (SL < 0.1) return(SL); minstop = MarketInfo(Symbol(),MODE_STOPLEVEL); if (Digits == 3 || Digits == 5) minstop = minstop / 10; newSL = SL; if (type == OP_BUY) { if((price - SL) < minstop*myPoint) newSL = price - minstop*myPoint; } if (type == OP_SELL) { if((SL-price) < minstop*myPoint) newSL = price + minstop*myPoint; } newSL = NormalizeDouble(newSL,Digits); return(newSL); } double ValidTakeProfit(int type, double price, double TP) { double newTP, temp; temp = MarketInfo(Symbol(), MODE_STOPLEVEL) * myPoint; newTP = TP; if (type == OP_BUY) { if((TP - price) < temp) newTP = price + temp; } if (type == OP_SELL) { if((price - TP) < temp) newTP = price - temp; } newTP = NormalizeDouble(newTP,Digits); return(newTP); } double StopLong(double price,int stop) { if(stop==0) return(0); else return(price-(stop*myPoint)); } double StopShort(double price,int stop) { if(stop==0) return(0); else return(price+(stop*myPoint)); } double TakeLong(double price,int take) { if(take==0) return(0); else return(price+(take*myPoint)); } double TakeShort(double price,int take) { if(take==0) return(0); else return(price-(take*myPoint)); } double SetPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); }