Jump to content

Recommended Posts

Posted

i know that there alot of talented coders here...

please add a time filter for this one.... [-O<

 

extern int PercentEquityToClose = 10;
extern int Dist = 500;
extern int TP = 500;
extern int SL = 500;
extern int kolOrder = 10;
extern double Lots = 0.1;
extern int Slippage = 1;
extern int Magic = 1;
extern bool TrailingProfit = TRUE;
extern int prchoose = 0;
extern int TrailingStop = 0;
extern string File_Name = "Ask.csv";
bool gi_unused_132 = FALSE;
double gd_136;
double gd_144;
double gd_152;
double gd_172;

int init() {
  gd_172 = AccountBalance();
  if (IsTesting() || IsOptimization()) gd_152 = Ask;
  else gd_152 = Fileread();
  return (0);
}

int start() {
  int li_0 = Dist;
  for (int li_4 = 1; li_4 <= kolOrder; li_4++) {
     if (SL > 0) gd_136 = gd_152 + li_0 * Point - SL * Point;
     else gd_136 = 0;
     if (TP > 0) gd_144 = gd_152 + li_0 * Point + TP * Point;
     else gd_144 = 0;
     if (!ExistOrders("", OP_BUYSTOP, Magic, 0, NormalizeDouble(gd_152 + li_0 * Point, Digits))) SetOrder("", OP_BUYSTOP, Lots, gd_152 + li_0 * Point, gd_136, gd_144, Magic);
     if (SL > 0) gd_136 = gd_152 - li_0 * Point + SL * Point;
     else gd_136 = 0;
     if (TP > 0) gd_144 = gd_152 - li_0 * Point - TP * Point;
     else gd_144 = 0;
     if (!ExistOrders("", OP_SELLSTOP, Magic, 0, NormalizeDouble(gd_152 - li_0 * Point, Digits))) SetOrder("", OP_SELLSTOP, Lots, gd_152 - li_0 * Point, gd_136, gd_144, Magic);
     li_0 += Dist;
  }
  if (gd_152 + Dist * Point < Ask) gd_152 += Dist * Point;
  if (gd_152 - Dist * Point > Ask) gd_152 -= Dist * Point;
  if (TrailingProfit) TProf();
  if (PercentEquityToClose != 0) {
     if (gd_172 / 100.0 * PercentEquityToClose + gd_172 <= AccountEquity()) {
        CPD("", -1, Magic);
        gd_172 = AccountBalance();
     }
  }
  return (0);
}

void CPBS() {
  bool l_ord_close_0;
  color l_color_4;
  double l_ord_lots_8;
  double l_ask_16;
  double l_bid_24;
  double l_price_32;
  int l_error_40;
  if (OrderType() == OP_BUY || OrderType() == OP_SELL) {
     for (int li_44 = 1; li_44 <= 1; li_44++) {
        if (!IsTesting() && !IsExpertEnabled() || IsStopped()) break;
        while (!IsTradeAllowed()) Sleep(5000);
        RefreshRates();
        l_ask_16 = MarketInfo(OrderSymbol(), MODE_ASK);
        l_bid_24 = MarketInfo(OrderSymbol(), MODE_BID);
        if (OrderType() == OP_BUY) l_price_32 = l_bid_24;
        else l_price_32 = l_ask_16;
        l_ord_lots_8 = OrderLots();
        l_ord_close_0 = OrderClose(OrderTicket(), l_ord_lots_8, l_price_32, Slippage, l_color_4);
        if (l_ord_close_0) break;
        l_error_40 = GetLastError();
        if (l_error_40 == 146/* TRADE_CONTEXT_BUSY */) while (IsTradeContextBusy()) Sleep(11000);
        Sleep(5000);
     }
  }
}

void CPD(string as_0 = "", int a_cmd_8 = -1, int a_magic_12 = -1) {
  int l_ord_total_20 = OrdersTotal();
  if (as_0 == "0") as_0 = Symbol();
  for (int l_pos_16 = l_ord_total_20 - 1; l_pos_16 >= 0; l_pos_16--) {
     if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES)) {
        if (OrderSymbol() == as_0 || as_0 == "" && a_cmd_8 < OP_BUY || OrderType() == a_cmd_8) {
           if (OrderType() == OP_BUY || OrderType() == OP_SELL)
              if (a_magic_12 < 0 || OrderMagicNumber() == a_magic_12) CPBS();
        }
     }
  }
}

void TProf() {
  int l_cmd_4;
  double l_price_8;
  double ld_16;
  bool li_24;
  double l_ord_open_price_32;
  int l_ticket_40;
  bool l_bool_44;
  if (prchoose == 0) prchoose = MarketInfo(Symbol(), MODE_STOPLEVEL);
  for (int li_0 = 1; li_0 <= OrdersTotal(); li_0++) {
     if (OrderSelect(li_0 - 1, SELECT_BY_POS) == TRUE) {
        l_cmd_4 = OrderType();
        l_price_8 = OrderStopLoss();
        while (true) {
           if (TrailingStop == 0) TrailingStop = MarketInfo(Symbol(), MODE_STOPLEVEL);
           ld_16 = TrailingStop;
           li_24 = FALSE;
           switch (l_cmd_4) {
           case OP_BUY:
              if (NormalizeDouble(l_price_8, Digits) < NormalizeDouble(Bid - ld_16 * Point, Digits) && MarketInfo(OrderSymbol(), MODE_BID) - OrderOpenPrice() >= prchoose * Point) {
                 l_price_8 = Bid - ld_16 * Point;
                 li_24 = TRUE;
              }
              break;
           case OP_SELL:
              if (NormalizeDouble(l_price_8, Digits) > NormalizeDouble(Ask + ld_16 * Point, Digits) || NormalizeDouble(l_price_8, Digits) == 0.0 && OrderOpenPrice() - MarketInfo(OrderSymbol(), MODE_ASK) >= prchoose * Point) {
                 l_price_8 = Ask + ld_16 * Point;
                 li_24 = TRUE;
              }
           }
           if (li_24 == FALSE) break;
           l_ord_open_price_32 = OrderOpenPrice();
           l_ticket_40 = OrderTicket();
           l_bool_44 = OrderModify(l_ticket_40, l_ord_open_price_32, l_price_8, OrderTakeProfit(), 0, Green);
           if (l_bool_44 != TRUE) continue;
           break;
        }
     }
  }
}

