#include <stdlib.mqh>
#include <WinUser32.mqh>
// exported variables
extern double BuyLots6 = 0.1;
extern int BuyStoploss6 = 0;
extern int BuyTakeprofit6 = 10;
extern int TrailingGap8 = 8;
extern int NewTakeProfit8 = 30;
extern int TradeUpPoint8 = 5;
extern double Lots8 = 0.1;
// local variables
double PipValue=1; // this variable is here to support 5-digit brokers
bool Terminated = false;
string LF = "\n"; // use this in custom or utility blocks where you need line feeds
int NDigits = 4; // used mostly for NormalizeDouble in Flex type blocks
int ObjCount = 0; // count of all objects created on the chart, allows creation of objects with unique names
int current = 0;
datetime BarTime7 = 0;
int init()
{
NDigits = Digits;
if (false) ObjectsDeleteAll(); // clear the chart
Comment(""); // clear the chart
}
// Expert start
int start()
{
if (Bars < 10)
{
Comment("Not enough bars");
return (0);
}
if (Terminated == true)
{
Comment("EA Terminated.");
return (0);
}
OnEveryNewBar7();
}
void OnEveryNewBar7()
{
if (true == false && false) PipValue = 10;
if (true && (NDigits == 3 || NDigits == 5)) PipValue = 10;
if (BarTime7 < Time[0])
{
// we have a new bar opened
BarTime7 = Time[0]; // keep the new bar open time
BuyOrder6();
TradeUpTrailing8();
}
}
void BuyOrder6()
{
double SL = Ask - BuyStoploss6*PipValue*Point;
if (BuyStoploss6 == 0) SL = 0;
double TP = Ask + BuyTakeprofit6*PipValue*Point;
if (BuyTakeprofit6 == 0) TP = 0;
int ticket = -1;
if (true)
ticket = OrderSend(Symbol(), OP_BUY, BuyLots6, Ask, 4, 0, 0, "Buy SE v2", 1, 0, Blue);else
ticket = OrderSend(Symbol(), OP_BUY, BuyLots6, Ask, 4, SL, TP, "Buy SE v2", 1, 0, Blue);
if (ticket > -1)
{
if (true)
{
OrderSelect(ticket, SELECT_BY_TICKET);
bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, Blue);
if (ret == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
}
TradeUpTrailing8();
}
else
{
Print("OrderSend() error - ", ErrorDescription(GetLastError()));
}
}
void TradeUpTrailing8()
{
double lots = 0;
double takeprofit = 0, stoploss = 0;
for (int i=OrdersTotal()-1; i >= 0; i--)
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
{
if (OrderType() == OP_BUY && Ask - OrderOpenPrice() > TradeUpPoint8*PipValue*Point && (OrderStopLoss() < Ask-(TradeUpPoint8+TrailingGap8)*PipValue*Point))
{
stoploss = Ask-TrailingGap8*PipValue*Point;
takeprofit = Ask+NewTakeProfit8*PipValue*Point;
if (NewTakeProfit8 == 0) takeprofit = OrderTakeProfit();
bool ret1 = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, OrderExpiration(), White);
if (ret1 == false)
Print("OrderClose() error - ", ErrorDescription(GetLastError()));
else
{
int ticket = -1;
if (true)
ticket = OrderSend(Symbol(), OP_BUY, Lots8, Ask, 4, 0, 0, "Buy SE v2", 1, 0, White);else
ticket = OrderSend(Symbol(), OP_BUY, Lots8, Ask, 4, stoploss, takeprofit, "Buy SE v2", 1, 0, White);
if (ticket > -1)
{
if (true)
{
OrderSelect(ticket, SELECT_BY_TICKET);
bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, 0, White);
if (ret == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
}
}
}
}
if (OrderType() == OP_SELL && OrderOpenPrice() - Bid > TradeUpPoint8*PipValue*Point && (OrderStopLoss() > Bid+(TradeUpPoint8+TrailingGap8)*PipValue*Point))
{
stoploss = Bid+TrailingGap8*PipValue*Point;
takeprofit = Bid-NewTakeProfit8*PipValue*Point;
if (NewTakeProfit8 == 0) takeprofit = OrderTakeProfit();
bool ret2 = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, OrderExpiration(), White);
if (ret2 == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
else
{
int ticket2 = -1;
if (true)
ticket2 = OrderSend(Symbol(), OP_SELL, Lots8, Bid, 4, 0, 0, "Buy SE v2", 1, 0, White);else
ticket2 = OrderSend(Symbol(), OP_SELL, Lots8, Bid, 4, stoploss, takeprofit, "Buy SE v2", 1, 0, White);
if (ticket2 > -1)
{
if (true)
{
OrderSelect(ticket2, SELECT_BY_TICKET);
bool ret4 = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, 0, White);
if (ret4 == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
}
}
}
}
}
}
else
Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
}
int deinit()
{
if (false) ObjectsDeleteAll();
}