Skip to content

Instantly share code, notes, and snippets.

@jonathascosta
Created July 5, 2014 01:35
Show Gist options
  • Save jonathascosta/5c361da04be02459f51c to your computer and use it in GitHub Desktop.
Save jonathascosta/5c361da04be02459f51c to your computer and use it in GitHub Desktop.
extern int MonthsToLimitZone = 8;
extern double volume = 0.01;
extern double spaceBetweenOrders = 0.001;
extern int MagicNumber = 1234567890;
extern double profitInCurrency = 0.01;
extern double stopLossOrderLimit = 5.0;
double lowestDay, highestDay;
double beginOfSellZone, endOfSellZone;
double beginOfBuyZone, endOfBuyZone;
int slipPage = 3;
double stopLevel = 0;
string counterOrderCommentFormat = "COUNTER ORDER FOR ";
int init()
{
stopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL) * Point;
SetBoundaries();
Print("lowestDay", lowestDay);
}
int start()
{
CloseCounterOrders();
CreateOrders();
CreateCounterOrders();
}
void SetBoundaries()
{
lowestDay = iLow(Symbol(), PERIOD_MN1, iLowest(NULL, PERIOD_MN1, MODE_LOW, MonthsToLimitZone, 0));
highestDay = iHigh(Symbol(), PERIOD_MN1, iHighest(NULL, PERIOD_MN1, MODE_HIGH, MonthsToLimitZone, 0));
double diff = (highestDay - lowestDay);
beginOfSellZone = highestDay;
endOfSellZone = lowestDay + (diff * 2 / 5);
beginOfBuyZone = lowestDay;
endOfBuyZone = highestDay - (diff * 2 / 5);
}
bool IsInSellZone(double value)
{
return (value <= beginOfSellZone && value >= endOfSellZone);
}
bool IsInBuyZone(double value)
{
return (value >= beginOfBuyZone && value <= endOfBuyZone);
}
void CloseCounterOrders()
{
for(int i = 0; i < OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS);
if ((OrderMagicNumber() == MagicNumber) && (OrderSymbol() == Symbol()) && StringFind(OrderComment(), counterOrderCommentFormat) >= 0)
{
// is counter
if (OrderProfit() > 0)
{
if (OrderType() == OP_BUY && (iMA(Symbol(), 0,0,0,0,0,0) - iMA(Symbol(), 0,0,0,0,0,0)) < 0)
{
OrderClose(OrderTicket(), OrderLots(), Bid, slipPage, Blue);
}
else if (OrderType() == OP_SELL && (iMA(Symbol(), 0,0,0,0,0,0) - iMA(Symbol(), 0,0,0,0,0,0)) > 0)
{
OrderClose(OrderTicket(), OrderLots(), Ask, slipPage, Red);
}
}
}
}
}
void CreateOrders()
{
for (double value = lowestDay; value <= highestDay; value += spaceBetweenOrders)
{
int op = CheckIfOrderIsAlreadyOpen(value);
if ((op % 2 != 0) && IsInBuyZone(value) && MathAbs(value - Ask) > stopLevel)
{
if (value > Ask)
OrderSend(Symbol(), OP_BUYSTOP, volume, NormalizeDouble(value, 4), slipPage, 0, NormalizeDouble(value, 4) + profitInCurrency, "", MagicNumber, 0, Blue);
else
OrderSend(Symbol(), OP_BUYLIMIT, volume, NormalizeDouble(value, 4), slipPage, 0, NormalizeDouble(value, 4) + profitInCurrency, "", MagicNumber, 0, Blue);
}
if ((op % 3 != 0) && IsInSellZone(value) && MathAbs(value - Bid) > stopLevel)
{
if (value < Bid)
OrderSend(Symbol(), OP_SELLSTOP, volume, value, slipPage, 0, NormalizeDouble(value, 4) - profitInCurrency, "", MagicNumber, 0, Red);
else
OrderSend(Symbol(), OP_SELLLIMIT, volume, value, slipPage, 0, NormalizeDouble(value, 4) - profitInCurrency, "", MagicNumber, 0, Red);
}
}
}
void CreateCounterOrders()
{
for(int i = 0; i < OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS);
int orderTicket = OrderTicket();
string counterOrderComment = StringConcatenate(counterOrderCommentFormat, orderTicket);
if ((OrderMagicNumber() == MagicNumber) && (OrderSymbol() == Symbol()))
{
if (OrderProfit() < -stopLossOrderLimit)
{
int counterOrderTicket = 0;
for(int j = 0; j < OrdersTotal(); j++)
{
OrderSelect(j, SELECT_BY_POS);
if (OrderComment() == counterOrderComment)
{
counterOrderTicket = OrderTicket();
continue;
}
}
if (counterOrderTicket == 0)
{
CreateCounterOrder(orderTicket);
}
}
}
}
}
void CreateCounterOrder(int orderTicket)
{
int ticket;
OrderSelect(orderTicket, SELECT_BY_TICKET);
string counterOrderComment = StringConcatenate(counterOrderCommentFormat, orderTicket);
if (OrderType() == OP_BUY)
{
ticket = OrderSend(Symbol(), OP_SELL, OrderLots(), NormalizeDouble(Bid, 4), slipPage, 0, NormalizeDouble(Ask, 4) - profitInCurrency, counterOrderComment, MagicNumber, 0, Red);
}
else
{
ticket = OrderSend(Symbol(), OP_BUY, OrderLots(), NormalizeDouble(Ask, 4), slipPage, 0, NormalizeDouble(Bid, 4) + profitInCurrency, counterOrderComment, MagicNumber, 0, Blue);
}
Print("Opening counter order #", ticket, " for order #", orderTicket);
}
int CheckIfOrderIsAlreadyOpen(double value)
{
int op = 1;
for(int i = 0; i < OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS);
if ((OrderMagicNumber() == MagicNumber) && (OrderSymbol() == Symbol()))
{
if ((MathAbs(OrderOpenPrice() - value) <= spaceBetweenOrders) && (OrderType() == OP_BUY || OrderType() == OP_BUYSTOP || OrderType() == OP_BUYLIMIT))
op = op * 2;
if ((MathAbs(OrderOpenPrice() - value) <= spaceBetweenOrders) && (OrderType() == OP_SELL || OrderType() == OP_SELLSTOP || OrderType() == OP_SELLLIMIT))
op = op * 3;
if (op == 6)
return (op);
}
}
return (op);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment