I wrote this expert advisor for a 1H chart the only problem I have is once the veriable,
if(iStochastic(Symbol(),Period(),21,3,3, 0,1,0,0)>=uplimit21)
if(iStochastic(Symbol(),Period(),9,3,3, 0,1,0,0)<=downlimit9)
is hit and an order is opened and closed if the veriable is still there when the order is closed, profit is taken or stoploss reached, it opens another position.
Does any one know how to get it to hold for 10 bars untill the next order will be opened.
perhaps you could use sleep function, To Sleep, or Not to Sleep?
HTH
Keith
Save the Number of the current Bar when your signal is comming ...
int buybar=0; (somewhere on the bginning but NOT in the start-function)
if(iStochastic(Symbol(),Period(),21,3,3, 0,1,0,0)>=uplimit21)
if(iStochastic(Symbol(),Period(),9,3,3, 0,1,0,0)<=downlimit9)
buybar=Bars;
after you can compare buybar and how many bars before your signal was hitting
Save the Number of the current Bar when your signal is comming ...
int buybar=0; (somewhere on the bginning but NOT in the start-function)
if(iStochastic(Symbol(),Period(),21,3,3, 0,1,0,0)>=uplimit21)
if(iStochastic(Symbol(),Period(),9,3,3, 0,1,0,0)<=downlimit9)
buybar=Bars;
after you can compare buybar and how many bars before your signal was hitting
This will work in backtest, but not in real trading. Whenever EA is restarted, the value will be lost and EA may something it was not supposed to do.
You should keep you EA stateless (ie. use no global variables to hold some info between ticks).
EA should be able to figure out everything it did from current and historical trades.
Try calculating distance from order open time using function:
int getDistanceTF(datetime time, int timeframe) {
return ((iTime(NULL, timeframe, 0) - time) / (60 * timeframe));
}
This one doesn't now there is little bars on weekend.
Calculates the distance on any timeframe you want.
If you need something more accurate you could iterate on iTime() values and find the closest to OrderOpenTime().
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I wrote this expert advisor for a 1H chart the only problem I have is once the veriable,
if(iStochastic(Symbol(),Period(),21,3,3, 0,1,0,0)>=uplimit21)
if(iStochastic(Symbol(),Period(),9,3,3, 0,1,0,0)<=downlimit9)
is hit and an order is opened and closed if the veriable is still there when the order is closed, profit is taken or stoploss reached, it opens another position.
Does any one know how to get it to hold for 10 bars untill the next order will be opened.
extern string ________Order_______;
extern double Lots=0.01;
extern int SL=25; //stop loss
extern int TP=35; //take profit
extern string ________StochasticSell_______;
extern int uplimit21=90;
extern int downlimit9=75;
extern string ________StochasticBuy_______;
extern int Uplimit9=25;
extern int Downlimit21=10;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int start()
{
//----
if(iStochastic(Symbol(),Period(),21,3,3, 0,1,0,0)>=uplimit21)
if(iStochastic(Symbol(),Period(),9,3,3, 0,1,0,0)<=downlimit9)
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point,Ask+TP*Point);
if(iStochastic(Symbol(),Period(),21,3,3, 0,1,0,0)<=Downlimit21)
if(iStochastic(Symbol(),Period(),9,3,3, 0,1,0,0)>=Uplimit9)
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+SL*Point,Bid-TP*Point);
}