If function not working in case of numeric characters - textbox

So i have used the following code
If (txtbedroom + txtkitchen + txtbathroom + txtlivingroom + 0) > 9 Or (txtbedroom + txtkitchen + txtbathroom + txtlivingroom + 0) < 7 Then
If MsgBox("Please ensure the number of rooms you entered is entered correctly.", vbYesNo) = vbNo Then
Exit Sub
End If
End If
even if the result of the following is less than 9 and greater than 7 the if function still gets satisfied... what can be done??

Anyway I solved the problem. I used a variable and the code CInt() like x = CInt(txtbedroom) to convert the text to number and then it worked...

Related

How can I select only the last closed order(s) on Metatrader 4?

Right now I select all history trades using a loop whenever there is a new history trade (onTimer handler with 1 second timer period):
/*
3.) History Trades
*/
static int historyTradesTotal=0;
if(OrdersHistoryTotal()==historyTradesTotal) return;
historyTradesTotal = OrdersHistoryTotal();
int i,hstTotal = OrdersHistoryTotal();
string historical_trades = "";
for(i=hstTotal; i >= 0; i--)
{
//---- check selection result
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) continue;
historical_trades = historical_trades +
"historical_trades|" +
version + "|" +
DID + "|" +
IntegerToString(AccountNumber()) + "|" +
IntegerToString(OrderTicket()) + "|" +
TimeToString(OrderOpenTime(), TIME_DATE|TIME_SECONDS) + "|" +
TimeToString(OrderCloseTime(), TIME_DATE|TIME_SECONDS) + "|" +
IntegerToString(OrderType()) + "|" +
DoubleToString(OrderLots(),2) + "|" +
OrderSymbol() + "|" +
DoubleToString(OrderOpenPrice(),5) + "|" +
DoubleToString(OrderClosePrice(),5) + "|" +
DoubleToString(OrderStopLoss(),5) + "|" +
DoubleToString(OrderTakeProfit(),5) + "|" +
DoubleToString(OrderCommission(),2) + "|" +
DoubleToString(OrderSwap(),2) + "|" +
DoubleToString(OrderProfit(),2) + "|" +
"<" + OrderComment() + ">|";
}
}
So now how can I just select the orders that have been closed between the last onTimer event to avoid looping through all trades whenever a new one is closed all the time?
The solution should also consider the rare case if two or more trades are closed parallely within one second e.g..
int i = OrdersHistoryTotal();
int a = OrdersHistoryTotal() - 10;
int b = OrdersHistoryTotal();
for (i = a;i < b ;i++)
{
if (OrderSelect(i,SELECT_BY_POS, MODE_HISTORY) == true)
{
Alert("orders found"+ OrderTicket() + " " + OrderCloseTime());
}
}
This code works and it's starts from the 10th order from the most previous order in history but if you wanted to check just the previous order you could subsitute 10 for 1 in int a. if you want to check only the last 5 orders change 10 to 5. Simple.
That is not possible in MQL4. Orders are stored and returned by their ids, you cannot apply custom sort. so you have to loop over them all starting from the last to find the one you need. Alternative is to keep all the open orders in CArrayInt or array (add when a new trade is open, delete when found a closed one, read/write when doing init/deinit) and check the live_trades_array elements are still open. That might be much faster if your ea has just several orders and several different ea's on the platform.

How to filter traded symbols in Metatrader 4 / MQL4

I am searching for a solution to just and only consider real forex pairs in my loop. I don't want CFDs, commodities, silver, gold, etc. to be included because they have a complete different logic when it comes to calculating pips etc. etc. (I want to use the data for a FX dashboard).
How can I implement a filter to do so without building if-statements for every FX pair existing?
If possible, the solution should be independent of the broker used (since the offered FX pairs might be different from broker to broker, so a common approach would be the all-in solution).
This is my current code that doesn't seperate between fx and non fx:
/*
2.) Create a string format for each pending and running trade
*/
int live_orders = OrdersTotal();
string live_trades = "";
for(int i=live_orders; i >= 0; i--)
{
if(OrderSelect(i,SELECT_BY_POS)==false) continue;
live_trades = live_trades +
"live_trades|" +
version + "|" +
DID + "|" +
AccountNumber() + "|" +
IntegerToString(OrderTicket()) + "|" +
TimeToString(OrderOpenTime(), TIME_DATE|TIME_SECONDS) + "|" +
TimeToString(OrderCloseTime(), TIME_DATE|TIME_SECONDS) + "|" +
IntegerToString(OrderType()) + "|" +
DoubleToString(OrderLots(),2) + "|" +
OrderSymbol() + "|" +
DoubleToString(OrderOpenPrice(),5) + "|" +
DoubleToString(OrderClosePrice(),5) + "|" +
DoubleToString(OrderStopLoss(),5) + "|" +
DoubleToString(OrderTakeProfit(),5) + "|" +
DoubleToString(OrderCommission(),2) + "|" +
DoubleToString(OrderSwap(),2) + "|" +
DoubleToString(OrderProfit(),2) + "|" +
"<" + OrderComment() + ">|";
}
This is probably the easiest way. Prefix symbols might be a problem (e.g. mEURUSD) but easily solved by shifting the StringSubstr values by the prefix size. Suffix is not a problem as we take the first 6 chars of the symbol string.
const string FX_CURRENCIES[]={"EUR","GBP","USD","NZD","AUD","CHF","CAD","JPY"};//add other currencies if needed
bool isFxPair(const string symbol){
return StringLen(symbol)>=6 && getCurrencyIdx(StringSubStr(symbol,0,3))>=0 &&
getCurrencyIdx(StringSubStr(symbol,3,3))>=0;
}
int getCurrencyIdx(const string smb){
for(int i=ArraySize(FX_CURRENCIES)-1;i>=0;i--){
if(FX_CURRENCIES[i]==smb)
return i;
}
return -1;
}
Use of CStringArray and caching FX symbols might be another good idea that may potentially work faster, but it seems to use the similar logic as above(but you'll have to sort the cache every time you add something in order to have CStringArray collection to work fast).
There is no direct method to ask whether a symbol is FX, CFD, Stock, Crypto or anything else.

How to avoid Round-up from NormalizeDouble and OrderSend Pending Order (SELLLIMIT, BUYLIMIT) get Error -1 Invalid stops

Please advice me how to solve problem when OrderSend Pending Order ( BUYLIMIT, SELLLIMIT) got error -1 Invalid stops.
also tell me the rule of BUYLIMIT and SELLLIMIT.
the simple code like below:
`
double digit = MarketInfo(symbol,MODE_DIGITS);
POPRICE = NormalizeDouble(BBMVAL[0],digit);
TPPRICE = NormalizeDouble(POPRICE + (30*point),digit)
SLPRICE = NormalizeDouble(POPRICE - (30*point),digit)
ticket1=OrderSend(symbol,OP_BUYLIMIT,0.1, POPRICE,Slippage,SLPRICE,TPPRICE,BUYLIMIT,magic,(TimeCurrent()+(3600*2)),CLR_NONE);
Sleep(10);
while(IsTradeContextBusy()) Sleep(100);
RefreshRates();
if(ticket1 < 0)
{
SendMail
(
symbol+"-"+ IntegerToString(Period())+"-" + "FAILED-BUYLIMIT ",
symbol+"-"+ IntegerToString(Period())+"-" + "FAILED-BUYLIMIT "+"(#PO-Price:"+POPRICE+"#TP:"+TPPRICE++"#SL:"+SLPRICE+"#RespID:"+ticket1+"#Status: "+ErrorDescription(GetLastError())+")"
);
ticket1 = 0;
}
if (ticket1 > 0)
{
b_Status = 1;
SendMail
(
symbol+"-"+ IntegerToString(Period())+"-" + "SUCCESSED-BUYLIMIT ",
symbol+"-"+ IntegerToString(Period())+"-" + "SUCCESSED-BUYLIMIT "+"(#PO- Price:"+POPRICE+"#TP:"+TPPRICE+"#SL:"+SLPRICE+"#RespID:"+ticket1+"#Status: "+ErrorDescription(GetLastError())+")"
);
ticket1 = 0;
}
the objective is :
If the pair EURUSD 5-digit, How to make the SLPRICE, POPRICE, TPPRICE always with 5-digit. sometimes with 5 digit and sometime roundup with 4-digit. is it right to use NormalizeDouble ? how to avoid the roundup.
when got Failed ( tiket1 < 0 ), the error is -1 ( invalid stops), what is the real reason of this error. sometimes successed and sometime failed. what is the rules of selllimit and the buylimit.
and when got Failed, there are many emails send to email-address. how to avoid this problem too.
THank you very much.
It is perfectly okay to have a 4 digit price when 5 digit decimal is expected, but since double is a floating-point number, sometimes you don't get the exact price you might expect,
for example:
let's say you are calculating TP for a long trade, 1.10000 + 0.0020 = 1.10020(expected) but you might get something like this 1.10020001, in these cases the price level is rejected, causing errors. That is why price normalizing is important.
In some pairs, you might find that NormalizeDouble() sometimes gives false values, in those cases use the following function.
double NP(const double price)
{
double tickSize = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_SIZE);
return (MathRound(price / tickSize) * tickSize );
}
As for getting the many emails: you can limit the number of emails per period of time.
Here I made it so that you get 1 email per new candle:
if(ticket1 < 0)
{
static datetime prevTime = 0;
if(Time[0] > prevTime)
{
SendMail
(
symbol + "-" + IntegerToString(Period()) + "-" + "FAILED-BUYLIMIT ",
symbol + "-" + IntegerToString(Period()) + "-" + "FAILED-BUYLIMIT " + "(#PO-Price:" + POPRICE + "#TP:" + TPPRICE++"#SL:" + SLPRICE + "#RespID:" + ticket1 + "#Status: " + ErrorDescription(GetLastError()) + ")"
);
prevTime = Time[0];
}
ticket1 = 0;
}

Excel 2010 Macro does not find value

I have a macro that searches but does not find the “7” (If Right(pair, 2) = 7 Then). The thing is when I change the number to 11 or 12 etc. (any two digits) and the findXX in the code, it works fine. Does anyone know what’s occurring and what is the exact change I need to do.
Option Explicit
Sub DivideSomeStuff()
Dim pair As Range, accumulator As Range
Dim findSeven As Double
Dim remainder As Long
For Each pair In Range("B30, F30, J30")
If Right(pair, 2) = 7 Then
If pair.Offset(0, 2) <= 12 Then
remainder = 0
Else
remainder = pair.Offset(0, 2) Mod 10
End If
findSeven = (pair.Offset(0, 2) - remainder) / 10
For Each accumulator In Range("A36, D36, G36, J36, M36, A40, D40, G40, J40, M40")
If accumulator.Offset(-1, 0) = Val(Left(pair, InStr(pair, "-") - 1)) Then
accumulator.Value = accumulator.Value + remainder
End If
accumulator.Value = accumulator.Value + findSeven
Next accumulator
End If
Next pair
End Sub
Change it from ...
Right(pair, 2) = 7
... to ...
Right(pair, 1) = 7
You’re currently getting the 2 right values when 7 is a single character.
You may need to put quotes around the 7 too, see if works without them though.

finding the rth term of a sequence

the question is to give a possible formula for the rth term.
i'm able to solve two questions but rest i can't seems to be of a different way or like weird.as i'm studying alevels i think there's a common rule or maybe an easy way to solve sequence related problems.i never understood sequence well enough-it's just that hard for me.
6 18 54 162
i'm able to solve it by 2*3^r
4 7 12 19
by r^2+3
but
4 12 24 40 60
i'm trying so many ways but i can't find the answer.i think there's a common rule for solving all these not much marks are there so it should be solved in an easy way but i'm not getting how to.please help
Here's a formula in R for the sequence:
g <- function(n) 6*n + 2*n^2 + 4
g(0:4)
[1] 4 12 24 40 60
Here is one way to solve this relation. First, recognize that it is quadratic as the difference is an arithmetic sequence (linear).
Then note that g(x + 1) = g(x) + 8 + 4x. Represent g(x) = a*x^2 + b*x + c.
Then:
g(x+1) = a(x+1)^2 + b(x+1) + c = g(x) + 8 + 4x = a*x^2 + b*x + c + 8 * 4x
ax^2 + 2ax + a + b*x + b + c = a*x^2 + b*x + c + 8 + 4x
Thus
2ax + a +b = 8 + 4x
As this holds for all x, it must be that 2ax = 4x or a = 2. Thus
4x + 2 + b = 8 + 4x
So b = 6. With these known, c is determined by g(0) = c = 4.

Resources