• Welcome back! Thank you for being a part of this Traders Community. Let's discuss and share :)
    Selamat datang kembali! Trimakasih telah menjadi bagian dari Komunitas Trader ini. Mari berdiskusi dan berbagi :)

Question CAN ANYONE REPLACE BUYSTOP AND SELLSTOP WITH BUYLIMMIT AND SELL LIMIT IN THIS ROBOT?

ezequiel89

Member
Credit Hunter
Credits
0
#define VERSION "1.0"
#property version VERSION
#define PROJECT_NAME MQLInfoString(MQL_PROGRAM_NAME)
#include <Trade/Trade.mqh>
input double Lots = 0.01;
input double RiskPercent = 2.0; //RiskPercent (0 = Fix)
input int OrderDistPoints = 1;
input int TpPoints = 10000;
input int SlPoints = 50;
input int TslPoints = 5;
input int TslTriggerPoints = 5;
input ENUM_TIMEFRAMES Timeframe = PERIOD_H1;
input int BarsN = 5;
input int ExpirationHours = 50;
input int Magic = 153;
CTrade trade;
ulong buyPos, sellPos;
int totalBars;
int OnInit(){
trade.SetExpertMagicNumber(Magic);
if(!trade.SetTypeFillingBySymbol(_Symbol)){
trade.SetTypeFilling(ORDER_FILLING_RETURN);
}
static bool isInit = false;
if(!isInit){
isInit = true;
Print(__FUNCTION__," > EA (re)start...");
Print(__FUNCTION__," > EA version ",VERSION,"...");

for(int i = PositionsTotal()-1; i >= 0; i--){
CPositionInfo pos;
if(pos.SelectByIndex(i)){
if(pos.Magic() != Magic) continue;
if(pos.Symbol() != _Symbol) continue;
Print(__FUNCTION__," > Found open position with ticket #",pos.Ticket(),"...");
if(pos.PositionType() == POSITION_TYPE_BUY) buyPos = pos.Ticket();
if(pos.PositionType() == POSITION_TYPE_SELL) sellPos = pos.Ticket();
}
}
for(int i = OrdersTotal()-1; i >= 0; i--){
COrderInfo order;
if(order.SelectByIndex(i)){
if(order.Magic() != Magic) continue;
if(order.Symbol() != _Symbol) continue;
Print(__FUNCTION__," > Found pending order with ticket #",order.Ticket(),"...");
if(order.OrderType() == ORDER_TYPE_BUY_STOP) buyPos = order.Ticket();
if(order.OrderType() == ORDER_TYPE_SELL_STOP) sellPos = order.Ticket();
}
}
}
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason){
}
void OnTick(){
processPos(buyPos);
processPos(sellPos);
int bars = iBars(_Symbol,Timeframe);
if(totalBars != bars){
totalBars = bars;

if(buyPos <= 0){
double high = findHigh();
if(high > 0){
executeBuy(high);
}
}

if(sellPos <= 0){
double low = findLow();
if(low > 0){
executeSell(low);
}
}
}
}
void OnTradeTransaction(
const MqlTradeTransaction& trans,
const MqlTradeRequest& request,
const MqlTradeResult& result
){

if(trans.type == TRADE_TRANSACTION_ORDER_ADD){
COrderInfo order;
if(order.Select(trans.order)){
if(order.Magic() == Magic){
if(order.OrderType() == ORDER_TYPE_BUY_STOP){
buyPos = order.Ticket();
}else if(order.OrderType() == ORDER_TYPE_SELL_STOP){
sellPos = order.Ticket();
}
}
}
}
}
void processPos(ulong &posTicket){
if(posTicket <= 0) return;
if(OrderSelect(posTicket)) return;

CPositionInfo pos;
if(!pos.SelectByTicket(posTicket)){
posTicket = 0;
return;
}else{
if(pos.PositionType() == POSITION_TYPE_BUY){
double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);

if(bid > pos.PriceOpen() + TslTriggerPoints * _Point){
double sl = bid - TslPoints * _Point;
sl = NormalizeDouble(sl,_Digits);

if(sl > pos.StopLoss()){
trade.PositionModify(pos.Ticket(),sl,pos.TakeProfit());
}
}
}else if(pos.PositionType() == POSITION_TYPE_SELL){
double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);

if(ask < pos.PriceOpen() - TslTriggerPoints * _Point){
double sl = ask + TslPoints * _Point;
sl = NormalizeDouble(sl,_Digits);

if(sl < pos.StopLoss() || pos.StopLoss() == 0){
trade.PositionModify(pos.Ticket(),sl,pos.TakeProfit());
}
}
}
}
}
void executeBuy(double entry){
entry = NormalizeDouble(entry,_Digits);

double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
if(ask > entry - OrderDistPoints * _Point) return;

double tp = entry + TpPoints * _Point;
tp = NormalizeDouble(tp,_Digits);

double sl = entry - SlPoints * _Point;
sl = NormalizeDouble(sl,_Digits);
double lots = Lots;
if(RiskPercent > 0) lots = calcLots(entry-sl);

datetime expiration = iTime(_Symbol,Timeframe,0) + ExpirationHours * PeriodSeconds(PERIOD_H1);
trade.BuyStop(lots,entry,_Symbol,sl,tp,ORDER_TIME_SPECIFIED,expiration);

buyPos = trade.ResultOrder();
}
void executeSell(double entry){
entry = NormalizeDouble(entry,_Digits);
double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
if(bid < entry + OrderDistPoints * _Point) return;
double tp = entry - TpPoints * _Point;
tp = NormalizeDouble(tp,_Digits);

double sl = entry + SlPoints * _Point;
sl = NormalizeDouble(sl,_Digits);

double lots = Lots;
if(RiskPercent > 0) lots = calcLots(sl-entry);
datetime expiration = iTime(_Symbol,Timeframe,0) + ExpirationHours * PeriodSeconds(PERIOD_H1);
trade.SellStop(lots,entry,_Symbol,sl,tp,ORDER_TIME_SPECIFIED,expiration);

sellPos = trade.ResultOrder();
}
double calcLots(double slPoints){
double risk = AccountInfoDouble(ACCOUNT_BALANCE) * RiskPercent / 100;

double ticksize = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
double tickvalue = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
double lotstep = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);

double moneyPerLotstep = slPoints / ticksize * tickvalue * lotstep;
double lots = MathFloor(risk / moneyPerLotstep) * lotstep;

lots = MathMin(lots,SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX));
lots = MathMax(lots,SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN));

return lots;
}
double findHigh(){
double highestHigh = 0;
for(int i = 0; i < 200; i++){
double high = iHigh(_Symbol,Timeframe,i);
if(i > BarsN && iHighest(_Symbol,Timeframe,MODE_HIGH,BarsN*2+1,i-BarsN) == i){
if(high > highestHigh){
return high;
}
}
highestHigh = MathMax(high,highestHigh);
}
return -1;
}
double findLow(){
double lowestLow = DBL_MAX;
for(int i = 0; i < 200; i++){
double low = iLow(_Symbol,Timeframe,i);
if(i > BarsN && iLowest(_Symbol,Timeframe,MODE_LOW,BarsN*2+1,i-BarsN) == i){
if(low < lowestLow){
return low;
}
}
lowestLow = MathMin(low,lowestLow);
}
return -1;
}
 
I Think its ok now. Please try.


#define VERSION "1.0"
#property version VERSION
#define PROJECT_NAME MQLInfoString(MQL_PROGRAM_NAME)
#include <Trade/Trade.mqh>
input double Lots = 0.01;
input double RiskPercent = 2.0; //RiskPercent (0 = Fix)
input int OrderDistPoints = 1;
input int TpPoints = 10000;
input int SlPoints = 50;
input int TslPoints = 5;
input int TslTriggerPoints = 5;
input ENUM_TIMEFRAMES Timeframe = PERIOD_H1;
input int BarsN = 5;
input int ExpirationHours = 50;
input int Magic = 153;
CTrade trade;
ulong buyPos, sellPos;
int totalBars;
int OnInit(){
trade.SetExpertMagicNumber(Magic);
if(!trade.SetTypeFillingBySymbol(_Symbol)){
trade.SetTypeFilling(ORDER_FILLING_RETURN);
}
static bool isInit = false;
if(!isInit){
isInit = true;
Print(__FUNCTION__," > EA (re)start...");
Print(__FUNCTION__," > EA version ",VERSION,"...");

for(int i = PositionsTotal()-1; i >= 0; i--){
CPositionInfo pos;
if(pos.SelectByIndex(i)){
if(pos.Magic() != Magic) continue;
if(pos.Symbol() != _Symbol) continue;
Print(__FUNCTION__," > Found open position with ticket #",pos.Ticket(),"...");
if(pos.PositionType() == POSITION_TYPE_BUY) buyPos = pos.Ticket();
if(pos.PositionType() == POSITION_TYPE_SELL) sellPos = pos.Ticket();
}
}
for(int i = OrdersTotal()-1; i >= 0; i--){
COrderInfo order;
if(order.SelectByIndex(i)){
if(order.Magic() != Magic) continue;
if(order.Symbol() != _Symbol) continue;
Print(__FUNCTION__," > Found pending order with ticket #",order.Ticket(),"...");
if(order.OrderType() == ORDER_TYPE_BUY_LIMIT) buyPos = order.Ticket();
if(order.OrderType() == ORDER_TYPE_SELL_LIMIT) sellPos = order.Ticket();
}
}
}
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason){
}
void OnTick(){
processPos(buyPos);
processPos(sellPos);
int bars = iBars(_Symbol,Timeframe);
if(totalBars != bars){
totalBars = bars;

if(buyPos <= 0){
double high = findHigh();
if(high > 0){
executeBuy(high);
}
}

if(sellPos <= 0){
double low = findLow();
if(low > 0){
executeSell(low);
}
}
}
}
void OnTradeTransaction(
const MqlTradeTransaction& trans,
const MqlTradeRequest& request,
const MqlTradeResult& result
){

if(trans.type == TRADE_TRANSACTION_ORDER_ADD){
COrderInfo order;
if(order.Select(trans.order)){
if(order.Magic() == Magic){
if(order.OrderType() == ORDER_TYPE_BUY_LIMIT){
buyPos = order.Ticket();
}else if(order.OrderType() == ORDER_TYPE_SELL_LIMIT){
sellPos = order.Ticket();
}
}
}
}
}
void processPos(ulong &posTicket){
if(posTicket <= 0) return;
if(OrderSelect(posTicket)) return;

CPositionInfo pos;
if(!pos.SelectByTicket(posTicket)){
posTicket = 0;
return;
}else{
if(pos.PositionType() == POSITION_TYPE_BUY){
double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);

if(bid > pos.PriceOpen() + TslTriggerPoints * _Point){
double sl = bid - TslPoints * _Point;
sl = NormalizeDouble(sl,_Digits);

if(sl > pos.StopLoss()){
trade.PositionModify(pos.Ticket(),sl,pos.TakeProfit());
}
}
}else if(pos.PositionType() == POSITION_TYPE_SELL){
double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);

if(ask < pos.PriceOpen() - TslTriggerPoints * _Point){
double sl = ask + TslPoints * _Point;
sl = NormalizeDouble(sl,_Digits);

if(sl < pos.StopLoss() || pos.StopLoss() == 0){
trade.PositionModify(pos.Ticket(),sl,pos.TakeProfit());
}
}
}
}
}
void executeBuy(double entry){
entry = NormalizeDouble(entry,_Digits);

double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
if(ask > entry - OrderDistPoints * _Point) return;

double tp = entry + TpPoints * _Point;
tp = NormalizeDouble(tp,_Digits);

double sl = entry - SlPoints * _Point;
sl = NormalizeDouble(sl,_Digits);
double lots = Lots;
if(RiskPercent > 0) lots = calcLots(entry-sl);

datetime expiration = iTime(_Symbol,Timeframe,0) + ExpirationHours * PeriodSeconds(PERIOD_H1);
trade.BuyLimit(lots,entry,_Symbol,sl,tp,ORDER_TIME_SPECIFIED,expiration);

buyPos = trade.ResultOrder();
}
void executeSell(double entry){
entry = NormalizeDouble(entry,_Digits);
double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
if(bid < entry + OrderDistPoints * _Point) return;
double tp = entry - TpPoints * _Point;
tp = NormalizeDouble(tp,_Digits);

double sl = entry + SlPoints * _Point;
sl = NormalizeDouble(sl,_Digits);

double lots = Lots;
if(RiskPercent > 0) lots = calcLots(sl-entry);
datetime expiration = iTime(_Symbol,Timeframe,0) + ExpirationHours * PeriodSeconds(PERIOD_H1);
trade.SellLimit(lots,entry,_Symbol,sl,tp,ORDER_TIME_SPECIFIED,expiration);

sellPos = trade.ResultOrder();
}
double calcLots(double slPoints){
double risk = AccountInfoDouble(ACCOUNT_BALANCE) * RiskPercent / 100;

double ticksize = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
double tickvalue = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
double lotstep = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);

double moneyPerLotstep = slPoints / ticksize * tickvalue * lotstep;
double lots = MathFloor(risk / moneyPerLotstep) * lotstep;

lots = MathMin(lots,SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX));
lots = MathMax(lots,SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN));

return lots;
}
double findHigh(){
double highestHigh = 0;
for(int i = 0; i < 200; i++){
double high = iHigh(_Symbol,Timeframe,i);
if(i > BarsN && iHighest(_Symbol,Timeframe,MODE_HIGH,BarsN*2+1,i-BarsN) == i){
if(high > highestHigh){
return high;
}
}
highestHigh = MathMax(high,highestHigh);
}
return -1;
}
double findLow(){
double lowestLow = DBL_MAX;
for(int i = 0; i < 200; i++){
double low = iLow(_Symbol,Timeframe,i);
if(i > BarsN && iLowest(_Symbol,Timeframe,MODE_LOW,BarsN*2+1,i-BarsN) == i){
if(low < lowestLow){
return low;
}
}
lowestLow = MathMin(low,lowestLow);
}
return -1;
}
 
Acho que está tudo bem agora. Tente por favor.


#define VERSÃO "1.0"
#property versão VERSÃO
#define PROJECT_NAME MQLInfoString(MQL_PROGRAM_NAME)
#include <Trade/Trade.mqh>
input double Lotes = 0,01;
input double RiskPercent = 2,0; //RiskPercent (0 = Fix)
input int OrderDistPoints = 1;
entrada int TpPoints = 10000;
entrada int SlPoints = 50;
entrada int TslPoints = 5;
input int TslTriggerPoints = 5;
input ENUM_TIMEFRAMES Prazo = PERIOD_H1;
entrada int BarsN = 5;
input int ExpirationHours = 50;
entrada int Magia = 153;
CTrade comércio;
ulong buyPos, sellPos;
int totalBars;
int OnInit(){
trade.SetExpertMagicNumber(Magic);
if(!trade.SetTypeFillingBySymbol(_Symbol)){
trade.SetTypeFilling(ORDER_FILLING_RETURN);
}
static bool isInit = false;
if(!isInit){
isInit = verdadeiro;
Print(__FUNCTION__," > EA (re)iniciar...");
Print(__FUNCTION__,"> versão EA ",VERSION,"...");

for(int i = PositionsTotal()-1; i >= 0; i--){
CPositionInfo pos;
if(pos.SelectByIndex(i)){
if(pos.Magic() != Magic) continue;
if(pos.Symbol() != _Symbol) continuar;
Print(__FUNCTION__," > Encontrada posição em aberto com ticket #",pos.Ticket(),"...");
if(pos.PositionType() == POSITION_TYPE_BUY) buyPos = pos.Ticket();
if(pos.PositionType() == POSITION_TYPE_SELL) sellPos = pos.Ticket();
}
}
for(int i = OrdersTotal()-1; i >= 0; i--){
ordem COrderInfo;
if(order.SelectByIndex(i)){
if(order.Magic() != Magic) continue;
if(order.Symbol() != _Symbol) continue;
Print(__FUNCTION__," > Encontrado pedido pendente com ticket #",order.Ticket(),"...");
if(order.OrderType() == ORDER_TYPE_BUY_LIMIT) buyPos = order.Ticket();
if(order.OrderType() == ORDER_TYPE_SELL_LIMIT) sellPos = order.Ticket();
}
}
}
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason){
}
void OnTick(){
processoPos(compraPos);
processoPos(vendaPos);
int bars = iBars(_Symbol,Timeframe);
if(totalBars != bares){
totaisBarras = barras;

if(compraPos <= 0){
duplo alto = encontrarAlto();
if(alta > 0){
executeBuy(alta);
}
}

if(vendaPos <= 0){
duplo baixo = encontrarBaixo();
if(baixo > 0){
executeVenda(baixa);
}
}
}
}
void OnTradeTransaction(
const MqlTradeTransaction& trans,
const MqlTradeRequest& request,
const MqlTradeResult& resultado
){

if(trans.type == TRADE_TRANSACTION_ORDER_ADD){
ordem COrderInfo;
if(ordem.Select(trans.ordem)){
if(order.Magic() == Magic){
if(order.OrderType() == ORDER_TYPE_BUY_LIMIT){
buyPos = pedido.Ticket();
}else if(order.OrderType() == ORDER_TYPE_SELL_LIMIT){
sellPos = pedido.Ticket();
}
}
}
}
}
void processPos(ulong &posTicket){
if(posTicket <= 0) return;
if(OrderSelect(posTicket)) return;

CPositionInfo pos;
if(!pos.SelectByTicket(posTicket)){
pósTicket = 0;
Retorna;
}outro{
if(pos.PositionType() == POSITION_TYPE_BUY){
lance duplo = SymbolInfoDouble(_Symbol,SYMBOL_BID);

if(bid > pos.PriceOpen() + TslTriggerPoints * _Point){
double sl = lance - TslPoints * _Point;
sl = NormalizeDouble(sl,_Digitos);

if(sl > pos.StopLoss()){
trade.PositionModify(pos.Ticket(),sl,pos.TakeProfit());
}
}
}else if(pos.PositionType() == POSITION_TYPE_SELL){
pergunta dupla = SymbolInfoDouble(_Symbol,SYMBOL_ASK);

if(pergunte < pos.PriceOpen() - TslTriggerPoints * _Point){
double sl = ask + TslPoints * _Point;
sl = NormalizeDouble(sl,_Digitos);

if(sl < pos.StopLoss() || pos.StopLoss() == 0){
trade.PositionModify(pos.Ticket(),sl,pos.TakeProfit());
}
}
}
}
}
void executeBuy(entrada dupla){
entrada = NormalizeDouble(entrada,_Digitos);

pergunta dupla = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
if(pergunte > entrada - OrderDistPoints * _Point) return;

double tp = entrada + TpPoints * _Point;
tp = NormalizeDouble(tp,_Digits);

double sl = entrada - SlPoints * _Point;
sl = NormalizeDouble(sl,_Digitos);
lotes duplos = Lotes;
if(RiskPercent > 0) lotes = calcLots(entry-sl);

data e hora expiração = iTime(_Symbol,Timeframe,0) + ExpirationHours * PeriodSeconds(PERIOD_H1);
trade.BuyLimit(lotes,entrada,_Símbolo,sl,tp,ORDER_TIME_SPECIFIED,expiração);

buyPos = trade.ResultOrder();
}
void executeSell(entrada dupla){
entrada = NormalizeDouble(entrada,_Digitos);
lance duplo = SymbolInfoDouble(_Symbol,SYMBOL_BID);
if(lance < entrada + OrderDistPoints * _Point) return;
double tp = entrada - TpPoints * _Point;
tp = NormalizeDouble(tp,_Digits);

double sl = entrada + SlPoints * _Point;
sl = NormalizeDouble(sl,_Digitos);

lotes duplos = Lotes;
if(RiskPercent > 0) lotes = calcLots(sl-entry);
data e hora expiração = iTime(_Symbol,Timeframe,0) + ExpirationHours * PeriodSeconds(PERIOD_H1);
trade.SellLimit(lotes,entrada,_Símbolo,sl,tp,ORDER_TIME_SPECIFIED,expiração);

sellPos = trade.ResultOrder();
}
double calcLots(double slPoints){
risco duplo = AccountInfoDouble(ACCOUNT_BALANCE) * RiskPercent / 100;

double ticksize = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
double tickvalue = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
double lotstep = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);

double moneyPerLotstep = slPoints / ticksize * tickvalue * lotstep;
lotes duplos = MathFloor(risco / dinheiroPorLotstep) * lotstep;

lotes = MathMin(lotes,SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX));
lotes = MathMax(lotes,SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN));

devolver lotes;
}
double findHigh(){
duplo mais altoAlto = 0;
for(int i = 0; i < 200; i++){
duplo alto = iHigh(_Symbol,Timeframe,i);
if(i > BarsN && iHighest(_Symbol,Timeframe,MODE_HIGH,BarsN*2+1,i-BarsN) == i){
if(alto > mais altoAlto){
retorno alto;
}
}
maiorAlto = MathMax(alto,maiorAlto);
}
retornar -1;
}
double findLow(){
duplo mais baixoBaixo = DBL_MAX;
for(int i = 0; i < 200; i++){
duplo baixo = iLow(_Symbol,Timeframe,i);
if(i > BarsN && iLowest(_Symbol,Timeframe,MODE_LOW,BarsN*2+1,i-BarsN) == i){
if(baixo <menorbaixo){
retorno baixo;
}
}
menorBaixo = MathMin(baixo,menorbaixo);
}
retornar -1;
}[/CITAR]

did not compile, 101 errors appeared
 
Acho que está tudo bem agora. Tente por favor.


#define VERSÃO "1.0"
#property versão VERSÃO
#define PROJECT_NAME MQLInfoString(MQL_PROGRAM_NAME)
#include <Trade/Trade.mqh>
input double Lotes = 0,01;
input double RiskPercent = 2,0; //RiskPercent (0 = Fix)
input int OrderDistPoints = 1;
entrada int TpPoints = 10000;
entrada int SlPoints = 50;
entrada int TslPoints = 5;
input int TslTriggerPoints = 5;
input ENUM_TIMEFRAMES Prazo = PERIOD_H1;
entrada int BarsN = 5;
input int ExpirationHours = 50;
entrada int Magia = 153;
CTrade comércio;
ulong buyPos, sellPos;
int totalBars;
int OnInit(){
trade.SetExpertMagicNumber(Magic);
if(!trade.SetTypeFillingBySymbol(_Symbol)){
trade.SetTypeFilling(ORDER_FILLING_RETURN);
}
static bool isInit = false;
if(!isInit){
isInit = verdadeiro;
Print(__FUNCTION__," > EA (re)iniciar...");
Print(__FUNCTION__,"> versão EA ",VERSION,"...");

for(int i = PositionsTotal()-1; i >= 0; i--){
CPositionInfo pos;
if(pos.SelectByIndex(i)){
if(pos.Magic() != Magic) continue;
if(pos.Symbol() != _Symbol) continuar;
Print(__FUNCTION__," > Encontrada posição em aberto com ticket #",pos.Ticket(),"...");
if(pos.PositionType() == POSITION_TYPE_BUY) buyPos = pos.Ticket();
if(pos.PositionType() == POSITION_TYPE_SELL) sellPos = pos.Ticket();
}
}
for(int i = OrdersTotal()-1; i >= 0; i--){
ordem COrderInfo;
if(order.SelectByIndex(i)){
if(order.Magic() != Magic) continue;
if(order.Symbol() != _Symbol) continue;
Print(__FUNCTION__," > Encontrado pedido pendente com ticket #",order.Ticket(),"...");
if(order.OrderType() == ORDER_TYPE_BUY_LIMIT) buyPos = order.Ticket();
if(order.OrderType() == ORDER_TYPE_SELL_LIMIT) sellPos = order.Ticket();
}
}
}
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason){
}
void OnTick(){
processoPos(compraPos);
processoPos(vendaPos);
int bars = iBars(_Symbol,Timeframe);
if(totalBars != bares){
totaisBarras = barras;

if(compraPos <= 0){
duplo alto = encontrarAlto();
if(alta > 0){
executeBuy(alta);
}
}

if(vendaPos <= 0){
duplo baixo = encontrarBaixo();
if(baixo > 0){
executeVenda(baixa);
}
}
}
}
void OnTradeTransaction(
const MqlTradeTransaction& trans,
const MqlTradeRequest& request,
const MqlTradeResult& resultado
){

if(trans.type == TRADE_TRANSACTION_ORDER_ADD){
ordem COrderInfo;
if(ordem.Select(trans.ordem)){
if(order.Magic() == Magic){
if(order.OrderType() == ORDER_TYPE_BUY_LIMIT){
buyPos = pedido.Ticket();
}else if(order.OrderType() == ORDER_TYPE_SELL_LIMIT){
sellPos = pedido.Ticket();
}
}
}
}
}
void processPos(ulong &posTicket){
if(posTicket <= 0) return;
if(OrderSelect(posTicket)) return;

CPositionInfo pos;
if(!pos.SelectByTicket(posTicket)){
pósTicket = 0;
Retorna;
}outro{
if(pos.PositionType() == POSITION_TYPE_BUY){
lance duplo = SymbolInfoDouble(_Symbol,SYMBOL_BID);

if(bid > pos.PriceOpen() + TslTriggerPoints * _Point){
double sl = lance - TslPoints * _Point;
sl = NormalizeDouble(sl,_Digitos);

if(sl > pos.StopLoss()){
trade.PositionModify(pos.Ticket(),sl,pos.TakeProfit());
}
}
}else if(pos.PositionType() == POSITION_TYPE_SELL){
pergunta dupla = SymbolInfoDouble(_Symbol,SYMBOL_ASK);

if(pergunte < pos.PriceOpen() - TslTriggerPoints * _Point){
double sl = ask + TslPoints * _Point;
sl = NormalizeDouble(sl,_Digitos);

if(sl < pos.StopLoss() || pos.StopLoss() == 0){
trade.PositionModify(pos.Ticket(),sl,pos.TakeProfit());
}
}
}
}
}
void executeBuy(entrada dupla){
entrada = NormalizeDouble(entrada,_Digitos);

pergunta dupla = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
if(pergunte > entrada - OrderDistPoints * _Point) return;

double tp = entrada + TpPoints * _Point;
tp = NormalizeDouble(tp,_Digits);

double sl = entrada - SlPoints * _Point;
sl = NormalizeDouble(sl,_Digitos);
lotes duplos = Lotes;
if(RiskPercent > 0) lotes = calcLots(entry-sl);

data e hora expiração = iTime(_Symbol,Timeframe,0) + ExpirationHours * PeriodSeconds(PERIOD_H1);
trade.BuyLimit(lotes,entrada,_Símbolo,sl,tp,ORDER_TIME_SPECIFIED,expiração);

buyPos = trade.ResultOrder();
}
void executeSell(entrada dupla){
entrada = NormalizeDouble(entrada,_Digitos);
lance duplo = SymbolInfoDouble(_Symbol,SYMBOL_BID);
if(lance < entrada + OrderDistPoints * _Point) return;
double tp = entrada - TpPoints * _Point;
tp = NormalizeDouble(tp,_Digits);

double sl = entrada + SlPoints * _Point;
sl = NormalizeDouble(sl,_Digitos);

lotes duplos = Lotes;
if(RiskPercent > 0) lotes = calcLots(sl-entry);
data e hora expiração = iTime(_Symbol,Timeframe,0) + ExpirationHours * PeriodSeconds(PERIOD_H1);
trade.SellLimit(lotes,entrada,_Símbolo,sl,tp,ORDER_TIME_SPECIFIED,expiração);

sellPos = trade.ResultOrder();
}
double calcLots(double slPoints){
risco duplo = AccountInfoDouble(ACCOUNT_BALANCE) * RiskPercent / 100;

double ticksize = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
double tickvalue = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
double lotstep = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);

