I would like to create a stoploss order that will be placed above the high of the previous order's initiation bar in case this is a Sell order OR below the low of the previous order's initiation bar in case this is a Buy order.
Here is a picture to illustrate the issue ( the example depicts a sell order case ):
Any idea how to do that? The code below works fine if I use stoploss that is fixed. If I replace the stoploss with variables that are based on High or Low no orders are fired.
Here is my code:
//| Expert initialization function |
//+------------------------------------------------------------------+
/* -----------------------------------------------------------------------------
KINDLY RESPECT THIS & DO NOT MODIFY THE EDITS AGAIN
MQL4 FORMAT IS NOT INDENTATION SENSITIVE,
HAS IDE-HIGHLIGHTING
AND
HAS NO OTHER RESTRICTIVE CONDITIONS ----------- THIS CODING-STYLE HELPS A LOT
FOR BOTH
EASY & FAST
TRACKING OF NON-SYNTACTIC ERRORS
AND
IMPROVES FAST ORIENTATION
IN ALGORITHM CONSTRUCTORS' MODs
DURING RAPID PROTOTYPING
IF YOU CANNOT RESIST,
SOLVE RATHER ANY OTHER PROBLEM,
THAT MAY HELP SOMEONE ELSE's POST, THX
------------------------------------------- KINDLY RESPECT
THE AIM OF StackOverflow
------------------------------------------- TO HELP OTHERS DEVELOP UNDERSTANDING,
THEIRS UNDERSTANDING, OK? */
extern int StartHour = 14;
extern int TakeProfit = 70;
extern int StopLoss = 40;
extern double Lots = 0.01;
extern int MA_period = 20;
extern int MA_period_1 = 45;
extern int RSI_period14 = 14;
extern int RSI_period12 = 12;
void OnTick() {
static bool IsFirstTick = true;
static int ticket = 0;
double R_MA = iMA( Symbol(), Period(), MA_period, 0, 0, 0, 1 );
double R_MA_Fast = iMA( Symbol(), Period(), MA_period_1, 0, 0, 0, 1 );
double R_RSI14 = iRSI( Symbol(), Period(), RSI_period14, 0, 0 );
double R_RSI12 = iRSI( Symbol(), Period(), RSI_period12, 0, 0 );
double HH = High[1];
double LL = Low[ 1];
if ( Hour() == StartHour ) {
if ( IsFirstTick == true ) {
IsFirstTick = false;
bool res1 = OrderSelect( ticket, SELECT_BY_TICKET );
if ( res1 == true ) {
if ( OrderCloseTime() == 0 ) {
bool res2 = OrderClose( ticket, Lots, OrderClosePrice(), 10 );
if ( res2 == false ) {
Alert( "Error closing order # ", ticket );
}
}
}
if ( High[1] < R_MA
&& R_RSI12 > R_RSI14
&& R_MA_Fast >= R_MA
){
ticket = OrderSend( Symbol(),
OP_BUY,
Lots,
Ask,
10,
Bid - LL * Point * 10,
Bid + TakeProfit * Point * 10,
"Set by SimpleSystem"
);
}
if ( ticket < 0 ) {
Alert( "Error Sending Order!" );
}
else {
if ( High[1] > R_MA
&& R_RSI12 > R_RSI14
&& R_MA_Fast <= R_MA
){
ticket = OrderSend( Symbol(),
OP_SELL,
Lots,
Bid,
10,
Ask + HH * Point * 10,
Ask - TakeProfit * Point * 10,
"Set by SimpleSystem"
);
}
if ( ticket < 0 ) {
Alert( "Error Sending Order!" );
}
}
}
}
else {
IsFirstTick = true;
}
}
Major issue
Once having assigned ( per each Market Event Quote Arrival )
Your instruction to
OP_SELL
shall be repaired:Symmetrically review and modify the
OP_BUY
case.For Broker T&C collisions ( these need not get reflected in backtest ) review:
or inspect in the
MT4.Terminal
in theMarketWatch
aMouseRightClick Symbols -> Properties for STOPLEVEL distance.Minor Issue
Review also your code for
OrderClose()
-- this will fail due to having wrong Price: