- extern int TakeProfit = 20;
- extern double Lot = 0.5;
- extern int Slippage = 3;
- extern int Magic = 050120120031;
- bool modify = false;
- //+------------------------------------------------------------------+
- //| expert initialization function |
- //+------------------------------------------------------------------+
- int init()
- {
- //----
- //----
- return(0);
- }
- //+------------------------------------------------------------------+
- //| expert deinitialization function |
- //+------------------------------------------------------------------+
- int deinit()
- {
- //----
- //----
- return(0);
- }
- //+------------------------------------------------------------------+
- //| expert start function |
- //+------------------------------------------------------------------+
- int start()
- {
- //----
- if (all () == 0) two_orders ();
- if (all () == 2 && all_type (0) == 1 && all_type (1) == 0) return (0);
- if (all () == 1 && all_type (0) == 1 && !modify)
- {
- two_orders ();
- modify_profit (0);
- }
- if (all () == 1 && all_type (0) == 1 && modify)
- {
- close ();
- two_orders ();
- modify = false;
- }
- if(all () == 1 && all_type (1) == 1 && !modify)
- {
- two_orders ();
- modify_profit (1);
- }
- if(all () == 1 && all_type (1) == 1 && modify)
- {
- close ();
- two_orders ();
- modify = false;
- }
- if (all () > 1 && all_type (0) == 0 && all_type (1) > 1)
- {
- two_orders ();
- modify_profit (1);
- }
- if (all () > 1 && all_type (1) == 0 && all_type (0) > 1)
- {
- two_orders ();
- modify_profit (0);
- }
- //----
- return(0);
- }
- //+------------------------------------------------------------------+
- void two_orders ()
- {
- double ask = NormalizeDouble (Ask, Digits);
- OrderSend (Symbol (), OP_BUY, /*lot_buy ()*/Lot, ask, Slippage, 0, NormalizeDouble (ask + TakeProfit*Point, Digits), "", Magic, 0, CLR_NONE);
- double bid = NormalizeDouble (Bid, Digits);
- OrderSend (Symbol (), OP_SELL, /*lot_sell ()*/Lot, bid, Slippage, 0, NormalizeDouble (bid - TakeProfit*Point, Digits), "", Magic, 0, CLR_NONE);
- }
- void modify_profit (int type)
- {
- double average_price, lots;
- int b, s;
- for (int i = OrdersTotal () - 1; i >= 0 ; i --)
- {
- if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber () == Magic && OrderSymbol () == Symbol () && OrderType () == type)
- {
- average_price += OrderOpenPrice() * OrderLots();
- lots += OrderLots();
- }
- }
- average_price = NormalizeDouble (average_price/lots, Digits);
- for (i = OrdersTotal () - 1; i >= 0 ; i --)
- {
- if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber () == Magic && OrderSymbol () == Symbol () && OrderType () == type)
- {
- OrderModify (OrderTicket (), 0, 0, average_price, 0, CLR_NONE);
- modify = true;
- }
- }
- }
- void close ()
- {
- for (int i = OrdersTotal () - 1; i >= 0; i --)
- {
- if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber () == Magic && OrderSymbol () == Symbol ())
- {
- if (OrderType () == 0)
- OrderClose (OrderTicket (), OrderLots (), NormalizeDouble (Bid, Digits), Slippage, CLR_NONE);
- if (OrderType () == 1)
- OrderClose (OrderTicket (), OrderLots (), NormalizeDouble (Ask, Digits), Slippage, CLR_NONE);
- }
- }
- }
- int all ()
- {
- int a;
- for (int i = OrdersTotal () - 1; i >= 0; i --)
- if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber () == Magic && OrderSymbol () == Symbol ())
- a ++;
- return (a);
- }
- int all_type (int type)
- {
- int a;
- for (int i = OrdersTotal () - 1; i >= 0; i --)
- {
- if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber () == Magic && OrderSymbol () == Symbol () && OrderType () == type)
- a ++;
- }
- return (a);
- }
- /*double Lot_open (int type)
- {
- for (int i = OrdersTotal () - 1; i >= 0; i --)
- {
- if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber () == Magic && OrderSymbol () == Symbol () && OrderType () == type)
- return (OrderLots ());
- }
- }
- double lot_buy ()
- {
- if (all_type (0) == 0) return (Lot);
- if (all_type (0) == 1 && all () == 1) return (NormalizeDouble (Lot + Lot, 2));
- if (all_type (1) == 0 && all_type (0) > 1) return (NormalizeDouble (Lot_open (0) + Lot, 2));
- return (Lot);
- }
- double lot_sell ()
- {
- if (all_type (1) == 0) return (Lot);
- if (all_type (1) == 1 && all () == 2) return (NormalizeDouble (Lot + Lot, 2));
- if (all_type (0) == 1 && all_type (1) > 1) return (NormalizeDouble (Lot_open (1) + Lot, 2));
- return (Lot);
- }*/