-
int nolongs =0; ⋮ void OnTick() { ⋮ if(buyCondition && nolongs==0 ){
EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?
Use a OrderSelect / Position select loop on the first tick to recover, or persistent storage (GV+flush or files) of ticket numbers required.
-
double StopPriceB = Ask-(stopLoss*Point); double TakeProfitPriceB = Ask+(takeProfit*Point);
You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.
-
Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
-
Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 -
The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).
-
- Daniel Smith: I have a histogram and I only want to trade the signal on candle close, or next candles open. I tried to capture the previous histogram value, hoping that the EA would only place a buy order as soon as it switched but it did not work and provided weird behavior.
You never know when a candle closes, only when the next opens.
- Stop looking at the current candle, your change is candle one vs. two.
-
EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?
Use a OrderSelect / Position select loop on the first tick to recover, or persistent storage (GV+flush or files) of ticket numbers required.
-
You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.
-
Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
-
Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 -
The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).
-
-
You never know when a candle closes, only when the next opens.
- Stop looking at the current candle, your change is candle one vs. two.
Hi! Thank you for the advice.
For number 1, can I just put some logic to look for open orders and positions in the onInit function? Is this what magic numbers are for? And for number 4, does this mean my shifts in iCustom() should be 1 and 2, instead of 0 and 1?
I am still a bit confused on what you mean in number 2, seems I got bid and ask mixed up. Should be like this right?
double StopPriceB = Bid-(stopLoss*Point); double TakeProfitPriceB = Bid+(takeProfit*Point);
Daniel Smith #:
For number 1, can I just put some logic to look for open orders and positions in the onInit function? Is this what magic numbers are for?
And for number 4, does this mean my shifts in iCustom() should be 1 and 2, instead of 0 and 1?
I am still a bit confused on what you mean in number 2, seems I got bid and ask mixed up. Should be like this right?
- No. Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
- Yes.
- Yes, for the buy.
if(buyCondition && nolongs==0 ){ CloseSellPositions(); nolongs = 1; noshorts =0; waitD=0; ticket1=OrderSend(_Symbol,OP_BUY, lotSize, Ask,1000, StopPriceB, TakeProfitPriceB, "long",1,0,clrWhite); ticket2=OrderSend(_Symbol,OP_BUY, lotSize, Ask,1000, StopPriceB, 0, "long",1,0,clrWhite); } else if (sellCondition && noshorts ==0 ){ CloseBuyPositions(); noshorts =1; nolongs = 0; ticket1=OrderSend(_Symbol,OP_SELL, lotSize, Bid,1000, StopPriceS, TakeProfitPriceS, "short",1,0,clrRed); ticket2=OrderSend(_Symbol,OP_SELL, lotSize, Bid,1000, StopPriceS, 0, "short",1,0,clrRed); } OrderSelect(ticket1,SELECT_BY_TICKET); if((OrderClosePrice() !=0)&& OrdersTotal()==1) { OrderModify(ticket2,OrderOpenPrice(),OrderOpenPrice(),0,0,clrLime); } candletime=Time[0]; }
de now.
Probably because your broker's implementation of FIFO rules.
Since 2009, hedging is not permitted for US traders.
NFA Enforces FIFO Rule, Bans Forex Hedging in US Forex Accounts - Trading Heroes (2016)
FAQ: FIFO in the Forex Market - BabyPips.com (2011)
I suggest only opening one order, no TP. and do a partial close when reached.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi!
I have a histogram and I only want to trade the signal on candle close, or next candles open. I tried to capture the previous histogram value, hoping that the EA would only place a buy order as soon as it switched but it did not work and provided weird behavior.
I'm really lost here, attatched is a picture showing the behavior i want - only place a trade on the opening candle after the signal/histogram change