72 lines
5.4 KiB
MQL5
72 lines
5.4 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| Buy.mq5 |
|
|
//| Copyright 2018, Romeu Bertho. |
|
|
//| https://www.mql5.com/pt/users/c4b3l3r4 |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2018, Romeu Bertho."
|
|
#property link "https://www.mql5.com/pt/users/c4b3l3r4"
|
|
#property version "1.00"
|
|
#property script_show_inputs
|
|
|
|
//--- input parameters
|
|
input string op0="-------------- Parâmetros de Entrada --------------"; // -------------- Parâmetros de Entrada --------------
|
|
input double Volume=1; // Quantidade
|
|
input int TakeProfit=100; // TakeProfit
|
|
input int StopLoss=100; // Stoploss
|
|
input int Magic=12345;
|
|
|
|
ENUM_ORDER_TYPE_FILLING type_filling=ORDER_FILLING_FOK;
|
|
ENUM_ORDER_TYPE_TIME expiration=ORDER_TIME_GTC;
|
|
//+------------------------------------------------------------------+
|
|
//| Script program start function |
|
|
//+------------------------------------------------------------------+
|
|
void OnStart()
|
|
{
|
|
//---
|
|
Buy(_Symbol,0,Volume,TakeProfit,StopLoss,"Buy");
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool Buy(string symbol,double price,double volume,int takeProfit,int stopLoss,string comment)
|
|
{
|
|
MqlTradeRequest request={0};
|
|
MqlTradeResult result={0};
|
|
double SL=0;
|
|
double TP=0;
|
|
if(price==0)
|
|
price=SymbolInfoDouble(symbol,SYMBOL_ASK);
|
|
if(stopLoss>0)
|
|
SL=NormalizeDouble(price-stopLoss*_Point,_Digits);
|
|
if(takeProfit>0)
|
|
TP=NormalizeDouble(price+takeProfit*_Point,_Digits);
|
|
if(comment==NULL)
|
|
comment=StringFormat("Long %s %G lotes ",symbol,volume);
|
|
//--- Preenchimento da estrutura
|
|
request.action=TRADE_ACTION_DEAL;
|
|
request.symbol=symbol;
|
|
request.volume=volume;
|
|
request.type=ORDER_TYPE_BUY;
|
|
request.price=price;
|
|
request.tp=TP;
|
|
request.sl=SL;
|
|
request.deviation=10;
|
|
request.magic=Magic;
|
|
request.type_filling=type_filling;
|
|
request.expiration=expiration;
|
|
request.comment=comment;
|
|
//--- Envio da ordem
|
|
if(!OrderSend(request,result))
|
|
{
|
|
//--- Falha na ordem
|
|
Print("Ordem de compra falhou. Código de retorno=",result.retcode);
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
Print("Ordem de compra executada com sucesso. Código de retorno=",result.retcode);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
//+------------------------------------------------------------------+
|