18 July, 2009

An efficient way to declare SL and TP

Previously we have discussed about how to put down a stop loss in the EA. Now, it is time to further improve it. Originally, the EA came with fraction variable declared on its Take Profit column. The reason for using "double" in the code is to hold the value of market price, such as bid on EURUSD as 1.4002. Previous code explanation as below:

Ask+TakeProfit*Point = Actual take profit
1.4002 + (50pips * 0.0001) = 1.4052

And therefore the EA will put down 1.4052 as our take profit. Here is the problem. What if we revalue the parameter of Take Profit as ZERO? Further elaboration as below:

Ask+TakeProfit*Point = Actual take profit
1.4002 + (ZERO * 0.0001) = 1.4002

Now, I doubted what is the purpose for you to trade something stupid like opening and close trade at breakeven? I even doubted whether your broker is going to allow you to do so. Most broker has the minimum 5pips limit between their order open price and their take profit/stop loss... hmmm... here is what I did. Change it from "double" to "int", add new variable as SL and TP to accommodate the actual value of the price we want to take profit and stop loss.



Screenshot above is showing how I declare my new variable. I've added "//" to remark the old codes.


2nd screenshot. I've removed the TakeProfit checking routine. We will check the take profit value later.



Here is the screenshot I added the new set of codes to allow 0 stop loss and 0 take profit to the system. The logic as below:

StopLoss=50
if StopLoss>0
Ask-StopLoss*Point = SL
1.4002 - (50pips * 0.0001) = 1.3952
Otherwise
SL = 0

Now we have a real 0 Stop Loss.



Here is the set of code I added to sell position.