Automatic Loss Recovery System (ALR) - read this first

Post your EA's using ALR here
Post Reply
kotsh
Trader
Posts: 31
Joined: Mon Sep 02, 2013 1:58 pm

Automatic Loss Recovery System (ALR)

Post by kotsh »

Hi all,

I also saw the videos of ALR and I believe it is a very valuable strategy that I actually thought of before and did all the math for it a few month back. But unfortunately I don't know how to write MQL, and doing the trades manually is a big risk if you make any errors or enter wrong orders, so I parked it aside.

But I am glad to see in this forum there is some like minded people, so lets work together on creating an EA for that, we could all even agree to donate something for whoever volunteers for it. Better than everyone of us paying 2k :)

The only difference between my idea and the ALR in the videos is that it is a good idea to have seperate TP levels on each side of the trade so you can set it to a logical level like support or resistance that is a multiple of ATR away. Price is more likely to hit these levels and thus it would reduce the number of opened legs to close all the trade at BE.

The TP levels are "virtual" meaning that they will only be used for calculations, rather than actually set on the trades.
The trade is closed based on net profit of all positions when it reaches a preconfigured value. (basket trade)

I will attempt to outline all the math and logic needed to code the EA based on some previous programming experience.

Below are the variables that would be an input for the EA
Untitled.png
A = original Position size
P0,1,2,3..n = ALR trades in direction of original trade
p = TP distance in pips in direction of original trade set at logical value
H0,1,2,3..n = ALR trades hedging the original trade
h = TP distance in pips in OPPOSITE direction of original trade set at logical value
Z = ALR zone which is where we would put SL
C = Target profit in $ (this would be set to a small value to offset swap and rounding)
S = Spread (EA could read current thread from chart)

The formula for first ALR position in hedge direction is calculated based on that when price reached the TP value of the hedge position, the NET profit would be "C" $
H0 (s+h) - A (z+h) = C ---> H0 = [ A(z+h) + C]/ [s+h]

Formula for second ALR trade in direction of original trade using same logic as above:
(P0+A) (p+s) - H0 (z+p+s) = C ---> P0 = [(H0 (z+p+s) +C )/ (p+s) ] - A

Using the same logic for subsequent ALR trades:

H1 = {[(P0+A)(z+h) + C]/[s+h]} - H0
P1 = {[(H1+H0)(z+h+s) + C]/[p+s]} - P0 -A

etc...

General formula is:
H(n) = {[(sum(existing P positions)(z+h) +C]/[s+h]} - sum(existing H positions)
P(n) = {[(sum(existing H positions)(z+h+s) + C]/[p+s]} - sum(existing P positions) - A

Using this formula the EA could calculate the ALR position sizes one by one referencing the previous position sizes.

If original position was BUY then H positions would be sell and P positions would be buy
If original position was SELL then H positions would be buy and P positions would be sell

When the price crosses on ALR boundary, the EA checks the total position size in each direction.
If the total position size in the opposite direction is greater than the total position size in the direction the price is moving, the next ALR trade is opened in the following order
H0, P0, H1, P1, H2, P2, H(n), P(n)

Exposure can be calculated by simply adding all P positions + original positions MINUS H positions

The EA then has all the values needed to get attached to the chart and start working.
We could also print out the projected position values as showed in the videos.

The EA keeps monitoring the net profit of the open trades on that pair and once it reaches the preset value C all positions are closed. Although the closing is based on net profit, the TP/SL levels could be inserted as a safety net in case the EA stops working or the chart is closed.

One thing to note is that one the EA is attached to the chart it should not be touched once the first ALR trade is opened, and no values should be changed as it will mess up all the previous calculations.

Please let me know what you think and if this is enough details too code an EA.

Cheers
You do not have the required permissions to view the files attached to this post.
Wib
Posts: 1
Joined: Mon Jun 23, 2014 6:15 pm

Automatic Loss Recovery System (ALR)

Post by Wib »

Wonderful work, Kotsh! I think this is an exciting avenue to pursue and I do hope there is someone who can carry this the next step with some coding. It would be absolutely great to have a functioning EA capable of doing what you've outlined so thoroughly. I, too, saw the video and would be delighted to have something costing less than $2000.
Dewey McG
Trader
Posts: 435
Joined: Sat Nov 26, 2011 4:20 pm
Location: Tampa FL

Automatic Loss Recovery System (ALR)

Post by Dewey McG »

Update in Post 1.

Just trying to make the ALR Calculator a little easier to use. To set your SL and TP just use the first tab labeled SL & TP. IT will automatically copy those values to the risk tabs where you can play with different SL and TP values, risk settings etc.
You do not have the required permissions to view the files attached to this post.
Dewey McG
Trader
Posts: 435
Joined: Sat Nov 26, 2011 4:20 pm
Location: Tampa FL

Automatic Loss Recovery System (ALR)

Post by Dewey McG »

Some great contributions.

Though I would love to be told I am wrong I suspect the coding for this is going to be very complicated. Here is what I suggest to start:

Make a trade manager where the user enters the ALR value (how many pips against you to start the ALR recovery) TP and lot sizes for each turn and the number of turns. From there we can all test it on our various strategies. Once we see it is working, the next stage would be to add the calculations to the EA so the user only has to enter ALR, TP, number of turns and risk, then the EA does all the calculations. If adding the calculations in now is easy enough great, but I would like to get a working model for everyone to play with and test with various strategies.

I like Kotsh's idea of using predefined logical values. In my own trading I look at the order boards to see where the big money is placing their orders (see example in PIC), but unless I am wrong, the calculations to make it work would get very complicated and I would suggest looking at that after we get the basic ALR trade manager working correctly.

Can any of our top coders here comment about how easy or difficult this is going to be?
You do not have the required permissions to view the files attached to this post.
User avatar
SpiderX
Trader
Posts: 554
Joined: Thu Aug 22, 2013 4:50 pm

Automatic Loss Recovery System (ALR)

Post by SpiderX »

Hi

The instruction given by kotsh are fairly straightforward enough...if it is coding exactly as per the instructions then it should be possible.
Maybe Dewey...can you verify that the instructions are exact and accurate ?

Cheers
"Love is patient, love is kind. It does not envy, it does not boast, it is not proud. It does not dishonor others, it is not self-seeking, it is not easily angered, it keeps no record of wrongs.Love does not delight in evil but rejoices with the truth. It always protects, always trusts, always hopes, always perseveres."
-Corinthians 13:4-8
Dewey McG
Trader
Posts: 435
Joined: Sat Nov 26, 2011 4:20 pm
Location: Tampa FL

Automatic Loss Recovery System (ALR)

Post by Dewey McG »

SpiderX » Mon Jun 23, 2014 8:45 pm wrote:Hi

The instruction given by kotsh are fairly straightforward enough...if it is coding exactly as per the instructions then it should be possible.
Maybe Dewey...can you verify that the instructions are exact and accurate ?

Cheers
I am dead tired so feel free to double check, but just doing a quick check using Kotsh's formula the lot size was too large.
You do not have the required permissions to view the files attached to this post.
madpipa
Trader
Posts: 62
Joined: Fri Oct 19, 2012 12:59 am
Location: Gold Coast, Australia

Automatic Loss Recovery System (ALR)

Post by madpipa »

Hi Dewey,

The formula I am using seems fairly simple & gave the same lot sizes as Dustin's video:
for Buy = (((TakeProfit + StopLoss) * SellOrdersLots / TakeProfit) - BuyOrdersLots)+0.00499
for Sell = (((TakeProfit + StopLoss) * BuyOrdersLots / TakeProfit) - SellOrdersLots)+0.00499

The "0.00499" at the end is simply to round the lot size up, so can be disregarded from the formula. I am ignoring spread as it is fairly insignificant in the overall picture. And if we aim at a minimum $ profit for the set of trades it would be accounted for at this time.

In a spreadsheet I used this basic formula:
=CEILING((B$3+B$1)*B5/B$3,0.01)

The CEILING is again to round the lot size up. Otherwise it is the same formula as the EA uses.

Cheers,
Mick
kotsh
Trader
Posts: 31
Joined: Mon Sep 02, 2013 1:58 pm

Automatic Loss Recovery System (ALR)

Post by kotsh »

madpipa wrote:The formula I am using seems fairly simple & gave the same lot sizes as Dustin's video:
for Buy = (((TakeProfit + StopLoss) * SellOrdersLots / TakeProfit) - BuyOrdersLots)+0.00499
for Sell = (((TakeProfit + StopLoss) * BuyOrdersLots / TakeProfit) - SellOrdersLots)+0.00499
Hi madpipa,
You formula uses the same logic like the formula I posted, except that I am taking spread into consideration and take into factor that we can get a small profit.
Also I took into consideration that we will use different TP levels on both sides of the ALR zone, which IMO is very important to avoid getting chopped.
Last edited by kotsh on Tue Jun 24, 2014 8:28 am, edited 1 time in total.
kotsh
Trader
Posts: 31
Joined: Mon Sep 02, 2013 1:58 pm

Automatic Loss Recovery System (ALR)

Post by kotsh »

ALR_kotsh.xlsx
Dewey McG » Tue Jun 24, 2014 11:24 am wrote:
SpiderX » Mon Jun 23, 2014 8:45 pm wrote:Hi

The instruction given by kotsh are fairly straightforward enough...if it is coding exactly as per the instructions then it should be possible.
Maybe Dewey...can you verify that the instructions are exact and accurate ?

Cheers
I am dead tired so feel free to double check, but just doing a quick check using Kotsh's formula the lot size was too large.
Hi Dewey,

I am not sure what went wrong, but when I use the formulas the numbers I get are reasonable.

I have attached here the excel sheet I used to do the calculations.
ALR_kotsh.xlsx
Only change the field highlighted in the EA Input section and the rest will calculate.
You do not have the required permissions to view the files attached to this post.
kotsh
Trader
Posts: 31
Joined: Mon Sep 02, 2013 1:58 pm

Automatic Loss Recovery System (ALR)

Post by kotsh »

All, thanks for the feedback.

I took some more time to think of the logic on how the EA works.

First please download the excel sheet in my previous post (ALR_kotsh.xlsx)
ALR_kotsh.xlsx
The EA Input section in the sheet would obviously be the input parameters when the EA is attached to the chart

The EA initialization section in the sheet would be the initial calculations that the EA performs and stores the values in 2 arrays, one is the H (hedge) array, and the other the P (position) array.

Note: The formulas I posted in my first post had a typo in it, the correct version was used in the excel sheet
H(n) = {[(sum_of_P(n-1)+A)(z+h) +C]/[s+h]} - sum_of_H(n-1)
P(n) = {[(sum_of_H(n)(z+h+s) + C]/[p+s]} - [sum_of_P(n-1) + A]

The arrays are then merged into a T (trade) array to follow the trade opening sequence as shown in the right table and the position sizes can be optionally displayed on screen.

The ALR trade counter (ALR_count) starts at 0 (meaning no ALR trades open yet) and increments with each new ALR trade opened.

Now lets assume that the ALR zone boundary levels are named as follows:
- Zp = original trade entry level
- Zh = original trade SL level

Case1:
When price crosses the Zh level, "ALR_count" is checked:
- if it is an even number then it means we need to open a position with the size in the T array at the ALR_count position.
- If it is an odd number then do nothing, since we have our hedge trade already open
Example:
we opened a trade, it went against us, and it crossed "Zh" level (original SL); ALR_count is 0
EA now checks and sees that ALR_count is 0 (even) it means it needs to grab the position size from Tarray[0] and open a position in the OPPOSITE direction of the original trade
Once the trade is successfully opened, ALR_count is incremented by 1

Case2:
When price crosses the Zp level, "ALR_count" is checked:
- if it is an odd number then it means we need to open a position with the size in the T array at the ALR_count position.
- If it is an even number then do nothing
To continue previous Example:
Lets say the trade is going back to original opening level and just crossed "Zp" level (original trade open level); ALR count now is 1
EA now checks and sees that ALR_count is 1 (odd) it means it needs to grab the value from T array [1] and open a position in the SAME direction of the original trade
Once the trade is successfully opened, ALR_count is incremented by 1

Continuously the EA monitors the net profit of the pair on the chart it is attached to (or optionally all open trades). Once the net profit reaches the predefined value in the EA input, all trades are closed.

As a safety net, when the EA opens the ALR positions, it sets the TP and SL to the proper values in case the EA stops working or internet connection is lost.

Of course there might be smarter ways to do it, but the above is pretty simple and can be easily coded. Unfortunately I don't know the syntax of MQL or I would have had done it myself. So who is volunteering ? :)

Please let me know what you think about this approach and if you see any bugs in it.

Cheers!
You do not have the required permissions to view the files attached to this post.
Post Reply

Return to “Automated Loss Recovery”