Siscop2DayBreakOut

Author: siscop
Orders Execution
It automatically opens orders when conditions are reachedIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites
Siscop2DayBreakOut
#property copyright "siscop"

// ---- input externe variabeln
extern double Lots=0.1;
extern double pipsize=0.0001;
extern int puffer=3;
extern int TP=20;
extern int SL=10;
extern int TS=10;
extern int magic=12345;

// ---- init
int init()
{
	return(0);
}

// ---- deinit()
int deinit()
{
	return(0);
}

// ---- start
int start()
{

// ---- Variabeln
	static bool long;
	static bool short;
	static double TPPreis;
	static double SLPreis;

	double DHighVortag;
	double DLowVortag;
	double DHighHeute;
	double DLowHeute;
	int WochenTagHeute;
	int WochenTagVortag;
	int AnzahlBalkenHeute;
	int AnzahlBalkenVortag;
	static int ticket;
	int tmp;

// ---- WochenTagBestimmung
	
	WochenTagHeute=TimeDayOfWeek(TimeCurrent());
	
	switch(WochenTagHeute)
  	{
   	case 0:
		case 1:
      			WochenTagVortag=5;
      			break;
   	case 2:
   	case 3:
		case 4:
		case 5:
		case 6:
      			WochenTagVortag=WochenTagHeute-1;
             	break;
   	default:
      			Print("WochenTagFehler");
      			break;
  	}	

	AnzahlBalkenVortag=0;
	AnzahlBalkenHeute=0;
	tmp=0;
	while(true)
	{
		int WochenTagTmp=TimeDayOfWeek(Time[AnzahlBalkenVortag]);
		if (WochenTagTmp == WochenTagHeute)
		{
   		AnzahlBalkenHeute++;
			AnzahlBalkenVortag++;
			continue;
		}
	
 		if (WochenTagTmp == WochenTagVortag)
		{
		   tmp=1;
			AnzahlBalkenVortag++;
			continue;
		}
		if (tmp==0)
		{
			AnzahlBalkenVortag++;
			continue;
		}	
		break;
	}

// ---- HighLow von Heute und Vortagbestimmung

	DHighHeute=High[0];
	DLowHeute=Low[0];
	for (int i=2;i<=AnzahlBalkenHeute;i++)
	{
		if (DHighHeute<High[i])
			DHighHeute=High[i];
		if (DLowHeute>Low[i])
			DLowHeute=Low[i];
	}

	DHighVortag=High[AnzahlBalkenVortag-AnzahlBalkenHeute+1];
	DLowVortag=Low[AnzahlBalkenVortag-AnzahlBalkenHeute+1];
	for (i=AnzahlBalkenHeute+1;i<=AnzahlBalkenVortag;i++)
	{
		if (DHighVortag<High[i])
			DHighVortag=High[i];
		if (DLowVortag>Low[i])
			DLowVortag=Low[i];
	}

// ---- Kauf / Verkauf

	if (!long && !short)
	{
// ---- Kauf
		if (High[1]>DHighHeute && High[1]>DHighVortag)
		{
			if ((Close[0]+(puffer*pipsize))>High[1])
			{
				ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"2DayBreakOut",magic,0,Green);
				if(ticket>0)
				{
					if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
						Print("BUY order opened : ",OrderOpenPrice());
					long=true;
					// TPPreis=OrderOpenPrice()+(TP*pipsize);
				}
				else Print("Error opening BUY order : ",GetLastError());
				return(0);
			}
		}
// ---- Verkauf
		if (Low[1]<DLowHeute && Low[1]<DLowVortag)
		{
			if ((Close[0]-(puffer*pipsize))<Low[1])
			{
				ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"2DayBreakOut",magic,0,Red);
				if(ticket>0)
				{
					if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
						Print("SELL order opened : ",OrderOpenPrice());
					short=true;
					// TPPreis=OrderOpenPrice()-(TP*pipsize);
				}
				else Print("Error opening SELL order : ",GetLastError());
				return(0);
			}
		}
	}

	if (long || short)
		OrderSelect(0, SELECT_BY_POS, MODE_TRADES);

// ---- TrailingStop StopLoss Berechnung

	if(TS>0)
	{
		if (long)
		{
			if((High[0]-(TS*pipsize))>SLPreis)
			{
				SLPreis=High[0]-(TS*pipsize);
			}
		}
		if (short)
		{
			if((Low[0]+(TS*pipsize))<SLPreis)
			{
				SLPreis=Low[0]+(TS*pipsize);
			}
		}
	}
	else
		SLPreis=OrderOpenPrice()-(SL*pipsize);

// ---- TakeProfit Berechnung
	
	if (TP!=0)
	{
		if (long)
		{
			TPPreis=OrderOpenPrice()+(TP*pipsize);
		}
		if (short)
		{
			TPPreis=OrderOpenPrice()-(TP*pipsize);
		}
	}
	else
		TPPreis=9999999;

// ---- EXITKauf / Verkauf StopLoss

	if (long)
	{
		if (Close[0]<SLPreis)
		{
			OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
			long=false;
		}
	}
	if (short)
	{
		if (Close[0]>SLPreis)
		{
			OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
			short=false;
		}
	}

// ---- EXITKauf / Verkauf TakeProfit

	if (long)
	{
		if (Close[0]>TPPreis)
		{
			OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
			long=false;
		}
	}
	if (short)
	{
		if (Close[0]<TPPreis)
		{
			OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
			short=false;
		}
	}
	Print ("long",long);
	Print ("short",short);
	Print ("ticket",ticket);
}

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