Download MetaTrader 5
To add comments, please log in or register
Find and download Code Base apps via MetaTrader. It is fast!
Aramis Kessler Agostini
22
Aramis Kessler Agostini 2016.01.13 12:20 

Hello guys im coding my first EA and im with some problems with the stop loss

So, in the 1 minute time frame it wroked perfectly, but i tryed to test the EA in higher time frames

than i got an error about unmatched data, i downloaded from de history center data from the pairs and it runs the EA ok.

But in some of my orders it does not place the stops, my doubt is if the problem is with my code or it could be some unmatched data?

Thanks for your attention, sorry for my poor english.

 

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|     TRADE PLACING FUNCTION                                                             |
//+------------------------------------------------------------------+
void EnterTrade(int type)
  {

   int err=0;
   double price=Bid,sl=0,tp=0;
   if(type==OP_BUY)
      price=Ask;
//----
   int ticket=OrderSend(Symbol(),type,LotSize,price,Slippage,0,0," Trade",magic,0,Magenta);
   if(ticket>0)
     {
      if(OrderSelect(ticket,SELECT_BY_TICKET))
        {
         sl= StopSell(1);
         tp=TakeProfitSell(1);
        
         if(OrderType()==OP_BUY)
           {
            sl=StopBuy(1);
            tp= TakeProfitBuy(1);
           
           }
         if(!OrderModify(ticket,price,sl,tp,0,Magenta))
           {
            err=GetLastError();
            Print("Encountered an error during modification!"+(string)err+" "+ErrorDescription(err));
           }
        }
      else
        {//in case it fails to select the order for some reason 
         Print("Failed to Select Order ",ticket);
         err=GetLastError();
         Print("Encountered an error while seleting order "+(string)ticket+" error number "+(string)err+" "+ErrorDescription(err));
        }
     }
   else
     {//in case it fails to place the order and send us back a ticket number.
      err=GetLastError();
      Print("Encountered an error during order placement!"+(string)err+" "+ErrorDescription(err));
      if(err==ERR_TRADE_NOT_ALLOWED)MessageBox("You can not place a trade because \"Allow Live Trading\" is not checked in your options. Please check the \"Allow Live Trading\" Box!","Check Your Settings!");
     }
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|  StopLoss and TakeProfit                                         |
//+------------------------------------------------------------------+
double ATR(int i)
  {
   xtr=iATR(SSSR_sym,0,7,i);
   return(xtr);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double StopBuy(int i)
  {
   stpb=iLow(SSSR_sym,0,i)-(StopLoss*ATR(i));//iLow(SSSR_sym,0,i)
   return (stpb);
  }
//---
double StopSell(int i)
  {
   stps=iHigh(SSSR_sym,0,i)+(StopLoss*ATR(i));//iHigh(SSSR_sym,0,i)
   return(stps);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double TakeProfitBuy(int i)
  {
   tpb=iLow(SSSR_sym,0,i)+(iClose(SSSR_sym,0,i)-iLow(SSSR_sym,0,i))+TakeProfit*ATR(i);//iLow(SSSR_sym,0,i)+(iClose(SSSR_sym,0,i)-iLow(SSSR_sym,0,i))
   return(tpb);
  }
//---
double TakeProfitSell(int i)
  {
   tps=iHigh(SSSR_sym,0,i)-(iHigh(SSSR_sym,0,i)-iClose(SSSR_sym,0,i))-TakeProfit*ATR(i);//iHigh(SSSR_sym,0,i)-(iHigh(SSSR_sym,0,i)-iClose(SSSR_sym,0,i))
   return(tps);
  }
whroeder1
13627
whroeder1 2016.01.13 13:21  
stpb=iLow(SSSR_sym,0,i)-(StopLoss*ATR(i));

tpb=iLow(SSSR_sym,0,i)+(iClose(SSSR_sym,0,i)-iLow(SSSR_sym,0,i))+TakeProfit*ATR(i);
You are basing your stops off the previous candle; that has no relation to your entry.

TP is the close[1] plus n*ATR SL is Low[1] minus n*ATR
Aramis Kessler Agostini
22
Aramis Kessler Agostini 2016.01.13 18:39  
Hello WHRoeder, thank you very much for your help!
/
To add comments, please log in or register