Navigation´╝ÜHome > Content >

vForce1.mq4

Time: 2015-07-10 | Download file:vForce1.mq4

//+------------------------------------------------------------------+
#define SIGNAL_NONE 0
#define SIGNAL_BUY   1
#define SIGNAL_SELL  2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4

#property copyright "Ronald Raygun"

extern string Remark1 = "== Main Settings ==";
extern int MagicNumber = 0;
extern bool SignalsOnly = False;
extern bool Alerts = False;
extern bool SignalMail = False;
extern bool PlaySounds = False;
extern bool EachTickMode = True;
extern string Remark2 = "";
extern string Remark3 = "== Trade #1 Settings ==";
extern double Lots = 0;
extern bool MoneyManagement = False;
extern int Risk = 0;
extern int Slippage = 5;
extern  bool UseStopLoss = True;
extern bool OverRideStopLoss = False;
extern int StopLoss = 100;
extern bool UseTakeProfit = True;
extern int TakeProfit = 200;
extern bool UseTrailingStop = True;
extern int TrailingStop = 500;
extern bool MoveStopOnce = False;
extern int MoveStopWhenPrice = 50;
extern int MoveStopTo = 1;
extern string Remark4 = "";
extern string Remark5 = "== Trade #2 Settings ==";
extern double Lots1 = 0;
extern bool MoneyManagement1 = False;
extern int Risk1 = 0;
extern int Slippage1 = 5;
extern  bool UseStopLoss1 = True;
extern bool OverRideStopLoss1 = False;
extern int StopLoss1 = 100;
extern bool UseTakeProfit1 = False;
extern int TakeProfit1 = 200;
extern bool UseTrailingStop1 = True;
extern int TrailingStop1 = 500;
extern bool MoveStopOnce1 = False;
extern int MoveStopWhenPrice1 = 50;
extern int MoveStopTo1 = 1;



//Version 2.01

int OpenBarCount;
int CloseBarCount;

int RemainingBarCount;

datetime LastSignalBarUsed;