double moneyPerLotstep = slPoints / ticksize * tickvalue * lotstep;
lotes duplos = MathFloor(risco / dinheiroPorLotstep) * lotstep;

lotes = MathMin(lotes,SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX));
lotes = MathMax(lotes,SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN));

devolver lotes;
}
double findHigh(){
duplo mais altoAlto = 0;
for(int i = 0; i < 200; i++){
duplo alto = iHigh(_Symbol,Timeframe,i);
if(i > BarsN && iHighest(_Symbol,Timeframe,MODE_HIGH,BarsN*2+1,i-BarsN) == i){
if(alto > mais altoAlto){
retorno alto;
}
}
maiorAlto = MathMax(alto,maiorAlto);
}
retornar -1;
}
double findLow(){
duplo mais baixoBaixo = DBL_MAX;
for(int i = 0; i < 200; i++){
duplo baixo = iLow(_Symbol,Timeframe,i);
if(i > BarsN && iLowest(_Symbol,Timeframe,MODE_LOW,BarsN*2+1,i-BarsN) == i){
if(baixo <menorbaixo){
retorno baixo;
}
}
menorBaixo = MathMin(baixo,menorbaixo);
}
retornar -1;
}[/CITAR]

did not compile, 101 errors appeared
 
I Think its ok now. Please try.


