I've written some code on an Expert Advisor to trade on the Fiji-trend-indicator. The EA recognises the first arrow indicator and executes a trade accordingly, however, when the second arrow is supposed to show up on a back test, the rest of the code doesn't do anything.
I see that the custom price box in the indicator that follows the bid price on the chart stops moving up or down in value when the EA executes the first trade, maybe that's where thing go wrong?
double CloseAllTradesThisPair()
{
for (int i=OrdersTotal();i>=0;i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if (OrderSymbol()==Symbol())
{
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),10,Red);
}
}
}
extern bool bought = false;
extern bool sold = false;
double Lotsize = AccountBalance()/5000;
double Lots = NormalizeDouble(Lotsize,2);
void OnTick()
{
for(int i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
return(0);
}
double uparrow = iCustom(Symbol(),PERIOD_CURRENT,"fiji-trend-indicator",2,0);
double dnarrow = iCustom(Symbol(),PERIOD_CURRENT,"fiji-trend-indicator",3,0);
if(uparrow < 1000 && i==0)
{
double buyticket = OrderSend(Symbol(),OP_BUY,0.01,Ask,10,0,0,NULL,0,0,Green);
bought = true;
}
if(dnarrow < 1000 && i==0)
{
double sellticket = OrderSend(Symbol(),OP_SELL,0.01,Bid,10,0,0,NULL,0,0,Red);
sold = true;
}
if(bought != true && dnarrow < 1000 )
{
CloseAllTradesThisPair();
double sticket = OrderSend(Symbol(),OP_SELL,0.01,Bid,10,0,0,NULL,0,0,Red);
}
if(sold != true && uparrow < 1000)
{
CloseAllTradesThisPair();
double bticket = OrderSend(Symbol(),OP_BUY,0.01,Ask,10,0,0,NULL,0,0,Green);
}
}
Q : "when the EA executes the first trade, maybe that's where thing go wrong?"
Oh sure it is.
Given the code, the EA does exactly what it is instructed to in the void OnTick(){...}
-code:
...
for ( int i = 0; i < OrdersTotal(); i++ )
{
OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
return( 0 ); // _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _THE_TERMINATOR_ >>> FINITO...
}
...
This fragment does nothing, until a 1st order was placed (as then the i
stopped to meet the condition < OrdersTotal()
.
The rest is obvious: return( 0 );
...besides a flaw to return a value from void
... terminates under no exceptions any and all next code-passes through the OnTick()
-code, so effectively avoiding any trading at all: