Jump to content

Trade*manager EA - Update with .mq4 version


Recommended Posts

Re: Trade*manager EA - Update with .mq4 version

 

Hi,

 

I attached the EA here, can someone add the max trades user input for this EA?

 

I would want the EA to stop placing orders once reached 30 open trades.

 

==============================================

#property copyright "Copyright © 2009, Sterling Blackstone"

#property link "http://www.sterlingblackstone.com"

 

#include <WinUser32.mqh>

/*

#import "MTFTP.dll"

string gConnect(string a0, string a1, string a2, string a3);

#import

*/

string gs_76 = "1.6";

extern bool TradeEnabled = true;

extern int TradeSpacing = 20;

extern int TradeTakeProfit = 20;

extern double TradeLotSize = 0.1;

extern int TradeStopLoss = 0;

extern int TradeMinRange = 0;

extern bool TradeCloseAll = FALSE;

extern string AverageHeader = "!=!=!Average Settings!=!=!";

extern bool AverageDynamic = FALSE;

extern double AveragePercentSpace = 0.0;

extern double AveragePercentTP = 0.0;

extern double AveragePercentSL = 0.0;

extern int AverageDays = 20;

extern int AverageMonths = 3;

extern bool InstantOrders = TRUE;

extern int MaxSpread = 8;

extern string DisplayHeader = "!=!=!Display Settings!=!=!";

extern bool DisplayOn = TRUE;

extern color DisplayTextColor = LightGray;

extern color DisplayDataColor = White;

extern int DisplayFontSize = 12;

extern bool DisplayArrows = TRUE;

extern int DisplayCornerMain = 1;

extern int DisplayCornerAvg = 0;

extern bool DisplayShowAnchor = TRUE;

extern color DisplayAnchorClr = LightBlue;

extern bool DisplayShowUpLine = TRUE;

extern color DisplayUpLineClr = Red;

extern bool DisplayShowDownLine = TRUE;

extern color DisplayDownLineClr = Green;

extern string NotificationHeader = "!=!=!Notification Settings!=!=!";

extern bool NotifyAlerts = TRUE;

extern bool NotifyLog = TRUE;

extern string OtherTradeHeader = "!=!=!Other Trade Settings!=!=!";

extern int TradeMagicNumber = 12345;

extern int TradeMaxAttempts = 5;

string gs_260 = "TM (OIL)";

int g_slippage_268 = 0;

bool gi_272;

bool gi_276;

int gi_unused_280;

int gi_284;

double gd_288;

double g_bid_296;

double gd_304;

double gd_312;

double gd_320;

double gd_328;

double gd_336;

double gd_344;

int g_count_352;

int g_count_356;

double gd_360;

string gs_368;

int gi_376;

bool gi_380;

bool gi_384;

bool gi_388 = TRUE;//FALSE;

int gi_392 = -1;

 

int init() {

gi_276 = TRUE;

if (TradeCloseAll) {

if (MessageBox("Close all trades?", "Close All Requested", MB_YESNO|MB_ICONQUESTION) == IDYES) {

closeAllTrades();

if (MessageBox("All Trades Closed. Continue running?", "Enable Trading", MB_YESNO|MB_ICONQUESTION) == IDYES) TradeEnabled = TRUE;

else TradeEnabled = FALSE;

}

}

resetADR();

int l_stoplevel_0 = MarketInfo(Symbol(), MODE_STOPLEVEL);

if (TradeStopLoss < l_stoplevel_0 && TradeStopLoss > 0) logError("TradeStopLoss value was less than Stop Level! Symbol = " + Symbol() + " Stop Loss = " + TradeStopLoss + " Stop Level = " + l_stoplevel_0);

if (TradeTakeProfit < l_stoplevel_0 && TradeTakeProfit > 0) logError("TradeTakeProfit value was less than Stop Level! Symbol = " + Symbol() + " Take Profit = " + TradeTakeProfit + " Stop Level = " + l_stoplevel_0);

if (TradeStopLoss < TradeSpacing && TradeStopLoss > 0) logError("TradeStopLoss value was less than TradeSpacing! Symbol = " + Symbol() + " Stop Loss = " + TradeStopLoss + " Trade Spacing = " + TradeSpacing);

if (TradeTakeProfit < TradeSpacing && TradeTakeProfit > 0) logError("TradeTakeProfit value was less than TradeSpacing! Symbol = " + Symbol() + " Take Profit = " + TradeTakeProfit + " Trade Spacing = " + TradeSpacing);

TradeLotSize = NormalizeDouble(TradeLotSize, 2);

gi_272 = TRUE;

gi_376 = 0;

gi_unused_280 = 0;

g_count_352 = 0;

g_count_356 = 0;

checkUserAuth(1);

updateDisplay();

return (0);

}

 

void checkUserAuth(int ai_0) {

/* if (IsDemo()) {

gs_368 = "DEMO";

gi_380 = TRUE;

return;

}

string l_acc_number_4 = AccountNumber();

string l_str_concat_12 = StringConcatenate("p", l_acc_number_4);

if (QuickCheck("ftp.powertradeliveauth.com", l_str_concat_12, AccountNumber(), ai_0)) {*/

gs_368 = "LIVE";

gi_380 = TRUE;

/* return;

}

gs_368 = "INVALID";

gi_380 = FALSE;*/

}

 

int deinit() {

ObjectsDeleteAll();

return (0);

}

 

int start() {

int li_0;

int l_ticket_4;

double l_price_8;

double l_price_16;

bool l_bool_24;

checkUserAuth(0);

if (gi_380) {

if ((Ask - Bid) / Point >= MaxSpread) logError("Trade Manager skipping this tick. Spread is too high! Bid=" + Bid + " Ask=" + Ask);

else {

if (GlobalVariableCheck("$EnableTrading")) gi_384 = GlobalVariableGet("$EnableTrading");

else gi_384 = TRUE;

if (GlobalVariableCheck("$TradeManagerReset")) {

li_0 = GlobalVariableGet("$TradeManagerReset");

if (li_0 == 1) {

Print("Recieved a \"TradeManagerReset\" command from Profit Manager!");

gi_272 = TRUE;

GlobalVariableSet("$TradeManagerReset", 0);

}

}

resetADR();

if (gi_272) {

resetAnchorPoint(Bid);

gi_unused_280 = 0;

gi_272 = FALSE;

}

if (Ask <= gd_304) {

gi_376 = 3;

if (TradeEnabled && gi_384 && checkMinRange()) {

if (InstantOrders) {

l_price_8 = Ask - TradeStopLoss * Point;

l_price_16 = Ask + TradeTakeProfit * Point;

if (TradeStopLoss != 0 && TradeTakeProfit != 0) l_ticket_4 = openOrder(Symbol(), OP_BUY, TradeLotSize, Ask, l_price_8, l_price_16, gs_260);

else {

if (TradeStopLoss != 0) l_ticket_4 = openOrder(Symbol(), OP_BUY, TradeLotSize, Ask, l_price_8, 0, gs_260);

else {

if (TradeTakeProfit != 0) l_ticket_4 = openOrder(Symbol(), OP_BUY, TradeLotSize, Ask, 0, l_price_16, gs_260);

else l_ticket_4 = openOrder(Symbol(), OP_BUY, TradeLotSize, Ask, 0, 0, gs_260);

}

}

if (l_ticket_4 < 1) logError("Error placing trade: BUY " + TradeLotSize + " Lots at " + Ask + " SL/TP = " + l_price_8 + "/" + l_price_16);

else placeLongIndicator(Ask);

} else {

l_ticket_4 = openOrder(Symbol(), OP_BUY, TradeLotSize, Ask, 0, 0, gs_260);

if (l_ticket_4 < 1) logError("Error placing market trade: BUY " + TradeLotSize + " Lots at " + Ask + " SL/TP = " + l_price_8 + "/" + l_price_16);

else {

if (!OrderSelect(l_ticket_4, SELECT_BY_TICKET, MODE_TRADES)) logError("OrderSelect failed for ticket #" + l_ticket_4 + " after opening market order. No SL or TP set!");

l_price_8 = OrderOpenPrice() - TradeStopLoss * Point;

l_price_16 = OrderOpenPrice() + TradeTakeProfit * Point;

NormalizeDouble(l_price_8, MarketInfo(Symbol(), MODE_DIGITS));

NormalizeDouble(l_price_16, MarketInfo(Symbol(), MODE_DIGITS));

if (TradeStopLoss != 0 && TradeTakeProfit != 0) l_bool_24 = OrderModify(l_ticket_4, OrderOpenPrice(), l_price_8, l_price_16, 0);

else {

if (TradeStopLoss != 0) l_bool_24 = OrderModify(l_ticket_4, OrderOpenPrice(), l_price_8, OrderTakeProfit(), 0);

else

if (TradeTakeProfit != 0) l_bool_24 = OrderModify(l_ticket_4, OrderOpenPrice(), OrderStopLoss(), l_price_16, 0);

}

if (l_bool_24 == FALSE) logError("OrderModify failed for ticket #" + l_ticket_4 + " after opening market order. No SL or TP set!");

}

placeLongIndicator(Ask);

}

}

gd_312 = gd_304;

gd_304 = gd_312 - gd_288 * Point;

resetDownLine(gd_304);

gd_344 = gd_304 + (gd_288 + TradeSpacing) * Point;

}

if (Bid >= gd_328) {

gi_376 = 2;

if (TradeEnabled && gi_384 && checkMinRange()) {

if (InstantOrders) {

l_price_8 = Bid + TradeStopLoss * Point;

l_price_16 = Bid - TradeTakeProfit * Point;

NormalizeDouble(l_price_8, MarketInfo(Symbol(), MODE_DIGITS));

NormalizeDouble(l_price_16, MarketInfo(Symbol(), MODE_DIGITS));

if (TradeStopLoss != 0 && TradeTakeProfit != 0) l_ticket_4 = openOrder(Symbol(), OP_SELL, TradeLotSize, Bid, l_price_8, l_price_16, gs_260);

else {

if (TradeStopLoss != 0) l_ticket_4 = openOrder(Symbol(), OP_SELL, TradeLotSize, Bid, l_price_8, 0, gs_260);

else {

if (TradeTakeProfit != 0) l_ticket_4 = openOrder(Symbol(), OP_SELL, TradeLotSize, Bid, 0, l_price_16, gs_260);

else l_ticket_4 = openOrder(Symbol(), OP_SELL, TradeLotSize, Bid, 0, 0, gs_260);

}

}

if (l_ticket_4 < 1) logError("Error placing instant trade: SELL " + TradeLotSize + " Lots at " + Bid + " SL/TP = " + l_price_8 + "/" + l_price_16);

else placeShortIndicator(Bid);

} else {

l_ticket_4 = openOrder(Symbol(), OP_SELL, TradeLotSize, Bid, 0, 0, gs_260);

if (l_ticket_4 < 1) logError("Error placing market trade: SELL " + TradeLotSize + " Lots at " + Bid + " SL/TP = " + l_price_8 + "/" + l_price_16);

else {

if (!OrderSelect(l_ticket_4, SELECT_BY_TICKET, MODE_TRADES)) logError("OrderSelect failed for ticket #" + l_ticket_4 + " after opening market order. No SL or TP set!");

l_price_8 = OrderOpenPrice() + TradeStopLoss * Point;

l_price_16 = OrderOpenPrice() - TradeTakeProfit * Point;

NormalizeDouble(l_price_8, MarketInfo(Symbol(), MODE_DIGITS));

NormalizeDouble(l_price_16, MarketInfo(Symbol(), MODE_DIGITS));

if (TradeStopLoss != 0 && TradeTakeProfit != 0) l_bool_24 = OrderModify(l_ticket_4, OrderOpenPrice(), l_price_8, l_price_16, 0);

else {

if (TradeStopLoss != 0) l_bool_24 = OrderModify(l_ticket_4, OrderOpenPrice(), l_price_8, OrderTakeProfit(), 0);

else

if (TradeTakeProfit != 0) l_bool_24 = OrderModify(l_ticket_4, OrderOpenPrice(), OrderStopLoss(), l_price_16, 0);

}

if (l_bool_24 == FALSE) logError("OrderModify failed for ticket #" + l_ticket_4 + " after opening market order. No SL or TP set!");

}

placeShortIndicator(Bid);

}

}

gd_336 = gd_328;

gd_328 = gd_336 + gd_288 * Point;

resetUpLine(gd_328);

gd_320 = gd_328 - (gd_288 + TradeSpacing) * Point;

}

if (Ask <= gd_320 && gi_376 == 2) {

gd_320 -= gd_288 * Point;

gd_328 -= gd_288 * Point;

resetUpLine(gd_328);

}

if (Bid >= gd_344 && gi_376 == 3) {

gd_344 += gd_288 * Point;

gd_304 += gd_288 * Point;

resetDownLine(gd_304);

}

updateDisplay();

gi_272 = FALSE;

}

}

return (0);

}

 

int checkMinRange() {

if (TradeMinRange == 0) return (1);

for (int l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) {

if (OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES)) {

if (OrderMagicNumber() == TradeMagicNumber) {

if (OrderType() == OP_BUY && MathAbs((OrderOpenPrice() - Ask) / Point) <= TradeMinRange) {

Print("BUY order #" + OrderTicket() + " was too close to new order! OrderOpenPrice=" + OrderOpenPrice() + " Ask =" + Ask);

return (0);

}

if (OrderType() == OP_SELL && MathAbs((OrderOpenPrice() - Bid) / Point) <= TradeMinRange) {

Print("SELL order #" + OrderTicket() + " was too close to new order! OrderOpenPrice=" + OrderOpenPrice() + " Bid =" + Bid);

return (0);

}

}

}

}

return (1);

}

 

void resetAnchorPoint(double a_price_0) {

Print("Resetting the anchor point to: " + Bid);

gi_284 = Ask - Bid;

gd_288 = TradeSpacing + gi_284 / Point;

g_bid_296 = Bid;

if (DisplayShowAnchor) {

ObjectDelete("AnchorLine");

ObjectCreate("AnchorLine", OBJ_HLINE, 0, Time[0], a_price_0);

ObjectSet("AnchorLine", OBJPROP_COLOR, DisplayAnchorClr);

ObjectSet("AnchorLine", OBJPROP_WIDTH, 2);

}

gd_344 = 999;

gd_328 = Ask + TradeSpacing * Point;

resetUpLine(gd_328);

gd_320 = 0;

gd_304 = Bid - TradeSpacing * Point;

resetDownLine(gd_304);

gi_unused_280 = 0;

gi_376 = 1;

}

 

void resetUpLine(double a_price_0) {

if (DisplayShowUpLine) {

ObjectDelete("LongLine");

ObjectCreate("LongLine", OBJ_HLINE, 0, Time[0], a_price_0);

ObjectSet("LongLine", OBJPROP_COLOR, DisplayUpLineClr);

ObjectSet("LongLine", OBJPROP_WIDTH, 2);

}

}

 

void resetDownLine(double a_price_0) {

if (DisplayShowDownLine) {

ObjectDelete("ShortLine");

ObjectCreate("ShortLine", OBJ_HLINE, 0, Time[0], a_price_0);

ObjectSet("ShortLine", OBJPROP_COLOR, DisplayDownLineClr);

ObjectSet("ShortLine", OBJPROP_WIDTH, 2);

}

}

 

void placeLongIndicator(double a_price_0) {

g_count_352++;

ObjectCreate("LongArrow" + g_count_352, OBJ_ARROW, 0, Time[0], a_price_0);

ObjectSet("LongArrow" + g_count_352, OBJPROP_COLOR, Green);

ObjectSet("LongArrow" + g_count_352, OBJPROP_ARROWCODE, 3);

ObjectSet("LongArrow" + g_count_352, OBJPROP_WIDTH, 3);

}

 

void placeShortIndicator(double a_price_0) {

g_count_356++;

ObjectCreate("ShortArrow" + g_count_356, OBJ_ARROW, 0, Time[0], a_price_0);

ObjectSet("ShortArrow" + g_count_356, OBJPROP_COLOR, Red);

ObjectSet("ShortArrow" + g_count_356, OBJPROP_ARROWCODE, 3);

ObjectSet("ShortArrow" + g_count_356, OBJPROP_WIDTH, 3);

}

 

void resetADR() {

gd_360 = GetDailyRange(AverageDays, 0);

if (AverageDynamic) {

TradeSpacing = gd_360 * AveragePercentSpace / 100.0;

TradeTakeProfit = gd_360 * AveragePercentTP / 100.0;

TradeStopLoss = gd_360 * AveragePercentSL / 100.0;

}

}

 

int openOrder(string as_0, int a_cmd_8, double ad_12, double a_price_20, double a_price_28, double a_price_36, string a_comment_44) {

int l_cmd_64;

int l_count_52 = 0;

RefreshRates();

if (a_cmd_8 == OP_BUY || a_cmd_8 == OP_BUYSTOP || a_cmd_8 == OP_BUYLIMIT) l_cmd_64 = 0;

else

if (a_cmd_8 == OP_SELL || a_cmd_8 == OP_SELLSTOP || a_cmd_8 == OP_SELLLIMIT) l_cmd_64 = 1;

AccountFreeMarginCheck(as_0, l_cmd_64, ad_12);

int l_error_60 = GetLastError();

if (l_error_60 > 0/* NO_ERROR */) {

logError("Error during margin check while opening a new position: " + l_error_60);

return (0);

}

int l_ticket_56 = OrderSend(as_0, a_cmd_8, ad_12, a_price_20, g_slippage_268, a_price_28, a_price_36, a_comment_44, TradeMagicNumber);

if (l_ticket_56 > 0) {

logInfo("Order #" + l_ticket_56 + " was opened successfully.");

return (l_ticket_56);

}

while (l_count_52 < TradeMaxAttempts) {

l_count_52++;

l_error_60 = processErrors();

if (l_error_60 == 1/* NO_RESULT */) {

Sleep(1000);

RefreshRates();

if (a_cmd_8 == OP_BUY) a_price_20 = Ask;

else

if (a_cmd_8 == OP_SELL) a_price_20 = Bid;

l_ticket_56 = OrderSend(as_0, a_cmd_8, ad_12, a_price_20, g_slippage_268, a_price_28, a_price_36, a_comment_44, TradeMagicNumber);

if (l_ticket_56 > 0) {

logInfo("Order #" + l_ticket_56 + " was opened successfully.");

return (l_ticket_56);

}

}

if (l_error_60 > 1/* NO_RESULT */) {

logError(TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS) + " - ERROR: Failed to place order due to critical error.");

return (0);

}

}

logError(TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS) + " - ERROR: Failed to place order after " + TradeMaxAttempts + " tries.");

return (0);

}

 

int closeOrder(int a_ticket_0, double a_lots_4) {

double l_price_12;

int li_20;

int l_count_24 = 0;

logInfo("Enter closeOrder with Ticket = " + a_ticket_0 + " Lots = " + a_lots_4);

a_lots_4 = NormalizeDouble(a_lots_4, 2);

while (l_count_24 < TradeMaxAttempts) {

if (OrderSelect(a_ticket_0, SELECT_BY_TICKET)) {

if (OrderType() == OP_BUY) l_price_12 = NormalizeDouble(MarketInfo(OrderSymbol(), MODE_BID), MarketInfo(OrderSymbol(), MODE_DIGITS));

if (OrderType() == OP_SELL) l_price_12 = NormalizeDouble(MarketInfo(OrderSymbol(), MODE_ASK), MarketInfo(OrderSymbol(), MODE_DIGITS));

logInfo("Calling OrderClose(" + OrderTicket() + "," + a_lots_4 + "," + l_price_12 + "," + g_slippage_268 + ")");

if (OrderClose(OrderTicket(), a_lots_4, l_price_12, TradeMaxAttempts)) {

logInfo("Order # " + OrderTicket() + " closed successfully...");

return (1);

}

l_count_24++;

li_20 = processErrors();

if (li_20 > 1) {

logError(TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS) + " - Failed to place order with serious error.. Manual closing of the order # " + OrderTicket() + " needed!");

return (0);

}

RefreshRates();

} else logInfo("OrderSelect by ticket failed during closeOrder!");

}

logError(TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS) + " - ERROR: Failed to place order after " + TradeMaxAttempts + " tries.. Manual closing of the order # " +

OrderTicket() + " needed!");

return (0);

}

 

void closeAllTrades() {

for (int l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) {

if (OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES))

if (OrderMagicNumber() == TradeMagicNumber) closeOrder(OrderTicket(), OrderLots());

}

}

 

int processErrors() {

bool l_bool_8;

int l_count_12;

int l_error_0 = GetLastError();

logInfo("Last error: " + l_error_0);

switch (l_error_0) {

case 0/* NO_ERROR */:

return (0);

case 2/* COMMON_ERROR */:

logError("System failure. Reboot the computer/check the server!");

return (3);

case 3/* INVALID_TRADE_PARAMETERS */:

logError("Error of the logic of the EA!");

return (3);

case 4/* SERVER_BUSY */:

logInfo("Trading server is busy! Wait for 2 minutes.");

Sleep(120000);

return (2);

case 6/* NO_CONNECTION */:

l_bool_8 = FALSE;

l_count_12 = 0;

logInfo("Disconnected!");

while (!l_bool_8 || l_count_12 > 60) {

Sleep(10000);

logInfo("Connection not restored " + (10 * l_count_12) + " seconds passed!");

l_bool_8 = IsConnected();

if (l_bool_8) {

logInfo("Connection restored");

return (2);

}

l_count_12++;

}

logError("Connection problems!");

return (3);

case 8/* TOO_FREQUENT_REQUESTS */:

logError("Frequent requests!");

return (3);

case 64/* ACCOUNT_DISABLED */:

logError("Account is blocked!");

return (3);

case 65/* INVALID_ACCOUNT */:

logError("Wrong account number!");

return (3);

case 128/* TRADE_TIMEOUT */:

logWarning("Waiting of transaction timed out!");

return (2);

case 129/* INVALID_PRICE */:

logWarning("Wrong price!");

return (1);

case 130/* INVALID_STOPS */:

logWarning("Wrong stop!");

return (1);

case 131/* INVALID_TRADE_VOLUME */:

logError("Wrong calculation of trade volume!");

return (3);

case 132/* MARKET_CLOSED */:

logError("Market closed!");

case 134/* NOT_ENOUGH_MONEY */:

logError("Lack of margin for performing operation!");

case 135/* PRICE_CHANGED */:

logWarning("Prices changed!");

return (1);

case 136/* OFF_QUOTES */:

logWarning("No price!");

return (2);

case 138/* REQUOTE */:

logWarning("Requote again!");

return (1);

case 139/* ORDER_LOCKED */:

logWarning("The order is in process. Program glitch!");

return (2);

case 141/* TOO_MANY_REQUESTS */:

logError("Too many requests!");

case 148:

logError("Transaction volume too large!");

}

return (0);

}

 

void logError(string as_0) {

if (NotifyAlerts) Alert(as_0);

if (NotifyLog) logMessage(as_0);

Print(as_0);

}

 

void logWarning(string as_0) {

if (NotifyLog) logMessage(as_0);

Print(as_0);

}

 

void logInfo(string as_0) {

if (NotifyLog) logMessage(as_0);

Print(as_0);

}

 

void logMessage(string as_unused_0) {

}

 

string getTradeEnabledString() {

if (TradeEnabled) return ("TRUE");

else return ("FALSE");

}

 

string getTrendDirectionString() {

if (gi_376 == 0) return ("INIT");

else {

if (gi_376 == 1) return ("FINDING");

else {

if (gi_376 == 2) return ("UP");

else

if (gi_376 == 3) return ("DOWN");

}

}

return ("");

}

 

string getTrendDirectionFont() {

if (gi_376 == 0) return ("Times New Roman");

else {

if (gi_376 == 1) return ("Times New Roman");

else {

if (gi_376 == 2) return ("Times New Roman");

else

if (gi_376 == 3) return ("Times New Roman");

}

}

return ("");

}

 

int getTrendDirectionColor() {

if (gi_376 == 0) return (DisplayDataColor);

else {

if (gi_376 == 1) return (DisplayDataColor);

else {

if (gi_376 == 2) return (32768);

else

if (gi_376 == 3) return (255);

}

}

return (0);

}

 

void updateDisplay() {

int li_0 = 1;

AdvancedComment(gi_276, DisplayCornerMain, li_0, gs_260 + " v" + gs_76, "", Gold, Gold, DisplayFontSize + 4, "Times New Roman", "Times New Roman");

li_0++;

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Account Type:", gs_368, DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Trade Enabled:", getTradeEnabledString(), DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Trade Spacing:", TradeSpacing, DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Take Profit:", TradeTakeProfit, DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Stop Loss:", TradeStopLoss, DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Lot Size:", DoubleToStr(TradeLotSize, 2), DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Anchor:", DoubleToStr(g_bid_296, MarketInfo(Symbol(), MODE_DIGITS)), DisplayAnchorClr, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Short Entry:", DoubleToStr(gd_328, MarketInfo(Symbol(), MODE_DIGITS)), DisplayUpLineClr, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Long Entry:", DoubleToStr(gd_304, MarketInfo(Symbol(), MODE_DIGITS)), DisplayDownLineClr, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Trend:", getTrendDirectionString(), DisplayTextColor, getTrendDirectionColor(), DisplayFontSize, "Times New Roman", getTrendDirectionFont());

li_0 = 1;

AdvancedComment(gi_276, DisplayCornerAvg, li_0, "ADR Days:", AverageDays, DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerAvg, li_0, "ADR:", DoubleToStr(gd_360, 0), DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerAvg, li_0, "AMR Months:", AverageMonths, DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

if (AverageMonths > 0) AdvancedComment(gi_276, DisplayCornerAvg, li_0, "AMR:", DoubleToStr(GetMonthlyRange(AverageMonths, 0), 0), DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

else AdvancedComment(gi_276, DisplayCornerAvg, li_0, "AMR:", DoubleToStr(GetMonthlyRange(AverageMonths, 0), 0), DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

gi_276 = FALSE;

ObjectsRedraw();

}

 

int AdvancedComment(bool ai_0, int a_corner_4, int ai_8, string a_text_12, string a_text_20, color a_color_28, color a_color_32, int ai_36, string a_fontname_40, string a_fontname_48) {

int l_fontsize_64;

int l_x_76;

int l_x_80;

int li_56 = 10;

int l_fontsize_60 = 10;

int li_68 = 15;

if (ai_36 > li_56) l_fontsize_64 = ai_36;

else l_fontsize_64 = li_56;

l_fontsize_60 = l_fontsize_64;

if (a_corner_4 == 0 || a_corner_4 == 2) {

l_x_76 = 5;

l_x_80 = 100;

} else {

l_x_76 = 70;

l_x_80 = 5;

}

int l_y_72 = ai_8 * li_68;

if (ai_0) {

ObjectDelete("PanelLabel" + ai_8 + a_corner_4);

ObjectCreate("PanelLabel" + ai_8 + a_corner_4, OBJ_LABEL, 0, 0, 0);

ObjectSetText("PanelLabel" + ai_8 + a_corner_4, a_text_12, l_fontsize_60, a_fontname_40, a_color_28);

ObjectSet("PanelLabel" + ai_8 + a_corner_4, OBJPROP_CORNER, a_corner_4);

ObjectSet("PanelLabel" + ai_8 + a_corner_4, OBJPROP_XDISTANCE, l_x_76);

ObjectSet("PanelLabel" + ai_8 + a_corner_4, OBJPROP_YDISTANCE, l_y_72);

ObjectDelete("PanelData" + ai_8 + a_corner_4);

ObjectCreate("PanelData" + ai_8 + a_corner_4, OBJ_LABEL, 0, 0, 0);

ObjectSet("PanelData" + ai_8 + a_corner_4, OBJPROP_CORNER, a_corner_4);

ObjectSet("PanelData" + ai_8 + a_corner_4, OBJPROP_XDISTANCE, l_x_80);

ObjectSet("PanelData" + ai_8 + a_corner_4, OBJPROP_YDISTANCE, l_y_72);

}

ObjectSetText("PanelData" + ai_8 + a_corner_4, a_text_20, l_fontsize_64, a_fontname_48, a_color_32);

return (0);

}

 

int GetDailyRange(int ai_0, int ai_unused_4) {

double l_ihigh_28;

double l_ilow_36;

bool li_8 = FALSE;

int l_index_12 = 0;

int l_count_16 = 0;

double ld_ret_44 = 0;

int l_day_20 = TimeDay(Time[0]);

int l_day_24 = l_day_20;

while (!li_8) {

if (TimeDay(Time[l_index_12]) != l_day_24 && TimeDayOfWeek(Time[l_index_12]) != 0) {

l_ihigh_28 = iHigh(Symbol(), PERIOD_D1, iBarShift(Symbol(), PERIOD_D1, Time[l_index_12]));

l_ilow_36 = iLow(Symbol(), PERIOD_D1, iBarShift(Symbol(), PERIOD_D1, Time[l_index_12]));

ld_ret_44 += (l_ihigh_28 - l_ilow_36) / Point;

l_count_16++;

if (l_count_16 == ai_0) li_8 = TRUE;

}

l_day_24 = TimeDay(Time[l_index_12]);

if (l_index_12 < Bars - 1) l_index_12++;

else li_8 = TRUE;

}

ld_ret_44 /= ai_0;

return (ld_ret_44);

}

 

int GetMonthlyRange(int ai_0, int ai_unused_4) {

double l_ihigh_28;

double l_ilow_36;

bool li_8 = FALSE;

int l_index_12 = 0;

int l_count_16 = 0;

double ld_ret_44 = 0;

if (ai_0 == 0) return (0);

int l_month_20 = TimeMonth(Time[0]);

int l_month_24 = l_month_20;

while (!li_8) {

if (TimeMonth(Time[l_index_12]) != l_month_24) {

l_ihigh_28 = iHigh(Symbol(), PERIOD_MN1, iBarShift(Symbol(), PERIOD_MN1, Time[l_index_12]));

l_ilow_36 = iLow(Symbol(), PERIOD_MN1, iBarShift(Symbol(), PERIOD_MN1, Time[l_index_12]));

ld_ret_44 += (l_ihigh_28 - l_ilow_36) / Point;

l_count_16++;

if (l_count_16 == ai_0) li_8 = TRUE;

}

l_month_24 = TimeMonth(Time[l_index_12]);

if (l_index_12 < Bars - 1) l_index_12++;

else li_8 = TRUE;

}

ld_ret_44 /= ai_0;

return (ld_ret_44);

}

/*

bool QuickCheck(string as_0, string as_8, string as_16, bool ai_24) {

string ls_28;

int l_str2int_36 = StrToInteger(as_8);

int li_40 = l_str2int_36 & 768 / 255 + 1;

int li_44 = l_str2int_36 & 240 / 16 + 1;

int li_48 = l_str2int_36 & 15;

string l_var_name_52 = as_8;

if (GlobalVariableCheck(l_var_name_52)) gi_392 = GlobalVariableGet(l_var_name_52);

else GlobalVariableSet(l_var_name_52, -1);

if (ai_24) {

ls_28 = gConnect(as_0, as_8, as_16, "ROOT");

if (ls_28 == "Connected") gi_388 = TRUE;

else gi_388 = FALSE;

GlobalVariableSet(l_var_name_52, DayOfWeek());

} else {

if (li_40 == DayOfWeek() && li_44 == Hour() && li_48 == Minute()) {

if (gi_392 != DayOfWeek()) {

ls_28 = gConnect(as_0, as_8, as_16, "ROOT");

if (ls_28 == "Connected") gi_388 = TRUE;

else gi_388 = FALSE;

}

GlobalVariableSet(l_var_name_52, DayOfWeek());

}

}

return (gi_388);

}

*/

 

======================================================

Link to comment
Share on other sites

Re: Trade*manager EA - Update with .mq4 version

 

I add some codes and here they are,

with MaxOpenTrades = 30;

 

Enjoy :)>-

 

#property copyright "Copyright © 2009, Sterling Blackstone"

#property link "http://www.sterlingblackstone.com"

 

#include <WinUser32.mqh>

/*

#import "MTFTP.dll"

string gConnect(string a0, string a1, string a2, string a3);

#import

*/

string gs_76 = "1.6";

extern bool TradeEnabled = true;

extern int TradeSpacing = 20;

extern int TradeTakeProfit = 20;

extern double TradeLotSize = 0.1;

extern int TradeStopLoss = 0;

extern int TradeMinRange = 0;

extern bool TradeCloseAll = FALSE;

extern string AverageHeader = "!=!=!Average Settings!=!=!";

extern bool AverageDynamic = FALSE;

extern double AveragePercentSpace = 0.0;

extern double AveragePercentTP = 0.0;

extern double AveragePercentSL = 0.0;

extern int AverageDays = 20;

extern int AverageMonths = 3;

extern bool InstantOrders = TRUE;

extern int MaxSpread = 8; //300;

extern string DisplayHeader = "!=!=!Display Settings!=!=!";

extern bool DisplayOn = TRUE;

extern color DisplayTextColor = LightGray;

extern color DisplayDataColor = White;

extern int DisplayFontSize = 12;

extern bool DisplayArrows = TRUE;

extern int DisplayCornerMain = 1;

extern int DisplayCornerAvg = 0;

extern bool DisplayShowAnchor = TRUE;

extern color DisplayAnchorClr = LightBlue;

extern bool DisplayShowUpLine = TRUE;

extern color DisplayUpLineClr = Red;

extern bool DisplayShowDownLine = TRUE;

extern color DisplayDownLineClr = Green;

extern string NotificationHeader = "!=!=!Notification Settings!=!=!";

extern bool NotifyAlerts = TRUE;

extern bool NotifyLog = TRUE;

extern string OtherTradeHeader = "!=!=!Other Trade Settings!=!=!";

extern int TradeMagicNumber = 12345;

extern int TradeMaxAttempts = 5;

extern int MaxOpenTrades = 30;

int NoOfTrades = 0;

string gs_260 = "Trade Manager";

int g_slippage_268 = 0;

bool gi_272;

bool gi_276;

int gi_unused_280;

int gi_284;

double gd_288;

double g_bid_296;

double gd_304;

double gd_312;

double gd_320;

double gd_328;

double gd_336;

double gd_344;

int g_count_352;

int g_count_356;

double gd_360;

string gs_368;

int gi_376;

bool gi_380;

bool gi_384;

bool gi_388 = TRUE;//FALSE;

int gi_392 = -1;

 

int init() {

gi_276 = TRUE;

if (TradeCloseAll) {

if (MessageBox("Close all trades?", "Close All Requested", MB_YESNO|MB_ICONQUESTION) == IDYES) {

closeAllTrades();

if (MessageBox("All Trades Closed. Continue running?", "Enable Trading", MB_YESNO|MB_ICONQUESTION) == IDYES) TradeEnabled = TRUE;

else TradeEnabled = FALSE;

}

}

resetADR();

int l_stoplevel_0 = MarketInfo(Symbol(), MODE_STOPLEVEL);

if (TradeStopLoss < l_stoplevel_0 && TradeStopLoss > 0) logError("TradeStopLoss value was less than Stop Level! Symbol = " + Symbol() + " Stop Loss = " + TradeStopLoss + " Stop Level = " + l_stoplevel_0);

if (TradeTakeProfit < l_stoplevel_0 && TradeTakeProfit > 0) logError("TradeTakeProfit value was less than Stop Level! Symbol = " + Symbol() + " Take Profit = " + TradeTakeProfit + " Stop Level = " + l_stoplevel_0);

if (TradeStopLoss < TradeSpacing && TradeStopLoss > 0) logError("TradeStopLoss value was less than TradeSpacing! Symbol = " + Symbol() + " Stop Loss = " + TradeStopLoss + " Trade Spacing = " + TradeSpacing);

if (TradeTakeProfit < TradeSpacing && TradeTakeProfit > 0) logError("TradeTakeProfit value was less than TradeSpacing! Symbol = " + Symbol() + " Take Profit = " + TradeTakeProfit + " Trade Spacing = " + TradeSpacing);

TradeLotSize = NormalizeDouble(TradeLotSize, 2);

gi_272 = TRUE;

gi_376 = 0;

gi_unused_280 = 0;

g_count_352 = 0;

g_count_356 = 0;

checkUserAuth(1);

updateDisplay();

return (0);

}

 

void checkUserAuth(int ai_0) {

/* if (IsDemo()) {

gs_368 = "DEMO";

gi_380 = TRUE;

return;

}

string l_acc_number_4 = AccountNumber();

string l_str_concat_12 = StringConcatenate("p", l_acc_number_4);

if (QuickCheck("ftp.powertradeliveauth.com", l_str_concat_12, AccountNumber(), ai_0)) {*/

gs_368 = "LIVE";

gi_380 = TRUE;

/* return;

}

gs_368 = "INVALID";

gi_380 = FALSE;*/

}

 

int deinit() {

ObjectsDeleteAll();

return (0);

}

 

int start() {

int li_0;

int l_ticket_4;

double l_price_8;

double l_price_16;

bool l_bool_24;

checkUserAuth(0);

if (gi_380) {

if ((Ask - Bid) / Point >= MaxSpread) logError("Trade Manager skipping this tick. Spread is too high! Bid=" + Bid + " Ask=" + Ask);

else {

if (GlobalVariableCheck("$EnableTrading")) gi_384 = GlobalVariableGet("$EnableTrading");

else gi_384 = TRUE;

if (GlobalVariableCheck("$TradeManagerReset")) {

li_0 = GlobalVariableGet("$TradeManagerReset");

if (li_0 == 1) {

Print("Recieved a \"TradeManagerReset\" command from Profit Manager!");

gi_272 = TRUE;

GlobalVariableSet("$TradeManagerReset", 0);

}

}

resetADR();

if (gi_272) {

resetAnchorPoint(Bid);

gi_unused_280 = 0;

gi_272 = FALSE;

}

if (Ask <= gd_304) {

gi_376 = 3;

if (TradeEnabled && gi_384 && checkMinRange()) {

if (InstantOrders) {

l_price_8 = Ask - TradeStopLoss * Point;

l_price_16 = Ask + TradeTakeProfit * Point;

if (TradeStopLoss != 0 && TradeTakeProfit != 0) l_ticket_4 = openOrder(Symbol(), OP_BUY, TradeLotSize, Ask, l_price_8, l_price_16, gs_260);

else {

if (TradeStopLoss != 0) l_ticket_4 = openOrder(Symbol(), OP_BUY, TradeLotSize, Ask, l_price_8, 0, gs_260);

else {

if (TradeTakeProfit != 0) l_ticket_4 = openOrder(Symbol(), OP_BUY, TradeLotSize, Ask, 0, l_price_16, gs_260);

else l_ticket_4 = openOrder(Symbol(), OP_BUY, TradeLotSize, Ask, 0, 0, gs_260);

}

}

if (l_ticket_4 < 1) logError("Error placing trade: BUY " + TradeLotSize + " Lots at " + Ask + " SL/TP = " + l_price_8 + "/" + l_price_16);

else placeLongIndicator(Ask);

} else {

l_ticket_4 = openOrder(Symbol(), OP_BUY, TradeLotSize, Ask, 0, 0, gs_260);

if (l_ticket_4 < 1) logError("Error placing market trade: BUY " + TradeLotSize + " Lots at " + Ask + " SL/TP = " + l_price_8 + "/" + l_price_16);

else {

if (!OrderSelect(l_ticket_4, SELECT_BY_TICKET, MODE_TRADES)) logError("OrderSelect failed for ticket #" + l_ticket_4 + " after opening market order. No SL or TP set!");

l_price_8 = OrderOpenPrice() - TradeStopLoss * Point;

l_price_16 = OrderOpenPrice() + TradeTakeProfit * Point;

NormalizeDouble(l_price_8, MarketInfo(Symbol(), MODE_DIGITS));

NormalizeDouble(l_price_16, MarketInfo(Symbol(), MODE_DIGITS));

if (TradeStopLoss != 0 && TradeTakeProfit != 0) l_bool_24 = OrderModify(l_ticket_4, OrderOpenPrice(), l_price_8, l_price_16, 0);

else {

if (TradeStopLoss != 0) l_bool_24 = OrderModify(l_ticket_4, OrderOpenPrice(), l_price_8, OrderTakeProfit(), 0);

else

if (TradeTakeProfit != 0) l_bool_24 = OrderModify(l_ticket_4, OrderOpenPrice(), OrderStopLoss(), l_price_16, 0);

}

if (l_bool_24 == FALSE) logError("OrderModify failed for ticket #" + l_ticket_4 + " after opening market order. No SL or TP set!");

}

placeLongIndicator(Ask);

}

}

gd_312 = gd_304;

gd_304 = gd_312 - gd_288 * Point;

resetDownLine(gd_304);

gd_344 = gd_304 + (gd_288 + TradeSpacing) * Point;

}

if (Bid >= gd_328) {

gi_376 = 2;

if (TradeEnabled && gi_384 && checkMinRange()) {

if (InstantOrders) {

l_price_8 = Bid + TradeStopLoss * Point;

l_price_16 = Bid - TradeTakeProfit * Point;

NormalizeDouble(l_price_8, MarketInfo(Symbol(), MODE_DIGITS));

NormalizeDouble(l_price_16, MarketInfo(Symbol(), MODE_DIGITS));

if (TradeStopLoss != 0 && TradeTakeProfit != 0) l_ticket_4 = openOrder(Symbol(), OP_SELL, TradeLotSize, Bid, l_price_8, l_price_16, gs_260);

else {

if (TradeStopLoss != 0) l_ticket_4 = openOrder(Symbol(), OP_SELL, TradeLotSize, Bid, l_price_8, 0, gs_260);

else {

if (TradeTakeProfit != 0) l_ticket_4 = openOrder(Symbol(), OP_SELL, TradeLotSize, Bid, 0, l_price_16, gs_260);

else l_ticket_4 = openOrder(Symbol(), OP_SELL, TradeLotSize, Bid, 0, 0, gs_260);

}

}

if (l_ticket_4 < 1) logError("Error placing instant trade: SELL " + TradeLotSize + " Lots at " + Bid + " SL/TP = " + l_price_8 + "/" + l_price_16);

else placeShortIndicator(Bid);

} else {

l_ticket_4 = openOrder(Symbol(), OP_SELL, TradeLotSize, Bid, 0, 0, gs_260);

if (l_ticket_4 < 1) logError("Error placing market trade: SELL " + TradeLotSize + " Lots at " + Bid + " SL/TP = " + l_price_8 + "/" + l_price_16);

else {

if (!OrderSelect(l_ticket_4, SELECT_BY_TICKET, MODE_TRADES)) logError("OrderSelect failed for ticket #" + l_ticket_4 + " after opening market order. No SL or TP set!");

l_price_8 = OrderOpenPrice() + TradeStopLoss * Point;

l_price_16 = OrderOpenPrice() - TradeTakeProfit * Point;

NormalizeDouble(l_price_8, MarketInfo(Symbol(), MODE_DIGITS));

NormalizeDouble(l_price_16, MarketInfo(Symbol(), MODE_DIGITS));

if (TradeStopLoss != 0 && TradeTakeProfit != 0) l_bool_24 = OrderModify(l_ticket_4, OrderOpenPrice(), l_price_8, l_price_16, 0);

else {

if (TradeStopLoss != 0) l_bool_24 = OrderModify(l_ticket_4, OrderOpenPrice(), l_price_8, OrderTakeProfit(), 0);

else

if (TradeTakeProfit != 0) l_bool_24 = OrderModify(l_ticket_4, OrderOpenPrice(), OrderStopLoss(), l_price_16, 0);

}

if (l_bool_24 == FALSE) logError("OrderModify failed for ticket #" + l_ticket_4 + " after opening market order. No SL or TP set!");

}

placeShortIndicator(Bid);

}

}

gd_336 = gd_328;

gd_328 = gd_336 + gd_288 * Point;

resetUpLine(gd_328);

gd_320 = gd_328 - (gd_288 + TradeSpacing) * Point;

}

if (Ask <= gd_320 && gi_376 == 2) {

gd_320 -= gd_288 * Point;

gd_328 -= gd_288 * Point;

resetUpLine(gd_328);

}

if (Bid >= gd_344 && gi_376 == 3) {

gd_344 += gd_288 * Point;

gd_304 += gd_288 * Point;

resetDownLine(gd_304);

}

updateDisplay();

gi_272 = FALSE;

}

}

return (0);

}

 

int checkMinRange() {

if (TradeMinRange == 0) return (1);

for (int l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) {

if (OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES)) {

if (OrderMagicNumber() == TradeMagicNumber) {

if (OrderType() == OP_BUY && MathAbs((OrderOpenPrice() - Ask) / Point) <= TradeMinRange) {

Print("BUY order #" + OrderTicket() + " was too close to new order! OrderOpenPrice=" + OrderOpenPrice() + " Ask =" + Ask);

return (0);

}

if (OrderType() == OP_SELL && MathAbs((OrderOpenPrice() - Bid) / Point) <= TradeMinRange) {

Print("SELL order #" + OrderTicket() + " was too close to new order! OrderOpenPrice=" + OrderOpenPrice() + " Bid =" + Bid);

return (0);

}

}

}

}

return (1);

}

 

void resetAnchorPoint(double a_price_0) {

Print("Resetting the anchor point to: " + Bid);

gi_284 = Ask - Bid;

gd_288 = TradeSpacing + gi_284 / Point;

g_bid_296 = Bid;

if (DisplayShowAnchor) {

ObjectDelete("AnchorLine");

ObjectCreate("AnchorLine", OBJ_HLINE, 0, Time[0], a_price_0);

ObjectSet("AnchorLine", OBJPROP_COLOR, DisplayAnchorClr);

ObjectSet("AnchorLine", OBJPROP_WIDTH, 2);

}

gd_344 = 999;

gd_328 = Ask + TradeSpacing * Point;

resetUpLine(gd_328);

gd_320 = 0;

gd_304 = Bid - TradeSpacing * Point;

resetDownLine(gd_304);

gi_unused_280 = 0;

gi_376 = 1;

}

 

void resetUpLine(double a_price_0) {

if (DisplayShowUpLine) {

ObjectDelete("LongLine");

ObjectCreate("LongLine", OBJ_HLINE, 0, Time[0], a_price_0);

ObjectSet("LongLine", OBJPROP_COLOR, DisplayUpLineClr);

ObjectSet("LongLine", OBJPROP_WIDTH, 2);

}

}

 

void resetDownLine(double a_price_0) {

if (DisplayShowDownLine) {

ObjectDelete("ShortLine");

ObjectCreate("ShortLine", OBJ_HLINE, 0, Time[0], a_price_0);

ObjectSet("ShortLine", OBJPROP_COLOR, DisplayDownLineClr);

ObjectSet("ShortLine", OBJPROP_WIDTH, 2);

}

}

 

void placeLongIndicator(double a_price_0) {

g_count_352++;

ObjectCreate("LongArrow" + g_count_352, OBJ_ARROW, 0, Time[0], a_price_0);

ObjectSet("LongArrow" + g_count_352, OBJPROP_COLOR, Green);

ObjectSet("LongArrow" + g_count_352, OBJPROP_ARROWCODE, 3);

ObjectSet("LongArrow" + g_count_352, OBJPROP_WIDTH, 3);

}

 

void placeShortIndicator(double a_price_0) {

g_count_356++;

ObjectCreate("ShortArrow" + g_count_356, OBJ_ARROW, 0, Time[0], a_price_0);

ObjectSet("ShortArrow" + g_count_356, OBJPROP_COLOR, Red);

ObjectSet("ShortArrow" + g_count_356, OBJPROP_ARROWCODE, 3);

ObjectSet("ShortArrow" + g_count_356, OBJPROP_WIDTH, 3);

}

 

void resetADR() {

gd_360 = GetDailyRange(AverageDays, 0);

if (AverageDynamic) {

TradeSpacing = gd_360 * AveragePercentSpace / 100.0;

TradeTakeProfit = gd_360 * AveragePercentTP / 100.0;

TradeStopLoss = gd_360 * AveragePercentSL / 100.0;

}

}

 

