เมื่อ EA เทรดไปจนครบค่า Maxlevelแล้วให้เปลี่ยนขนาด LotและMaxlevel ตามที่เรากำหนด
extern double Lots = 0.1; /* reques lot size */
extern bool quotes_from_broker = FALSE;
extern bool BuyAndSell = true;
extern string CommentBuyOrSell = "Buy=True ; Sell=False;";
extern bool BuyOrSell = false;
extern bool FixStep = true;
extern int InitTakeProfit = 200;
extern int InitStop = 10000;
extern bool CloseAllOnTime = false;
extern string CommentCloseAllOnTimeDay = "0=Sunday;1=Monday;2=Tuesday;3=Wednesday;4=Thursday;5=Friday;6=Saturday;";
extern int CloseAllOnTimeDay = 5;
extern int CloseAllOnTimeHour = 22;
extern int CloseAllOnTimeMinute = 0;
extern bool UseRiskControl=false;
extern double MaxRisk=30;
extern int MaxLevel = 4;
extern bool Use_time = FALSE;
extern bool Sunday = TRUE;
extern bool Monday = TRUE;
extern bool Tuesday = TRUE;
extern bool Wednesday = TRUE;
extern bool Thursday = TRUE;
extern bool Friday = TRUE;
extern bool Saturday = TRUE;
extern int Step1 = 12;
extern int TPStep1 = 12;
extern int Step2 = 12;
extern int TPStep2 = 12;
extern int Step3 = 12;
extern int TPStep3 = 12;
extern int Step4 = 12;
extern int TPStep4 = 12;
extern int Step5 = 12;
extern int TPStep5 = 12;
extern int Step6 = 12;
extern int TPStep6 = 12;
extern int Step7 = 12;
extern int TPStep7 = 12;
extern int Step8 = 12;
extern int TPStep8 = 12;
extern int Step9 = 12;
extern int TPStep9 = 12;
extern int Step10 = 12;
extern int TPStep10 = 12;
int Slippage = 3;
int gi_unused_212 = 2;
int StartHour = 0;
int EndHour = 24;
extern int magic_nr = 4290;
bool gi_228 = FALSE;
bool TradeFlag = FALSE;
double dLots = 0.0;
bool Restart = false;
int init() {
if (quotes_from_broker) dLots = 100000.0 * Lots;
else dLots = Lots;
return (0);
}
int start() {
double l_price_0;
double SLPrice;
double TPPrice;
double l_lots_24;
double l_pips_32;
double l_pips_40;
int TicketBuy = 0;
int TicketSell = 0;
int TicketBuyLimit = 0;
int TicketSellLimit = 0;
int TicketBuyStop = 0;
int TicketSellStop = 0;
double LastOrderBuyLots = 0;
double LastOrderSellLots = 0;
double LastOrderBuyOpenPrice = 0;
double LastOrderSellOpenPrice = 0;
double LastOrderSellTakeProfit = 0;
double LastOrderBuyTakeProfit = 0;
double ld_unused_120 = 0;
double LastOrderStopLots = 0;
double ld_unused_136 = 0;
double LastOrderLimitLots = 0;
double ld_152 = 0;
double ld_160 = 0;
int TotalBuyOrder = 0;
int TotalSellOrder = 0;
int cnt, total;
TradeFlag = FALSE;
if (Restart) return (0);
if (Use_time == TRUE) {
switch (DayOfWeek()) {
case 0:
if (Sunday == TRUE) TradeFlag = TRUE;
break;
case 1:
if (Monday == TRUE) TradeFlag = TRUE;
break;
case 2:
if (Tuesday == TRUE) TradeFlag = TRUE;
break;
case 3:
if (Wednesday == TRUE) TradeFlag = TRUE;
break;
case 4:
if (Thursday == TRUE) TradeFlag = TRUE;
break;
case 5:
if (Friday == TRUE) TradeFlag = TRUE;
break;
case 6:
if (Saturday == TRUE) TradeFlag = TRUE;
}
} else TradeFlag = TRUE;
if (CloseAllOnTime && DayOfWeek() == CloseAllOnTimeDay
&& TimeHour(TimeCurrent()) == CloseAllOnTimeHour
&& TimeMinute(TimeCurrent()) >= CloseAllOnTimeMinute)
{
total = OrdersTotal();
for(cnt = total - 1; cnt >= 0; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber() == magic_nr) {
if (OrderType() == OP_BUY)
{
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,CLR_NONE);
}
if (OrderType() == OP_SELL)
{
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,CLR_NONE);
}
if (OrderType() == OP_BUYLIMIT)
{
OrderDelete(OrderTicket());
}
if (OrderType() == OP_SELLLIMIT)
{
OrderDelete(OrderTicket());
}
}
}
Restart = true;
return (0);
}
total = OrdersTotal();
TotalBuyOrder = 0;
TotalSellOrder = 0;
for (cnt = total - 1; cnt >=0; cnt--) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber() == magic_nr) {
if (OrderType() == OP_BUY && TicketBuy < OrderTicket()) {
LastOrderBuyLots = OrderLots();
LastOrderBuyOpenPrice = OrderOpenPrice();
TicketBuy = OrderTicket();
LastOrderBuyTakeProfit = OrderTakeProfit();
if (OrderLots() == dLots) ld_152 = OrderOpenPrice() - InitStop * Point;
}
if (OrderType() == OP_SELL && TicketSell < OrderTicket()) {
LastOrderSellLots = OrderLots();
LastOrderSellOpenPrice = OrderOpenPrice();
TicketSell = OrderTicket();
LastOrderSellTakeProfit = OrderTakeProfit();
if (OrderLots() == dLots) ld_160 = OrderOpenPrice() + InitStop * Point;
}
if (OrderType() == OP_BUYLIMIT) {
TicketBuyLimit = OrderTicket();
LastOrderLimitLots = OrderLots();
}
if (OrderType() == OP_SELLLIMIT) {
TicketSellLimit = OrderTicket();
LastOrderLimitLots = OrderLots();
}
if (OrderType() == OP_BUYSTOP) {
TicketBuyStop = OrderTicket();
LastOrderStopLots = OrderLots();
}
if (OrderType() == OP_SELLSTOP) {
TicketSellStop = OrderTicket();
LastOrderStopLots = OrderLots();
}
if (OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT)
TotalBuyOrder++;
if (OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT)
TotalSellOrder++;
}
}
if(UseRiskControl && AccountBalance() * (100 - MaxRisk) / 100 >= AccountEquity())
{
total = OrdersTotal();
for(cnt = total - 1; cnt >= 0; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber() == magic_nr) {
if (OrderType() == OP_BUY)
{
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,CLR_NONE);
}
if (OrderType() == OP_SELL)
{
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,CLR_NONE);
}
if (OrderType() == OP_BUYLIMIT)
{
OrderDelete(OrderTicket());
}
if (OrderType() == OP_SELLLIMIT)
{
OrderDelete(OrderTicket());
}
}
}
}
if ((BuyAndSell || BuyOrSell) && TicketBuy == 0) {
if (TicketBuyLimit != 0) OrderDelete(TicketBuyLimit);
if (TicketBuyStop != 0) OrderDelete(TicketBuyStop);
SLPrice = Ask - InitStop * Point;
if (InitStop <= 0) SLPrice = 0;
TPPrice = Ask + InitTakeProfit * Point;
if (TimeHour(TimeCurrent()) >= StartHour && TimeHour(TimeCurrent()) < EndHour && TradeFlag) OrderSend(Symbol(), OP_BUY, dLots, Ask, Slippage, SLPrice, TPPrice, "magic_nr = " + magic_nr, magic_nr, 0, CLR_NONE);
}
if ((BuyAndSell || !BuyOrSell) && TicketSell == 0) {
if (TicketSellLimit != 0) OrderDelete(TicketSellLimit);
if (TicketSellStop != 0) OrderDelete(TicketSellStop);
SLPrice = Bid + InitStop * Point;
if (InitStop <= 0) SLPrice = 0;
TPPrice = Bid - InitTakeProfit * Point;
if (TimeHour(TimeCurrent()) >= StartHour && TimeHour(TimeCurrent()) < EndHour && TradeFlag) OrderSend(Symbol(), OP_SELL, dLots, Bid, Slippage, SLPrice, TPPrice, "magic_nr = " + magic_nr, magic_nr, 0, CLR_NONE);
}
if ((BuyAndSell || BuyOrSell) && TicketBuy != 0 && TicketBuyLimit == 0 && TotalBuyOrder < MaxLevel) {
l_pips_40 = Finalinc(LastOrderBuyLots);
l_pips_32 = FinalincNr(LastOrderBuyLots);
l_price_0 = LastOrderBuyOpenPrice - l_pips_40 * Point;
if (gi_228 == FALSE) SLPrice = l_price_0 - InitStop * Point;
else SLPrice = ld_152;
if (InitStop <= 0) SLPrice = 0;
TPPrice = l_price_0 + l_pips_32 * Point;
l_lots_24 = NormalizeDouble(2.0 * LastOrderBuyLots, l_pips_32);
if (TradeFlag) OrderSend(Symbol(), OP_BUYLIMIT, l_lots_24, l_price_0, Slippage, SLPrice, TPPrice, "magic_nr = " + magic_nr, magic_nr, 0, CLR_NONE);
}
if ((BuyAndSell || !BuyOrSell) && TicketSell != 0 && TicketSellLimit == 0 && TotalSellOrder < MaxLevel) {
l_pips_40 = Finalinc(LastOrderSellLots);
l_pips_32 = FinalincNr(LastOrderSellLots);
l_price_0 = LastOrderSellOpenPrice + l_pips_40 * Point;
if (gi_228 == FALSE) SLPrice = l_price_0 + InitStop * Point;
else SLPrice = ld_160;
if (InitStop <= 0) SLPrice = 0;
TPPrice = l_price_0 - l_pips_32 * Point;
l_lots_24 = NormalizeDouble(2.0 * LastOrderSellLots, l_pips_32);
if (TradeFlag) OrderSend(Symbol(), OP_SELLLIMIT, l_lots_24, l_price_0, Slippage, SLPrice, TPPrice, "magic_nr = " + magic_nr, magic_nr, 0, CLR_NONE);
}
/*if ((BuyAndSell || BuyOrSell) && TicketBuy != 0 && TicketBuyStop == 0) {
l_pips_40 = Finalinc(LastOrderBuyLots);
l_pips_32 = FinalincNr(LastOrderBuyLots);
l_price_0 = LastOrderBuyOpenPrice + l_pips_32 * Point;
if (gi_228 == FALSE) SLPrice = l_price_0 + InitStop * Point;
else SLPrice = ld_152;
if (InitStop <= 0) SLPrice = 0;
TPPrice = l_price_0 + l_pips_32 * Point;
l_lots_24 = NormalizeDouble(2.0 * LastOrderBuyLots, l_pips_32);
if (TradeFlag) OrderSend(Symbol(), OP_BUYSTOP, l_lots_24, l_price_0, Slippage, SLPrice, TPPrice, "magic_nr = " + magic_nr, magic_nr, 0, CLR_NONE);
}
if ((BuyAndSell || !BuyOrSell) && TicketSell != 0 && TicketSellStop == 0) {
l_pips_40 = Finalinc(LastOrderSellLots);
l_pips_32 = FinalincNr(LastOrderSellLots);
l_price_0 = LastOrderSellOpenPrice - l_pips_40 * Point;
if (gi_228 == FALSE) SLPrice = l_price_0 - InitStop * Point;
else SLPrice = ld_160;
if (InitStop <= 0) SLPrice = 0;
TPPrice = l_price_0 - l_pips_32 * Point;
l_lots_24 = NormalizeDouble(2.0 * LastOrderSellLots, l_pips_32);
if (TradeFlag) OrderSend(Symbol(), OP_SELLSTOP, l_lots_24, l_price_0, Slippage, SLPrice, TPPrice, "magic_nr = " + magic_nr, magic_nr, 0, CLR_NONE);
}*/
total = OrdersTotal();
for (cnt = total - 1; cnt >=0; cnt--) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && TicketBuy != 0 && OrderMagicNumber() == magic_nr) {
if (gi_228 == FALSE) SLPrice = LastOrderBuyOpenPrice - InitStop * Point;
else SLPrice = ld_152;
if (InitStop <= 0) SLPrice = 0;
//if (TicketBuy != OrderTicket())
TPPrice = LastOrderBuyOpenPrice + FinalincNr(LastOrderBuyLots) * Point; // LastOrderBuyTakeProfit;
//else TPPrice = OrderTakeProfit();
if (TPPrice != OrderTakeProfit() || SLPrice != OrderStopLoss())
OrderModify(OrderTicket(), OrderOpenPrice(), SLPrice, TPPrice, 0, CLR_NONE);
}
if (OrderSymbol() == Symbol() && OrderType() == OP_SELL && TicketSell != 0 && OrderMagicNumber() == magic_nr) {
if (gi_228 == FALSE) SLPrice = LastOrderSellOpenPrice + InitStop * Point;
else SLPrice = ld_160;
if (InitStop <= 0) SLPrice = 0;
//if (TicketSell != OrderTicket())
TPPrice = LastOrderSellOpenPrice - FinalincNr(LastOrderSellLots) * Point; // LastOrderSellTakeProfit;
//else TPPrice = OrderTakeProfit();
if (TPPrice != OrderTakeProfit() || SLPrice != OrderStopLoss())
OrderModify(OrderTicket(), OrderOpenPrice(), SLPrice, TPPrice, 0, CLR_NONE);
}
}
return (0);
}
int Finalinc(double ad_0) {
ad_0 = NormalizeDouble(ad_0 / dLots, 0);
if(FixStep) return (InitTakeProfit);
switch (ad_0) {
case 1.0:
return (Step1);
case 2.0:
return (Step2);
case 4.0:
return (Step3);
case 8.0:
return (Step4);
case 16.0:
return (Step5);
case 32.0:
return (Step6);
case 64.0:
return (Step7);
case 128.0:
return (Step8);
case 256.0:
return (Step9);
case 512.0:
return (Step10);
}
return (Step10);
}
int FinalincNr(double ad_0) {
ad_0 = NormalizeDouble(ad_0 / dLots, 0);
if(FixStep) return (InitTakeProfit);
switch (ad_0) {
case 1.0:
return (TPStep1);
case 2.0:
return (TPStep2);
case 4.0:
return (TPStep3);
case 8.0:
return (TPStep4);
case 16.0:
return (TPStep5);
case 32.0:
return (TPStep6);
case 64.0:
return (TPStep7);
case 128.0:
return (TPStep8);
case 256.0:
return (TPStep9);
case 512.0:
return (TPStep10);
}
return (TPStep10);
}