//+------------------------------------------------------------------+ //| trend_v7mq4 | //| | //| http://www..com | //+------------------------------------------------------------------+ #property copyright "" //#include //-----------------------------------------------------------------------------+ // External Variables input bool DynamicLotSize = true; input double EquityPercentage = 1; input double FixedLotSize = 0.01; input int Slippage = 5; input int Multiplier = 1; input double MinProfit = 3; input double ProfitToProtect = 1; input double OpenLevel = 10; input double CloseLevel = 10; input int MagicNumber = 201601; //----------------------------------------------------------------------------------------------------------------------------+ // Global Variables double Spread,StopLevel,FreezeLevel,GetOutLevel,RiskAmount,LotSize,pips2dbl; double BuyTrigger,SellTrigger,TickValue,Profit,TotalProfit; int OTCurrentTick, OTLastTick,BuyTicket,SellTicket,orders,OpenOrders,pips2points,pips; bool ProfitLevelSet = false; //----------------------------------------------------------------------------------------------------------------------------+ int init () { if (Digits == 5 || Digits == 3) { pips2dbl = Point*10; pips2points = 10; pips = 1; } else { pips2dbl = Point; pips2points = 1; pips = 0; } Spread = MarketInfo(Symbol(),MODE_SPREAD)*Point; StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL)*Point; FreezeLevel =MarketInfo(Symbol(),MODE_FREEZELEVEL); return(0); } //+------------------------------------------------------------------------------------------------------------------------+ int start() {//0 //---- Lot size calculation if(DynamicLotSize == true) { RiskAmount = AccountEquity()*(EquityPercentage/10000); TickValue = MarketInfo(Symbol(),MODE_TICKVALUE); if(Point == 0.001 || Point == 0.00001)TickValue *= 10; LotSize = RiskAmount/TickValue; } else LotSize = FixedLotSize; //---- Lot size verification if(LotSize < MarketInfo(Symbol(),MODE_MINLOT)) { LotSize = MarketInfo(Symbol(),MODE_MINLOT); } else if(LotSize > MarketInfo(Symbol(),MODE_MAXLOT)) { LotSize = MarketInfo(Symbol(),MODE_MAXLOT); } if(MarketInfo(Symbol(),MODE_LOTSTEP) == 0.1) { LotSize = NormalizeDouble(LotSize,1); } else LotSize = NormalizeDouble(LotSize,2); //---- Open Orders OpenOrders = 0; for(int Counter = OrdersTotal()-1; Counter >= 0; Counter--) {//1 if(!OrderSelect(Counter,SELECT_BY_POS,MODE_TRADES))continue; {//2 if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber) {//3 OpenOrders = OrdersTotal(); } } } OTLastTick = OTCurrentTick; OTCurrentTick = OpenOrders; if(OTCurrentTick == 0 && OTLastTick > 0) { BuyTrigger = Bid + OpenLevel * pips2dbl; SellTrigger = Bid - OpenLevel * pips2dbl; } if(OTCurrentTick > 0)MinimumProfitIni(); if(OTCurrentTick == 0) { //---- Buy Order Condition to Open if(Bid > BuyTrigger) {//5 RefreshRates(); BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage*pips2points,0,0,"Initial Buy Order",MagicNumber,0,Green); if(BuyTicket > -1) {//6 PlaySound("alert.wav"); Print(" Initial Buy Order Placed - BuyTicket: ",BuyTicket," Open Buy Price: ",DoubleToStr(OrderOpenPrice(),Digits)," Spread: ",DoubleToStr(Spread,Digits)); }//6 else {//7 GetLastError(); }//7 return(0); }//5 //---- Sell Order Condition to Open if(Bid < SellTrigger) {//8 RefreshRates(); SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage*pips2points,0,0,"Initial Sell Order",MagicNumber,0,Red); if(SellTicket > -1) {//9 PlaySound("alert.wav"); Print(" Initial Sell order Placed: ", SellTicket," Open Sell Price: ",DoubleToStr(OrderOpenPrice(),Digits)," Spread: ",DoubleToStr(Spread,Digits)); }//9 else {//10 GetLastError(); }//10 }//8 }// string SettingsComment = " Instant Profit: " +TotalProfit+ " Acumulated Profit: " +Profit+ " Multiplier Factor: " +Multiplier+ " MinProfit: " +MinProfit+ " Spread: "+DoubleToStr(Spread,Digits)+" Account Equity: " +AccountEquity()+" Profit to Protect: " +ProfitToProtect+" Get Out Level: " +GetOutLevel; Comment(SettingsComment); return(0); }//0 //---- Trail Profit void MinimumProfitIni() //---- {//0 TotalProfit = 0.0; for(int Orders = OrdersTotal()-1; Orders >= 0; Orders--) {//1 if(!OrderSelect(Orders,SELECT_BY_POS,MODE_TRADES))continue; {//2 if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber) {//3 TotalProfit += OrderProfit() + OrderCommission() + OrderSwap(); }//3 }//2 }//1 if (TotalProfit > Profit) { Profit = TotalProfit; if(Profit > MinProfit) { GetOutLevel = Profit - ProfitToProtect; ProfitLevelSet = true; } } if(ProfitLevelSet && TotalProfit < GetOutLevel) { CloseOpen();PlaySound("stops.wav"); } //---- }//0 //------------------------------------------------------------------------------------------------------------------------------------+ void CloseOpen() //---- {//0 int OrdType; for(int OrderPos = OrdersTotal()-1; OrderPos >= 0; OrderPos--) if(OrderSelect(OrderPos, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber()== MagicNumber && OrderSymbol()== Symbol()) {//1 OrdType = OrderType(); if(OrdType == OP_BUY || OrdType==OP_SELL) {//2 RefreshRates(); if(!OrderClose(OrderTicket(), OrderLots(),NormalizeDouble (OrderClosePrice(),Digits),Slippage * pips2points, Yellow)) {//3 GetLastError(); }//3 }//2 }//1 Profit = 0.0; GetOutLevel = 0.0; TotalProfit = 0.0; ProfitLevelSet = false; //---- }//0 //-----------------------------------------------------------------------------+ void CloseOpenBuy() //---- {//0 int OrdType; for(int OrderPos = OrdersTotal()-1; OrderPos >= 0; OrderPos--) if(OrderSelect(OrderPos, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber()== MagicNumber && OrderSymbol()== Symbol()) {//1 OrdType = OrderType(); if(OrdType == OP_BUY && OrderOpenPrice() - Bid >= CloseLevel * pips2dbl) {//2 RefreshRates(); if(!OrderClose(OrderTicket(), OrderLots(),NormalizeDouble (OrderClosePrice(),Digits),Slippage * pips2points, Yellow)) {//3 GetLastError(); }//3 }//2 }//1 //---- }//0 //-----------------------------------------------------------------------------+ void CloseOpenSell() //---- {//0 int OrdType; for(int OrderPos = OrdersTotal()-1; OrderPos >= 0; OrderPos--) if(OrderSelect(OrderPos, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber()== MagicNumber && OrderSymbol()== Symbol()) {//1 OrdType = OrderType(); if(OrdType == OP_SELL && Ask - OrderOpenPrice() >= CloseLevel * pips2dbl) {//2 RefreshRates(); if(!OrderClose(OrderTicket(), OrderLots(),NormalizeDouble (OrderClosePrice(),Digits),Slippage * pips2points, Yellow)) {//3 GetLastError(); }//3 }//2 }//1 //---- }//0 //-----------------------------------------------------------------------------+