Nice job! I'm looking forward to getting this one fully operational...
To that end, I have one comment and then some code ( )
In LookForTradingOpportunities() you only move the prior trade to BE if you are trying to get in a new trade. Shouldn’t you try to move to BE regardless and then just verify the last trade is at BE before taking a new one? As it is, our additional filters can not only keep us from getting a new trade started they can prevent one from moving to BE. Also, the rules seem to say that you move to break even if in profit so you could just test OrderProfit() > 0, but I think we need to have a pips in profit threshold of some sort.
Now for the fun part. Consider the following as really detailed pseudo-code. It compiles, but is untested.
From the rules, I read three different scaling out strategies: close some, close half, close all but two strongest. I combined these to "close the weakest half".
I didn't really know where to stick this code. LookForTradeClosure() is about managing TP/SL on individual trades, so I created StrategyLookForTradeClosure(). It is:
Code: Select all
void StrategyLookForTradeClosure() {
if (OpenTrades == 0) return;
bool CloseHalf=false;
//check for buys
if (BuyOpen)
if (Close[i+1] < Low[PreviousUpBar(i+1)]) CloseHalf=true;
if (SellOpen)
if (Close[i+1] > High[PreviousDnBar(i+1)]) CloseHalf=true;
if (CloseHalf) {
//count the trades
int cnt = 0;
int total = OrdersTotal();
for (i=0;i<total;i++)
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
if (OrderType() != OP_BUY && OrderType() != OP_SELL) continue;
if (OrderMagicNumber() != MagicNumber) continue;
if (OrderSymbol() != Symbol()) continue;
cnt++;
}//if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
//and close half of them
for (int i=1;i<cnt/2;i++)
CloseWorstTrade();
}
}
Code: Select all
int PreviousUpBar(int i) {
i++;
while(Close[i]<=Open[i]) i++;
return(i);
}
int PreviousDnBar(int i) {
i++;
while(Close[i]>=Open[i]) i++;
return(i);
}
Code: Select all
void CloseWorstTrade() {
//find worst performing trade
int total = OrdersTotal();
int worst = 0;
double worst_profit = 10000000;
for (i=0;i<total;i++)
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
if (OrderType() != OP_BUY && OrderType() != OP_SELL) continue;
if (OrderMagicNumber() != MagicNumber) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderProfit()< worst_profit) {
worst = OrderTicket();
worst_profit = OrderProfit();
}//if (OrderProfit()< worst_profit)
}//if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
//close worst trade
bool result = CloseTrade(worst);
//Actions when trade close succeeds
if (result)
{
DeletePendingPriceLines();
TicketNo = -1;//TicketNo is the most recently trade opened, so this might need editing in a multi-trade EA
OpenTrades--;//Rather than OpenTrades = 0 to cater for multi-trade EA's
}//if (result)
}
As I like to say, you can learn from good examples as well as bad, so hopefully this will give you a headstart on the rest of the bot, one way or the other.
George