int SetOrder(string a_symbol_0, int a_cmd_8, double a_lots_12, double a_price_20, double a_price_28 = 0.0, double a_price_36 = 0.0, int a_magic_44 = 0, int a_datetime_48 = 0, string a_comment_52 = "") {
  color l_color_60;
  int l_datetime_64;
  double l_ask_68;
  double l_bid_76;
  double l_point_84;
  int l_error_92;
  int l_ticket_100;
  if (a_symbol_0 == "" || a_symbol_0 == "0") a_symbol_0 = Symbol();
  int l_stoplevel_104 = MarketInfo(a_symbol_0, MODE_STOPLEVEL);
  if (a_datetime_48 > 0 && a_datetime_48 < TimeCurrent()) a_datetime_48 = 0;
  for (int li_96 = 1; li_96 <= 1; li_96++) {
     if (!IsTesting() && !IsExpertEnabled() || IsStopped()) {
        Print("SetOrder(): ???à?îâêà ?à?î?û ô??ê?èè");
        break;
     }
     while (!IsTradeAllowed()) Sleep(5000);
     RefreshRates();
     l_datetime_64 = TimeCurrent();
     l_ticket_100 = OrderSend(a_symbol_0, a_cmd_8, a_lots_12, a_price_20, Slippage, a_price_28, a_price_36, a_comment_52, a_magic_44, a_datetime_48, l_color_60);
     if (l_ticket_100 > 0) return (l_ticket_100);
     l_error_92 = GetLastError();
     if (l_error_92 == 128/* TRADE_TIMEOUT */ || l_error_92 == 142 || l_error_92 == 143) {
        Sleep(66000);
        if (ExistOrders(a_symbol_0, a_cmd_8, a_magic_44, l_datetime_64)) break;
     } else {
        l_point_84 = MarketInfo(a_symbol_0, MODE_POINT);
        l_ask_68 = MarketInfo(a_symbol_0, MODE_ASK);
        l_bid_76 = MarketInfo(a_symbol_0, MODE_BID);
        if (l_ask_68 == 0.0 && l_bid_76 == 0.0) Comment("SetOrder(): ??îâ??ü?? â î?çî?? ?û?êà ?àëè÷è? ?è?âîëà " + a_symbol_0);
        if (l_error_92 == 130/* INVALID_STOPS */) {
           switch (a_cmd_8) {
           case OP_BUYLIMIT:
              if (a_price_20 > l_ask_68 - l_stoplevel_104 * l_point_84) a_price_20 = l_ask_68 - l_stoplevel_104 * l_point_84;
              if (a_price_28 > a_price_20 - (l_stoplevel_104 + 1) * l_point_84) a_price_28 = a_price_20 - (l_stoplevel_104 + 1) * l_point_84;
              if (a_price_36 > 0.0 && a_price_36 < a_price_20 + (l_stoplevel_104 + 1) * l_point_84) a_price_36 = a_price_20 + (l_stoplevel_104 + 1) * l_point_84;
              break;
           case OP_BUYSTOP:
              if (a_price_20 < l_ask_68 + (l_stoplevel_104 + 1) * l_point_84) a_price_20 = l_ask_68 + (l_stoplevel_104 + 1) * l_point_84;
              if (a_price_28 > a_price_20 - (l_stoplevel_104 + 1) * l_point_84) a_price_28 = a_price_20 - (l_stoplevel_104 + 1) * l_point_84;
              if (a_price_36 > 0.0 && a_price_36 < a_price_20 + (l_stoplevel_104 + 1) * l_point_84) a_price_36 = a_price_20 + (l_stoplevel_104 + 1) * l_point_84;
              break;
           case OP_SELLLIMIT:
              if (a_price_20 < l_bid_76 + l_stoplevel_104 * l_point_84) a_price_20 = l_bid_76 + l_stoplevel_104 * l_point_84;
              if (a_price_28 > 0.0 && a_price_28 < a_price_20 + (l_stoplevel_104 + 1) * l_point_84) a_price_28 = a_price_20 + (l_stoplevel_104 + 1) * l_point_84;
              if (a_price_36 > a_price_20 - (l_stoplevel_104 + 1) * l_point_84) a_price_36 = a_price_20 - (l_stoplevel_104 + 1) * l_point_84;
              break;
           case OP_SELLSTOP:
              if (a_price_20 > l_bid_76 - l_stoplevel_104 * l_point_84) a_price_20 = l_bid_76 - l_stoplevel_104 * l_point_84;
              if (a_price_28 > 0.0 && a_price_28 < a_price_20 + (l_stoplevel_104 + 1) * l_point_84) a_price_28 = a_price_20 + (l_stoplevel_104 + 1) * l_point_84;
              if (a_price_36 > a_price_20 - (l_stoplevel_104 + 1) * l_point_84) a_price_36 = a_price_20 - (l_stoplevel_104 + 1) * l_point_84;
           }
           Print("SetOrder(): ?êî???ê?è?îâà?û ???îâû? ??îâ?è");
        }
        if (l_error_92 == 2/* COMMON_ERROR */ || l_error_92 == 64/* ACCOUNT_DISABLED */ || l_error_92 == 65/* INVALID_ACCOUNT */ || l_error_92 == 133/* TRADE_DISABLED */) break;
        if (l_error_92 == 4/* SERVER_BUSY */ || l_error_92 == 131/* INVALID_TRADE_VOLUME */ || l_error_92 == 132/* MARKET_CLOSED */) {
           Sleep(300000);
           break;
        }
        if (l_error_92 == 8/* TOO_FREQUENT_REQUESTS */ || l_error_92 == 141/* TOO_MANY_REQUESTS */) Sleep(100000);
        if (l_error_92 == 139/* ORDER_LOCKED */ || l_error_92 == 140/* LONG_POSITIONS_ONLY_ALLOWED */ || l_error_92 == 148/* ERR_TRADE_TOO_MANY_ORDERS */) break;
        if (l_error_92 == 146/* TRADE_CONTEXT_BUSY */) while (IsTradeContextBusy()) Sleep(11000);
        if (l_error_92 == 147/* ERR_TRADE_EXPIRATION_DENIED */) a_datetime_48 = 0;
        else
           if (l_error_92 != 135/* PRICE_CHANGED */ && l_error_92 != 138/* REQUOTE */) Sleep(7700.0);
     }
  }
  return (0);
}

