Hedging trade
-
Hello!
I have a problem with strategy.
Let me give you an example.
A strategy open BUY position on a currency pair (for example: BUY 0.1 / BUY 0.2 / BUY 0.40 / BUY 0.50). The sum of these trades is 1.20 lots. The market goes bad and at some point I have a loss, for example, -500 USD.
When the loss is -500 USD I would like to open a SELL position of value such as trades are BUY. In this case, sum is 1.2 of BUY lots, but the others may be, for example, 2.00 lot (and loss of -500 USD).https://fxdreema.com/shared/dBB8SAD0e
I would like to know how the loss of -500 USD to open opposite trades the same value.
Or how to open 50 % and 150 % of trades. For example strategy opened 1,00 lot total, and I want open 50 % of total trades (0,5 lot) or 150 % (1,5 lot).
-
Here is a way to get the sum of lots: https://fxdreema.com/shared/RFQWrZdjd
I user 2 variables for that. Notice how I used them in both pink blocks. "Modify Variables" or "Custom MQL4 code" can be used as well, but they take some space. You will see OrderLots(), this is the native MQL4 function that gets the lot size of the currently loaded trade.
So, block 1 loads each trade one after another, goes to block 2 as many times as many trades there are, and then at the end it goes to it's yellow output. -
But still thought about such a way
https://fxdreema.com/shared/aUUS027Oc
But when I have a loss of -20 usd and positions BUY 0.01 / BUY 0.02 / 0.04 BUY EA opens SELL 4.00 lots. Too much. Why?
Can I try this way? Maybe another function "Trade Statistic"?
-
You can monitor the profit/loss if you want, these blue blocks are for that. If you want to monitor the global account profit/loss, better use Condition -> Account -> Profit.
Trade statistics are probably fine, but when you use them they make the EA slow (when backtest) because some function that tracks statistics on every tick is enabled. In MetaTrader everything is fine to be used in live, but when it comes for backtesting... there is no room for much fancy things here, unfortunately

Anyway, I think you have to read this: viewtopic.php?p=5868#p5868
It's interesting how most of the people expect the EA structure to be. Now I'm starting to wonder if this way can actually work
-
But my EA opens too mauch lots? Function "Longs count" does not count "Now opened long trades" .
It opens too much lots. Is some bugs or I do something wrong ?
-
Well, Longs count is the count of Buy trades - how many Buy trades are there - 1, 2, 3, 4... This is not Lot size

-
I dont use strategy on backtest. I test live on demo account.
I try in another way. Volume size (onTrade)
https://fxdreema.com/shared/I0DUGxjZ
but now when loss is -20 USD and opened trades is (for example) BUY 0.1/0.3/0.5 my EA opens SELL 0.5 (when BUY 0.5 is open on candle).
Is it possible to function "Volume size (lots)" add OrderLots () to include all the trades and not just the last?
Later I will create EA according to your way. But I thought there is another way.

-
Well, all the data from (on trade) is about that particular trade/order that caused that event. There is nowhere in blocks something to calculate group lots or group anything and give the result as a value. But the example I posted above does that... with 2 blocks and 2 variables

I was thinking before how to do something like this many times actually. One way is to add option in Condition to select group of trades, to filter them out and return... bunch of things. The other way is to have block like "Select group of trades" and then in Condition to have same options with, but not selecting and filtering trades there.
There is another issue - I prefer to keep projects with same blocks for MT4 and MT5, and functions like this are MT4 only.
The way I posted above (calculating everything in a loop) is the most universal and it's actually faster than what will be if this was inside Condition. Well, it requires some additional variables

By the way in this example you are missing something maybe important - to filter the event.
-
Hello
I have other EA which trades.
And I created a new "EA hedge" that protects my EA.https://fxdreema.com/shared/9yP7HBkyb
"EA hedge" works in the way that if the pair is to open a certain number of trades (for example, 3 SELL) and the price is above the last trade of 1 pip. EA opens a trade hedge (BUY).
But I have a question.
How do to my "EA hedge" opened only transactions with the specified Time Frame?
The first EA works on the M5 and I would like to "hedge EA" seen only trades with the M5. Not trades M15 - first EA works on M15 too.
How to do it?
-
The timeframe is not connected in any way with the trades and orders. Timeframe is only used for candles, indicators, stuff like that. What differentiates trades and orders is their Magic Number (well, also type ** and market [symbol]) and I can see you are using Group number, which is basically some form of Magic Number.
-
Hi !
I have another problem.
I have a strategy that opens trades. This Strategy is managed by the magic number of 1000. If the strategy of MagicNumber 1000 opens 7 trades BUY another strategy opens 1 trades SELL with Group 2 (or group 1 for BUY).
And now: 7 trades BUY (Magic Number 1000), 1 trades SELL (Group 2)- if SELL trades will be bigger than 1 USD of the last two trades BUY - closes the last two BUY and SELL.I created strategy but it does't work. Does not closes 2 last trades and 1 hedge trade. What is problem ?
Could you fix my strategy or show me example. -
Blocks 5 and 11 are for average profit, not sum of profits. The other problem is that "For each trade" is supposed to work with other pink blocks from it's group. Those blue blocks at the end already have the "For each trade" functionality inside, they don't care of "For each trade" block that is used above.
The biggest problem here is how to compare the profit of 1 trade to the sum of profits of 2 other trades in different group. This can be made with some pink blocks and the result will be - too many blocks used for something relatively simple (but too "custom"). It will be a little bit better for all the logic to be in a single custom block, or two, but if you don't get MQL this block will be a black box with no options to customize.
But I wonder why you want to wait for 7 trades (and 7 more for the opposite type) to open another 1 (or 2), then to wait for that last 1 to become profitable, and not only this, but profitable enough compared to the last 2 of the initial 7. Well, I don't know the strategy exactly, maybe there is some reasonable logic that I can't see.
Meanwhile, I have unreleased block that gets the most losable trade (imagine that there is a mix of opened trades), then searches in other profitable trades for sum of profit that is bigger than that loss (with X money) and closes those trades (the biggest loss and profits). If it's only 1 trade it can close it when the desired profit is reached. Does a block like this sound good?
-
Otherwise here are some examples of how to calculate sum of profits in multiple filtered trades: https://fxdreema.com/shared/78kNK7MQb
In the example on the left I reset the variable (SumofProfits) in block 1 before the block starts, see the additional panel for controlling variables. Then the profit of the current trade is added to the variable in block 2.
But I like when things takes less space, so in the right example everything happens in block 4... well... at least you should know what OrderProfit(), OrderCommission() and OrderSwap() means in MQL4.
V1 means the name of the variable. The real name can be used instead, but V1 will be renamed to what is the real name.When "For each trade" does it's job it goes to it's yellow output, so this is the exit after calculations where you can use that variable to compare it with something else.