A very simple partial closing EA

1
I am looking for help to code a very simple partial closing EA as follows.

//SET VISIBLE STOP LOSS
extern double *number of pips*

EXAMPLE. 10 pips.

//SET FIRST TARGET (T1) AS A PROPORTION OF THE STOPLOSS

extern double *PROPORTION OF STOP LOSS MULTIPLE*

EXAMPLE. 1.5 - means first target (partial close) will close at 1.5 x the stop loss

***IF PROPORTION IS TOO GRANULAR, like 0.3 x 0.23 lots, rounds up to nearest acceptable***

//SLIPPAGE

extern double slippagefirsttarget *number of pips to be added to the target to account for slippage*

//SET PARTIAL CLOSE AT TARGET 1 AS A DECIMAL PROPORTION OF ORIGINAL POSITION

extern double *amount of position to close at first target between 0 and 1*

EXAMPLE. 0.75 - to close 0.75 of the position at the first target

//STOP TO BREAK EVEN + p number of PIPS AT REACH OF T1.

extern double bepips = 0 (or higher - moves stop to break even + bepips when T1 is reached)

//SET SECOND TARGET (T2) AS A MULTIPLE OF THE ORIGINAL STOPLOSS

extern double secondtarget *MULTIPLE OF STOPLOSS* --- must be larger than first target.

//SLIPPAGE SECOND TARGET

*how much to add in pips to second target to allow for slippage.*

I will try to research how to do this, but if an experienced coder wants to see to it, of course, it can be developed on this thread.

SUMMARY.

