//+------------------------------------------------------------------+ //| Speed2.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 = 20; extern double dTrailingStop = 0; extern double dSlipPage = 3; extern int iNumPoints = 20; // Number of points for enter long position extern int iNumBarsBack = 5; // Number of bars back for enter long position /** Other variables */ static string sExpertName = "Speed2"; 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 < iNumBarsBack + 2) { Print("Bars less than", iNumBarsBack + 2); return(0); } return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { int iTicket; bool bExitLong, bExitShort; double dSum1; 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 dSum1 = 0.0; for (iBar = 2; iBar < iNumBarsBack + 2; iBar++) { dSum1 += High[iBar] - Low[iBar]; } if (Close[1] > (dSum1 + Point * iNumPoints) && bNewBar) { 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); } } 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); } } //+------------------------------------------------------------------+