//+------------------------------------------------------------------+ //| SMC Autotrader Momentum.mq4 | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ #include extern int MagicNumber = 16384; extern double TakeProfit = 50; extern double Lots = 0.1; extern double InitialStop = 30; extern double TrailingStop = 20; extern int LockInPips = 50; // Set to 0 for no lock in pips extern bool CheckHistory = true; datetime BarTime; static string DirTrgS = "None"; static string DirTrgM= "None"; static string DirTrgL = "None"; double wt[30]; double lsma_ma[30]; double myPoint; //##################################################################### int init() { //---- BarTime=0; myPoint = SetPoint(Symbol()); //---- return(0); } //##################################################################### int start() { int cnt,total,ticket,MinDist,tmp; double Spread; double ATR; double StopMA; double SetupHigh, SetupLow; // int LSMA_Period = 25; // int Rperiod = 25; int length; double lsma_length; int i; double lsma10, lsma25; //############################################################################ if(Bars<100) {Print("bars less than 100"); return(0);} //exit if not new bar if(BarTime == Time[0]) {return(0);} //new bar, update bartime BarTime = Time[0]; //######################################################################################## length = 15; //Hourly set to 15 bars lsma_length = 15; DirTrgS = GetDirTrg(length, lsma_length); // Moved 4 hr and daily below before code to open new trades // Only DirTrgS is needed for open trades to be closed // This will speed up backtest //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //######################################################################################### //~~~~~~~~~~~~~~~~Miscellaneous setup stuff~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MinDist=MarketInfo(Symbol(),MODE_STOPLEVEL); Spread=(Ask-Bid); // use an indicator for data values // ATR =iATR(NULL,0,10,0); // BE CAREFUL OF EFFECTING THE AUTO TRAIL STOPS double TrendS=iMA(NULL,0,5,0,MODE_SMA,PRICE_CLOSE,0); double TrendM=iMA(NULL,0,12,0,MODE_SMA,PRICE_CLOSE,0); HandleOpenPositions(DirTrgS); //~~~~~~~~~~~~START of NEW ORDERS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //######################### NEW POSITIONS ? ###################################### //Possibly add in timer to stop multiple entries within Period // Check Margin available // ONLY ONE ORDER per SYMBOL // Loop around orders to check symbol doesn't appear more than once // Check for elapsed time from last entry to stop multiple entries on same bar if (TradeClosedToday()) return(0); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ total=OrdersTotal(); if(total>0) { for(cnt=0;cnt0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { Print("BUY order opened : ",OrderOpenPrice()); if (InitialStop > 0 || TakeProfit > 0) { TPprice = 0.0; if (TakeProfit > 0) TPprice=TakeLong(OrderOpenPrice(), TakeProfit, myPoint); STprice = 0.0; if (InitialStop > 0) { STprice=StopLong(OrderOpenPrice(), InitialStop, myPoint); STprice = ValidStopLoss(Symbol(), OP_BUY,Bid, STprice, myPoint); } // 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); } } } else { err = GetLastError(); Print("Error opening BUY order (" + err + ") " + ErrorDescription(err)); return(0); } } if (cmd == OP_SELL) { Alert(Symbol()," Short"); ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"Trigger Short",MagicNumber,0,Red); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { Print("SELL order opened : ",OrderOpenPrice()); if (InitialStop != 0 || TakeProfit != 0) { TPprice = 0.0; if (TakeProfit > 0) TPprice=TakeShort(OrderOpenPrice(),TakeProfit, myPoint); STprice = 0.0; if (InitialStop > 0) { STprice=StopShort(OrderOpenPrice(), InitialStop, myPoint); STprice = ValidStopLoss(Symbol(), OP_SELL,Ask, STprice, myPoint); } // 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); } } } else { err = GetLastError(); Print("Error opening SELL order (" + err + ") " + ErrorDescription(err)); } } } double LSMA(int length, int shift) { double sum, tmpTG, myWt; int i, lengthvar; sum = 0; for(i = length; i >= 1 ; i--) //LSMA loop { lengthvar = length + 1; //lengthvar = 21 lengthvar /= 3; //lengthvar = 7 tmpTG = 0; tmpTG = ( i - lengthvar)*Close[length-i+shift]; //tmp = 20 - 7 * close[20-i+shift] sum+=tmpTG; } myWt = sum*6/(length*(length+1)); return(myWt); } string GetDirTrg(int length, int lsma_length) { int shift,i,j; string DirTrg; for(shift = 30; shift >= 0; shift--) // MAIN For Loop { wt[shift] = LSMA(length, shift); j = shift; lsma_ma[shift] = wt[j+1] + (wt[j]-wt[j+1])* 2/(lsma_length+1); } DirTrg = "None"; if(wt[1] > lsma_ma[1] && wt[2] > lsma_ma[2] ) DirTrg = "Long"; //wt[2] < lsma_ma[2] && if(wt[1] < lsma_ma[1] && wt[2] < lsma_ma[2] ) DirTrg = "Short"; // wt[2] > lsma_ma[2] && return(DirTrg); } //######################################################################################### //######################################################################################## //################## ORDER CLOSURE ################################################### // If Orders are in force then check for closure against Technicals LONG & SHORT void HandleOpenPositions(string DirTrgS) { int total, cnt; double LockInStop; int MinDist; MinDist=MarketInfo(Symbol(),MODE_STOPLEVEL); total=OrdersTotal(); if(total>0) { for(cnt=total-1;cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol()!=Symbol()) continue; if (OrderMagicNumber() != MagicNumber) continue; //CLOSE LONG Entries if(OrderType()==OP_BUY) { if(DirTrgS == "Short") { OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close LONG position return; } //################################################################################ //################## LOCK IN 50 points profit ################################### if(LockInPips > 0) { LockInStop = OrderOpenPrice() + LockInPips * myPoint; if (Close[0] > LockInStop + MinDist * myPoint) { if (OrderStopLoss() < LockInStop) { OrderModify(OrderTicket(),OrderOpenPrice(),LockInStop,OrderTakeProfit(),0,White); } } } //############################################################################## //################## ORDER TRAILING STOP Adjustment ####################### if (TrailingStop > 0) { if(Bid-OrderOpenPrice()> (myPoint*TrailingStop) && OrderStopLoss() 0) { LockInStop = OrderOpenPrice() - LockInPips * myPoint; if (Close[0] < LockInStop - MinDist * myPoint) { if (OrderStopLoss() < LockInStop || OrderStopLoss() < 0.1) { OrderModify(OrderTicket(),OrderOpenPrice(),LockInStop,OrderTakeProfit(),0,White); } } } //############################################################################## //################## ORDER TRAILING STOP Adjustment ####################### if (TrailingStop > 0) { if ( OrderOpenPrice()-Ask > (myPoint*TrailingStop) && OrderStopLoss() > Ask+(myPoint*TrailingStop) ) { OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(myPoint*TrailingStop),OrderTakeProfit(),0,Yellow); return(0); } } } } // for loop return } // close 1st if } bool TradeClosedToday() { int cnt, total; if (CheckHistory) // switch to turn ON/OFF history check { total=HistoryTotal(); if(total>0) { for(cnt=0;cnt