// MT4 #property copyright "(C) S.Projects" #property link "http://trader.snowseed.com" // We buy or sell when indicator crosses this level // (buy) or 0.9 - this level (sell) extern double dBuySellLevel = 0.01; //0.08; // Interval and MA used to calculate CLV extern int nClvInterval = 49; //30; extern int nClvMa = 3; //4; // Period, used to calculate ADX extern int nAdxPeriod = 28; //15; // Note, that dStopLoss is used in the library file, // do not change this name. Same applies to dTakeProfit // and dTrailingStop extern double dStopLoss = 110; //200; extern double useMemoryManagement = 1; //extern double lotSize = 1; // ------ // We are not using take profit, and we make trailing stop // equal to stop loss. Nevertheless, these variables // must be declared, as they are used in the library file. // However, we don't have to make them extern. double dTakeProfit = 0; double dTrailingStop; // Fixed or variable lot size bool bUseMm = false; //true; //false; // Name of our expert. Used for reporting purposes string strExpert = "snowseed-eCLV"; // ------ // Incluse the library file #include "snowseed-lib.mq4" // ------ // Buy and sell levels are calculated from dBuySellLevel double dBuyLevel; double dSellLevel; ////////////////// int init () { // This code, together with the library functions, // makes sure we only trade at the beginning of the bar, // and experts are executed one after another (no // competition). if (useMemoryManagement == 0) bUseMm = false; else bUseMm = true; nBars = Bars; if(!IsTesting() && !GlobalVariableCheck(strTradeSemaphore)) GlobalVariableSet(strTradeSemaphore, 0.0); // ------ // Here, we can list as many currencies and timeframes as // we want, providing each with its own set of parameters. // For convenience, the combination of parameters, that // we find during the testing, is written next to the // code, commented. if(Symbol() == "EURUSD" && Period() == 60) { // 0.08,30,4,15,200: 3140, 134 if(!IsTesting()) { dBuySellLevel = 0.11; nClvInterval = 26; nClvMa = 6; nAdxPeriod = 3; dStopLoss = 200 * Point; dTrailingStop = dStopLoss; } else { dStopLoss = dStopLoss * Point; dTrailingStop = dStopLoss; } // This particular combination of expert, currency // and timeframe is assigned a magic number 35 nMagic = 35; } dBuyLevel = dBuySellLevel + 0.1; dSellLevel = 0.9 - dBuySellLevel; return(0); } ////////////////// int deinit() { // Release the global semaphore variable if(!IsTesting()) GlobalVariableSetOnCondition(strTradeSemaphore, 0.0, nMagic); return(0); } // ------ int start() { // If we don't have enough bars if(Bars < nClvInterval + nClvMa) return(0); // Once a day, write trades history to file. // For reporting purposes only. Report(strExpert, nMagic, bReportDone); // ------ // If it is not a beginning of the new bar if(!IsBarEnd()) return(0); // Wait until a semaphore is free (if some // other expert is trading, wait for it to finish). CheckTradeSemaphore(); // ------ // Calculate values of the indicators. double dClv = iCustom(NULL, 0, "snowseed-iCLV", nClvInterval, nClvMa, 0, 1); // Print("dClv = " + dClv); double dClvPrev = iCustom(NULL, 0, "snowseed-iCLV", nClvInterval, nClvMa, 0, 2); double dAdxUp = iADX(NULL, 0, nAdxPeriod, PRICE_CLOSE, MODE_PLUSDI, 1); double dAdxDn = iADX(NULL, 0, nAdxPeriod, PRICE_CLOSE, MODE_MINUSDI, 1); // If money management is used, prepare to calculate the size of a lot if(bUseMm == true) { dProfit = 0; for(int nCnt = 0; nCnt < HistoryTotal(); nCnt++) { OrderSelect(nCnt, SELECT_BY_POS, MODE_HISTORY); if(OrderMagicNumber() == nMagic && OrderType() <= OP_SELL) { dProfit += OrderProfit(); } } } // If it is time to close orders, that are currently // opened. First, we look for orders with the current // magic number, then check the condition (if it should // be closed). // Note, CloseBuy() and CloseSell() are functions from // our library for(nCnt = 0; nCnt < OrdersTotal(); nCnt++) { OrderSelect(nCnt, SELECT_BY_POS, MODE_TRADES); if(OrderMagicNumber() == nMagic) { if(OrderType() == OP_BUY) { if(dClvPrev >= dSellLevel && dClv <= dSellLevel) { CloseBuy(strExpert); break; } } else if(OrderType() == OP_SELL) { if(dClvPrev <= dBuyLevel && dClv >= dBuyLevel) { CloseSell(strExpert); break; } } } } // Now, we need to trade ONLY if there are no // opened orders. int nNumOfOpenedOrders = 0; for(nCnt = OrdersTotal() - 1; nCnt >= 0; nCnt--) { OrderSelect(nCnt, SELECT_BY_POS, MODE_TRADES); if(OrderMagicNumber() == nMagic) nNumOfOpenedOrders++; } // Check the order open conditions if(nNumOfOpenedOrders == 0) { //Print("nNumOfOpenedOrders " + nNumOfOpenedOrders ); if(dClvPrev <= dBuyLevel && dClv >= dBuyLevel && dAdxUp > dAdxDn) { //Print("buy!!"); Buy(strExpert); } else if(dClvPrev >= dSellLevel && dClv <= dSellLevel && dAdxUp < dAdxDn) { //Print("SELL!!"); Sell(strExpert); } } // ------ // This function moves stops closer to the price. It is // from our library ModifyOrders(); // ------ // Release the semaphore, after we are done if(!IsTesting()) { Sleep(1000); GlobalVariableSet(strTradeSemaphore, 0.0); } // ------ return(0); }
Sample
Analysis
Market Information Used:
Indicator Curves created:
Indicators Used:
Movement directional index
Custom Indicators Used:
snowseed-iCLV
Order Management characteristics:
Checks for the total of open orders
Other Features: