//+------------------------------------------------------------------+ //| Speed1.mq4 | //| Copyright © 2006, Elite E Services | //| http://www.ees.net.nz | //+------------------------------------------------------------------+ #property copyright "Copyright © 2006, Elite E Services" #property link "http://www.ees.net.nz" /** Extern variables */ // Trading inputs extern double dLots = 1; extern double dStopLoss = 15; extern double dTakeProfit = 30; extern double dTrailingStop = 0; extern double dSlipPage = 3; // Input variables for calculation of RSI (for enter short position) extern int iRSIPeriod = 14; // Period for RSI extern int iRSIOverboughtLevel = 70; // Overbought level for RSI extern int iRSIAppliedPrice = PRICE_CLOSE; // Applied price for RSI // Number of bars back for enter long position extern int iNumBarsBack = 5; /** Other variables */ static string sExpertName = "NewStrategy"; int iBar, i, j; bool bLongPosition, bShortPosition; double dTradeLots, dTradeOpen, dTradeLimit, dTradeStop; int iTradeNum; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { // Does not trade late friday, early monday or weekend if ((TimeDayOfWeek(CurTime()) == 5 && TimeHour(CurTime()) >= 14) || //Friday, 2pm NY time, (TimeDayOfWeek(CurTime()) == 6) || (TimeDayOfWeek(CurTime()) == 0) || (TimeDayOfWeek(CurTime()) == 1 && TimeHour(CurTime()) < 1) ) { Print("Does not trade late friday, early monday or weekend"); return(0); } if (Bars < iRSIPeriod) { Print("Bars less than", iRSIPeriod); return(0); } return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { int iTicket; bool bExitLong, bExitShort; double dAvr1; double dAvr2; bool bNewBar = false; if (CurTime() - Time[0] < 30) { bNewBar = true; } bLongPosition = false; bShortPosition = false; for (i = 0; i < OrdersTotal(); i++) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { continue; } if (OrderSymbol() == Symbol()) { if (OrderType() == OP_BUY) { bLongPosition = true; } else if (OrderType() == OP_SELL) { bShortPosition = true; } else { Print("Not BUY or SELL order number: ", OrderTicket()); return(0); } dTradeLots = OrderLots(); dTradeStop = OrderStopLoss(); dTradeOpen = OrderOpenPrice(); dTradeLimit = OrderTakeProfit(); iTradeNum = OrderTicket(); break; //terminate FOR loop, since we have an order for this currency pair. } } // No opened orders identified if (!bLongPosition && !bShortPosition) { // No money if (AccountFreeMargin() < (1000 * dLots)) { Print("No money. Free Margin = ", AccountFreeMargin()); return(0); } // Check for long position dAvr1 = 0.0; dAvr2 = 0.0; for (iBar = 1; iBar <= iNumBarsBack; iBar++) { dAvr1 += (iHigh(NULL, 0, iBar) - iClose(NULL, 0, iBar)); dAvr2 += (iHigh(NULL, 0, iBar + 1) - iClose(NULL, 0, iBar + 1)); } if ((dAvr1 / iNumBarsBack) > (dAvr2 / iNumBarsBack) && bNewBar) { // if ((dAvr1 / iNumBarsBack) > (dAvr2 / iNumBarsBack)) { iTicket = OrderSend(Symbol(), OP_BUY, dLots, Ask, dSlipPage, SLBuyTPSell(Ask, dStopLoss), SLSellTPBuy(Ask, dTakeProfit), sExpertName, 0, 0, Blue); if (iTicket > 0) { if (OrderSelect(iTicket, SELECT_BY_TICKET, MODE_TRADES)) { Print("BUY order opened : ",OrderOpenPrice()); } } else { Print("Error opening BUY order : ",GetLastError()); return(0); } } // Check for short position if (iRSI(NULL, 0, iRSIPeriod, iRSIAppliedPrice, 0) > iRSIOverboughtLevel && bNewBar) { // if (iRSI(NULL, 0, iRSIPeriod, iRSIAppliedPrice, 1) > iRSIOverboughtLevel) { iTicket = OrderSend(Symbol(), OP_SELL, dLots, Bid, dSlipPage, SLSellTPBuy(Bid, dStopLoss), SLBuyTPSell(Bid, dTakeProfit), sExpertName, 0, 0, Red); if (iTicket > 0) { if (OrderSelect(iTicket, SELECT_BY_TICKET, MODE_TRADES)) { Print("SELL order opened : ",OrderOpenPrice()); } } else { Print("Error opening SELL order : ",GetLastError()); } } return(0); } return(0); } // Calculates 'stop loss' for BUY order or 'take profit' for SELL order double SLBuyTPSell(double dPrice, double dSLTP) { if (dSLTP == 0.0) { return(0); } else { return(dPrice - dSLTP * Point); } } // Calculates 'stop loss' for SELL order or 'take profit' for BUY order double SLSellTPBuy(double dPrice, double dSLTP) { if (dSLTP == 0.0) { return(0); } else { return(dPrice + dSLTP * Point); } } //+------------------------------------------------------------------+