int Current;
bool TickCheck = False;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init() {
   OpenBarCount = Bars;
   CloseBarCount = Bars;
   


   if (EachTickMode) Current = 0; else Current = 1;

   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit() {

ObjectDelete("EntryLevel");
ObjectDelete("SignalCandle");
ObjectDelete("StopLevel");


   return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() 


{
   int Order = SIGNAL_NONE;
   int Total, Ticket;
   double StopLossLevel, TakeProfitLevel;
   double StopLossLevel1, TakeProfitLevel1;



   if (EachTickMode && Bars != CloseBarCount) TickCheck = False;
   Total = OrdersTotal();
   Order = SIGNAL_NONE;

//Money Management sequence
 if (MoneyManagement)
   {
      if (Risk<1 || Risk>100)
      {
         Comment("Invalid Risk Value.");
         return(0);
      }
      else
      {
         Lots=MathFloor((AccountFreeMargin()*AccountLeverage()*Risk*Point*100)/(Ask*MarketInfo(Symbol(),MODE_LOTSIZE)*MarketInfo(Symbol(),MODE_MINLOT)))*MarketInfo(Symbol(),MODE_MINLOT);
      }
   }

 if (MoneyManagement1)
   {
      if (Risk1<1 || Risk1>100)
      {
         Comment("Invalid Risk Value.");
         return(0);
      }
      else
      {
         Lots1=MathFloor((AccountFreeMargin()*AccountLeverage()*Risk1*Point*100)/(Ask*MarketInfo(Symbol(),MODE_LOTSIZE)*MarketInfo(Symbol(),MODE_MINLOT)))*MarketInfo(Symbol(),MODE_MINLOT);
      }
   }
   //+------------------------------------------------------------------+
   //| Variable Begin                                                   |
   //+------------------------------------------------------------------+



int LastSignalBar = 0;
int BarUsed = 0;
string LastBarFound = "False";

while (LastBarFound == "False")
   {
   if(iCustom(NULL,0,"vForceAlerts",0, BarUsed) != 0 || iCustom(NULL,0,"vForceAlerts",1, BarUsed) != 0)
      {
      LastSignalBar = BarUsed;
      LastBarFound = "True";
      }
      else
      {
      BarUsed++;
      }
   }

if(LastSignalBarUsed != LastSignalBar) 
   {
   LastSignalBarUsed = LastSignalBar;
   RemainingBarCount = 1;
   }

double EntryPrice = 0;
if(iCustom(NULL,0,"vForceAlerts",0, LastSignalBar) != 0) EntryPrice = iHigh(NULL, 0, LastSignalBar + 1) + 0.0012;
if(iCustom(NULL,0,"vForceAlerts",1, LastSignalBar) != 0) EntryPrice = iLow(NULL, 0, LastSignalBar + 1) - 0.0010;

double StopLevel = 0;
if(iCustom(NULL,0,"vForceAlerts",0, LastSignalBar) != 0) StopLevel = iLow(NULL, 0, LastSignalBar + 1) - 0.0010;
if(iCustom(NULL,0,"vForceAlerts",1, LastSignalBar) != 0) StopLevel = iHigh(NULL, 0, LastSignalBar + 1) + 0.0010;


   
string TradeTrigger = "None";
if(RemainingBarCount > 0 && Open[0] < EntryPrice && Close[0] >= EntryPrice && LastSignalBar <= 4) TradeTrigger = "Open Long";
if(RemainingBarCount > 0 && Open[0] > EntryPrice && Close[0] <= EntryPrice && LastSignalBar <= 4) TradeTrigger = "Open Short";

Comment("Last Signal Bar: ", LastSignalBar, " bars ago.\n",
        "Entry Price: ", EntryPrice, "\n",
        "Stop Loss Level: ", StopLevel, "\n",
        "Trade Trigger: ", TradeTrigger, "\n");
        
/*
ObjectDelete("EntryLevel");
ObjectCreate("EntryLevel", OBJ_HLINE, 0, 0, EntryPrice);
ObjectSet("EntryLevel", OBJPROP_STYLE, STYLE_DASH);
ObjectSet("EntryLevel", OBJPROP_COLOR, Aqua);

ObjectDelete("StopLevel");
ObjectCreate("StopLevel", OBJ_HLINE, 0, 0, StopLevel);
ObjectSet("StopLevel", OBJPROP_STYLE, STYLE_DASH);
ObjectSet("StopLevel", OBJPROP_COLOR, Orange);

ObjectDelete("SignalCandle");
ObjectCreate("SignalCandle", OBJ_VLINE, 0, Time[LastSignalBar + 1], 0);
ObjectSet("SignalCandle", OBJPROP_STYLE, STYLE_DASH);
ObjectSet("SignalCandle", OBJPROP_COLOR, Orange);
*/


        
   //+------------------------------------------------------------------+
   //| Variable End                                                     |
   //+------------------------------------------------------------------+

   //Check position
   bool IsTrade = False;

   for (int i = 0; i < Total; i ++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderType() <= OP_SELL &&  OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && (OrderComment() == "Buy(#" + MagicNumber + ") Trade #1" || OrderComment() == "Sell(#" + MagicNumber + ") Trade #1")) {
         IsTrade = True;
         if(OrderType() == OP_BUY) {
         
            
            //Close

            //+------------------------------------------------------------------+
            //| Signal Begin(Exit Buy)                                           |
            //+------------------------------------------------------------------+


  

            //+------------------------------------------------------------------+
            //| Signal End(Exit Buy)                                             |
            //+------------------------------------------------------------------+

            if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != CloseBarCount)))) {
               OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
               if (!EachTickMode) CloseBarCount = Bars;
               IsTrade = False;
               continue;
            }
            //MoveOnce
            if(MoveStopOnce && MoveStopWhenPrice > 0) {
               if(Bid - OrderOpenPrice() >= Point * MoveStopWhenPrice) {
                  if(OrderStopLoss() < OrderOpenPrice() + Point * MoveStopTo) {
                  OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() + Point * MoveStopTo, OrderTakeProfit(), 0, Red);
                     if (!EachTickMode) CloseBarCount = Bars;
                     continue;
                  }
               }
            }
            //Trailing stop
            if(UseTrailingStop && TrailingStop > 0) {                 
               if(Bid - OrderOpenPrice() > Point * TrailingStop) {
                  if(OrderStopLoss() < Bid - Point * TrailingStop) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
                     if (!EachTickMode) CloseBarCount = Bars;
                     continue;
                  }
               }
            }
         } else {
        
            //Close

            //+------------------------------------------------------------------+
            //| Signal Begin(Exit Sell)                                          |
            //+------------------------------------------------------------------+


            //+------------------------------------------------------------------+
            //| Signal End(Exit Sell)                                            |
            //+------------------------------------------------------------------+

            if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != CloseBarCount)))) {
               OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
               if (!EachTickMode) CloseBarCount = Bars;
               IsTrade = False;
               continue;
            }
            //MoveOnce
            if(MoveStopOnce && MoveStopWhenPrice > 0) {
               if(OrderOpenPrice() - Ask >= Point * MoveStopWhenPrice) {
                  if(OrderStopLoss() > OrderOpenPrice() - Point * MoveStopTo) {
                  OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() - Point * MoveStopTo, OrderTakeProfit(), 0, Red);
                     if (!EachTickMode) CloseBarCount = Bars;
                     continue;
                  }
               }
            }
            //Trailing stop
            if(UseTrailingStop && TrailingStop > 0) {                 
               if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
                  if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
                     if (!EachTickMode) CloseBarCount = Bars;
                     continue;
                  }
               }
            }
         }
      }
   }

  for (int i1 = 0; i1 < Total; i1 ++) {
      OrderSelect(i1, SELECT_BY_POS, MODE_TRADES);
      if(OrderType() <= OP_SELL &&  OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && (OrderComment() == "Buy(#" + MagicNumber + ") Trade #2" || OrderComment() == "Sell(#" + MagicNumber + ") Trade #2")) {
         IsTrade = True;
         if(OrderType() == OP_BUY) {
         
            
            //Close

            //+------------------------------------------------------------------+
            //| Signal Begin(Exit Buy)                                           |
            //+------------------------------------------------------------------+


  

            //+------------------------------------------------------------------+
            //| Signal End(Exit Buy)                                             |
            //+------------------------------------------------------------------+

            if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != CloseBarCount)))) {
               OrderClose(OrderTicket(), OrderLots(), Bid, Slippage1, MediumSeaGreen);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
               if (!EachTickMode) CloseBarCount = Bars;
               IsTrade = False;
               continue;
            }
            //MoveOnce
            if(MoveStopOnce1 && MoveStopWhenPrice1 > 0) {
               if(Bid - OrderOpenPrice() >= Point * MoveStopWhenPrice1) {
                  if(OrderStopLoss() < OrderOpenPrice() + Point * MoveStopTo1) {
                  OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() + Point * MoveStopTo1, OrderTakeProfit(), 0, Red);
                     if (!EachTickMode) CloseBarCount = Bars;
                     continue;
                  }
               }
            }
            //Trailing stop
            if(UseTrailingStop1 && TrailingStop1 > 0) {                 
               if(Bid - OrderOpenPrice() > Point * TrailingStop1) {
                  if(OrderStopLoss() < Bid - Point * TrailingStop1) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop1, OrderTakeProfit(), 0, MediumSeaGreen);
                     if (!EachTickMode) CloseBarCount = Bars;
                     continue;
                  }
               }
            }
         } else {
        
            //Close

            //+------------------------------------------------------------------+
            //| Signal Begin(Exit Sell)                                          |
            //+------------------------------------------------------------------+


            //+------------------------------------------------------------------+
            //| Signal End(Exit Sell)                                            |
            //+------------------------------------------------------------------+

            if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != CloseBarCount)))) {
               OrderClose(OrderTicket(), OrderLots(), Ask, Slippage1, DarkOrange);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
               if (!EachTickMode) CloseBarCount = Bars;
               IsTrade = False;
               continue;
            }
            //MoveOnce
            if(MoveStopOnce1 && MoveStopWhenPrice1 > 0) {
               if(OrderOpenPrice() - Ask >= Point * MoveStopWhenPrice1) {
                  if(OrderStopLoss() > OrderOpenPrice() - Point * MoveStopTo1) {
                  OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() - Point * MoveStopTo1, OrderTakeProfit(), 0, Red);
                     if (!EachTickMode) CloseBarCount = Bars;
                     continue;
                  }
               }
            }
            //Trailing stop
            if(UseTrailingStop1 && TrailingStop1 > 0) {                 
               if((OrderOpenPrice() - Ask) > (Point * TrailingStop1)) {
                  if((OrderStopLoss() > (Ask + Point * TrailingStop1)) || (OrderStopLoss() == 0)) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop1, OrderTakeProfit(), 0, DarkOrange);
                     if (!EachTickMode) CloseBarCount = Bars;
                     continue;
                  }
               }
            }
         }
      }
   }

   //+------------------------------------------------------------------+
   //| Signal Begin(Entry)                                              |
   //+------------------------------------------------------------------+

