Need some help with coding...
Ok I didn't know it was permitted.
double Force, Force_prev, Stoch_m, Stoch_s, Lts, Balance, Bar_L, Bar_H, SL_B, TP_B, SL_S, TP_S;
bool New_Bar=false;
// ----------------------------- START EA -------------------------------
int start()
{
Force=iForce(NULL,0,21,MODE_SMA, PRICE_CLOSE,0); // Force of the 0 bar
Force_prev=iForce(NULL,0,21,MODE_SMA, PRICE_CLOSE,2); // Force of the 2 bar
Stoch_m=iStochastic(NULL,0,6,4,5,MODE_SMA,0,MODE_MAIN,0); // SO main line
Stoch_s=iStochastic(NULL,0,6,4,5,MODE_SMA,0,MODE_SIGNAL,0); // SO signal line
if (Criteria_Buy()==true) // User-defined function for...
{ // a long position
RefreshRates();
OrderSend(Symbol(),OP_BUY,Lots(),Ask,10,0,0); // Open buy with Lots() calculated...
return; // in another user-defined fincion
}
if (Criteria_Sell()==true) // User-defined function for...
{ // a short position
RefreshRates();
OrderSend(Symbol(),OP_SELL,Lots(),Bid,10,0,0); // Open sell with Lots() calculated...
return; // in another user-defined fincion
}
static datetime New_Time=0; // Using it to search for a new bar
New_Bar=false; // No new bar
if (New_Time!=Time[0]) // Comparing time...
{
New_Time=Time[0];
New_Bar=true; // A new bar is formed...
} // which becomes the 0 one
Bar_L=iLow(Symbol(),PERIOD_H1,1); // Get the low of the previous bar
Bar_H=iHigh(Symbol(),PERIOD_H1,1); // Get the high of the previous bar
if (New_Bar==true && (OrderSelect(OrderTicket(),SELECT_BY_TICKET)==true))
{ // When a new bar is formed...
if (OrderType()==0) Modify_Buy(); // modifying the order with...
if (OrderType()==1) Modify_Sell(); // calculated ST and TP levels
return; // in two separate user-defined functions
}
return; // Exiting start()
}
I would be really grateful if you caould tell me a good code, to insert here so that the EA doesn't let more than one position to be opened. Thanks!
I would be really grateful if you caould tell me a good code, to insert here so that the EA doesn't let more than one position to be opened. Thanks!
if (Criteria_Buy()==true && OrdersTotal() == 0)
same for sell.
Yes, thanks! It seems the indicators are now appearing and it managed to open a position. Although there are two things not right:
1) When it opens and order "OrderSend(Symbol(),OP_SELL,Lots(),Bid,10,0,0);" the Lots() user-defined function always gets the minimum 0.01 lots. Here is the code of Lots():
double Lots ()
{
RefreshRates();
Balance=AccountBalance();
Lts=NormalizeDouble(MathRound((Balance*0.07)/1000),2);
if (Lts > 0.01)
return(Lts);
else Lts=0.01;
return(Lts);
}
Where am I wrong?
2) The last part of the code when it has to detect a new bar, get the low and high of the previous one and modify the position just opened with the calculated SL and TP in te corresponding Modify_Buy() and Modify_Sell() user-defined functions. This one doesn't seem to be exacuted and the EA just opens a posotion at the first signal it finds and then keeps it till the end of the testing. Any ideas?
Here's a money management function I use that calcs lot size.
Stick it in your includes folder & andadd:
#include <JA_Money_Management.mq4> >
at the top of your code.
This is the function:
double calcPositionSize(double amountToRisk, int pipRange, string currentSymbol, string myCurrency)
use it like:
double lotSize = calcPositionSize(AccountEquity() * .02, 120, "EURUSD", "USD", int item);
Here's a few good sites on that:
oops, supposed to be:
double lotSize = calcPositionSize(AccountEquity() * .02, 120, "EURUSD", "USD");
As far as the order modify issue, on thing I see off the bat is this line:
if (New_Bar==true && (OrderSelect(OrderTicket(),SELECT_BY_TICKET)==true))
you need to know the ticket number or order position 1st before using OrderSelect().
This is one way I do it:
int currentOrder = getCurrentOrder(); int getCurrentOrder() { int c; for(c=0; c<OrdersTotal(); c++){ OrderSelect(c,SELECT_BY_POS); if(Symbol() == OrderSymbol()) return(c); } return(-1); }
Hello everyone! I've just started writing my simple EA yesterday and I've been bumping into two problems over and over.
1) When I run the EA it doesn't finish with the testing and the completion bar just stands still until I stop the testing.
2) It completes the testing, but no positions are open, nor the called technical indicator values appear.
The journal also freezes at the "started for testing" message.
I am doing something wrong, but I can't seem to find it. Maybe there are some important stuff missing I don't know about. If anyone wants to help me (he just can't hold himself ) please PM me and we can talk.