bool ExistOrders(string as_0 = "", int a_cmd_8 = -1, int a_magic_12 = -1, int ai_16 = 0, double ad_20 = 0.0) {
  int l_cmd_36;
  int l_ord_total_32 = OrdersTotal();
  if (as_0 == "0") as_0 = Symbol();
  for (int l_pos_28 = 0; l_pos_28 < l_ord_total_32; l_pos_28++) {
     if (OrderSelect(l_pos_28, SELECT_BY_POS, MODE_TRADES)) {
        l_cmd_36 = OrderType();
        if (l_cmd_36 > OP_SELL && l_cmd_36 < 6) {
           if (OrderSymbol() == as_0 || as_0 == "" && a_cmd_8 < OP_BUY || l_cmd_36 == a_cmd_8) {
              if (a_magic_12 < 0 || OrderMagicNumber() == a_magic_12) {
                 if (ai_16 <= OrderOpenTime()) {
                    if (ad_20 != 0.0)
                       if (NormalizeDouble(ad_20, Digits) == NormalizeDouble(OrderOpenPrice(), Digits)) return (TRUE);
                 }
              }
           }
        }
     }
  }
  return (FALSE);
}

void Filewrite(double ad_0 = 0.0) {
  int l_file_8 = FileOpen(File_Name, FILE_CSV|FILE_WRITE, ";");
  if (l_file_8 == -1) {
     Print("??è?êà ï?è î?ê?û?èè ôàéëà. ", "?îç?î??î, ôàéë çà??? ????è? ï?èëî???è??");
     return;
  }
  int li_12 = FileWrite(l_file_8, ad_0);
  if (li_12 < 0) {
     Print("??è?êà çàïè?è â ôàéë ", GetLastError());
     FileClose(l_file_8);
     return;
  }
  FileClose(l_file_8);
}

double Fileread() {
  int l_file_0 = FileOpen(File_Name, FILE_CSV|FILE_READ, ";");
  if (l_file_0 == -1) {
     Print("?àéë ëè?î çà???,ëè?î ??î ?? ??ù???â???, . ", "?îç?à?? ôàéë ?àé???à");
     Filewrite(Ask);
     return (Ask);
  }
  double ld_ret_4 = FileReadDouble(l_file_0);
  if (ld_ret_4 < 0.0) {
     Print("??è?êà ÷???è? èç ôàéëà ", GetLastError());
     return ((l_file_0));
  }
  FileClose(l_file_0);
  return (ld_ret_4);
}

Posted

Re: time filter

 

Hi deadsoul,

 

Good day,

 

It seems that the time filter didn't work. It is O.K then :) .

 

I changed the time filter on your previous thread so now you can check it whether it works or not.

 

Best wishes,

a New Year 2011 has come, and the challenge has just started 8-)

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...