if(TradeTrigger == "Open Long") Order = SIGNAL_BUY;
if(TradeTrigger == "Open Short") Order = SIGNAL_SELL;

   //+------------------------------------------------------------------+
   //| Signal End                                                       |
   //+------------------------------------------------------------------+

   //Buy
   if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != OpenBarCount)))) {
      if(SignalsOnly) {
         if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
         if (Alerts) Alert("[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
         if (PlaySounds) PlaySound("alert.wav");
     
      }
      
      if(!IsTrade && !SignalsOnly) {
         //Check free margin
         if (AccountFreeMargin() < (1000 * Lots)) {
            Print("We have no money. Free Margin = ", AccountFreeMargin());
            return(0);
         }

         if (UseStopLoss && OverRideStopLoss) StopLossLevel = Ask - StopLoss * Point; 
         if (UseStopLoss && !OverRideStopLoss) StopLossLevel = StopLevel;
         if (!UseStopLoss) StopLossLevel = 0.0;
         if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;
         
         if (UseStopLoss1 && OverRideStopLoss1) StopLossLevel1 = Ask - StopLoss1 * Point; 
         if (UseStopLoss1 && !OverRideStopLoss1) StopLossLevel1 = StopLevel;
         if (!UseStopLoss1) StopLossLevel1 = 0.0;
         if (UseTakeProfit1) TakeProfitLevel1 = Ask + TakeProfit1 * Point; else TakeProfitLevel1 = 0.0;

         Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ") Trade #1", MagicNumber, 0, DodgerBlue);
         Ticket = OrderSend(Symbol(), OP_BUY, Lots1, Ask, Slippage1, StopLossLevel1, TakeProfitLevel1, "Buy(#" + MagicNumber + ") Trade #2", MagicNumber, 0, DodgerBlue);
         if(Ticket > 0) {
            if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
				Print("BUY order opened : ", OrderOpenPrice());
                if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
			       if (Alerts) Alert("[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
                if (PlaySounds) PlaySound("alert.wav");
                RemainingBarCount--;
			} else {
				Print("Error opening BUY order : ", GetLastError());
			}
         }
         if (EachTickMode) TickCheck = True;
         if (!EachTickMode) OpenBarCount = Bars;
         return(0);
      }
   }

   //Sell
   if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != OpenBarCount)))) {
      if(SignalsOnly) {
          if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
          if (Alerts) Alert("[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
          if (PlaySounds) PlaySound("alert.wav");
         }
      if(!IsTrade && !SignalsOnly) {
         //Check free margin
         if (AccountFreeMargin() < (1000 * Lots)) {
            Print("We have no money. Free Margin = ", AccountFreeMargin());
            return(0);
         }

         if (UseStopLoss && OverRideStopLoss) StopLossLevel = Bid + StopLoss * Point;
         if (UseStopLoss && !OverRideStopLoss) StopLossLevel = StopLevel;
         if (!UseStopLoss) StopLossLevel = 0.0;
         if (UseTakeProfit) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;
         
         if (UseStopLoss1 && OverRideStopLoss1) StopLossLevel1 = Bid + StopLoss1 * Point;
         if (UseStopLoss1 && !OverRideStopLoss1) StopLossLevel1 = StopLevel;
         if (!UseStopLoss1) StopLossLevel1 = 0.0;
         if (UseTakeProfit1) TakeProfitLevel1 = Bid - TakeProfit1 * Point; else TakeProfitLevel1 = 0.0;

         Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ") Trade #1", MagicNumber, 0, DeepPink);
         Ticket = OrderSend(Symbol(), OP_SELL, Lots1, Bid, Slippage1, StopLossLevel1, TakeProfitLevel1, "Sell(#" + MagicNumber + ") Trade #2", MagicNumber, 0, DeepPink);
         if(Ticket > 0) {
            if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
				Print("SELL order opened : ", OrderOpenPrice());
                if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
			       if (Alerts) Alert("[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
                if (PlaySounds) PlaySound("alert.wav");
                RemainingBarCount--;
			} else {
				Print("Error opening SELL order : ", GetLastError());
			}
         }
         if (EachTickMode) TickCheck = True;
         if (!EachTickMode) OpenBarCount = Bars;
         return(0);
      }
   }

   if (!EachTickMode) CloseBarCount = Bars;

   return(0);
}

Recommend