Forum Think Finance - Bolsa e Forex

Geral => Sistemas Automáticos => Tópico iniciado por: vdap em 2013-01-05 16:23:26

Título: Funções para quem programa EA...
Enviado por: vdap em 2013-01-05 16:23:26
Funções para quem programa EA...

Estava aqui à procura de de uma função, e deparei-me com um tópico que já tinha deixado, noutro fórum, altura que ainda lá escrevia...

Mas como disponibilizei duas funções publicamente, queria deixar também aqui neste fórum, para quem se quer dedicar a estas lides.

O tópico original está em:

- http://caldeiraodebolsa.jornaldenegocios.pt/viewtopic.php?t=79891&highlight= (http://caldeiraodebolsa.jornaldenegocios.pt/viewtopic.php?t=79891&highlight=)
(têm que aceder à página 2, para ver as funções disponibilizadas)

As funções que disponibilizei na altura:

Utilizar esta função para ter o stoploss e o takeprofit "escondido da corretora"

Citar
//+=============================================================================================+
 //| HideStoplossTakeprofit() function |
 //| |
 //| Parametros: |
 //| * HST.SignalHST: Habilita ou desabilita a função |
 //| * HST.MagicNumber: MagicNumber do EA |
 //| * HST.Stoploss: Pips para o Stoploss |
 //| * HST.Takeprofit: Pips para o Takeprofit |
 //+=============================================================================================+

 bool HideStoplossTakeprofit(bool HST.SignalHST,int HST.MagicNumber, double HST.Stoploss, double HST.Takeprofit)
 {
 if(!HST.SignalHST)
 return(true);

 if(HST.Stoploss<=0 || HST.Takeprofit<=0) {
 Print("Function HideStoplossTakeprofit: Stoploss / Takeprofit têm que ser amiores que zero. Valor do SL: ",
 HST.Stoploss," TP: ",HST.Takeprofit);
 return(false);
 }

 string HST.Symbol=Symbol();
 HST.Stoploss=NormalizeDouble(HST.Stoploss*Point,Digits);
 HST.Takeprofit=NormalizeDouble(HST.Takeprofit*Point,Digits);
 int HST.maxslip=2;
 int HST.TradeAllowed;
 bool HST.Ticket;

 for(int HST.i=0;HST.i<OrdersTotal();HST.i++)
 {
 if(!OrderSelect(HST.i,SELECT_BY_POS,MODE_TRADES)) continue;

 if(OrderMagicNumber()!=HST.MagicNumber || OrderSymbol()!=HST.Symbol) continue;
 //---- check order type
 if(OrderType()==OP_BUY)
 {
 Print("OrderOpenPrice()+HST.Takeprofit: ",OrderOpenPrice()+HST.Takeprofit," OrderOpenPrice()-HST.Stoploss: ",OrderOpenPrice()-HST.Stoploss);
 if(Bid>OrderOpenPrice()+HST.Takeprofit) {
 HST.TradeAllowed = TradeAllow();
 if(HST.TradeAllowed < 0) return(-1);
 if(HST.TradeAllowed == 0) RefreshRates();
 HST.Ticket=OrderClose(OrderTicket(),OrderLots(),Bid,HST.maxslip,White);
 if(!HST.Ticket) Print("Function HideStoplossTakeprofit: Erro ao fechar ordem BUY p/ symbol ",HST.Symbol,".TAKEPROFIT");
 }

 if(Ask<OrderOpenPrice()-HST.Stoploss) {
 HST.TradeAllowed = TradeAllow();
 if(HST.TradeAllowed < 0) return(-1);
 if(HST.TradeAllowed == 0) RefreshRates();
 HST.Ticket=OrderClose(OrderTicket(),OrderLots(),Bid,HST.maxslip,White);
 if(!HST.Ticket) Print("Function HideStoplossTakeprofit: Erro ao fechar ordem BUY p/ symbol ",HST.Symbol,". STOPLOSS");
 }
 break;
 }// IF DO OP_BUY

 if(OrderType()==OP_SELL)
 {
 Print("OrderOpenPrice()-HST.Takeprofit: ",OrderOpenPrice()-HST.Takeprofit," OrderOpenPrice()+HST.Stoploss: ",OrderOpenPrice()+HST.Stoploss);
 if(Ask<OrderOpenPrice()-HST.Takeprofit) {
 HST.TradeAllowed = TradeAllow();
 if(HST.TradeAllowed < 0) return(-1);
 if(HST.TradeAllowed == 0) RefreshRates();
 HST.Ticket=OrderClose(OrderTicket(),OrderLots(),Ask,HST.maxslip,White);
 if(!HST.Ticket) Print("Function HideStoplossTakeprofit: Erro ao fechar ordem SELL p/ symbol ",HST.Symbol,". TAKEPROFIT");
 }

 if(Bid>OrderOpenPrice()+HST.Stoploss) {
 HST.TradeAllowed = TradeAllow();
 if(HST.TradeAllowed < 0) return(-1);
 if(HST.TradeAllowed == 0) RefreshRates();
 HST.Ticket=OrderClose(OrderTicket(),OrderLots(),Ask,HST.maxslip,White);
 if(!HST.Ticket) Print("Function HideStoplossTakeprofit: Erro ao fechar ordem SELL p/ symbol ",HST.Symbol,". STOPLOSS ");
 }
 break;
 }// IF DO OP_SELL
 }//END DO FOR
 return(true);
 }


"devolve o ATR segundo o timeframe pedido à função, mas esta função já sofreu alterações, porque devolvia sempre para 5 periodos"

Citar
double StopLossPricebyATR1(datetime SLPA.ATRTimeFrame)
 {
 int SLPA.ATRPeriod = 5;
 double SLPA.CalculatedStopLossPrice=0.;
 static double SLPA.ATRArray[3],SLPA.ATRArrayMax=0.;
 static int SLPA.BarOpenATRTimeFrame;
 double SLPA.Spread = MarketInfo(Symbol(),MODE_SPREAD)*Point;
 if(iBars(Symbol(),SLPA.ATRTimeFrame)<SLPA.ATRPeriod+1)
 {
 Print("Not enough bars in the specified chart for computing stoploss by ATR");
 Print("Current timedate = ",TimeToStr(TimeCurrent())," and bar count is ",iBars(Symbol(),SLPA.ATRTimeFrame));
 int StopLossMaximo=300;
 return(StopLossMaximo);
 }
 if(iTime(Symbol(),SLPA.ATRTimeFrame,0)!=SLPA.BarOpenATRTimeFrame) {
 SLPA.ATRArray[0]=iATR(NULL,SLPA.ATRTimeFrame,SLPA.ATRPeriod,0);
 SLPA.ATRArray[1]=iATR(NULL,SLPA.ATRTimeFrame,1,0);
 SLPA.ATRArray[2]=iATR(NULL,SLPA.ATRTimeFrame,1,1);
 SLPA.ATRArrayMax=SLPA.ATRArray[ArrayMaximum(SLPA.ATRArray)];
 SLPA.BarOpenATRTimeFrame=iTime(Symbol(),SLPA.ATRTimeFrame,0);
 }
 RefreshRates();
 double SLPA.Price=(Bid+Ask)/2;
 SLPA.CalculatedStopLossPrice=NormalizeDouble(SLPA.Price-SLPA.ATRArrayMax,MarketInfo(Symbol(),MODE_DIGITS));
 SLPA.CalculatedStopLossPrice=(SLPA.Price-SLPA.CalculatedStopLossPrice)/MarketInfo(Symbol(),MODE_POINT);
 return(SLPA.CalculatedStopLossPrice);

 }