CODING A BOLLINGER BAND RE-ENTRY EXPERT ADVISOR STEP BY STEP CONTINUED:
Now here the first thing I am going to test is in case of buy orders:
I know that I am normally passing in an orderType buy stop. But, just in case it is an orderType buy, I have tested the orderType passed in. So, this argument using the modulus operator, the percent sign 2 (%2). So, an order type buy and an order type buy stop will both have the same value modulus 2...
if (orderType%2==ORDER_TYPE_BUY)
So, I just need to compare this to an ORDER_TYPE_BUY.
In that case, the price…the current market price is the ask price.
price = Ask;
And I am saying that if the current ask price is greater than or equal to the entry price, minus this stops level that we just calculated (i.e., if that price is too high to enter a buy stop order) then I am going to change the orderType to an ORDER_TYPE_BUY.
if (price>=(entryPrice-stopsLevel) )
So, I will be executing a market order, and I am going to change the entry price to the current market price.
{
entryPrice = price;
orderType = ORDER_TYPE_BUY;
}
And then I calculate the take profit price using the normalized double function again, and I am just taking the entry price, which may have been modified here, and adding the take profit value. And I am also taking the entry price and subtracting the stop loss value to get the stop loss price.
tpPrice = NormalizeDouble(entryPrice+tp, Digits());
slPrice = NormalizeDouble(entryPrice-sl, Digits());
So, that was the code for the buy type order.
Then I have an else statement and the code for the sell type order is the same. If the orderType %2 is ORDER_TYPE_SELL, then the price is the bid price.
If the price in this case is less than or equal to entry price plus the stops level, then again, I am going to execute a market order. So, I change the order type to a type sell, and my entry price becomes the market price.
And then, the take profit in this case is entry price minus take profit. And the stop loss price is entry price plus the stop loss.
So, these statementc here...
...and here...
...simply convert my call into a market order by changing the type to a buy or a sell type instead of a buy stop or sell stop; and changing the entry price to the current market price.
And then I just execute an order send, passing in the market symbol, the order type, the volume from my inputs, the entry price (which may have been modified here if executing a market order), using zero slippage, stop loss price, take profit price, the comment that I input, magic number that was input, and the expiration time that I calculated above.).
If this is a market order, then that expiration is going to be ignored. But, if it is a buy stop or a sell stop, then that order will be deleted if the expiry time is reached without the order being executed.
And then I just return that.
The orderSend will return a ticket number.
If it fails for some reason, then the order ticket number will be less than or equal to zero, so I just return that ticket number here.
(Remember that the open order function returns an integer...)
So next, I will need to go back up to the OnTick event, to the ORDER_TYPE_SELL_STOP and continue the description from there...
(Line #78)
Now here the first thing I am going to test is in case of buy orders:
I know that I am normally passing in an orderType buy stop. But, just in case it is an orderType buy, I have tested the orderType passed in. So, this argument using the modulus operator, the percent sign 2 (%2). So, an order type buy and an order type buy stop will both have the same value modulus 2...
if (orderType%2==ORDER_TYPE_BUY)
So, I just need to compare this to an ORDER_TYPE_BUY.
In that case, the price…the current market price is the ask price.
price = Ask;
And I am saying that if the current ask price is greater than or equal to the entry price, minus this stops level that we just calculated (i.e., if that price is too high to enter a buy stop order) then I am going to change the orderType to an ORDER_TYPE_BUY.
if (price>=(entryPrice-stopsLevel) )
So, I will be executing a market order, and I am going to change the entry price to the current market price.
{
entryPrice = price;
orderType = ORDER_TYPE_BUY;
}
And then I calculate the take profit price using the normalized double function again, and I am just taking the entry price, which may have been modified here, and adding the take profit value. And I am also taking the entry price and subtracting the stop loss value to get the stop loss price.
tpPrice = NormalizeDouble(entryPrice+tp, Digits());
slPrice = NormalizeDouble(entryPrice-sl, Digits());
So, that was the code for the buy type order.
Then I have an else statement and the code for the sell type order is the same. If the orderType %2 is ORDER_TYPE_SELL, then the price is the bid price.
If the price in this case is less than or equal to entry price plus the stops level, then again, I am going to execute a market order. So, I change the order type to a type sell, and my entry price becomes the market price.
And then, the take profit in this case is entry price minus take profit. And the stop loss price is entry price plus the stop loss.
So, these statementc here...
...and here...
...simply convert my call into a market order by changing the type to a buy or a sell type instead of a buy stop or sell stop; and changing the entry price to the current market price.
And then I just execute an order send, passing in the market symbol, the order type, the volume from my inputs, the entry price (which may have been modified here if executing a market order), using zero slippage, stop loss price, take profit price, the comment that I input, magic number that was input, and the expiration time that I calculated above.).
If this is a market order, then that expiration is going to be ignored. But, if it is a buy stop or a sell stop, then that order will be deleted if the expiry time is reached without the order being executed.
And then I just return that.
The orderSend will return a ticket number.
If it fails for some reason, then the order ticket number will be less than or equal to zero, so I just return that ticket number here.
(Remember that the open order function returns an integer...)
So next, I will need to go back up to the OnTick event, to the ORDER_TYPE_SELL_STOP and continue the description from there...
(Line #78)