evanio
March 29, 2020, 5:20pm
1
Friends
I am trying to code an EA but I have a small problem: when I close the order or the EA closes the order the EA again opens another order in the same direction of the trend, I would like the EA to open another order only when another signal appears in the opposite direction.
In other words, the EA must open an order and after the order is closed the next order must be opened only when a new signal appears (and in the opposite direction), I want the EA to be able to open orders in the sequence BUY-SELL-BUY- SELL …
//±-----------------------------------------------------------------+
//| |
//±-----------------------------------------------------------------+
void TradeRules()
{
if(UseTrailing)
Trailing2(TrailingStop_,Trailing_MinimumProfit,Trailing_Step);
bool IsNewBar=Time[0]>candletime;
if(IsNewBar)
{
if(UseCloseOppositeSignals && indicator2(0,1)>0)
CloseAll(1);
if(UseCloseOppositeSignals && indicator2(1,1)>0)
CloseAll(0);
if(BuySignal())
{
OrderOpen(0);
candletime=Time[0];
}
//---
if(SellSignal())
{
OrderOpen(1);
candletime=Time[0];
}
}
}
//±-----------------------------------------------------------------+
//| |
//±-----------------------------------------------------------------+
bool BuySignal()
{
bool signal=false;
if(indicator1(0,1)>0 && indicator2(0,1)>0)
{
signal=true;
}
return signal;
}
//±-----------------------------------------------------------------+
//| |
//±-----------------------------------------------------------------+
bool SellSignal()
{
bool signal=false;
if(indicator1(1,1)>0 && indicator2(1,1)>0)
{
signal=true;
}
return signal;
}
Your open and close conditions are the same for indicator2. So it all depends on what indicator1 is doing at the time you close the trade. Doesn’t make a lot of sense that your condition for opening a trade is the same as your condition for closing it, but I don’t have time to read your whole code and work out what you’re doing right now…
if(UseCloseOppositeSignals && indicator2(0,1)>0 )
CloseAll(1);
bool BuySignal()
{
bool signal=false;
if(indicator1(0,1)>0 && indicator2(0,1)>0 )
{
signal=true;
}
return signal;
}
evanio
March 29, 2020, 6:38pm
3
The EA logic is correct. Open and close transactions as I want, the problem is that if I close an order manually or for take profit the EA opens another order again. I want it to open only at the opposite sign.
I can’t fix the code
Can you add a variable that tracks what position was last opened and stop it from reopening when it is closed?
evanio
March 29, 2020, 7:08pm
5
That’s exactly what I thought, but I don’t know how to do it.
static string lastorder;
if(BuySignal() && lastorder == “sell”)
{
OrderOpen(0);
candletime=Time[0];
lastorder = “buy”;
}
does that work? it’s a huge amount of code to try and read through.
evanio
March 29, 2020, 7:16pm
7
I will test here, but I believe it will solve my problem, thank you very much.
1 Like
Good luck.
Please share your back testing and progress on here. I’m always interested
evanio
March 29, 2020, 7:29pm
10
//±-----------------------------------------------------------------+
//| |
//±-----------------------------------------------------------------+
void TradeRules()
{
if(UseTrailing)
Trailing2(TrailingStop_,Trailing_MinimumProfit,Trailing_Step);
bool IsNewBar=Time[0]>candletime;
if(IsNewBar)
{
if(UseCloseOppositeSignals && indicator2(0,1)>0)
CloseAll(1);
if(UseCloseOppositeSignals && indicator2(1,1)>0)
CloseAll(0);
if(BuySignal() && lastorder == "sell")
{
OrderOpen(0);
candletime=Time[0];
lastorder = "buy";
}
//---
if(SellSignal() && lastorder == "buy")
{
OrderOpen(1);
candletime=Time[0];
lastorder = "sell";
}
}
}
I tried this here. I believe it will resolve.
//±-----------------------------------------------------------------+
//| |
//±-----------------------------------------------------------------+
1 Like
evanio
April 3, 2020, 5:21pm
11
Did not work. Don’t open orders
you need to create function lastordertypeclosed(), then use that function to filter your buy and sell signal.
if(BuySignal() && lastordertypeclosed()==OP_SELL)
{
OrderOpen(0);
candletime=Time[0];
}
//—
if(SellSignal() && lastordertypeclosed()==OP_BUY)
{
OrderOpen(1);
candletime=Time[0];
}
function lastordertypeclosed()
int lastordertypeclosed(){
//-------create function to get last order type
}
evanio
April 6, 2020, 10:56pm
13
My friends thank you very much, thanks to the help of you I managed to solve the problem in my EA. God bless you.
evanio
April 6, 2020, 10:56pm
14
I managed to solve a lot thanks my dear
it is great you solve it my friend
evanio
April 15, 2020, 6:34pm
18
Friends I need one more help:
I would like to replace the orders with BUY LIMIT and SELL LIMIT
ie when the EA opens the orders it opens Sell Limit and BUY limit
evanio
April 15, 2020, 7:00pm
19
//±-----------------------------------------------------------------+
int OrderOpen(int type)
{
double openprice=0;
double stoploss=0;
double takeprofit=0;
double lot=LotSize;
if(Count(0)+Count(1)>=MaxOrders)
return 0;
if(type==0)
{
openprice=Ask;
if(StopLoss>0)
stoploss=openprice-(StopLoss*_Point);
if(UseAtrSL)
stoploss=openprice-(Atr(1)AtrMultipliers);
if(TakeProfit>0)
takeprofit=openprice+(TakeProfit _Point);
}
//—
if(type==1)
{
openprice=Bid;
if(StopLoss>0)
stoploss=openprice+(StopLoss*_Point);
if(UseAtrSL)
stoploss=openprice+(Atr(1)AtrMultipliers);
if(TakeProfit>0)
takeprofit=openprice-(TakeProfit _Point);
}