#define VERSION "1.0"
#property version VERSION
#define PROJECT_NAME MQLInfoString(MQL_PROGRAM_NAME)
#include <Trade/Trade.mqh>
input double Lots = 0.01;
input double RiskPercent = 2.0; //RiskPercent (0 = Fix)
input int OrderDistPoints = 1;
input int TpPoints = 10000;
input int SlPoints = 50;
input int TslPoints = 5;
input int TslTriggerPoints = 5;
input ENUM_TIMEFRAMES Timeframe = PERIOD_H1;
input int BarsN = 5;
input int ExpirationHours = 50;
input int Magic = 153;
CTrade trade;
ulong buyPos, sellPos;
int totalBars;
int OnInit(){
trade.SetExpertMagicNumber(Magic);
if(!trade.SetTypeFillingBySymbol(_Symbol)){
trade.SetTypeFilling(ORDER_FILLING_RETURN);
}
static bool isInit = false;
if(!isInit){
isInit = true;
Print(__FUNCTION__," > EA (re)start...");
Print(__FUNCTION__," > EA version ",VERSION,"...");

for(int i = PositionsTotal()-1; i >= 0; i--){
CPositionInfo pos;
if(pos.SelectByIndex(i)){
if(pos.Magic() != Magic) continue;
if(pos.Symbol() != _Symbol) continue;
Print(__FUNCTION__," > Found open position with ticket #",pos.Ticket(),"...");
if(pos.PositionType() == POSITION_TYPE_BUY) buyPos = pos.Ticket();
if(pos.PositionType() == POSITION_TYPE_SELL) sellPos = pos.Ticket();
}
}
for(int i = OrdersTotal()-1; i >= 0; i--){
COrderInfo order;
if(order.SelectByIndex(i)){
if(order.Magic() != Magic) continue;
if(order.Symbol() != _Symbol) continue;
Print(__FUNCTION__," > Found pending order with ticket #",order.Ticket(),"...");
if(order.OrderType() == ORDER_TYPE_BUY_LIMIT) buyPos = order.Ticket();
if(order.OrderType() == ORDER_TYPE_SELL_LIMIT) sellPos = order.Ticket();
}
}
}
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason){
}
void OnTick(){
processPos(buyPos);
processPos(sellPos);
int bars = iBars(_Symbol,Timeframe);
if(totalBars != bars){
totalBars = bars;

if(buyPos <= 0){
double high = findHigh();
if(high > 0){
executeBuy(high);
}
}

if(sellPos <= 0){
double low = findLow();
if(low > 0){
executeSell(low);
}
}
}
}
void OnTradeTransaction(
const MqlTradeTransaction& trans,
const MqlTradeRequest& request,
const MqlTradeResult& result
){

if(trans.type == TRADE_TRANSACTION_ORDER_ADD){
COrderInfo order;
if(order.Select(trans.order)){
if(order.Magic() == Magic){
if(order.OrderType() == ORDER_TYPE_BUY_LIMIT){
buyPos = order.Ticket();
}else if(order.OrderType() == ORDER_TYPE_SELL_LIMIT){
sellPos = order.Ticket();
}
}
}
}
}
void processPos(ulong &posTicket){
if(posTicket <= 0) return;
if(OrderSelect(posTicket)) return;

CPositionInfo pos;
if(!pos.SelectByTicket(posTicket)){
posTicket = 0;
return;
}else{
if(pos.PositionType() == POSITION_TYPE_BUY){
double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);

if(bid > pos.PriceOpen() + TslTriggerPoints * _Point){
double sl = bid - TslPoints * _Point;
sl = NormalizeDouble(sl,_Digits);

if(sl > pos.StopLoss()){
trade.PositionModify(pos.Ticket(),sl,pos.TakeProfit());
}
}
}else if(pos.PositionType() == POSITION_TYPE_SELL){
double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);

if(ask < pos.PriceOpen() - TslTriggerPoints * _Point){
double sl = ask + TslPoints * _Point;
sl = NormalizeDouble(sl,_Digits);

if(sl < pos.StopLoss() || pos.StopLoss() == 0){
trade.PositionModify(pos.Ticket(),sl,pos.TakeProfit());
}
}
}
}
}
void executeBuy(double entry){
entry = NormalizeDouble(entry,_Digits);

double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
if(ask > entry - OrderDistPoints * _Point) return;

double tp = entry + TpPoints * _Point;
tp = NormalizeDouble(tp,_Digits);

double sl = entry - SlPoints * _Point;
sl = NormalizeDouble(sl,_Digits);
double lots = Lots;
if(RiskPercent > 0) lots = calcLots(entry-sl);

datetime expiration = iTime(_Symbol,Timeframe,0) + ExpirationHours * PeriodSeconds(PERIOD_H1);
trade.BuyLimit(lots,entry,_Symbol,sl,tp,ORDER_TIME_SPECIFIED,expiration);

buyPos = trade.ResultOrder();
}
void executeSell(double entry){
entry = NormalizeDouble(entry,_Digits);
double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
if(bid < entry + OrderDistPoints * _Point) return;
double tp = entry - TpPoints * _Point;
tp = NormalizeDouble(tp,_Digits);

double sl = entry + SlPoints * _Point;
sl = NormalizeDouble(sl,_Digits);

double lots = Lots;
if(RiskPercent > 0) lots = calcLots(sl-entry);
datetime expiration = iTime(_Symbol,Timeframe,0) + ExpirationHours * PeriodSeconds(PERIOD_H1);
trade.SellLimit(lots,entry,_Symbol,sl,tp,ORDER_TIME_SPECIFIED,expiration);

sellPos = trade.ResultOrder();
}
double calcLots(double slPoints){
double risk = AccountInfoDouble(ACCOUNT_BALANCE) * RiskPercent / 100;

double ticksize = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
double tickvalue = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
double lotstep = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);

double moneyPerLotstep = slPoints / ticksize * tickvalue * lotstep;
double lots = MathFloor(risk / moneyPerLotstep) * lotstep;

lots = MathMin(lots,SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX));
lots = MathMax(lots,SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN));

return lots;
}
double findHigh(){
double highestHigh = 0;
for(int i = 0; i < 200; i++){
double high = iHigh(_Symbol,Timeframe,i);
if(i > BarsN && iHighest(_Symbol,Timeframe,MODE_HIGH,BarsN*2+1,i-BarsN) == i){
if(high > highestHigh){
return high;
}
}
highestHigh = MathMax(high,highestHigh);
}
return -1;
}
double findLow(){
double lowestLow = DBL_MAX;
for(int i = 0; i < 200; i++){
double low = iLow(_Symbol,Timeframe,i);
if(i > BarsN && iLowest(_Symbol,Timeframe,MODE_LOW,BarsN*2+1,i-BarsN) == i){
if(low < lowestLow){
return low;
}
}
lowestLow = MathMin(low,lowestLow);
}
return -1;
}
buylimit and selllimit you changed is not opening orders please solve the problem
 
Back
Top