-
void OnTick()
It's a EA not a Script.
-
if(LastBars != Bars) LastBars = Bars; else return;
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
MT4: New candle - MQL4 programming forum #3 (2014)
MT5: Accessing variables - MQL4 programming forum #3 (2022)I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
Running EA once at the start of each bar - MQL4 programming forum (2011) -
MACurrent=iMA(NULL,30,MovingPeriod,MovingShift,MODE_EMA,PRICE_CLOSE,0); MAPrevious=iMA(NULL,30,MovingPeriod,MovingShift,MODE_EMA,PRICE_CLOSE,1);
You shouldn't be looking at bar zero.
- JamesFleming: but then doesn't appear to do anything, no orders are ever placed.
void BuyDetermination()
Perhaps you should call your function somewhere.
-
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"Long Buy",0,0,Green);
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).
-
-
Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.
-
You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce, the stop goes below the support.
-
AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP but it takes account of the exchange rates of the pair vs. your account currency.)
-
Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
Lot value calculation off by a factor of 100 - MQL5 programming forum (2019) -
You must normalize lots properly and check against min and max.
-
You must also check FreeMargin to avoid stop out
-
For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)
Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.
-
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello! I'm a newbie to MQL4 and Meta Trader. I've programmed in C++ many moons ago, so I'm hoping I've got the bulk of my code correct. I've not yet written all the script, doing it in stages, I've currently written most of the "buy point" determination and order sending, I have not yet programmed anything to close an order. The script compiles with no issues. I have tried running both the strategy tester and real data debugger, the script appears to initialise okay, but then doesn't appear to do anything, no orders are ever placed. I'm hoping I have missed something simple.
My script is:
Any help is hugely appreciated!
Many thanks,
James