Author: Copyright � 2006, Forex-TSD.com
Price Data Components
Series array that contains close prices for each bar
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reached
Indicators Used
Parabolic Stop and Reverse system
0 Views
0 Downloads
0 Favorites
SARKAMA
//+------------------------------------------------------------------+
//|                                                   SARKAMA_v1.mq4 |
//|                                  Copyright © 2006, Forex-TSD.com |
//|                         Written by IgorAD,igorad2003@yahoo.co.uk |   
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |                                      
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Forex-TSD.com "
#property link      "http://www.forex-tsd.com/"


//---- input parameters
extern string     Expert_Name    = "---- SARKAMA_v1 ----";

extern int        Magic          = 10000;
extern int        Slippage       =     6;

extern string     Main_data      = " Trade Volume & Trade Method";
extern double     Lots           =   0.1;
extern double     TakeProfit     =     0;    // Take Profit Value 
extern double     InitialStop    =   150;    // Initial Stop Value
extern double     TrailingStop   =    20;    // Trailing Stop Value   

extern string     SAR_inputs     = " Parabolic SAR parameters ";
extern int        MainTimeFrame  =  1440;    // Large Time Frame in min
extern double     Step           =  0.05;    // 
extern double     Maximum        =  0.05;    // 
extern int        BarsBack       =     1;    // 

extern string     KAMA_inputs    = " Kaufman AMA parameters ";
extern int        periodAMA      =     9;
extern int        nfast          =     2;
extern int        nslow          =    30;
extern double     G              =   2.0;
extern double     dK             =   2.0;

extern string     MM_inputs      = " MoneyManagement by L.Williams ";
extern bool       MM             = false;    // ÌÌ Switch
extern double     MMRisk         =  0.15;    // Risk Factor
extern double     MaxLoss        =  1000;    // Maximum Loss by 1 Lot

int    MainTrend=0, Trend=0, MainTrend1=0, Trend1=0, cnt=0, total =0, ticket=0;
double MainSAR, MainClose, SAR, UpKAMA,DnKAMA,UpKAMA1,DnKAMA1, 
       SellStop,BuyStop,SellProfit,BuyProfit;
bool   BuySignal = false, SellSignal = false;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
   
//----
   return(0);
  }

// ---- Scan Trades
int ScanTrades()
{   
   int total = OrdersTotal();
   int numords = 0;
      
   for(int cnt=0; cnt<total; cnt++) 
   {        
   OrderSelect(cnt, SELECT_BY_POS);            
   if(OrderSymbol() == Symbol() && OrderType()<=OP_SELLSTOP && OrderMagicNumber() == Magic) 
   numords++;
   }
   return(numords);
}  

void MainSAR()
{   
   MainSAR   = iSAR(NULL,MainTimeFrame,Step,Maximum,0); 
   MainClose = iClose(NULL,MainTimeFrame,0);
   MainTrend = MainTrend1;
   if ( MainSAR < MainClose ) MainTrend = 1;
   else if( MainSAR > MainClose ) MainTrend = -1;  
   MainTrend1 = MainTrend; 
}

void SAR()
{
   SAR   = iSAR(NULL,0,Step,Maximum,BarsBack); 
   Trend = Trend1;
   if ( SAR < Close[BarsBack] ) Trend = 1;
   else if( SAR > Close[BarsBack] ) Trend = -1;   
   Trend1= Trend; 
}
  



void TradeSignal()
{         

   UpKAMA  = iCustom(NULL,0,"Kaufman", periodAMA, nfast, nslow, G, dK, 1,BarsBack); 
   DnKAMA  = iCustom(NULL,0,"Kaufman", periodAMA, nfast, nslow, G, dK, 2,BarsBack);   
   UpKAMA1 = iCustom(NULL,0,"Kaufman", periodAMA, nfast, nslow, G, dK, 1,BarsBack+1); 
   DnKAMA1 = iCustom(NULL,0,"Kaufman", periodAMA, nfast, nslow, G, dK, 2,BarsBack+1);   
   //Print ( " Kama=",UpKAMA," KamaD=",DnKAMA);
   BuySignal = (  
               UpKAMA > 0
               &&
               MainTrend > 0 
               &&
               Trend > 0
               && 
               UpKAMA1 == NULL   
               );
  
   SellSignal= (  
               DnKAMA > 0
               &&
               MainTrend < 0
               &&
               Trend < 0
               && 
               DnKAMA1 == NULL
               );
} 


double MoneyManagement ( bool flag, double Lots, double risk, double maxloss)
{
   double Lotsi=Lots;
	    
   if ( flag ) Lotsi=Lots*NormalizeDouble(Lots*AccountFreeMargin()*MMRisk/MaxLoss,1);   
     
   if (Lotsi<0.1) Lotsi=0.1;  
   return(Lotsi);
}   


void TrailStop()
{
   for (cnt=0;cnt<OrdersTotal();cnt++)
   { 
   OrderSelect(cnt, SELECT_BY_POS);   
   int mode=OrderType();    
      if ( OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) 
      {
         if (mode==OP_BUY) 
         {
			BuyStop = Bid - TrailingStop*Point;
			   if( OrderOpenPrice() < BuyStop || OrderStopLoss() == 0 ) 
            {   
			      if ( BuyStop > OrderStopLoss() ) 
			      {
			      bool result = OrderModify(OrderTicket(),OrderOpenPrice(),
			                                NormalizeDouble(BuyStop, Digits),
			                                OrderTakeProfit(),0,LightGreen);
			         if( !result )
                  {
                  Print("BUY: OrderModify failed with error #",GetLastError());
                  }
			      return(0);
               }            
            }
         }   
// - SELL Orders          
         if (mode==OP_SELL)
         {
         SellStop = Ask + Point * TrailingStop;   
            if( OrderOpenPrice() > SellStop) 
            {
               if( OrderStopLoss() > SellStop || OrderStopLoss() == 0 ) 
               {
               OrderModify(OrderTicket(), OrderOpenPrice(),
                           NormalizeDouble(SellStop, Digits),
			                  OrderTakeProfit(),0,DarkOrange);
                  if( !result )
                  {
                  Print("SELL: OrderModify failed with error #",GetLastError());
                  }
               return(0);
               }   
   			}	    
         }
      }
   }     
}

// ---- Open Sell Orders
void SellOrdOpen()
{		     
   double SellPrice = Bid;
   double StopPrice = Ask;
   int    Mode = OP_SELL;
        	  
	  if (InitialStop > 0) SellStop  = StopPrice + InitialStop*Point; 
	                       else SellStop = 0;
     if (TakeProfit  > 0) SellProfit = SellPrice - TakeProfit*Point; 
                          else SellProfit=0;
	ticket = OrderSend( Symbol(),Mode,MoneyManagement ( MM, Lots, MMRisk, MaxLoss),
	                    NormalizeDouble(SellPrice , Digits),
	                    Slippage,
	                    NormalizeDouble(SellStop  , Digits),
	                    NormalizeDouble(SellProfit, Digits),
	                    "sell",Magic,0,Red);
      if(ticket<0)
      {
      Print("SELL: OrderSend failed with error #",GetLastError());
      }
   return(0);
}
// ---- Open Buy Orders
void BuyOrdOpen()
{		     
   double BuyPrice = Ask;
   double StopPrice = Bid;
   int    Mode     = OP_BUY;
      
      if (InitialStop > 0) BuyStop  = StopPrice - InitialStop*Point; 
		                     else BuyStop = 0;
      if (TakeProfit  > 0) BuyProfit= BuyPrice + TakeProfit*Point;  
                           else BuyProfit=0;  
		 
	ticket = OrderSend(Symbol(),Mode, MoneyManagement ( MM, Lots, MMRisk, MaxLoss),
	                   NormalizeDouble(BuyPrice , Digits),
	                   Slippage,
	                   NormalizeDouble(BuyStop  , Digits), 
	                   NormalizeDouble(BuyProfit, Digits),
	                   "buy",Magic,0,Blue);
      if(ticket<0)
      {
      Print("BUY : OrderSend failed with error #",GetLastError());
      }
   return(0);
} 

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

  MainSAR();
  SAR();
  TradeSignal();
  if ( TrailingStop > 0 ) TrailStop();
   
  if (ScanTrades()<1 && BuySignal ) BuyOrdOpen() ;
  if (ScanTrades()<1 && SellSignal) SellOrdOpen();
  
//----
   return(0);
} //start()
//+------------------------------------------------------------------+

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 ---