Exemplo n.º 1
0
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double ma;
//--- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//--- get Moving Average 
   ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//---
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //--- check order type 
      if(OrderType()==OP_BUY)
        {
         if(Open[1]>ma && Close[1]<ma)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
               Print("OrderClose error ",GetLastError());
           }
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(Open[1]<ma && Close[1]>ma)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
               Print("OrderClose error ",GetLastError());
           }
         break;
        }
     }
//---
  }
Exemplo n.º 2
0
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//--- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//--- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
           {
            Print("Error in history!");
            break;
           }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
            continue;
         //---
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1)
         lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//--- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }
Exemplo n.º 3
0
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//---
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//--- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
Exemplo n.º 4
0
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
{
   double ma_cur;
   double ma_prev1;
   double ma_prev2;
   
//---- go trading only for first tiks of new bar
   if(Volume[0] > 1) {
     return;
   }
//---- get Moving Average
   ma_cur = iMA(NULL, 0, MA_PERIOD, 0, MODE_SMA, PRICE_CLOSE, 0);
   ma_prev1 = iMA(NULL, 0, MA_PERIOD, 0, MODE_SMA, PRICE_CLOSE, 1);
   ma_prev2 = iMA(NULL, 0, MA_PERIOD, 0, MODE_SMA, PRICE_CLOSE, 2);   
//----
   for(int i = 0;i < OrdersTotal();i++)
     {
       if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false){
	      break;
       }
       if(OrderMagicNumber() != MAGICMA || OrderSymbol() != Symbol()){
	 continue;
       }
       //---- check order type 
       if(OrderType() == OP_BUY)
	 {
	   if(cross_below(ma_cur, ma_prev1) || touch_bottom(ma_cur, ma_prev1, ma_prev2) || OrderProfit() > AccountFreeMargin()*(-0.07)){
	     OrderClose(OrderTicket(), OrderLots(), Bid, 3, White);
	   }
	   break;
        }
      if(OrderType() == OP_SELL)
        {
	  if(cross_above(ma_cur, ma_prev1) || touch_top(ma_cur, ma_prev1, ma_prev2)|| OrderProfit() > AccountFreeMargin()*(-0.07)){
	    OrderClose(OrderTicket(), OrderLots(), Ask, 3, White);
	  }  
	  break;
        }
     }
//----
}
Exemplo n.º 5
0
Arquivo: ai.c Projeto: bengelbert/ifsc
int start()
{
    int err = 0;
    int total = 0;
    int prevticket = 0;
    int modespread = MarketInfo(Symbol(), MODE_SPREAD);
    
    //----
    if (IsTradeAllowed()) {
        RefreshRates();
        modespread = MarketInfo(Symbol(), MODE_SPREAD);
    } else {
        return (0);
    }
    
    ObjectSetText(WT_INFO_NAME,
        "Perceptron("+gOrderbar+"): " +
        "Spread("+modespread+") "+
        ""+DoubleToStr(perceptron(), 2)+"",
        7, "Arial", White);

    // check for opened position
    total = OrdersTotal();
    
    //----
    for (int i = 0; i < total; i++) {
        OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        // check for symbol & magic number
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
            prevticket = OrderTicket();
            
            gOrderbar = iBarShift(Symbol(), 0, OrderOpenTime());
            
            // long position is opened
            if (OrderType() == OP_BUY) {
                // check profit 
                if (Bid > (OrderStopLoss() + (sl * 2 + modespread) * Point)) {
                    if (perceptron() < 0) { // reverse
                        gOrderTicket = OrderSend(Symbol(), OP_SELL, OrderLots(), Bid, 3,
                            Ask + sl * Point, 0, "AI Rev", MagicNumber, 0, Red);
                        Sleep(10000);
                        //----
                        if (gOrderTicket >= 0) {
                            OrderCloseBy(gOrderTicket, prevticket, Blue);
                        } else {
                            err=GetLastError();
                            Print("error[1](",err,"): ",ErrorDescription(err));
                            
                            if (OrderStopLoss() < OrderOpenPrice()) {
                                if (!OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), 0, 0, Blue)) {
                                    Sleep(1000);
                                }
                            }
                            
                        }
                    } else { // trailing stop
                        if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - sl * Point, 0, 0, Blue)) {
                            Sleep(1000);
                        }
                    }
                }
                // short position is opened
            } else {
                // check profit 
                if (Ask < (OrderStopLoss() - (sl * 2 + modespread) * Point)) {
                    if (perceptron() > 0) { // reverse
                        gOrderTicket = OrderSend(Symbol(), OP_BUY, OrderLots(), Ask, 3,
                            Bid - sl * Point, 0, "AI Rev", MagicNumber, 0, Blue);
                        Sleep(10000);
                        //----
                        if (gOrderTicket >= 0) {
                            OrderCloseBy(gOrderTicket, prevticket, Blue);
                        } else {
                            err=GetLastError();
                            Print("error[2](",err,"): ",ErrorDescription(err));
                            
                            if (OrderStopLoss() > OrderOpenPrice()) {
                                if (!OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), 0, 0, Blue)) {
                                    Sleep(1000);
                                }
                            }
                        }
                    } else { // trailing stop
                        if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + sl * Point,
                            0, 0, Blue)) {
                            Sleep(1000);
                        }
                    }
                }
            }
            // exit
            return (0);
        }
    }

    if (gOrderbar == iBarShift(Symbol(), 0, 0)) {
        return (0);
    }

    lots = NormalizeDouble(AccountBalance() * aiGetRisk(Period()) / (sl + modespread), 2);
    if (lots < 0.01) lots = 0.01;
    
    // check for long or short position possibility
    if (perceptron() > 0) { //long
        gOrderTicket = OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Bid - sl * Point, 0, "AI",
            MagicNumber, 0, Blue);
        //----
        if (gOrderTicket < 0) {
            Sleep(10000);
            err=GetLastError();
            Print("error[3](",err,"): ",ErrorDescription(err));
        }
    } else { // short
        gOrderTicket = OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Ask + sl * Point, 0, "AI",
            MagicNumber, 0, Red);
        if (gOrderTicket < 0) {
            Sleep(10000);
            err=GetLastError();
            Print("error[4](",err,"): ",ErrorDescription(err));
        }
    }
    //--- exit
    return (0);
}