Sadukey_exp





//+------------------------------------------------------------------+
//|                                                  Sadukey_exp.mq4 |
//+------------------------------------------------------------------+

#property  copyright "Copyright © 2009, "
#property link      ""

#include <stdlib.mqh>

extern int TakeProfit = 300;
extern int StopLoss = 200;
extern int TrailingStop = 120;
extern int TrailingStep = 20;
extern bool CloseOppositePosition = false;

extern double Lots = 0.1;      // ëîò
extern int Slippage = 1;

extern string s______ = "Ïàðàìåòðû èíäèêàòîðà Stoch";
extern int       KPeriod     =  30;
extern int       Slowing     =  10;
extern int       DPeriod     =  10;
extern string note4 = "0=sma, 1=ema, 2=smma, 3=lwma";
extern int       MAMethod    =   0;
extern string note5 = "0=high/low, 1=close/close";
extern int       PriceField  =   1;
extern string note6 = "overbought level";
extern int       overBought  =  60;
extern string note7 = "oversold level";
extern int       overSold    =  40;


#define Buy 0
#define Sell 1
#define Unknown 2

#define Up 0
#define Dn 1

double spread;

int BuyMagic = 5124000;
int SellMagic = 51250000;

bool CanOpenBuy = true;
bool CanOpenSell = true;

int prevBars = -1;

int init()
{
	spread = Ask - Bid;

	CanOpenBuy = true;
	CanOpenSell = true;

	BuyMagic = 5124000;
	SellMagic = 5125000;

	prevBars = -1;
	
	RunIndicator();
	
	return(0);
}

int start()
{
	DoTrail();
	int curBars = iBars(Symbol(), Period());
	if (prevBars == -1)
	{
		prevBars = curBars;
	}
	if (curBars == prevBars)
	{
		return;
	}
	// ïîÿâèëñÿ íîâûé áàð
	prevBars = curBars;
	
	double Stoch = iCustom(Symbol(), 0, "Color Stochastic", "", KPeriod, Slowing, DPeriod, "", MAMethod, "", PriceField, "", overBought, "", overSold, 1, 1);
	
	double Filter1 = iCustom(Symbol(), 0, "Figure", 0, 1);
	double Filter2 = iCustom(Symbol(), 0, "Figure", 2, 1);
	double Filter3 = iCustom(Symbol(), 0, "Figure", 3, 1);
	
	int Direction;
	
	if(Filter1 > Filter3 && Filter2 > Filter3)
	{
		CanOpenSell = true;
		if(Filter1 > Filter2 && Filter2 > Filter3)
		{
			Direction = Up;
		}
		else
		{
			Direction = Unknown;
		}
	}
	else if(Filter1 < Filter3 && Filter2 < Filter3)
	{
		CanOpenBuy = true;
		if(Filter1 < Filter2 && Filter2 < Filter3)
		{
			Direction = Dn;
		}
		else
		{
			Direction = Unknown;
		}
	}
	else
	{
		Direction = Unknown;
	}

	if(CanOpenBuy && Stoch >= overBought && Direction == Up)
	{
		if(CloseOppositePosition)
		{
			CloseAllSell();
		}
		CanOpenBuy = false;
		OpenBuy();
	}

	if(CanOpenSell && Stoch <= overSold && Direction == Dn)
	{
		if(CloseOppositePosition)
		{
			CloseAllBuy();
		}
		CanOpenSell = false;
		OpenSell();
	}

	return(0);
}

// ôóíêöèÿ ïðîãîíÿåò èíäèêàòîð íà èñòîðèè äî òåêóùåãî ìîìåíòà äëÿ òîãî, ÷òîá óñòàíîâèòü òåêóùåå íàïðàâëåíèå èíäèêàòîðà è 
// óñòàíîâèòü ïðàâèëüíûå çíà÷åíèÿ ïåðåìåííûõ, îòâå÷àþùèõ çà òîðãîâëþ
void RunIndicator()
{
	int i;
	
	for(i = 100; i >= 1; i--)
	{
		double Stoch = iCustom(Symbol(), 0, "Color Stochastic", "", KPeriod, Slowing, DPeriod, "", MAMethod, "", PriceField, "", overBought, "", overSold, 1, i);
	
		double Filter1 = iCustom(Symbol(), 0, "Figure", 0, i);
		double Filter2 = iCustom(Symbol(), 0, "Figure", 2, i);
		double Filter3 = iCustom(Symbol(), 0, "Figure", 3, i);
	
		int Direction;
	
		if(Filter1 > Filter3 && Filter2 > Filter3)
		{
			CanOpenSell = true;
			if(Filter1 > Filter2 && Filter2 > Filter3)
			{
				Direction = Up;
			}
			else
			{
				Direction = Unknown;
			}
		}
		else if(Filter1 < Filter3 && Filter2 < Filter3)
		{
			CanOpenBuy = true;
			if(Filter1 < Filter2 && Filter2 < Filter3)
			{
				Direction = Dn;
			}
			else
			{
				Direction = Unknown;
			}
		}
		else
		{
			Direction = Unknown;
		}

		if(CanOpenBuy && Stoch >= overBought && Direction == Up)
		{
			CanOpenBuy = false;
		}

		if(CanOpenSell && Stoch <= overSold && Direction == Dn)
		{
			CanOpenSell = false;
		}
	}
}

void OpenBuy()
{
	int i;		
	int Orders = OrdersTotal();
	double BuyOpen;
	double BuyTP = 0;
	double BuySL = 0;
	
	RefreshRates();
	BuyOpen  = Ask;
	if(TakeProfit != 0)
		BuyTP = BuyOpen + TakeProfit*Point;
	
	if(StopLoss != 0)
	{
		BuySL = BuyOpen - StopLoss*Point;
	}

	//----------------------------------
	datetime begin = TimeCurrent();
	int ticket = OrderSend(Symbol(), OP_BUY, Lots, BuyOpen, Slippage, BuySL, BuyTP, "", BuyMagic, 0, Blue);
	
	int error;
	if (ticket==-1)
	{
		while(ticket == -1 && TimeCurrent() - begin < 20)
		{
			error = GetLastError();
			Print("Îøèáêà îòêðûòèÿ îðäåðà BUY. " + ErrorDescription(error));
			Sleep(100);
			Print("Ïîâòîð");
			RefreshRates();

			BuyOpen  = Ask;
			if(TakeProfit != 0)
				BuyTP = BuyOpen + TakeProfit*Point;
			if(StopLoss != 0)
			{
				BuySL = BuyOpen - StopLoss*Point;
			}
		    ticket = OrderSend(Symbol(), OP_BUY, Lots, BuyOpen, Slippage, BuySL, BuyTP, "", BuyMagic, 0, Blue);
		}
		if(ticket == -1)
		{
			error = GetLastError();
			Print("Îøèáêà îòêðûòèÿ îðäåðà BUY. " + ErrorDescription(error));
		}
	}
	
	if (ticket != -1)
	{
		Print("Îòêðûòî BUY íà " + Symbol() + " " + Period() + " îáúåìîì " + Lots + "!");
	}	
}

void OpenSell()
{
	int i;
	int Orders = OrdersTotal();
	double SellOpen;
	double SellTP = 0;
	double SellSL = 0;

	RefreshRates();
	
	SellOpen = Bid;
	if(TakeProfit != 0)
		SellTP = SellOpen - TakeProfit*Point;
	if(StopLoss != 0)
	{
		SellSL = SellOpen + StopLoss*Point;
	}
	
	datetime begin = TimeCurrent();
   int ticket = OrderSend(Symbol(), OP_SELL, Lots, SellOpen, Slippage, SellSL, SellTP, "", SellMagic, 0, Red);

	int error;
	if (ticket==-1)
	{
		//error=GetLastError();
		//Print("SELL error(",error,"): ",ErrorDescription(error));
		while(ticket == -1 && TimeCurrent() - begin < 20)
		{
			error = GetLastError();
			Print("Îøèáêà îòêðûòèÿ îðäåðà SELL. " + ErrorDescription(error));
			Sleep(100);
			Print("Ïîâòîð");
			RefreshRates();

			SellOpen = Bid;
			if(TakeProfit != 0)
				SellTP = SellOpen - TakeProfit*Point;
			if(StopLoss != 0)
			{
				SellSL = SellOpen + StopLoss*Point;
			}

		    ticket = OrderSend(Symbol(), OP_SELL, Lots, SellOpen, Slippage, SellSL, SellTP, "", SellMagic, 0, Red);
		}
		if(ticket == -1)
		{
			error = GetLastError();
			Print("Îøèáêà îòêðûòèÿ îðäåðà SELL. " + ErrorDescription(error));
		}
	}

    if (ticket != -1)
   	{
		Print("Îòêðûòî SELL íà " + Symbol() + " " + Period() + " îáúåìîì " + Lots + "!");
	}
}

void DoTrail()
{
	double TickSize = 0;
	if(Digits == 5)
	{
		TickSize = 0.00005;
	}
	else
	{
		TickSize = 0;
	}
	for (int i=0; i < OrdersTotal(); i++) 
	{
		OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

		if (OrderSymbol() == Symbol())
		{		  
			if(OrderType() == OP_BUY)
			{
				if(OrderMagicNumber() == BuyMagic)
				{
					double newstop = (Bid - MathMod(Bid, TickSize)) - (TrailingStop*Point - MathMod(TrailingStop*Point, TickSize));
					
					if(newstop > OrderOpenPrice() && (OrderOpenPrice() > OrderStopLoss() || OrderStopLoss() == 0))
					{
						if(!OrderModify(OrderTicket(), OrderOpenPrice(), newstop, OrderTakeProfit(), 0, Pink))
						{
							Print("Íå ïîëó÷èëîñü ìîäèôèöèðîâàòü îðäåð ¹" + OrderTicket() + ". Îøèáêà¹" + GetLastError());
						}
					}
					else if (OrderStopLoss() > OrderOpenPrice() && newstop - OrderStopLoss() >= TrailingStep*Point)
					{
						if(!OrderModify(OrderTicket(), OrderOpenPrice(), newstop, OrderTakeProfit(), 0, Pink))
						{
							Print("Íå ïîëó÷èëîñü ìîäèôèöèðîâàòü îðäåð ¹" + OrderTicket() + ". Îøèáêà¹" + GetLastError());
						}
					}
				}
			}
			else if (OrderType() == OP_SELL)
			{
				if(OrderMagicNumber() == SellMagic)
				{
					newstop = (Ask - MathMod(Ask, TickSize)) + (TrailingStop*Point - MathMod(TrailingStop*Point, TickSize));
					if(newstop < OrderOpenPrice() && (OrderStopLoss() > OrderOpenPrice() || OrderStopLoss() == 0))
					{
						if(!OrderModify(OrderTicket(), OrderOpenPrice(), newstop, OrderTakeProfit(), 0 ,Pink))
						{
							Print("Íå ïîëó÷èëîñü ìîäèôèöèðîâàòü îðäåð ¹" + OrderTicket() + ". Îøèáêà¹" + GetLastError());
						}
					}
					else if(OrderStopLoss() < OrderOpenPrice() && OrderStopLoss() - (newstop) >= TrailingStep*Point)
					{
						if(!OrderModify(OrderTicket(), OrderOpenPrice(), newstop, OrderTakeProfit(), 0 ,Pink))
						{
							Print("Íå ïîëó÷èëîñü ìîäèôèöèðîâàòü îðäåð ¹" + OrderTicket() + ". Îøèáêà¹" + GetLastError());
						}
					}
				}
			}
		}
	}
}

void CloseAllSell()
{
	int i;
	int orders = OrdersTotal();
	bool result;
	datetime begin;
	
	if(orders > 0)
	{
		for(i = orders-1; i >= 0; i--)
		{
			if( OrderSelect(i, SELECT_BY_POS, MODE_TRADES) )
			{
				if(OrderSymbol() == Symbol() && OrderType() == OP_SELL && OrderMagicNumber() == SellMagic)
				{
					begin = TimeCurrent();
					result = OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Yellow);			// çàêðûâàåì âñå âñòðå÷íûå îðäåðà
					while(!result && TimeCurrent() - begin <= 30 && !IsTesting())
					{
						Sleep(100);
						RefreshRates();
						result = OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Yellow);			// çàêðûâàåì âñå âñòðå÷íûå îðäåðà
					}
					if(!result)
					{
						int error = GetLastError();
						Print("Îøèáêà çàêðûòèÿ îðäåðà SELL #" + OrderTicket() + " " + ErrorDescription(error));
					}
				}
			}
			else
			{
				Print("Íå óäàëîñü âûáðàòü îòêðûòûé îðäåð:" + ErrorDescription(error));
			}
		}
	}
}

void CloseAllBuy()
{
	int i;
	int Orders = OrdersTotal();
	bool result;
	datetime begin;
	
	if(Orders > 0)
	{
		for(i = Orders-1; i >= 0; i--)
		{
			if( OrderSelect(i, SELECT_BY_POS, MODE_TRADES) )
			{
				if(OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderMagicNumber() == BuyMagic)
				{
					begin = TimeCurrent();
					result = OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Yellow);			// çàêðûâàåì âñå âñòðå÷íûå îðäåðà
					while(!result && TimeCurrent() - begin <= 30 && !IsTesting())
					{
						Sleep(100);
						RefreshRates();
						result = OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Yellow);			// çàêðûâàåì âñå âñòðå÷íûå îðäåðà
					}
					if(!result)
					{
						int error = GetLastError();
						Print("Îøèáêà çàêðûòèÿ îðäåðà BUY #" + OrderTicket() + " " + ErrorDescription(error));
					}
				}
			}
			else
			{
				Print("Íå óäàëîñü âûáðàòü îòêðûòûé îðäåð:" + ErrorDescription(error));
			}
		}
	}
}

int deinit()
{
	return(0);
}





Sample





Analysis



Market Information Used:



Indicator Curves created:


Indicators Used:




Custom Indicators Used:
Color Stochastic
Figure

Order Management characteristics:
Checks for the total of open orders
It automatically opens orders when conditions are reached

It can change open orders parameters, due to possible stepping strategy
It Closes Orders by itself

Other Features:

BackTest : EURUSD on H1

From 2009-08-01 to 2009-10-01 Profit Factor:1.15 Total Net Profit:5.90

BackTest : EURUSD on H1

From 2009-12-01 to 2010-01-17 Profit Factor:0.66 Total Net Profit:-27.02

BackTest : EURUSD on H1

From 2010-03-01 to 2010-03-27 Profit Factor:0.00 Total Net Profit:12.90

BackTest : EURUSD on H1

From 2010-04-01 to 2010-04-30 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-05-01 to 2010-05-31 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-06-01 to 2010-06-30 Profit Factor:0.00 Total Net Profit:0.00

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-02-27 Profit Factor:0.17 Total Net Profit:-33.55

BackTest : USDCAD on H1

From 2009-01-01 to 2010-01-01 Profit Factor:0.87 Total Net Profit:-45.55

BackTest : USDCAD on H1

From 2009-12-01 to 2010-01-01 Profit Factor:0.76 Total Net Profit:-9.25

BackTest : USDCHF on H1

From 2009-12-01 to 2010-01-01 Profit Factor:0.00 Total Net Profit:-96.91

BackTest : USDJPY on H1

From 2009-11-01 to 2009-11-30 Profit Factor:0.53 Total Net Profit:-42.18

Request Backtest for Sadukey_exp


From : (yyyy/mm/dd) To: (yyyy/mm/dd)

Pair: Period: