TrueSort_1100





//+------------------------------------------------------------------+
//|                                                TrueSort_1100.mq4 |
//|                                                TrueSort(v 1.1.00)|
//|                                                           MaxBau |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MaxBau"
#property link      ""

extern double lots = 0.1;
extern double risk = 5;          //%
extern int stloss = 90;          //ñòîï-ëîññ
extern int tprofit = 270;        //òåéê-ïðîôèò
extern int magicnum = 10000;

double ma_10[3], ma_20[3], ma_50[3], ma_100[3], ma_200[3];
int ord_tsort;

int prevtime = 0;

int tsort = 0;

double eq_lot2()
{
  lots=NormalizeDouble(AccountBalance() * risk / 100 / 1000, 2);
  double Min_Lot = MarketInfo(Symbol(), MODE_MINLOT);
  if (lots == 0) lots = Min_Lot;                    
  return(lots);
}

int init()
{
  eq_lot2();
  return(0);
}

int deinit()
{
  return(0);
}

void setstopbuy(int otc)
{
  OrderSelect (otc, SELECT_BY_TICKET);
  if (OrderType() == OP_BUY)
  {
   if (OrderStopLoss() < OrderOpenPrice())
   {
    if (NormalizeDouble((Bid-OrderOpenPrice())/Point,Digits) > stloss)
      OrderModify (otc, OrderOpenPrice(), Bid-stloss*Point, Bid+tprofit*Point, 0);
   }
   else
    if (OrderStopLoss() > OrderOpenPrice())
    {
      if (NormalizeDouble((Bid-OrderStopLoss())/Point,Digits) > stloss)
        OrderModify (otc, OrderOpenPrice(), Bid-stloss*Point, Bid+tprofit*Point, 0);
    } 
  }
}

void setstopsell(int otc)
{
  OrderSelect (otc, SELECT_BY_TICKET);
  if (OrderType() == OP_BUY)
  {
   if (OrderStopLoss() > OrderOpenPrice())
   {
    if (NormalizeDouble((OrderOpenPrice()-Ask)/Point,Digits) > stloss)
      OrderModify (otc, OrderOpenPrice(), Ask+stloss*Point, Ask-tprofit*Point, 0);
   }
   else
    if (OrderStopLoss() < OrderOpenPrice())
    {
      if (NormalizeDouble((OrderStopLoss()-Ask)/Point,Digits) > stloss)
        OrderModify (otc, OrderOpenPrice(), Ask+stloss*Point, Ask-tprofit*Point, 0);
    } 
  }
}

void checkopenedorders ()
{
  tsort = 0;
  int i, n, otp, magn, otc;
  n = 0;
  int ordstotal = OrdersTotal();
  for (i = 0; i < ordstotal; i++)
  {
     OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
     otp = OrderType();
     magn = OrderMagicNumber();
     otc = OrderTicket();
     if (magn == magicnum)
     {
       ord_tsort = otc;
       if (otp == OP_BUY)
       {
         tsort = 1; 
         setstopbuy(ord_tsort);
       }
       else
         if (otp == OP_SELL)
         {
           tsort = -1;
           setstopsell(ord_tsort);
         }
     }
   }
}

void do_it (int dimens)
{
  if (NormalizeDouble(AccountFreeMargin() / AccountBalance(), 2) < 0.2) return;
  int n, or;
  switch (dimens)
  {
    case 1:
            n = 0;
            ord_tsort = 0;
            while (n < 1000 && ord_tsort == 0)
            {
              ord_tsort = OrderSend (Symbol(), OP_BUY, lots, Ask, 3, ma_50[0], Bid+tprofit*Point, "Ïðàâèëüíûé ïîðÿäîê", magicnum, 0, Red);     
              n++;
            }
            break;
    case -1:
            n = 0;
            ord_tsort = 0;
            while (n < 1000 && ord_tsort == 0)
            {
              ord_tsort = OrderSend (Symbol(), OP_SELL, lots, Bid, 3, ma_50[0], Ask-tprofit*Point, "Ïðàâèëüíûé ïîðÿäîê", magicnum, 0, Red);           
              n++;
            }
            break;
  }
}

void eq_MA()
{
  for (int i = 0; i < 5; i++)
  {
    ma_10[i] = iMA (NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, i+1);
    ma_20[i] = iMA (NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, i+1);
    ma_50[i] = iMA (NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, i+1);
    ma_100[i] = iMA (NULL, 0, 100, 0, MODE_SMA, PRICE_CLOSE, i+1);
    ma_200[i] = iMA (NULL, 0, 200, 0, MODE_SMA, PRICE_CLOSE, i+1);
  }
  return;
}

bool to_buy()
{
  if (ma_10[0] > ma_20[0] && ma_20[0] > ma_50[0] && ma_50[0] > ma_100[0] && ma_100[0] > ma_200[0])
    if (ma_10[1] > ma_20[1] && ma_20[1] > ma_50[1] && ma_50[1] > ma_100[1] && ma_100[1] > ma_200[1])
        if (iADX(NULL,0,14,PRICE_HIGH,MODE_MAIN,0) > 20)
      return (true);
  return (false);
}


bool to_sell ()
{
  if (ma_10[0] < ma_20[0] && ma_20[0] < ma_50[0] && ma_50[0] < ma_100[0] && ma_100[0] < ma_200[0])
    if (ma_10[1] < ma_20[1] && ma_20[1] < ma_50[1] && ma_50[1] < ma_100[1] && ma_100[1] < ma_200[1])
        if (iADX(NULL,0,14,PRICE_HIGH,MODE_MAIN,0) > 20)
      return (true);
  return (false);
}

void check_buyclose ()
{
  bool cl;
  int n;
  if (ma_10[0] <= ma_20[0] || ma_20[0] <= ma_50[0] || ma_50[0] <= ma_100[0] || ma_100[0] <= ma_200[0])
  {
    OrderSelect (ord_tsort, SELECT_BY_TICKET);
    n = 0;
    cl = false;
    while (n < 1000 && cl == false)
    {  
      cl = OrderClose (ord_tsort, OrderLots(), Bid, 3, Blue);
      n++;
    }
  }
  return;
}

void check_sellclose ()  
{            
  bool cl;
  int n;
  if (ma_10[0] >= ma_20[0] || ma_20[0] >= ma_50[0] || ma_50[0] >= ma_100[0] || ma_100[0] >= ma_200[0])
  {
    OrderSelect (ord_tsort, SELECT_BY_TICKET);
    n = 0;
    cl = false;
    while (n < 1000 && cl == false)
    {  
      cl = OrderClose (ord_tsort, OrderLots(), Ask, 3, Blue);
      n++;
    }
  }
  return;
}

void sistem_2()
{
  eq_MA();
  switch (tsort)
  {
    case 0: 
       if (to_buy()==true)
       {
         do_it(1);
       }
       else 
         if (to_sell()==true) do_it(-1);
       break;
    case 1:
       check_buyclose();
       break;
    case -1:
       check_sellclose();
       break;
  }
  return;
}

int start()
{
  checkopenedorders ();
  
  if(Time[0] == prevtime) 
       return(0);
  prevtime = Time[0];
  
  sistem_2();

  return (0);
}



Sample





Analysis



Market Information Used:

Series array that contains open time of each bar


Indicator Curves created:


Indicators Used:

Movement directional index


Custom Indicators Used:

Order Management characteristics:
Checks for the total of open orders

Other Features: