#property copyright "Copyright ?2016.narod.ru"
#property link "http://2016.narod.ru"
extern int MMType = 1;
extern bool UseClose = FALSE;
extern bool UseAdd = TRUE;
extern double LotExponent = 1.667;
extern double slip = 3.0;
extern double Lots = 0.01;
extern double LotsDigits = 2.0;
extern double TakeProfit = 10.0;
double gd_128 = 500.0;
double gd_136 = 10.0;
double gd_144 = 10.0;
extern double PipStep = 30.0;
extern int MaxTrades = 10;
extern bool UseEquityStop = FALSE;
extern double TotalEquityRisk = 20.0;
extern bool UseTrailingStop = FALSE;
extern bool UseTimeOut = FALSE;
extern double MaxTradeOpenHours = 48.0;
int gi_192 = 12324;
double gd_196;
double gd_204;
double gd_212;
double gd_220;
double gd_228;
double gd_236;
double gd_244;
double gd_252;
double gd_260;
double gd_276;
bool gi_284;
string gs_288 = "陈";
int gt_296 = 0;
int gi_300;
int gi_304 = 0;
double gd_308;
int gi_316 = 0;
int gi_320;
double gd_324 = 0.0;
bool gi_332 = FALSE;
bool gi_336 = FALSE;
bool gi_340 = FALSE;
int gi_344;
bool gi_348 = FALSE;
int gi_352 = 0;
int gi_356 = 0;
double gd_360;
double gd_368;
int init() {
gd_276 = MarketInfo(Symbol(), MODE_SPREAD) * Point;
return (0);
}
int deinit() {
return (0);
}
int start() {
double ld_8;
double ld_16;
double ld_24;
double ld_32;
if (UseTrailingStop) TrailingAlls(gd_136, gd_144, gd_228);
if (UseTimeOut) {
if (TimeCurrent() >= gi_300) {
CloseThisSymbolAll();
Print("Closed All due to TimeOut");
}
}
if (gt_296 == Time[0]) return (0);
gt_296 = Time[0];
double ld_0 = CalculateProfit();
if (UseEquityStop) {
if (ld_0 < 0.0 && MathAbs(ld_0) > TotalEquityRisk / 100.0 * AccountEquityHigh()) {
CloseThisSymbolAll();
Print("Closed All due to Stop Out");
gi_348 = FALSE;
}
}
gi_320 = CountTrades();
if (gi_320 == 0) gi_284 = FALSE;
for (gi_316 = OrdersTotal() - 1; gi_316 >= 0; gi_316--) {
OrderSelect(gi_316, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != gi_192) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == gi_192) {
if (OrderType() == OP_BUY) {
gi_336 = TRUE;
gi_340 = FALSE;
ld_8 = OrderLots();
break;
}
}
if (OrderSymbol() == Symbol() && OrderMagicNumber() == gi_192) {
if (OrderType() == OP_SELL) {
gi_336 = FALSE;
gi_340 = TRUE;
ld_16 = OrderLots();
break;
}
}
}
if (gi_320 > 0 && gi_320 <= MaxTrades) {
RefreshRates();
gd_252 = FindLastBuyPrice();
gd_260 = FindLastSellPrice();
if (gi_336 && gd_252 - Ask >= PipStep * Point) gi_332 = TRUE;
if (gi_340 && Bid - gd_260 >= PipStep * Point) gi_332 = TRUE;
}
if (gi_320 < 1) {
gi_340 = FALSE;
gi_336 = FALSE;
gi_332 = TRUE;
gd_204 = AccountEquity();
}
if (gi_332) {
gd_252 = FindLastBuyPrice();
gd_260 = FindLastSellPrice();
if (gi_340) {
if (UseClose) {
fOrderCloseMarket(0, 1);
gd_308 = NormalizeDouble(LotExponent * ld_16, LotsDigits);
} else gd_308 = fGetLots(OP_SELL);
if (UseAdd) {
gi_304 = gi_320;
if (gd_308 > 0.0) {
RefreshRates();
gi_344 = OpenPendingOrder(1, gd_308, Bid, slip, Ask, 0, 0, gs_288 + "-" + gi_304, gi_192, 0, HotPink);
if (gi_344 < 0) {
Print("Error: ", GetLastError());
return (0);
}
gd_260 = FindLastSellPrice();
gi_332 = FALSE;
gi_348 = TRUE;
}
}
} else {
if (gi_336) {
if (UseClose) {
fOrderCloseMarket(1, 0);
gd_308 = NormalizeDouble(LotExponent * ld_8, LotsDigits);
} else gd_308 = fGetLots(OP_BUY);
if (UseAdd) {
gi_304 = gi_320;
if (gd_308 > 0.0) {
gi_344 = OpenPendingOrder(0, gd_308, Ask, slip, Bid, 0, 0, gs_288 + "-" + gi_304, gi_192, 0, Lime);
if (gi_344 < 0) {
Print("Error: ", GetLastError());
return (0);
}
gd_252 = FindLastBuyPrice();
gi_332 = FALSE;
gi_348 = TRUE;
}
}
}
}
}
if (gi_332 && gi_320 < 1) {
ld_24 = iClose(Symbol(), 0, 2);
ld_32 = iClose(Symbol(), 0, 1);
gd_236 = Bid;
gd_244 = Ask;
if ((!gi_340) && !gi_336) {
gi_304 = gi_320;
if (ld_24 > ld_32) {
gd_308 = fGetLots(OP_SELL);
if (gd_308 > 0.0) {
gi_344 = OpenPendingOrder(1, gd_308, gd_236, slip, gd_236, 0, 0, gs_288 + "-" + gi_304, gi_192, 0, HotPink);
if (gi_344 < 0) {
Print(gd_308, "Error: ", GetLastError());
return (0);
}
gd_252 = FindLastBuyPrice();
gi_348 = TRUE;
}
} else {
gd_308 = fGetLots(OP_BUY);
if (gd_308 > 0.0) {
gi_344 = OpenPendingOrder(0, gd_308, gd_244, slip, gd_244, 0, 0, gs_288 + "-" + gi_304, gi_192, 0, Lime);
if (gi_344 < 0) {
Print(gd_308, "Error: ", GetLastError());
return (0);
}
gd_260 = FindLastSellPrice();
gi_348 = TRUE;
}
}
}
if (gi_344 > 0) gi_300 = TimeCurrent() + 60.0 * (60.0 * MaxTradeOpenHours);
gi_332 = FALSE;
}
gi_320 = CountTrades();
gd_228 = 0;
double ld_40 = 0;
for (gi_316 = OrdersTotal() - 1; gi_316 >= 0; gi_316--) {
OrderSelect(gi_316, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != gi_192) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == gi_192) {
if (OrderType() == OP_BUY || OrderType() == OP_SELL) {
gd_228 += OrderOpenPrice() * OrderLots();
ld_40 += OrderLots();
}
}
}
if (gi_320 > 0) gd_228 = NormalizeDouble(gd_228 / ld_40, Digits);
if (gi_348) {
for (gi_316 = OrdersTotal() - 1; gi_316 >= 0; gi_316--) {
OrderSelect(gi_316, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != gi_192) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == gi_192) {
if (OrderType() == OP_BUY) {
gd_196 = gd_228 + TakeProfit * Point;
gd_212 = gd_196;
gd_324 = gd_228 - gd_128 * Point;
gi_284 = TRUE;
}
}
if (OrderSymbol() == Symbol() && OrderMagicNumber() == gi_192) {
if (OrderType() == OP_SELL) {
gd_196 = gd_228 - TakeProfit * Point;
gd_220 = gd_196;
gd_324 = gd_228 + gd_128 * Point;
gi_284 = TRUE;
}
}
}
}
if (gi_348) {
if (gi_284 == TRUE) {
for (gi_316 = OrdersTotal() - 1; gi_316 >= 0; gi_316--) {
OrderSelect(gi_316, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != gi_192) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == gi_192) OrderModify(OrderTicket(), gd_228, OrderStopLoss(), gd_196, 0, Yellow);
gi_348 = FALSE;
}
}
}
return (0);
}
double ND(double ad_0) {
return (NormalizeDouble(ad_0, Digits));
}
int fOrderCloseMarket(bool ai_0 = TRUE, bool ai_4 = TRUE) {
int li_8 = 0;
for (int li_12 = OrdersTotal() - 1; li_12 >= 0; li_12--) {
if (OrderSelect(li_12, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol() == Symbol() && OrderMagicNumber() == gi_192) {
if (OrderType() == OP_BUY && ai_0) {
RefreshRates();
if (!IsTradeContextBusy()) {
if (!OrderClose(OrderTicket(), OrderLots(), ND(Bid), 5, CLR_NONE)) {
Print("Error close BUY " + OrderTicket());
li_8 = -1;
}
} else {
if (gi_352 != iTime(NULL, 0, 0)) {
gi_352 = iTime(NULL, 0, 0);
Print("Need close BUY " + OrderTicket() + ". Trade Context Busy");
}
return (-2);
}
}
if (OrderType() == OP_SELL && ai_4) {
RefreshRates();
if (!IsTradeContextBusy()) {
if (!(!OrderClose(OrderTicket(), OrderLots(), ND(Ask), 5, CLR_NONE))) continue;
Print("Error close SELL " + OrderTicket());
li_8 = -1;
continue;
}
if (gi_356 != iTime(NULL, 0, 0)) {
gi_356 = iTime(NULL, 0, 0);
Print("Need close SELL " + OrderTicket() + ". Trade Context Busy");
}
return (-2);
}
}
}
}
return (li_8);
}
double fGetLots(int ai_0) {
double ld_4;
int li_16;
switch (MMType) {
case 0:
ld_4 = Lots;
break;
case 1:
ld_4 = NormalizeDouble(Lots * MathPow(LotExponent, gi_304), LotsDigits);
break;
case 2:
li_16 = 0;
ld_4 = Lots;
for (int li_20 = OrdersHistoryTotal() - 1; li_20 >= 0; li_20--) {
if (OrderSelect(li_20, SELECT_BY_POS, MODE_HISTORY)) {
if (OrderSymbol() == Symbol() && OrderMagicNumber() == gi_192) {
if (li_16 < OrderCloseTime()) {
li_16 = OrderCloseTime();
if (OrderProfit() < 0.0) {
ld_4 = NormalizeDouble(OrderLots() * LotExponent, LotsDigits);
continue;
}
ld_4 = Lots;
}
}
} else return (-3);
}
}
if (AccountFreeMarginCheck(Symbol(), ai_0, ld_4) <= 0.0) return (-1);
if (GetLastError() == 134/* NOT_ENOUGH_MONEY */) return (-2);
return (ld_4);
}
int CountTrades() {
int li_0 = 0;
for (int li_4 = OrdersTotal() - 1; li_4 >= 0; li_4--) {
OrderSelect(li_4, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != gi_192) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == gi_192)
if (OrderType() == OP_SELL || OrderType() == OP_BUY) li_0++;
}
return (li_0);
}
void CloseThisSymbolAll() {
for (int li_0 = OrdersTotal() - 1; li_0 >= 0; li_0--) {
OrderSelect(li_0, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol()) {
if (OrderSymbol() == Symbol() && OrderMagicNumber() == gi_192) {
if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), Bid, slip, Blue);
if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, slip, Red);
}
Sleep(1000);
}
}
}
int OpenPendingOrder(int ai_0, double ad_4, double ad_12, int ai_20, double ad_24, int ai_32, int ai_36, string as_40, int ai_48, int ai_52, color ai_56) {
int li_60 = 0;
int li_64 = 0;
int li_68 = 0;
int li_72 = 100;
switch (ai_0) {
case 2:
for (li_68 = 0; li_68 < li_72; li_68++) {
li_60 = OrderSend(Symbol(), OP_BUYLIMIT, ad_4, ad_12, ai_20, StopLong(ad_24, ai_32), TakeLong(ad_12, ai_36), as_40, ai_48, ai_52, ai_56);
li_64 = GetLastError();
if (li_64 == 0/* NO_ERROR */) break;
if (!(li_64 == 4/* SERVER_BUSY */ || li_64 == 137/* BROKER_BUSY */ || li_64 == 146/* TRADE_CONTEXT_BUSY */ || li_64 == 136/* OFF_QUOTES */)) break;
Sleep(1000);
}
break;
case 4:
for (li_68 = 0; li_68 < li_72; li_68++) {
li_60 = OrderSend(Symbol(), OP_BUYSTOP, ad_4, ad_12, ai_20, StopLong(ad_24, ai_32), TakeLong(ad_12, ai_36), as_40, ai_48, ai_52, ai_56);
li_64 = GetLastError();
if (li_64 == 0/* NO_ERROR */) break;
if (!(li_64 == 4/* SERVER_BUSY */ || li_64 == 137/* BROKER_BUSY */ || li_64 == 146/* TRADE_CONTEXT_BUSY */ || li_64 == 136/* OFF_QUOTES */)) break;
Sleep(5000);
}
break;
case 0:
for (li_68 = 0; li_68 < li_72; li_68++) {
RefreshRates();
li_60 = OrderSend(Symbol(), OP_BUY, ad_4, Ask, ai_20, StopLong(Bid, ai_32), TakeLong(Ask, ai_36), as_40, ai_48, ai_52, ai_56);
li_64 = GetLastError();
if (li_64 == 0/* NO_ERROR */) break;
if (!(li_64 == 4/* SERVER_BUSY */ || li_64 == 137/* BROKER_BUSY */ || li_64 == 146/* TRADE_CONTEXT_BUSY */ || li_64 == 136/* OFF_QUOTES */)) break;
Sleep(5000);
}
break;
case 3:
for (li_68 = 0; li_68 < li_72; li_68++) {
li_60 = OrderSend(Symbol(), OP_SELLLIMIT, ad_4, ad_12, ai_20, StopShort(ad_24, ai_32), TakeShort(ad_12, ai_36), as_40, ai_48, ai_52, ai_56);
li_64 = GetLastError();
if (li_64 == 0/* NO_ERROR */) break;
if (!(li_64 == 4/* SERVER_BUSY */ || li_64 == 137/* BROKER_BUSY */ || li_64 == 146/* TRADE_CONTEXT_BUSY */ || li_64 == 136/* OFF_QUOTES */)) break;
Sleep(5000);
}
break;
case 5:
for (li_68 = 0; li_68 < li_72; li_68++) {
li_60 = OrderSend(Symbol(), OP_SELLSTOP, ad_4, ad_12, ai_20, StopShort(ad_24, ai_32), TakeShort(ad_12, ai_36), as_40, ai_48, ai_52, ai_56);
li_64 = GetLastError();
if (li_64 == 0/* NO_ERROR */) break;
if (!(li_64 == 4/* SERVER_BUSY */ || li_64 == 137/* BROKER_BUSY */ || li_64 == 146/* TRADE_CONTEXT_BUSY */ || li_64 == 136/* OFF_QUOTES */)) break;
Sleep(5000);
}
break;
case 1:
for (li_68 = 0; li_68 < li_72; li_68++) {
li_60 = OrderSend(Symbol(), OP_SELL, ad_4, Bid, ai_20, StopShort(Ask, ai_32), TakeShort(Bid, ai_36), as_40, ai_48, ai_52, ai_56);
li_64 = GetLastError();
if (li_64 == 0/* NO_ERROR */) break;
if (!(li_64 == 4/* SERVER_BUSY */ || li_64 == 137/* BROKER_BUSY */ || li_64 == 146/* TRADE_CONTEXT_BUSY */ || li_64 == 136/* OFF_QUOTES */)) break;
Sleep(5000);
}
}
return (li_60);
}
double StopLong(double ad_0, int ai_8) {
if (ai_8 == 0) return (0);
return (ad_0 - ai_8 * Point);
}
double StopShort(double ad_0, int ai_8) {
if (ai_8 == 0) return (0);
return (ad_0 + ai_8 * Point);
}
double TakeLong(double ad_0, int ai_8) {
if (ai_8 == 0) return (0);
return (ad_0 + ai_8 * Point);
}
double TakeShort(double ad_0, int ai_8) {
if (ai_8 == 0) return (0);
return (ad_0 - ai_8 * Point);
}
double CalculateProfit() {
double ld_0 = 0;
for (gi_316 = OrdersTotal() - 1; gi_316 >= 0; gi_316--) {
OrderSelect(gi_316, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != gi_192) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == gi_192)
if (OrderType() == OP_BUY || OrderType() == OP_SELL) ld_0 += OrderProfit();
}
return (ld_0);
}
void TrailingAlls(int ai_0, int ai_4, double ad_8) {
int li_16;
double ld_20;
double ld_28;
if (ai_4 != 0) {
for (int li_36 = OrdersTotal() - 1; li_36 >= 0; li_36--) {
if (OrderSelect(li_36, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol() != Symbol() || OrderMagicNumber() != gi_192) continue;
if (OrderSymbol() == Symbol() || OrderMagicNumber() == gi_192) {
if (OrderType() == OP_BUY) {
li_16 = NormalizeDouble((Bid - ad_8) / Point, 0);
if (li_16 < ai_0) continue;
ld_20 = OrderStopLoss();
ld_28 = Bid - ai_4 * Point;
if (ld_20 == 0.0 || (ld_20 != 0.0 && ld_28 > ld_20)) OrderModify(OrderTicket(), ad_8, ld_28, OrderTakeProfit(), 0, Aqua);
}
if (OrderType() == OP_SELL) {
li_16 = NormalizeDouble((ad_8 - Ask) / Point, 0);
if (li_16 < ai_0) continue;
ld_20 = OrderStopLoss();
ld_28 = Ask + ai_4 * Point;
if (ld_20 == 0.0 || (ld_20 != 0.0 && ld_28 < ld_20)) OrderModify(OrderTicket(), ad_8, ld_28, OrderTakeProfit(), 0, Red);
}
}
Sleep(1000);
}
}
}
}
double AccountEquityHigh() {
if (CountTrades() == 0) gd_360 = AccountEquity();
if (gd_360 < gd_368) gd_360 = gd_368;
else gd_360 = AccountEquity();
gd_368 = AccountEquity();
return (gd_360);
}
double FindLastBuyPrice() {
double ld_8;
int li_24;
double ld_0 = 0;
int li_20 = 0;
for (int li_16 = OrdersTotal() - 1; li_16 >= 0; li_16--) {
OrderSelect(li_16, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != gi_192) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == gi_192 && OrderType() == OP_BUY) {
li_24 = OrderTicket();
if (li_24 > li_20) {
ld_8 = OrderOpenPrice();
ld_0 = ld_8;
li_20 = li_24;
}
}
}
return (ld_8);
}
double FindLastSellPrice() {
double ld_8;
int li_24;
double ld_0 = 0;
int li_20 = 0;
for (int li_16 = OrdersTotal() - 1; li_16 >= 0; li_16--) {
OrderSelect(li_16, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != gi_192) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == gi_192 && OrderType() == OP_SELL) {
li_24 = OrderTicket();
if (li_24 > li_20) {
ld_8 = OrderOpenPrice();
ld_0 = ld_8;
li_20 = li_24;
}
}
}
return (ld_8);
} |