Fonseca_MrPip

Author: Anamy
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself It can change open orders parameters, due to possible stepping strategy
Indicators Used
Moving average indicatorRelative strength index
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Fonseca_MrPip
//+------------------------------------------------------------------+
//|                                           Fonseca - Version 1.00 |
//+------------------------------------------------------------------+
//|                                              Programming:  Anamy |
//|                                        Trade Strategy:  JFonseca |
//|                               http://www.fxfisherman.com/forums/ |
//|21/11/07 - modified by k3iroll to add MM option                   |
//|22/11/07 - modified by MrPip to add mmLots                        |
//+------------------------------------------------------------------+
#property copyright "Anamy"
#property link      "www.fxfisherman.com/forums/"

//---- input parameters
extern string    str1 = " == Position Details == ";
extern double    Lots=0.1;
extern int       TakeProfit=1000;
extern int       Stoploss=1000;
extern int       TrailingStop=40;
extern bool      TrailingStopOnlyProfit=false;
extern bool      TrailingStopRegular=true;
extern double    MaximumRisk             = 0.05;     //
extern int       DecreaseFactor          = 0;        //
extern bool      MM                      = true;     //Money management
extern string    str2 = " == Indicators == ";
extern double    emaQUICKperiods=5;
extern double    emaMEDperiods=35;
extern double    emaSLOWperiods=50;
extern double    rsiPeriods=35;


//---- global variables
string commentString = "Fonseca_MrPip";
int magicNum = 44758;
double mmLots;

//+------------------------------------------------------------------+
//| initialization and deinitialization functions                    |
//+------------------------------------------------------------------+
int init(){ 
   return(0); 
}

int deinit(){ 
   return(0); 
}

//+------------------------------------------------------------------+
//| expert start fucntion                                            |
//+------------------------------------------------------------------+
int start(){

//+------------------------------------------------------------------+
   
   int OrdersPerSymbol = 0;
   double wmaQuick=iMA(NULL,0,emaQUICKperiods,0,MODE_LWMA,PRICE_CLOSE,0);
   double wmaMed=iMA(NULL,0,emaMEDperiods,0,MODE_LWMA,PRICE_CLOSE,0);
   double wmaSlow=iMA(NULL,0,emaSLOWperiods,0,MODE_LWMA,PRICE_CLOSE,0);
   double rsiInd=iRSI(NULL,0,rsiPeriods,PRICE_CLOSE,0);

//+------------------------------------------------------------------+
   if(MM==false){
      mmLots=Lots; 
   }    

   int decimalPlaces = 1;

   if (MM==true){
      mmLots=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,decimalPlaces);
   }

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

   bool DamianiSaysGo=false; 
   
   //double damiani_in = iCustom(NULL,0,"Damiani_volatmeter_fonseca",13,50,1.3,true,3,0);
   double damiani_in = iCustom(NULL,0,"Damiani_volatmeter_fonseca",10,60,1.4,true,3,0);
     
   if (damiani_in > 0){
      DamianiSaysGo = true;
      Print ("Indicator Damiani_volatmeter_fonseca advises to trade.");
   } 
   else if (damiani_in < 0){
      Print("Indicator Damiani_volatmeter_fonseca advises not to trade.");
   } 
   else {
      Print("Error:  damiani_in has received no value.");
   }

//+------------------------------------------------------------------+
 
   if (TrailingStopOnlyProfit == true && TrailingStopRegular == true){
      Alert("Both types of Trailing Stops are enabled; only one is allowed.");
      return(0);
   }

   for (int cnt=OrdersTotal();cnt>=0;cnt--){
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      
      if(OrderSymbol()==Symbol())
           OrdersPerSymbol++; 
   }
    
   if (OrdersPerSymbol > 0){
      if (TrailingStopRegular == true)
          regularTrailingStop();
      
      if (TrailingStopOnlyProfit == true)
          onlyProfitTrailingStop();
   }

   if (OrdersPerSymbol > 0){
      if (rsiInd<50 && wmaQuick<wmaMed && Ask<wmaQuick)
          closeBuyOrder();
          
      if (rsiInd>50 && wmaQuick>wmaMed && Ask>wmaQuick)
          closeSellOrder();
   }

   if (OrdersPerSymbol < 1) 
   {
      if (rsiInd>51 && wmaQuick>wmaSlow && Ask>wmaQuick && DamianiSaysGo==true)
         sendBuyOrder();
         
      if (rsiInd<49 && wmaQuick<wmaSlow && Bid<wmaQuick && DamianiSaysGo==true)
         sendSellOrder();
   }

//+------------------------------------------------------------------+
  
}  

//+------------------------------------------------------------------+
//| modules and functions                                            |
//+------------------------------------------------------------------+

void sendBuyOrder(){
   int ticket=OrderSend(Symbol(),OP_BUY,mmLots,Ask,3,Bid-Stoploss*Point,Ask+TakeProfit*Point,commentString + Period(),magicNum,0,Green);
      
   if(ticket>0){
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
         Print("BUY order opened : ",OrderOpenPrice());
   }
   else 
      Print("Error opening BUY order : ",GetLastError()); 
}

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

void sendSellOrder()
{
   int ticket=OrderSend(Symbol(),OP_SELL,mmLots,Bid,3,Ask+Stoploss*Point,Bid-TakeProfit*Point,commentString + Period(),magicNum,0,Red);
   if(ticket>0){
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
         Print("SELL order opened : ",OrderOpenPrice()); 
   }
   else 
      Print("Error opening SELL order : ",GetLastError()); 
}

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

void closeBuyOrder()
{
   int cnt, total = OrdersTotal();

   for(cnt=0;cnt<total;cnt++){
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()){
         if(OrderType()==OP_BUY){
            OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE); 
         }  
      }  
   }
}

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

void closeSellOrder()
   {
   
   int cnt, total = OrdersTotal();

   for(cnt=0;cnt<total;cnt++){
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()){
         if(OrderType()==OP_SELL){
            OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE); 
         }  
      }  
   }
}

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

void onlyProfitTrailingStop()
   {
   
   int cnt, total = OrdersTotal();

   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
        {if(OrderType()==OP_BUY)
           {if(TrailingStop>0)  
              {if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {if(OrderStopLoss()<Bid-Point*TrailingStop)
                    { OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green); 
                        }}}}
          else
           {if(TrailingStop>0)  
              {if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    { OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red); 
                        }}}}}}}
   
//+------------------------------------------------------------------+

void regularTrailingStop()
{
   int cnt, total = OrdersTotal();
   
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
        {if(OrderType()==OP_BUY)
           {if(TrailingStop>0)  
              {if(OrderStopLoss()<Bid-Point*TrailingStop)
                 { OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green); 
                 }}}
         else
           {if(TrailingStop>0)  
              {if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                 { OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red); 
                 }}}}}
     }

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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---