mql4 envelope EA opens two trades on same bar?
mql4 envelope EA opens two trades on same bar?
Loading saved threads...
Why So Serious · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Why So Serious
Original post: https://stackoverflow.com/questions/75497292
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
my strategy is based on the envelope indicator :
-for a buy order a candle has to close above the envelope line
-for a sell order the candle has to close below the envelope line
The EA seems to enter trades based on those rules but whenever a candle closes above the envelope line and the EA enters a buy trade if the sl has been it immediately opens another trade without checking the rules for a buy or sell order
//+------------------------------------------------------------------+
//| ProjectName |
//| Copyright 2018, CompanyName |
//| http://www.companyname.net |
//+------------------------------------------------------------------+
input double StopLoss=50.0; // Stop Loss in pips
input double TakeProfit=100.0; // Take Profit in pips
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
//create an empty string for the signal
string signal="";
//symbol, period,14 candles,SMA,no shift,close price,deviation 0.10,buffer line,candle 0
double LowerBand=iEnvelopes(_Symbol,_Period,14,MODE_SMA,0,PRICE_CLOSE,0.10,MODE_LOWER,0);
double UpperBand=iEnvelopes(_Symbol,_Period,14,MODE_SMA,0,PRICE_CLOSE,0.10,MODE_UPPER,0);
//if close price is below lower band
if(Close[1]<LowerBand)
{
signal="sell";
}
//if close price is above upper band
if(Close[1]>UpperBand)
{
signal="buy";
}
if(signal=="sell"&&OrdersTotal()==0)
{
//send a sell order
double sellSL=Bid+StopLoss*_Point;
double sellTP=Bid-TakeProfit*_Point;
OrderSend(_Symbol,OP_SELL,0.10,Bid,3,sellSL,sellTP,NULL,0,0,Red);
}
if(signal=="buy"&&OrdersTotal()==0)
{
//send a buy order
double buySL=Ask-StopLoss*_Point;
double buyTP=Ask+TakeProfit*_Point;
OrderSend(_Symbol,OP_BUY,0.10,Ask,3,buySL,buyTP,NULL,0,0,Green);
}
// create a chart output
Comment("BLZ TRADING EA");
}
//+------------------------------------------------------------------+
Quote
Report
nyuletony · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: nyuletony
Original post: https://stackoverflow.com/a/75537799
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
If I understood your query I think you are intending to have the program open one trade per bar. There are many approaches to it and I have applied this one which seem to be working. I don't know why the Stoploss is not working though.
//+------------------------------------------------------------------+
//| ProjectName |
//| Copyright 2018, CompanyName |
//| http://www.companyname.net |
//+------------------------------------------------------------------+
input double StopLoss=50.0; // Stop Loss in pips
input double TakeProfit=100.0; // Take Profit in pips
int trade_opened = false; // To check that there are no open trades(on
the bar)
double bar_index; //To check the bar number
double ticket; //To check OrderSend function
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
//create an empty string for the signal
string signal="";
//symbol, period,14 candles,SMA,no shift,close price,deviation 0.10,buffer line,candle 0
double LowerBand=iEnvelopes(_Symbol,_Period,14,MODE_SMA,0,PRICE_CLOSE,0.10,MODE_LOWER,0);
double UpperBand=iEnvelopes(_Symbol,_Period,14,MODE_SMA,0,PRICE_CLOSE,0.10,MODE_UPPER,0);
//if close price is below lower band
if(Close[1]<LowerBand)
{
signal="sell";
}
//if close price is above upper band
if(Close[1]>UpperBand)
{
signal="buy";
}
if(signal=="sell"&&OrdersTotal()==0)
{
//send a sell order
if (!trade_opened && bar_index == 1)
double sellSL=Bid+StopLoss*_Point;
double sellTP=Bid-TakeProfit*_Point;
ticket=OrderSend(_Symbol,OP_SELL,0.10,Bid,3,sellSL,sellTP,NULL,0,0,Red);
trade_opened = true;
}
if(signal=="buy"&&OrdersTotal()==0)
{
//send a buy order
if (!trade_opened && bar_index == 1)
double buySL=Ask-StopLoss*_Point;
double buyTP=Ask+TakeProfit*_Point;
ticket=OrderSend(_Symbol,OP_BUY,0.10,Ask,3,buySL,buyTP,NULL,0,0,Green);
trade_opened = true;
}
// create a chart output
Comment("BLZ TRADING EA");
}
//+------------------------------------------------------------------+
Quote
Report
Ssempala Ibrahim · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Ssempala Ibrahim
Original post: https://stackoverflow.com/a/79461792
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
first the data here should be of a boolean data type
int trade_opened = false; // To check that there are no open trades. Some times the code compiles but the code might no work as intended
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: Ssempala Ibrahim Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/a/79461792 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. first the data here should be of a boolean data type int trade_opened = false; // To check that there are no open trades. Some times the code compiles but the code might no work as intended
Checking account access…