Jump to content

Once per candle


Recommended Posts

Re: Once per candle

 

try this one...

 

//+------------------------------------------------------------------+
//|                                       Straddle_Close-Open_v.2.mq4|
//+------------------------------------------------------------------+

#property copyright "Free"
#property link      "For free help contact: [email][email protected][/email]"
//-----------------
#include <stdlib.mqh>
#include <stderror.mqh>
#include <WinUser32.mqh> // Needed to MessageBox function
//-------------------------
extern int     Slippage=3;
extern int     TimeFrame = 0;//0= current, H1=60, H4=240, Daily=1440
//-------------------------
extern int     MagicNumber_1 = 5614061;
extern int     MagicNumber_2 = 5614062;
extern int     MagicNumber_3 = 5614063;
//-------------------------
extern double  Lots_1 = 0.03;
extern double  Lots_2 = 0.02;
extern double  Lots_3 = 0.01;
//-----------------------
extern  int    Pip_To_Buy = 5;  
extern  int    Pip_To_Sell = 5; 
//----------------------------------------
extern  int    Candle_Range = 40; 
//----------------------------------------
extern bool    Time_Control = false;
extern string  Start_Time = "00:00";
extern string  End_Time = "23:00";
//---------------------------
extern bool    Delete_All_Pendings = true;
extern bool    Close_All_Orders = true;
//--------------------------------------                              
extern int     StopLoss_Buy_1 = 30;         
extern int     StopLoss_Buy_2 = 30;         
extern int     StopLoss_Buy_3 = 30;         
//--------------------------------------                              
extern int     StopLoss_Sell_1 = 30;         
extern int     StopLoss_Sell_2 = 30;         
extern int     StopLoss_Sell_3 = 30;         
//--------------------------------------                              
extern int     TakeProfit_Buy_1 = 10;       
extern int     TakeProfit_Buy_2 = 20;       
extern int     TakeProfit_Buy_3 = 60;      
//--------------------------------------                              
extern int     TakeProfit_Sell_1 = 10;       
extern int     TakeProfit_Sell_2 = 20;       
extern int     TakeProfit_Sell_3 = 60;      
//----------------------------
extern bool    TrailingAlls_1 = true;
extern int     Trail_1 = 10;            
extern int     TrailStart_1 = 1;      
//----------------------------
extern bool    TrailingAlls_2 = true;
extern int     Trail_2 = 15;            
extern int     TrailStart_2 = 1;       
//----------------------------
extern bool    TrailingAlls_3 = true;
extern int     Trail_3 = 25;            
extern int     TrailStart_3 = 1;       
//--------------------------
double         Poin; //to fix the 6 Digit Forex Quotes issue for MetaTrader Expert Advisors
//----------------------------------
string         EA_Name = "Straddle";
//--------------------------------------
datetime       timeprev=0;
static datetime lastbar = 0;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+

int init()
{

  //Initialization to Check for unconventional Point digits number
  if (Point == 0.00001) Poin = 0.0001; //6 digits
  else if (Point == 0.001) Poin = 0.01; //3 digits (for Yen based pairs)
  else Poin = Point; //Normal for 5 & 3 Digit Forex Quotes
  lastbar = Time[0];//iTime(Symbol(),PERIOD_H4,0); //Time[0];

return(0);
}

//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+

int deinit()
{

return(0);
}

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

int start()
{

//--------------------------------------------------

  //---- exit if chart period is smaller than 1 hour
  if(Period() < 60 )
  {
  Alert("Error - Chart period is smaller than 1 hour. Use with H1, H4 or D1.");
  return(-1); // then exit
  }

//--------------------------------------------------

  //---- exit if chart period is greater than 1 day
  if(Period() > 1440)
  {
  Alert("Error - Chart period is greater than 1 day. Use with H1, H4 or D1");
  return(-1); // then exit
  }

//+------------------------------------------------------------------+
//| checking trade contexts                                          |
//+------------------------------------------------------------------+

  int TradeAllow = Is_Trade_Allowed();
  if(TradeAllow < 0) 
  { 
  return(-1); 
  }
  
  if(TradeAllow == 0)
  {
  RefreshRates();
  }

//----------------------------

  Comment(CountLongs()," Buy",", ",CountShorts()," Sell"," order(s)"," Active." );// Comment to the window corner.

//+------------------------------------------------------------------+
//| Function calls                                            |
//+------------------------------------------------------------------+

  if (TrailingAlls_1) Trailingalls_1(TrailStart_1, Trail_1); 

//------------------------------------------

  if (TrailingAlls_2) Trailingalls_2(TrailStart_2, Trail_2); 

//------------------------------------------

  if (TrailingAlls_3) Trailingalls_3(TrailStart_3, Trail_3); 

//------------------------------------------------------------------+
//| Working only at a new candle rather than at every tick                                            |
//+------------------------------------------------------------------+

  if(timeprev==Time[0])//Time[0] is time of the cuurent bar
  return(0);
  timeprev=Time[0];

//+------------------------------------------------------------------+
//| deleting unnecessary pending orders                                   |
//+------------------------------------------------------------------+

     if(Delete_All_Pendings== true)
     {
        DeletePendings();
     }

//+------------------------------------------------------------------+
//| Closing all open orders                                   |
//+------------------------------------------------------------------+

     if(Close_All_Orders== true)
     {
        CloseAll();
     }

//+------------------------------------------------------------------+
//| indicators signals                                 |
//+------------------------------------------------------------------+

  double a_Open = iOpen(NULL, TimeFrame, 0);
  double c_Open = iOpen(NULL, TimeFrame, 1);
  double c_Close = iClose(NULL, TimeFrame, 1);

  //double Spread = MarketInfo(Symbol(), MODE_SPREAD);
  
  //TakeProfit_Buy_1 = TakeProfit_Buy_1 + NormalizeDouble(Spread,Digits);       
  //TakeProfit_Buy_2 = TakeProfit_Buy_2 + NormalizeDouble(Spread,Digits);       
  //TakeProfit_Buy_3 = TakeProfit_Buy_3 + NormalizeDouble(Spread,Digits);      

///+------------------------------------------------------------------+
//|  Lots                                  |
//+------------------------------------------------------------------+

//------ get broker's min/max
  double Min_Lots = MarketInfo(Symbol(), MODE_MINLOT);
  double Max_Lots = MarketInfo(Symbol(), MODE_MAXLOT);
//------------------------------------
  if (Lots_1 < Min_Lots) Lots_1=Min_Lots;
  if (Lots_1 > Max_Lots) Lots_1=Max_Lots;

  if (Lots_2 < Min_Lots) Lots_2=Min_Lots;
  if (Lots_2 > Max_Lots) Lots_2=Max_Lots;

  if (Lots_3 < Min_Lots) Lots_3=Min_Lots;
  if (Lots_3 > Max_Lots) Lots_3=Max_Lots;

//+------------------------------------------------------------------+
//| placing trades                                   |
//+------------------------------------------------------------------+

     if(Time_Control == false || (Time_Control == true && 
     CurTime()>=StrToTime(Start_Time)&& CurTime()<=StrToTime(End_Time)))
     {

//-------------Placing Buystops - Candle Bullish --------------------
 
     if(c_Close > c_Open && NormalizeDouble(c_Close - c_Open,Digits) >= Candle_Range*Poin)
     if(a_Open < c_Close + Pip_To_Buy*Poin) 
     {
     OrderSend(Symbol(),OP_BUYSTOP,Lots_1,
     NormalizeDouble(c_Close,Digits) + Pip_To_Buy*Poin,
     Slippage,
     StopLong(NormalizeDouble(c_Close,Digits) + Pip_To_Buy*Poin,StopLoss_Buy_1),
     TakeLong(NormalizeDouble(c_Close,Digits) + Pip_To_Buy*Poin,TakeProfit_Buy_1),
     EA_Name,MagicNumber_1,0,Blue);
     
     OrderSend(Symbol(),OP_BUYSTOP,Lots_2,
     NormalizeDouble(c_Close,Digits) + Pip_To_Buy*Poin,
     Slippage,
     StopLong(NormalizeDouble(c_Close,Digits) + Pip_To_Buy*Poin,StopLoss_Buy_2),
     TakeLong(NormalizeDouble(c_Close,Digits) + Pip_To_Buy*Poin,TakeProfit_Buy_2),
     EA_Name,MagicNumber_2,0,Blue);
     
     OrderSend(Symbol(),OP_BUYSTOP,Lots_3,
     NormalizeDouble(c_Close,Digits) + Pip_To_Buy*Poin,
     Slippage,
     StopLong(NormalizeDouble(c_Close,Digits) + Pip_To_Buy*Poin,StopLoss_Buy_3),
     TakeLong(NormalizeDouble(c_Close,Digits) + Pip_To_Buy*Poin,TakeProfit_Buy_3),
     EA_Name,MagicNumber_3,0,Blue);
     
//-------------Placing Sellstops - Candle Bullish --------------------
      
     OrderSend(Symbol(),OP_SELLSTOP, Lots_1,
     NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,
     Slippage,
     StopShrt(NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,StopLoss_Sell_1),
     TakeShrt(NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,TakeProfit_Sell_1),
     EA_Name,MagicNumber_1,0,Red);

     OrderSend(Symbol(),OP_SELLSTOP, Lots_2,
     NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,
     Slippage,
     StopShrt(NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,StopLoss_Sell_2),
     TakeShrt(NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,TakeProfit_Sell_2),
     EA_Name,MagicNumber_2,0,Red);

     OrderSend(Symbol(),OP_SELLSTOP, Lots_3,
     NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,
     Slippage,
     StopShrt(NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,StopLoss_Sell_3),
     TakeShrt(NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,TakeProfit_Sell_3),
     EA_Name,MagicNumber_3,0,Red);
     }

//-------------Placing Buystops - Candle Bearish --------------------
 
     if(c_Close<c_Open && NormalizeDouble(c_Open - c_Close,Digits) >= Candle_Range*Poin)
     if(a_Open > c_Close - Pip_To_Sell*Poin) 
     {
     OrderSend(Symbol(),OP_BUYSTOP,Lots_1,
     NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,
     Slippage,
     StopLong(NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,StopLoss_Buy_1),
     TakeLong(NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,TakeProfit_Buy_1),
     EA_Name,MagicNumber_1,0,Blue);
     
     OrderSend(Symbol(),OP_BUYSTOP,Lots_2,
     NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,
     Slippage,
     StopLong(NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,StopLoss_Buy_2),
     TakeLong(NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,TakeProfit_Buy_2),
     EA_Name,MagicNumber_2,0,Blue);
     
     OrderSend(Symbol(),OP_BUYSTOP,Lots_3,
     NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,
     Slippage,
     StopLong(NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,StopLoss_Buy_3),
     TakeLong(NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,TakeProfit_Buy_3),
     EA_Name,MagicNumber_3,0,Blue);
     
//-------------Placing Sellstops - Candle Bearish --------------------
      
     OrderSend(Symbol(),OP_SELLSTOP, Lots_1,
     NormalizeDouble(c_Close,Digits) - Pip_To_Sell*Poin,
     Slippage,
     StopShrt(NormalizeDouble(c_Close,Digits) - Pip_To_Sell*Poin,StopLoss_Sell_1),
     TakeShrt(NormalizeDouble(c_Close,Digits) - Pip_To_Sell*Poin,TakeProfit_Sell_1),
     EA_Name,MagicNumber_1,0,Red);

     OrderSend(Symbol(),OP_SELLSTOP, Lots_2,
     NormalizeDouble(c_Close,Digits) - Pip_To_Sell*Poin,
     Slippage,
     StopShrt(NormalizeDouble(c_Close,Digits) - Pip_To_Sell*Poin,StopLoss_Sell_2),
     TakeShrt(NormalizeDouble(c_Close,Digits) - Pip_To_Sell*Poin,TakeProfit_Sell_2),
     EA_Name,MagicNumber_2,0,Red);

     OrderSend(Symbol(),OP_SELLSTOP, Lots_3,
     NormalizeDouble(c_Close,Digits) - Pip_To_Sell*Poin,
     Slippage,
     StopShrt(NormalizeDouble(c_Close,Digits) - Pip_To_Sell*Poin,StopLoss_Sell_3),
     TakeShrt(NormalizeDouble(c_Close,Digits) - Pip_To_Sell*Poin,TakeProfit_Sell_3),
     EA_Name,MagicNumber_3,0,Red);
     }

//-------------Placing Market Buys - Candle Bullish --------------------
 
     if(c_Close > c_Open && NormalizeDouble(c_Close - c_Open,Digits) >= Candle_Range*Poin)
     if(a_Open >= c_Close + Pip_To_Buy*Poin) 
     {
     OrderSend(Symbol(),OP_BUY,Lots_1,Ask,Slippage,StopLong(Bid,StopLoss_Buy_1),
     TakeLong(Bid,TakeProfit_Buy_1), EA_Name,MagicNumber_1,0,Blue);
     
     OrderSend(Symbol(),OP_BUY,Lots_2,Ask,Slippage,StopLong(Bid,StopLoss_Buy_2),
     TakeLong(Bid,TakeProfit_Buy_2), EA_Name,MagicNumber_2,0,Blue);
     
     OrderSend(Symbol(),OP_BUY,Lots_3,Ask,Slippage,StopLong(Bid,StopLoss_Buy_3),
     TakeLong(Bid,TakeProfit_Buy_3), EA_Name,MagicNumber_3,0,Blue);
     
//-------------Placing Sellstops - Candle Bullish --------------------
      
     OrderSend(Symbol(),OP_SELLSTOP, Lots_1,
     NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,
     Slippage,
     StopShrt(NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,StopLoss_Sell_1),
     TakeShrt(NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,TakeProfit_Sell_1),
     EA_Name,MagicNumber_1,0,Red);

     OrderSend(Symbol(),OP_SELLSTOP, Lots_2,
     NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,
     Slippage,
     StopShrt(NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,StopLoss_Sell_2),
     TakeShrt(NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,TakeProfit_Sell_2),
     EA_Name,MagicNumber_2,0,Red);

     OrderSend(Symbol(),OP_SELLSTOP, Lots_3,
     NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,
     Slippage,
     StopShrt(NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,StopLoss_Sell_3),
     TakeShrt(NormalizeDouble(c_Open,Digits) - Pip_To_Sell*Poin,TakeProfit_Sell_3),
     EA_Name,MagicNumber_3,0,Red);
     }

//-------------Placing Buystops - Candle Bearish --------------------
 
     if(c_Close<c_Open && NormalizeDouble(c_Open - c_Close,Digits) >= Candle_Range*Poin)
     if(a_Open <= c_Close - Pip_To_Sell*Poin) 
     {
     OrderSend(Symbol(),OP_BUYSTOP,Lots_1,
     NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,
     Slippage,
     StopLong(NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,StopLoss_Buy_1),
     TakeLong(NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,TakeProfit_Buy_1),
     EA_Name,MagicNumber_1,0,Blue);
     
     OrderSend(Symbol(),OP_BUYSTOP,Lots_2,
     NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,
     Slippage,
     StopLong(NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,StopLoss_Buy_2),
     TakeLong(NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,TakeProfit_Buy_2),
     EA_Name,MagicNumber_2,0,Blue);
     
     OrderSend(Symbol(),OP_BUYSTOP,Lots_3,
     NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,
     Slippage,
     StopLong(NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,StopLoss_Buy_3),
     TakeLong(NormalizeDouble(c_Open,Digits) + Pip_To_Buy*Poin,TakeProfit_Buy_3),
     EA_Name,MagicNumber_3,0,Blue);
     
//-------------Placing Market Sells - Candle Bearish --------------------
      
     OrderSend(Symbol(),OP_SELL, Lots_1,Bid,Slippage,StopShrt(Ask,StopLoss_Sell_1),
     TakeShrt(Ask,TakeProfit_Sell_1),EA_Name,MagicNumber_1,0,Red);

     OrderSend(Symbol(),OP_SELL, Lots_2,Bid,Slippage,StopShrt(Ask,StopLoss_Sell_2),
     TakeShrt(Ask,TakeProfit_Sell_2),EA_Name,MagicNumber_2,0,Red);

     OrderSend(Symbol(),OP_SELL, Lots_3,Bid,Slippage,StopShrt(Ask,StopLoss_Sell_3),
     TakeShrt(Ask,TakeProfit_Sell_3),EA_Name,MagicNumber_3,0,Red);
     }
     }

//---------------------------------
     
return(0);
} //End of Start function