Set stop
Set target 1 (as a multiple of stop)
Set immediate stop loss movement to break even + p pips when T1 is hit.
Set amount to close at target 1 (as a proportion of the position size)
Set target 2 (multiple of stop)
---------------------------------------------------------
Set slippage control for target 1
Set slippage control for target 2
---------------------------------------------------------
Possible Additional options
1. Hidden stop / tp / closes options
2. Round up amount to close if proportion is not exact for broker minimums
3. Add 3rd target element (and 2nd partial close amount)
4. Display risk to reward minimum (i.e. closed out partial at first target, stopped out at BE+n) and maximum (closed out partial at T1, closed out remainder at T2 for profitable trade sequence.

***I found this***

Code: Select all


/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2009, IBFX"
#property link      "http://www.IBFX.com"

extern int InstanceID = 5;
extern int TicketNumber = 0;
extern int PipsTarget_1 = 140;
extern double CloseLotsPercentage_1 = 75.0;
extern int Move_SL_1 = 1;
extern int PipsTarget_2 = 210;
extern double CloseLotsPercentage_2 = 100;
extern int Move_SL_2 = 0;
extern int PipsTarget_3 = 50;
extern double CloseLotsPercentage_3 = 0.0;
extern int Move_SL_3 = 0;
extern int PipsTarget_4 = 100;
extern double CloseLotsPercentage_4 = 0.0;
extern int Move_SL_4 = 0;
string gs_ibfx_pc__148 = "IBFX-PC-";
int g_str_len_156 = 10;
int gi_160 = 1;
bool gi_164 = FALSE;
int abc = 2;

int init() {
   gs_ibfx_pc__148 = "IBFX-PC-" + InstanceID + "-";
   g_str_len_156 = StringLen(gs_ibfx_pc__148);
   if (MarketInfo(Symbol(), MODE_MINLOT) == 0.01) gi_160 = 2;
   fnGvCheck();
   return (0);
}

int start() {
   int l_ticket_4;
   int li_8;
   int l_global_var_12;
   int li_16;
   int li_unused_20;
   int li_24;
   double l_lots_28;
   bool l_ord_close_40;
   bool l_bool_44;
   double l_point_48;
   if (gi_164) return (0);
   if (StringFind(AccountServer(), "InterbankFX", 0) == 1 && StringFind(AccountServer(), "IBFX", 0) == 1) {
      Alert("InterbankFX users Only!!!");
      gi_164 = TRUE;
      return (0);
   }
   if (!IsExpertEnabled()) {
      Alert("You did not allow your expert to run live, change your settings and apply the expert again");
      gi_164 = TRUE;
      return (0);
   }
   for (int l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) {
      if (OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES)) {
         l_ticket_4 = OrderTicket();
         if (l_ticket_4 == TicketNumber || GlobalVariableCheck(StringConcatenate(gs_ibfx_pc__148, l_ticket_4))) {
            li_8 = fnGetPipProfit(OrderSymbol(), OrderOpenPrice(), OrderType());
            if (!GlobalVariableCheck(StringConcatenate(gs_ibfx_pc__148, l_ticket_4))) GlobalVariableSet(StringConcatenate(gs_ibfx_pc__148, DoubleToStr(l_ticket_4, 0)), 1);
            l_global_var_12 = GlobalVariableGet(StringConcatenate(gs_ibfx_pc__148, DoubleToStr(l_ticket_4, 0)));
            li_16 = 0;
            li_unused_20 = 0;
            li_24 = 0;
            l_lots_28 = 0.0;
            switch (l_global_var_12) {
            case 1:
               li_24 = Move_SL_1;
               li_16 = PipsTarget_1;
               l_lots_28 = NormalizeDouble(OrderLots() * CloseLotsPercentage_1 / 100.0, gi_160);
               break;
            case 2:
               li_24 = Move_SL_2;
               li_16 = PipsTarget_2;
               l_lots_28 = NormalizeDouble(OrderLots() * CloseLotsPercentage_2 / 100.0, gi_160);
               break;
            case 3:
               li_24 = Move_SL_3;
               li_16 = PipsTarget_3;
               l_lots_28 = NormalizeDouble(OrderLots() * CloseLotsPercentage_3 / 100.0, gi_160);
               break;
            case 4:
               li_24 = Move_SL_4;
               li_16 = PipsTarget_4;
               l_lots_28 = NormalizeDouble(OrderLots() * CloseLotsPercentage_4 / 100.0, gi_160);
               break;
            default:
               li_24 = 0;
               l_lots_28 = 0.0;
               li_16 = 0;
            }
            if (li_8 >= li_16 && li_16 != 0) {
               l_ord_close_40 = FALSE;
               l_bool_44 = FALSE;
               l_point_48 = MarketInfo(OrderSymbol(), MODE_POINT);
               if (l_point_48 == 0.001) l_point_48 = 0.01;
               else
                  if (l_point_48 == 0.00001) l_point_48 = 0.0001;
               switch (OrderType()) {
               case OP_BUY:
                  if (li_24 > 0) {
                     RefreshRates();
                     fnWait();
                     l_bool_44 = OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + li_24 * l_point_48, OrderTakeProfit(), 0, CLR_NONE);
                  }
                  if (l_lots_28 != 0.0) {
                     RefreshRates();
                     fnWait();
                     l_ord_close_40 = OrderClose(l_ticket_4, l_lots_28, MarketInfo(OrderSymbol(), MODE_BID), 0, CLR_NONE);
                  }
                  break;
               case OP_SELL:
                  if (li_24 > 0) {
                     RefreshRates();
                     fnWait();
                     l_bool_44 = OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - li_24 * l_point_48, OrderTakeProfit(), 0, CLR_NONE);
                  }
                  if (l_lots_28 != 0.0) {
                     RefreshRates();
                     fnWait();
                     l_ord_close_40 = OrderClose(l_ticket_4, l_lots_28, MarketInfo(OrderSymbol(), MODE_ASK), 0, CLR_NONE);
                  }
               }
               if (l_ord_close_40 || l_bool_44) fnCreateNewGv(l_ticket_4, l_global_var_12);
            }
         }
      }
   }
   fnGvCheck();
   return (0);
}

void fnWait() {
   if (IsConnected() && IsTradeAllowed()) while (IsTradeContextBusy()) Sleep(75);
}

void fnGvCheck() {
   string ls_4;
   int l_str2int_12;
   if (TicketNumber != 0) {
      for (int li_0 = GlobalVariablesTotal() - 1; li_0 >= 0; li_0--) {
         ls_4 = GlobalVariableName(li_0);
         if (StringFind(ls_4, gs_ibfx_pc__148, -1) != -1) {
            l_str2int_12 = StrToInteger(StringSubstr(ls_4, g_str_len_156, -1));
            if (OrderSelect(l_str2int_12, SELECT_BY_TICKET, MODE_TRADES))
               if (OrderCloseTime() != 0) GlobalVariableDel(ls_4);
         }
      }
   }
}

int fnGetPipProfit(string a_symbol_0, double ad_8, int ai_16) {
   bool li_ret_20 = FALSE;
   RefreshRates();
   double l_ask_24 = MarketInfo(a_symbol_0, MODE_ASK);
   double l_bid_32 = MarketInfo(a_symbol_0, MODE_BID);
   double l_point_40 = MarketInfo(a_symbol_0, MODE_POINT);
   if (l_point_40 == 0.001) l_point_40 = 0.01;
   else
      if (l_point_40 == 0.00001) l_point_40 = 0.0001;
   switch (ai_16) {
   case 0:
      li_ret_20 = MathCeil((l_bid_32 - ad_8) / l_point_40);
      break;
   case 1:
      li_ret_20 = MathCeil((ad_8 - l_ask_24) / l_point_40);
      break;
   default:
      li_ret_20 = FALSE;
   }
   return (li_ret_20);
}

void fnCreateNewGv(int ai_0, int ai_4) {
   string l_str_concat_8 = StringConcatenate("from #", ai_0);
   for (int l_pos_16 = OrdersTotal() - 1; l_pos_16 >= 0; l_pos_16--) {
      if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES))
         if (OrderComment() == l_str_concat_8 || OrderTicket() == ai_0) GlobalVariableSet(StringConcatenate(gs_ibfx_pc__148, OrderTicket()), ai_4 + 1);
   }
}



Re: A very simple partial closing EA

2
econome wrote: Sun Jul 02, 2017 9:47 pm I am looking for help to code a very simple partial closing EA as follows.

//SET VISIBLE STOP LOSS
extern double *number of pips*

EXAMPLE. 10 pips.

//SET FIRST TARGET (T1) AS A PROPORTION OF THE STOPLOSS

extern double *PROPORTION OF STOP LOSS MULTIPLE*

EXAMPLE. 1.5 - means first target (partial close) will close at 1.5 x the stop loss

***IF PROPORTION IS TOO GRANULAR, like 0.3 x 0.23 lots, rounds up to nearest acceptable***

//SLIPPAGE

extern double slippagefirsttarget *number of pips to be added to the target to account for slippage*

//SET PARTIAL CLOSE AT TARGET 1 AS A DECIMAL PROPORTION OF ORIGINAL POSITION

extern double *amount of position to close at first target between 0 and 1*

EXAMPLE. 0.75 - to close 0.75 of the position at the first target

//STOP TO BREAK EVEN + p number of PIPS AT REACH OF T1.

extern double bepips = 0 (or higher - moves stop to break even + bepips when T1 is reached)

//SET SECOND TARGET (T2) AS A MULTIPLE OF THE ORIGINAL STOPLOSS

extern double secondtarget *MULTIPLE OF STOPLOSS* --- must be larger than first target.

//SLIPPAGE SECOND TARGET

*how much to add in pips to second target to allow for slippage.*

I will try to research how to do this, but if an experienced coder wants to see to it, of course, it can be developed on this thread.

SUMMARY.

Set stop
Set target 1 (as a multiple of stop)
Set immediate stop loss movement to break even + p pips when T1 is hit.
Set amount to close at target 1 (as a proportion of the position size)
Set target 2 (multiple of stop)
---------------------------------------------------------
Set slippage control for target 1
Set slippage control for target 2
---------------------------------------------------------
Possible Additional options
1. Hidden stop / tp / closes options
2. Round up amount to close if proportion is not exact for broker minimums
3. Add 3rd target element (and 2nd partial close amount)
4. Display risk to reward minimum (i.e. closed out partial at first target, stopped out at BE+n) and maximum (closed out partial at T1, closed out remainder at T2 for profitable trade sequence.

***I found this***

Code: Select all


/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2009, IBFX"
#property link      "http://www.IBFX.com"

extern int InstanceID = 5;
extern int TicketNumber = 0;
extern int PipsTarget_1 = 140;
extern double CloseLotsPercentage_1 = 75.0;
extern int Move_SL_1 = 1;
extern int PipsTarget_2 = 210;
extern double CloseLotsPercentage_2 = 100;
extern int Move_SL_2 = 0;
extern int PipsTarget_3 = 50;
extern double CloseLotsPercentage_3 = 0.0;
extern int Move_SL_3 = 0;
extern int PipsTarget_4 = 100;
extern double CloseLotsPercentage_4 = 0.0;
extern int Move_SL_4 = 0;
string gs_ibfx_pc__148 = "IBFX-PC-";
int g_str_len_156 = 10;
int gi_160 = 1;
bool gi_164 = FALSE;
int abc = 2;

int init() {
   gs_ibfx_pc__148 = "IBFX-PC-" + InstanceID + "-";
   g_str_len_156 = StringLen(gs_ibfx_pc__148);
   if (MarketInfo(Symbol(), MODE_MINLOT) == 0.01) gi_160 = 2;
   fnGvCheck();
   return (0);
}

int start() {
   int l_ticket_4;
   int li_8;
   int l_global_var_12;
   int li_16;
   int li_unused_20;
   int li_24;
   double l_lots_28;
   bool l_ord_close_40;
   bool l_bool_44;
   double l_point_48;
   if (gi_164) return (0);
   if (StringFind(AccountServer(), "InterbankFX", 0) == 1 && StringFind(AccountServer(), "IBFX", 0) == 1) {
      Alert("InterbankFX users Only!!!");
      gi_164 = TRUE;
      return (0);
   }
   if (!IsExpertEnabled()) {
      Alert("You did not allow your expert to run live, change your settings and apply the expert again");
      gi_164 = TRUE;
      return (0);
   }
   for (int l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) {
      if (OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES)) {
         l_ticket_4 = OrderTicket();
         if (l_ticket_4 == TicketNumber || GlobalVariableCheck(StringConcatenate(gs_ibfx_pc__148, l_ticket_4))) {
            li_8 = fnGetPipProfit(OrderSymbol(), OrderOpenPrice(), OrderType());
            if (!GlobalVariableCheck(StringConcatenate(gs_ibfx_pc__148, l_ticket_4))) GlobalVariableSet(StringConcatenate(gs_ibfx_pc__148, DoubleToStr(l_ticket_4, 0)), 1);
            l_global_var_12 = GlobalVariableGet(StringConcatenate(gs_ibfx_pc__148, DoubleToStr(l_ticket_4, 0)));
            li_16 = 0;
            li_unused_20 = 0;
            li_24 = 0;
            l_lots_28 = 0.0;
            switch (l_global_var_12) {
            case 1:
               li_24 = Move_SL_1;
               li_16 = PipsTarget_1;
               l_lots_28 = NormalizeDouble(OrderLots() * CloseLotsPercentage_1 / 100.0, gi_160);
               break;
            case 2:
               li_24 = Move_SL_2;
               li_16 = PipsTarget_2;
               l_lots_28 = NormalizeDouble(OrderLots() * CloseLotsPercentage_2 / 100.0, gi_160);
               break;
            case 3:
               li_24 = Move_SL_3;
               li_16 = PipsTarget_3;
               l_lots_28 = NormalizeDouble(OrderLots() * CloseLotsPercentage_3 / 100.0, gi_160);
               break;
            case 4:
               li_24 = Move_SL_4;
               li_16 = PipsTarget_4;
               l_lots_28 = NormalizeDouble(OrderLots() * CloseLotsPercentage_4 / 100.0, gi_160);
               break;
            default:
               li_24 = 0;
               l_lots_28 = 0.0;
               li_16 = 0;
            }
            if (li_8 >= li_16 && li_16 != 0) {
               l_ord_close_40 = FALSE;
               l_bool_44 = FALSE;
               l_point_48 = MarketInfo(OrderSymbol(), MODE_POINT);
               if (l_point_48 == 0.001) l_point_48 = 0.01;
               else
                  if (l_point_48 == 0.00001) l_point_48 = 0.0001;
               switch (OrderType()) {
               case OP_BUY:
                  if (li_24 > 0) {
                     RefreshRates();
                     fnWait();
                     l_bool_44 = OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + li_24 * l_point_48, OrderTakeProfit(), 0, CLR_NONE);
                  }
                  if (l_lots_28 != 0.0) {
                     RefreshRates();
                     fnWait();
                     l_ord_close_40 = OrderClose(l_ticket_4, l_lots_28, MarketInfo(OrderSymbol(), MODE_BID), 0, CLR_NONE);
                  }
                  break;
               case OP_SELL:
                  if (li_24 > 0) {
                     RefreshRates();
                     fnWait();
                     l_bool_44 = OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - li_24 * l_point_48, OrderTakeProfit(), 0, CLR_NONE);
                  }
                  if (l_lots_28 != 0.0) {
                     RefreshRates();
                     fnWait();
                     l_ord_close_40 = OrderClose(l_ticket_4, l_lots_28, MarketInfo(OrderSymbol(), MODE_ASK), 0, CLR_NONE);
                  }
               }
               if (l_ord_close_40 || l_bool_44) fnCreateNewGv(l_ticket_4, l_global_var_12);
            }
         }
      }
   }
   fnGvCheck();
   return (0);
}

void fnWait() {
   if (IsConnected() && IsTradeAllowed()) while (IsTradeContextBusy()) Sleep(75);
}

void fnGvCheck() {
   string ls_4;
   int l_str2int_12;
   if (TicketNumber != 0) {
      for (int li_0 = GlobalVariablesTotal() - 1; li_0 >= 0; li_0--) {
         ls_4 = GlobalVariableName(li_0);
         if (StringFind(ls_4, gs_ibfx_pc__148, -1) != -1) {
            l_str2int_12 = StrToInteger(StringSubstr(ls_4, g_str_len_156, -1));
            if (OrderSelect(l_str2int_12, SELECT_BY_TICKET, MODE_TRADES))
               if (OrderCloseTime() != 0) GlobalVariableDel(ls_4);
         }
      }
   }
}

int fnGetPipProfit(string a_symbol_0, double ad_8, int ai_16) {
   bool li_ret_20 = FALSE;
   RefreshRates();
   double l_ask_24 = MarketInfo(a_symbol_0, MODE_ASK);
   double l_bid_32 = MarketInfo(a_symbol_0, MODE_BID);
   double l_point_40 = MarketInfo(a_symbol_0, MODE_POINT);
   if (l_point_40 == 0.001) l_point_40 = 0.01;
   else
      if (l_point_40 == 0.00001) l_point_40 = 0.0001;
   switch (ai_16) {
   case 0:
      li_ret_20 = MathCeil((l_bid_32 - ad_8) / l_point_40);
      break;
   case 1:
      li_ret_20 = MathCeil((ad_8 - l_ask_24) / l_point_40);
      break;
   default:
      li_ret_20 = FALSE;
   }
   return (li_ret_20);
}

void fnCreateNewGv(int ai_0, int ai_4) {
   string l_str_concat_8 = StringConcatenate("from #", ai_0);
   for (int l_pos_16 = OrdersTotal() - 1; l_pos_16 >= 0; l_pos_16--) {
      if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES))
         if (OrderComment() == l_str_concat_8 || OrderTicket() == ai_0) GlobalVariableSet(StringConcatenate(gs_ibfx_pc__148, OrderTicket()), ai_4 + 1);
   }
}

can you please post the original (non-decompiled) code so that it can be revised?


Who is online

Users browsing this forum: yamahaqs300 and 22 guests