int openOrder(string as_0, int a_cmd_8, double ad_12, double a_price_20, double a_price_28, double a_price_36, string a_comment_44) {

int l_cmd_64;

int l_count_52 = 0;

RefreshRates();

CountTrades();

if (NoOfTrades >= MaxOpenTrades) return (0);

if (a_cmd_8 == OP_BUY || a_cmd_8 == OP_BUYSTOP || a_cmd_8 == OP_BUYLIMIT) l_cmd_64 = 0;

else

if (a_cmd_8 == OP_SELL || a_cmd_8 == OP_SELLSTOP || a_cmd_8 == OP_SELLLIMIT) l_cmd_64 = 1;

AccountFreeMarginCheck(as_0, l_cmd_64, ad_12);

int l_error_60 = GetLastError();

if (l_error_60 > 0/* NO_ERROR */) {

logError("Error during margin check while opening a new position: " + l_error_60);

return (0);

}

int l_ticket_56 = OrderSend(as_0, a_cmd_8, ad_12, a_price_20, g_slippage_268, a_price_28, a_price_36, a_comment_44, TradeMagicNumber);

if (l_ticket_56 > 0) {

logInfo("Order #" + l_ticket_56 + " was opened successfully.");

return (l_ticket_56);

}

while (l_count_52 < TradeMaxAttempts) {

l_count_52++;

l_error_60 = processErrors();

if (l_error_60 == 1/* NO_RESULT */) {

Sleep(1000);

RefreshRates();

if (a_cmd_8 == OP_BUY) a_price_20 = Ask;

else

if (a_cmd_8 == OP_SELL) a_price_20 = Bid;

l_ticket_56 = OrderSend(as_0, a_cmd_8, ad_12, a_price_20, g_slippage_268, a_price_28, a_price_36, a_comment_44, TradeMagicNumber);

if (l_ticket_56 > 0) {

logInfo("Order #" + l_ticket_56 + " was opened successfully.");

return (l_ticket_56);

}

}

if (l_error_60 > 1/* NO_RESULT */) {

logError(TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS) + " - ERROR: Failed to place order due to critical error.");

return (0);

}

}

logError(TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS) + " - ERROR: Failed to place order after " + TradeMaxAttempts + " tries.");

return (0);

}

 

int closeOrder(int a_ticket_0, double a_lots_4) {

double l_price_12;

int li_20;

int l_count_24 = 0;

logInfo("Enter closeOrder with Ticket = " + a_ticket_0 + " Lots = " + a_lots_4);

a_lots_4 = NormalizeDouble(a_lots_4, 2);

while (l_count_24 < TradeMaxAttempts) {

if (OrderSelect(a_ticket_0, SELECT_BY_TICKET)) {

if (OrderType() == OP_BUY) l_price_12 = NormalizeDouble(MarketInfo(OrderSymbol(), MODE_BID), MarketInfo(OrderSymbol(), MODE_DIGITS));

if (OrderType() == OP_SELL) l_price_12 = NormalizeDouble(MarketInfo(OrderSymbol(), MODE_ASK), MarketInfo(OrderSymbol(), MODE_DIGITS));

logInfo("Calling OrderClose(" + OrderTicket() + "," + a_lots_4 + "," + l_price_12 + "," + g_slippage_268 + ")");

if (OrderClose(OrderTicket(), a_lots_4, l_price_12, TradeMaxAttempts)) {

logInfo("Order # " + OrderTicket() + " closed successfully...");

return (1);

}

l_count_24++;

li_20 = processErrors();

if (li_20 > 1) {

logError(TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS) + " - Failed to place order with serious error.. Manual closing of the order # " + OrderTicket() + " needed!");

return (0);

}

RefreshRates();

} else logInfo("OrderSelect by ticket failed during closeOrder!");

}

logError(TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS) + " - ERROR: Failed to place order after " + TradeMaxAttempts + " tries.. Manual closing of the order # " +

OrderTicket() + " needed!");

return (0);

}

 

void closeAllTrades() {

for (int l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) {

if (OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES))

if (OrderMagicNumber() == TradeMagicNumber) closeOrder(OrderTicket(), OrderLots());

}

}

 

int processErrors() {

bool l_bool_8;

int l_count_12;

int l_error_0 = GetLastError();

logInfo("Last error: " + l_error_0);

switch (l_error_0) {

case 0/* NO_ERROR */:

return (0);

case 2/* COMMON_ERROR */:

logError("System failure. Reboot the computer/check the server!");

return (3);

case 3/* INVALID_TRADE_PARAMETERS */:

logError("Error of the logic of the EA!");

return (3);

case 4/* SERVER_BUSY */:

logInfo("Trading server is busy! Wait for 2 minutes.");

Sleep(120000);

return (2);

case 6/* NO_CONNECTION */:

l_bool_8 = FALSE;

l_count_12 = 0;

logInfo("Disconnected!");

while (!l_bool_8 || l_count_12 > 60) {

Sleep(10000);

logInfo("Connection not restored " + (10 * l_count_12) + " seconds passed!");

l_bool_8 = IsConnected();

if (l_bool_8) {

logInfo("Connection restored");

return (2);

}

l_count_12++;

}

logError("Connection problems!");

return (3);

case 8/* TOO_FREQUENT_REQUESTS */:

logError("Frequent requests!");

return (3);

case 64/* ACCOUNT_DISABLED */:

logError("Account is blocked!");

return (3);

case 65/* INVALID_ACCOUNT */:

logError("Wrong account number!");

return (3);

case 128/* TRADE_TIMEOUT */:

logWarning("Waiting of transaction timed out!");

return (2);

case 129/* INVALID_PRICE */:

logWarning("Wrong price!");

return (1);

case 130/* INVALID_STOPS */:

logWarning("Wrong stop!");

return (1);

case 131/* INVALID_TRADE_VOLUME */:

logError("Wrong calculation of trade volume!");

return (3);

case 132/* MARKET_CLOSED */:

logError("Market closed!");

case 134/* NOT_ENOUGH_MONEY */:

logError("Lack of margin for performing operation!");

case 135/* PRICE_CHANGED */:

logWarning("Prices changed!");

return (1);

case 136/* OFF_QUOTES */:

logWarning("No price!");

return (2);

case 138/* REQUOTE */:

logWarning("Requote again!");

return (1);

case 139/* ORDER_LOCKED */:

logWarning("The order is in process. Program glitch!");

return (2);

case 141/* TOO_MANY_REQUESTS */:

logError("Too many requests!");

case 148:

logError("Transaction volume too large!");

}

return (0);

}

 

void logError(string as_0) {

if (NotifyAlerts) Alert(as_0);

if (NotifyLog) logMessage(as_0);

Print(as_0);

}

 

void logWarning(string as_0) {

if (NotifyLog) logMessage(as_0);

Print(as_0);

}

 

void logInfo(string as_0) {

if (NotifyLog) logMessage(as_0);

Print(as_0);

}

 

void logMessage(string as_unused_0) {

}

 

string getTradeEnabledString() {

if (TradeEnabled) return ("TRUE");

else return ("FALSE");

}

 

string getTrendDirectionString() {

if (gi_376 == 0) return ("INIT");

else {

if (gi_376 == 1) return ("FINDING");

else {

if (gi_376 == 2) return ("UP");

else

if (gi_376 == 3) return ("DOWN");

}

}

return ("");

}

 

string getTrendDirectionFont() {

if (gi_376 == 0) return ("Times New Roman");

else {

if (gi_376 == 1) return ("Times New Roman");

else {

if (gi_376 == 2) return ("Times New Roman");

else

if (gi_376 == 3) return ("Times New Roman");

}

}

return ("");

}

 

int getTrendDirectionColor() {

if (gi_376 == 0) return (DisplayDataColor);

else {

if (gi_376 == 1) return (DisplayDataColor);

else {

if (gi_376 == 2) return (32768);

else

if (gi_376 == 3) return (255);

}

}

return (0);

}

 

void updateDisplay() {

int li_0 = 1;

AdvancedComment(gi_276, DisplayCornerMain, li_0, gs_260 + " v" + gs_76, "", Gold, Gold, DisplayFontSize + 4, "Times New Roman", "Times New Roman");

li_0++;

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Account Type:", gs_368, DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Trade Enabled:", getTradeEnabledString(), DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Trade Spacing:", TradeSpacing, DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Take Profit:", TradeTakeProfit, DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Stop Loss:", TradeStopLoss, DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Lot Size:", DoubleToStr(TradeLotSize, 2), DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Anchor:", DoubleToStr(g_bid_296, MarketInfo(Symbol(), MODE_DIGITS)), DisplayAnchorClr, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Short Entry:", DoubleToStr(gd_328, MarketInfo(Symbol(), MODE_DIGITS)), DisplayUpLineClr, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Long Entry:", DoubleToStr(gd_304, MarketInfo(Symbol(), MODE_DIGITS)), DisplayDownLineClr, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerMain, li_0, "Trend:", getTrendDirectionString(), DisplayTextColor, getTrendDirectionColor(), DisplayFontSize, "Times New Roman", getTrendDirectionFont());

li_0 = 1;

AdvancedComment(gi_276, DisplayCornerAvg, li_0, "ADR Days:", AverageDays, DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerAvg, li_0, "ADR:", DoubleToStr(gd_360, 0), DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

AdvancedComment(gi_276, DisplayCornerAvg, li_0, "AMR Months:", AverageMonths, DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

li_0++;

if (AverageMonths > 0) AdvancedComment(gi_276, DisplayCornerAvg, li_0, "AMR:", DoubleToStr(GetMonthlyRange(AverageMonths, 0), 0), DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

else AdvancedComment(gi_276, DisplayCornerAvg, li_0, "AMR:", DoubleToStr(GetMonthlyRange(AverageMonths, 0), 0), DisplayTextColor, DisplayDataColor, DisplayFontSize, "Times New Roman", "Times New Roman");

gi_276 = FALSE;

ObjectsRedraw();

}

 

int AdvancedComment(bool ai_0, int a_corner_4, int ai_8, string a_text_12, string a_text_20, color a_color_28, color a_color_32, int ai_36, string a_fontname_40, string a_fontname_48) {

int l_fontsize_64;

int l_x_76;

int l_x_80;

int li_56 = 10;

int l_fontsize_60 = 10;

int li_68 = 15;

if (ai_36 > li_56) l_fontsize_64 = ai_36;

else l_fontsize_64 = li_56;

l_fontsize_60 = l_fontsize_64;

if (a_corner_4 == 0 || a_corner_4 == 2) {

l_x_76 = 5;

l_x_80 = 100;

} else {

l_x_76 = 70;

l_x_80 = 5;

}

int l_y_72 = ai_8 * li_68;

if (ai_0) {

ObjectDelete("PanelLabel" + ai_8 + a_corner_4);

ObjectCreate("PanelLabel" + ai_8 + a_corner_4, OBJ_LABEL, 0, 0, 0);

ObjectSetText("PanelLabel" + ai_8 + a_corner_4, a_text_12, l_fontsize_60, a_fontname_40, a_color_28);

ObjectSet("PanelLabel" + ai_8 + a_corner_4, OBJPROP_CORNER, a_corner_4);

ObjectSet("PanelLabel" + ai_8 + a_corner_4, OBJPROP_XDISTANCE, l_x_76);

ObjectSet("PanelLabel" + ai_8 + a_corner_4, OBJPROP_YDISTANCE, l_y_72);

ObjectDelete("PanelData" + ai_8 + a_corner_4);

ObjectCreate("PanelData" + ai_8 + a_corner_4, OBJ_LABEL, 0, 0, 0);

ObjectSet("PanelData" + ai_8 + a_corner_4, OBJPROP_CORNER, a_corner_4);

ObjectSet("PanelData" + ai_8 + a_corner_4, OBJPROP_XDISTANCE, l_x_80);

ObjectSet("PanelData" + ai_8 + a_corner_4, OBJPROP_YDISTANCE, l_y_72);

}

ObjectSetText("PanelData" + ai_8 + a_corner_4, a_text_20, l_fontsize_64, a_fontname_48, a_color_32);

return (0);

}

 

int GetDailyRange(int ai_0, int ai_unused_4) {

double l_ihigh_28;

double l_ilow_36;

bool li_8 = FALSE;

int l_index_12 = 0;

int l_count_16 = 0;

double ld_ret_44 = 0;

int l_day_20 = TimeDay(Time[0]);

int l_day_24 = l_day_20;

while (!li_8) {

if (TimeDay(Time[l_index_12]) != l_day_24 && TimeDayOfWeek(Time[l_index_12]) != 0) {

l_ihigh_28 = iHigh(Symbol(), PERIOD_D1, iBarShift(Symbol(), PERIOD_D1, Time[l_index_12]));

l_ilow_36 = iLow(Symbol(), PERIOD_D1, iBarShift(Symbol(), PERIOD_D1, Time[l_index_12]));

ld_ret_44 += (l_ihigh_28 - l_ilow_36) / Point;

l_count_16++;

if (l_count_16 == ai_0) li_8 = TRUE;

}

l_day_24 = TimeDay(Time[l_index_12]);

if (l_index_12 < Bars - 1) l_index_12++;

else li_8 = TRUE;

}

ld_ret_44 /= ai_0;

return (ld_ret_44);

}

 

int GetMonthlyRange(int ai_0, int ai_unused_4) {

double l_ihigh_28;

double l_ilow_36;

bool li_8 = FALSE;

int l_index_12 = 0;

int l_count_16 = 0;

double ld_ret_44 = 0;

if (ai_0 == 0) return (0);

int l_month_20 = TimeMonth(Time[0]);

int l_month_24 = l_month_20;

while (!li_8) {

if (TimeMonth(Time[l_index_12]) != l_month_24) {

l_ihigh_28 = iHigh(Symbol(), PERIOD_MN1, iBarShift(Symbol(), PERIOD_MN1, Time[l_index_12]));

l_ilow_36 = iLow(Symbol(), PERIOD_MN1, iBarShift(Symbol(), PERIOD_MN1, Time[l_index_12]));

ld_ret_44 += (l_ihigh_28 - l_ilow_36) / Point;

l_count_16++;

if (l_count_16 == ai_0) li_8 = TRUE;

}

l_month_24 = TimeMonth(Time[l_index_12]);

if (l_index_12 < Bars - 1) l_index_12++;

else li_8 = TRUE;

}

ld_ret_44 /= ai_0;

return (ld_ret_44);

}

/*

bool QuickCheck(string as_0, string as_8, string as_16, bool ai_24) {

string ls_28;

int l_str2int_36 = StrToInteger(as_8);

int li_40 = l_str2int_36 & 768 / 255 + 1;

int li_44 = l_str2int_36 & 240 / 16 + 1;

int li_48 = l_str2int_36 & 15;

string l_var_name_52 = as_8;

if (GlobalVariableCheck(l_var_name_52)) gi_392 = GlobalVariableGet(l_var_name_52);

else GlobalVariableSet(l_var_name_52, -1);

if (ai_24) {

ls_28 = gConnect(as_0, as_8, as_16, "ROOT");

if (ls_28 == "Connected") gi_388 = TRUE;

else gi_388 = FALSE;

GlobalVariableSet(l_var_name_52, DayOfWeek());

} else {

if (li_40 == DayOfWeek() && li_44 == Hour() && li_48 == Minute()) {

if (gi_392 != DayOfWeek()) {

ls_28 = gConnect(as_0, as_8, as_16, "ROOT");

if (ls_28 == "Connected") gi_388 = TRUE;

else gi_388 = FALSE;

}

GlobalVariableSet(l_var_name_52, DayOfWeek());

}

}

return (gi_388);

}

*/

 

void CountTrades(){

NoOfTrades = 0;

for (int Counter = OrdersTotal() - 1; Counter >= 0; Counter--)

{

OrderSelect(Counter, SELECT_BY_POS);

if (OrderType() == OP_BUY || OrderType() == OP_SELL)NoOfTrades++;

}

}

Link to comment
Share on other sites

Re: Trade*manager EA - Update with .mq4 version

 

Hi tom,

 

Basically, this EA is a counter trend grid placing. When the trend is up, the EA would place sell orders for the account.

 

By having a max orders in place, we can be sure that the EA does not placed any new orders and waiting for the current orders to close off.

 

I have run GRID trading EA and the only way to be successful in grid is to trade few pairs and reasonable lotsize with max orders.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...