//+------------------------------------------------------------------+
//| checking trade contexts                                          |
//+------------------------------------------------------------------+

int Is_Trade_Allowed(int MaxWaiting_sec = 30)
{
if(!IsTradeAllowed())
{
int StartWaitingTime = GetTickCount();
Print("Trade context is busy! Wait until it is free...");
while(true)
{
if(IsStopped()) 
{ 
Print("The expert was terminated by the user!"); 
return(-1); 
}
if(GetTickCount() - StartWaitingTime > MaxWaiting_sec * 1000)
{
Print("The waiting limit exceeded (" + MaxWaiting_sec + " ???.)!");
return(-2);
}
if(IsTradeAllowed())
{
Print("Trade context has become free!");
return(0);
}
}
}
else
{
return(1);
}
}

//+------------------------------------------------------------------+
//| modifying orders trailing stop 1                                  |
//+------------------------------------------------------------------+

void Trailingalls_1(int start,int stop)
{

if(stop==0)
 return;

int trade;
for(trade=OrdersTotal()-1;trade>=0;trade--)
{
 if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
  continue;

 if(OrderSymbol()!=Symbol())
  continue;
 
 if(OrderMagicNumber()!=MagicNumber_1)
  continue;
  
 if(TrailingAlls_1== false)
  continue;   

 if(start==0)
  continue;

 if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
  
 if(OrderSymbol()==Symbol())
 
 if(OrderMagicNumber()==MagicNumber_1)
 
 if(TrailingAlls_1== true)
  
 if(start>0)

 {
 if(OrderType()==OP_BUY)
 {
  double Long_profit=NormalizeDouble((Ask-OrderOpenPrice())/Poin,0);
  double Long_stopcal=NormalizeDouble(Ask,Digits)-(stop*Poin);
  double Long_stoptrade=OrderStopLoss();
  
  if(Long_profit<=start)
   continue;

  if(Long_stopcal<=Long_stoptrade)
   continue;
   
  if(Long_profit>start)

  
  if(Long_stoptrade==0||(Long_stoptrade!=0 && Long_stopcal>Long_stoptrade))
 {
     OrderModify(OrderTicket(),OrderOpenPrice(),Long_stopcal,OrderTakeProfit(),0,Blue);
     Print( "Buy order 1 trailing stop moved to : ", OrderStopLoss());
 }
 }
   
 if(OrderType()==OP_SELL)
 {
  double Short_profit=NormalizeDouble((OrderOpenPrice()-Bid)/Poin,0);
  double Short_stopcal=NormalizeDouble(Bid,Digits)+(stop*Poin);
  double Short_stoptrade=OrderStopLoss();
  
  if(Short_profit<=start)
   continue;
   
  if(Short_stopcal>=Short_stoptrade)
   continue;
   
  if(Short_profit>start)

  if(Short_stoptrade==0||(Short_stoptrade!=0 && Short_stopcal<Short_stoptrade))
 {
      OrderModify(OrderTicket(),OrderOpenPrice(),Short_stopcal,OrderTakeProfit(),0,Red);
      Print( "Sell order 1 trailing stop moved to : ", OrderStopLoss());
 }
 }
 
}
}
}

//+------------------------------------------------------------------+
//| modifying orders trailing stop 2                                  |
//+------------------------------------------------------------------+

void Trailingalls_2(int start,int stop)
{

if(stop==0)
 return;

int trade;
for(trade=OrdersTotal()-1;trade>=0;trade--)
{
 if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
  continue;

 if(OrderSymbol()!=Symbol())
  continue;
 
 if(OrderMagicNumber()!=MagicNumber_2)
  continue;
  
 if(TrailingAlls_2== false)
  continue;   

 if(start==0)
  continue;

 if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
  
 if(OrderSymbol()==Symbol())
 
 if(OrderMagicNumber()==MagicNumber_2)
 
 if(TrailingAlls_2== true)
  
 if(start>0)

 {
 if(OrderType()==OP_BUY)
 {
  double Long_profit=NormalizeDouble((Ask-OrderOpenPrice())/Poin,0);
  double Long_stopcal=NormalizeDouble(Ask,Digits)-(stop*Poin);
  double Long_stoptrade=OrderStopLoss();
  
  if(Long_profit<=start)
   continue;

  if(Long_stopcal<=Long_stoptrade)
   continue;
   
  if(Long_profit>start)

  
  if(Long_stoptrade==0||(Long_stoptrade!=0 && Long_stopcal>Long_stoptrade))
 {
     OrderModify(OrderTicket(),OrderOpenPrice(),Long_stopcal,OrderTakeProfit(),0,Blue);
     Print( "Buy order 2 trailing stop moved to : ", OrderStopLoss());
 }
 }
   
 if(OrderType()==OP_SELL)
 {
  double Short_profit=NormalizeDouble((OrderOpenPrice()-Bid)/Poin,0);
  double Short_stopcal=NormalizeDouble(Bid,Digits)+(stop*Poin);
  double Short_stoptrade=OrderStopLoss();
  
  if(Short_profit<=start)
   continue;
   
  if(Short_stopcal>=Short_stoptrade)
   continue;
   
  if(Short_profit>start)

  if(Short_stoptrade==0||(Short_stoptrade!=0 && Short_stopcal<Short_stoptrade))
 {
      OrderModify(OrderTicket(),OrderOpenPrice(),Short_stopcal,OrderTakeProfit(),0,Red);
      Print( "Sell order 2 trailing stop moved to : ", OrderStopLoss());
 }
 }
 
}
}
}

//+------------------------------------------------------------------+
//| modifying orders trailing stop 3                                  |
//+------------------------------------------------------------------+

void Trailingalls_3(int start,int stop)
{

if(stop==0)
 return;

int trade;
for(trade=OrdersTotal()-1;trade>=0;trade--)
{
 if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
  continue;

 if(OrderSymbol()!=Symbol())
  continue;
 
 if(OrderMagicNumber()!=MagicNumber_3)
  continue;
  
 if(TrailingAlls_3== false)
  continue;   

 if(start==0)
  continue;

 if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
  
 if(OrderSymbol()==Symbol())
 
 if(OrderMagicNumber()==MagicNumber_3)
 
 if(TrailingAlls_3== true)
  
 if(start>0)

 {
 if(OrderType()==OP_BUY)
 {
  double Long_profit=NormalizeDouble((Ask-OrderOpenPrice())/Poin,0);
  double Long_stopcal=NormalizeDouble(Ask,Digits)-(stop*Poin);
  double Long_stoptrade=OrderStopLoss();
  
  if(Long_profit<=start)
   continue;

  if(Long_stopcal<=Long_stoptrade)
   continue;
   
  if(Long_profit>start)

  
  if(Long_stoptrade==0||(Long_stoptrade!=0 && Long_stopcal>Long_stoptrade))
 {
     OrderModify(OrderTicket(),OrderOpenPrice(),Long_stopcal,OrderTakeProfit(),0,Blue);
     Print( "Buy order 3 trailing stop moved to : ", OrderStopLoss());
 }
 }
   
 if(OrderType()==OP_SELL)
 {
  double Short_profit=NormalizeDouble((OrderOpenPrice()-Bid)/Poin,0);
  double Short_stopcal=NormalizeDouble(Bid,Digits)+(stop*Poin);
  double Short_stoptrade=OrderStopLoss();
  
  if(Short_profit<=start)
   continue;
   
  if(Short_stopcal>=Short_stoptrade)
   continue;
   
  if(Short_profit>start)

  if(Short_stoptrade==0||(Short_stoptrade!=0 && Short_stopcal<Short_stoptrade))
 {
      OrderModify(OrderTicket(),OrderOpenPrice(),Short_stopcal,OrderTakeProfit(),0,Red);
      Print( "Sell order 3 trailing stop moved to : ", OrderStopLoss());
 }
 }
 
}
}
}

//+------------------------------------------------------------------+
//| calculating orders stoploss                                   |
//+------------------------------------------------------------------+

double StopLong(double price,int stop)
{
if(stop==0)
 return(0);
else
 return(price-(stop*Poin));
}

//+---------------------------------

double StopShrt(double price,int stop)
{
if(stop==0)
 return(0);
else
 return(price+(stop*Poin));
}

//+------------------------------------------------------------------+
//| calculating orders takeprofit                                   |
//+------------------------------------------------------------------+

double TakeLong(double price,int take)
{
if(take==0)
 return(0);
else
 return(price+(take*Poin));
}

//+--------------------------------

double TakeShrt(double price,int take)
{
if(take==0)
 return(0);
else
 return(price-(take*Poin));
}

//+------------------------------------------------------------------+
//| closing orders                                   |
//+------------------------------------------------------------------+

void CloseAll()
{
  int trade;
  for(trade=OrdersTotal()-1;trade>=0;trade--)
  {
  if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
  continue;

  if(OrderSymbol()!=Symbol())
  continue;
  
  OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

  if(OrderSymbol()==Symbol())

  if(OrderMagicNumber()==MagicNumber_1 || OrderMagicNumber()==MagicNumber_2 || OrderMagicNumber()==MagicNumber_3)
  
  if(OrderType()==OP_BUY)
  
     OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,SkyBlue);
  
  if(OrderType()==OP_SELL)
  
     OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Orange);
  
  }
}

//+------------------------------------------------------------------+
//| deleting unnecessary pending orders                                   |
//+------------------------------------------------------------------+

void DeletePendings()
{
     int total_1 = OrdersTotal()-1;

     for (int cnt_1 = total_1 ; cnt_1 >= 0 ; cnt_1--)
     {
     OrderSelect(cnt_1,SELECT_BY_POS,MODE_TRADES);
     if(OrderMagicNumber()==MagicNumber_1 || OrderMagicNumber()==MagicNumber_2 || OrderMagicNumber()==MagicNumber_3)
     if(OrderSymbol()==Symbol())
     if(OrderType()!=OP_BUY || OrderType()!=OP_SELL)
     {
     OrderDelete(OrderTicket());
     }
     }
}

//+------------------------------------------------------------------+
//| counting open orders                                   |
//+------------------------------------------------------------------+

int CountLongs()
{
  int count=0;
  int trade;
  for(trade=OrdersTotal()-1;trade>=0;trade--)
  {
  if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
  continue;

  if(OrderSymbol()!=Symbol())
  continue;
  
  OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
 
  if(OrderSymbol()==Symbol())

  if(OrderMagicNumber()==MagicNumber_1 || OrderMagicNumber()==MagicNumber_2 || OrderMagicNumber()==MagicNumber_3)
  
  if(OrderType()==OP_BUY)
  
     count++;
  
  }

return(count);
}

//--------------------------------

int CountShorts()
{
  int count=0;
  int trade;
  for(trade=OrdersTotal()-1;trade>=0;trade--)
  {
  if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
  continue;

  if(OrderSymbol()!=Symbol())
  continue;
  
  OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
 
  if(OrderSymbol()==Symbol())

  if(OrderMagicNumber()==MagicNumber_1 || OrderMagicNumber()==MagicNumber_2 || OrderMagicNumber()==MagicNumber_3)
  
  if(OrderType()==OP_SELL)
  
     count++;
  
  }

return(count);
}

//-----------------------------------------

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...