A Strange Hedge Strategy Problems

 
I am trying to develop a strategy that works on a tick basis. I thought I would code this strategy very easily at the beginning, but it started to get more complicated as I progressed. I would like you to help me find a solution to my three different problems or share your ideas.

This hedge strategy basically performs both buying and selling at the same price at the same time when the market price increases/decreases by a certain amount (pip or percent). Sometimes in the same lot amount and sometimes in different amounts, depending on the market situation.

1. If we try to open a positions with limit orders, I realized that since there is a concept called spread in the market, there is a risk that only one of the orders will be executed and the other will not be executed. For example;

- current price = ask,bid or mid price of the symbol.

- limit order price = current price (+) change value (pip/percent).

- Place BuyStop(BuyLotSize, limit order price) and SellLimit(SellLotSize, limit order price) orders.

- limit order price = current price (-) change value (pip/percent).

- Place SellStop(SellLotSize, limit order price) and BuyLimit(BuyLotSize, limit order price) orders.


When using limit orders, is there a way to eliminate the risk of one order being executed and the other not being executed? The solutions that come to my mind are;

- If there is one, find a broker that only works with commission and zero spread.
- Calculate the average spread and write the price value of the BuyStop order as the price of the SellLimit order + spread. The risk continues but decreases a little.

2. Then I made the strategy work with market orders, which caused my next problem. When I started working with market orders, I asked myself what about partial order execution. In fact, this is a problem that can also be experienced when writing this strategy with limit orders. However, for some reason it dawned on me at this point. I tried to find an example but failed. Can you write a simple sample code? For example, I need to guarantee that a market buy order of 10 lots is fully executed. If necessary, the strategy should wait for this at that time. I thought of entering a loop in a while block until the entire amount is realized. Do you have a more effective solution suggestion?

- current price = ask/bid or mid price of the symbol.

- order price = current price (+) change value (pip/percent).

- If the price is greater than the order price, enter the loop and wait for all orders to be executed; place Buy(BuyLotSize) and Sell(SellLotSize) market orders.

- order price = current price (-) change value (pip/percent).

- If the price is greater than the order price, enter the loop and wait for all orders to be executed; place Buy(BuyLotSize) and Sell(SellLotSize) market orders.


3. As I said at the beginning, this tick-based strategy works as expected most of the time when tested on a demo account, but occasionally it buys and/or sells too much. What do you think could be the reason? The only thing that comes to my mind is that after the tick that triggers the buy/sell orders, before the OnTick method is fully processed, many other ticks trigger the OnTick method and the market buy and/or sell orders are executed repeatedly. My solution is below but if you have more effective one, please share it.

- Define a global variable called lock

- At the beginning of the OnTick method, check the lock variable, if it is true return

- Otherwise set lock to true

- current price = ask/bid or mid price of the symbol.

- order price = current price (+) change value (pip/percent).

- If the price is greater than the order price, enter the loop and wait for all orders to be executed; place Buy(BuyLotSize) and Sell(SellLotSize) market orders.

- order price = current price (-) change value (pip/percent).

- If the price is greater than the order price, enter the loop and wait for all orders to be executed; place Buy(BuyLotSize) and Sell(SellLotSize) market orders.

- Just before exiting the OnTick method, set lock method to false


I tried to explain the strategy and problems as simply as I could. If there is anything unclear, please let me know and I will explain in more detail. I am waiting for your help and ideas.


Best Regards

 

You are asking too many questions in one sitting and expecting someone to explain it all to you in a few posts what took them years to learn and master. They could write an entire book just to explain everything you have asked.

Your questions also shows some lack of the basics of trading concepts. For example, with the following of your lines of text ...

current price = ask/bid or mid price of the symbol.

... There is no "mid" price on MetaTrader. There is a Ask price and a Bid price (and for some symbols, also a Last price).

  • A "buy" position opens at the Ask price and closes at the Bid price.
  • A "sell" position opens at the Bid price and closes at the Ask price.

Charts only shows the bars/candles based on Bid prices (Forex, CFDs, etc.) or Last prices (Futures, etc.).

As for the rest, if you need help with your code, then you will need to show your code, and ask for help with specific things not a generalised list of multiple questions. Be sensible!

 
aytacasan: This hedge strategy basically performs both buying and selling at the same price at the same time when the market price increases/decreases by a certain amount (pip or percent). Sometimes in the same lot amount and sometimes in different amounts, depending on the market situation.

Even at "zero" spread, that is a total waste of your money and destined to ruin your account. Learn the maths of Trading ...

Either go long (buy) or go short (sell) depending on the situation. Doing both on the same symbol, is the same as going nowhere except to pay twice the trading fees (spread, commission, etc.) and getting nothing else out of it.

PS! Your "ideal" strategy is simply not feasible in practice, because there are all manner of broker fees, be they the spread, commission, or others. 


 
Fernando Carreiro #:

You are asking too many questions in one sitting and expecting someone to explain it all to you in a few posts what took them years to learn and master. They could write an entire book just to explain everything you have asked.

Your questions also shows some lack of the basics of trading concepts. For example, with the following of your lines of text ...

... There is no "mid" price on MetaTrader. There is a Ask price and a Bid price (and for some symbols, also a Last price).

  • A "buy" position opens at the Ask price and closes at the Bid price.
  • A "sell" position opens at the Bid price and closes at the Ask price.

Charts only shows the bars/candles based on Bid prices (Forex, CFDs, etc.) or Last prices (Futures, etc.).

As for the rest, if you need help with your code, then you will need to show your code, and ask for help with specific things not a generalised list of multiple questions. Be sensible!

Hi Fernando,

First of all, thank you for your response to my post.

Since the multiple questions I asked here are related, I wanted to share them in the same post. However, if it is against the written or unwritten rules of this forum, I will do my best not to repeat it again. If you want, I can open a separate post for each question.

I cannot agree with my lack of knowledge about basic trading concepts. Since the post would already be long enough, I avoided giving some details. What I meant by mid price was (ask + bid / 2). Yes, I know that this price has no value. I also know that no transaction takes place in the market at this price. However, I am currently trying to develop a rough version of the strategy that will only work on tester and demo accounts. Of course, this will not provide the desired solution on a real account.

However, it is also obvious that I have a lack of knowledge about MetaTrader and MQL. The last time I developed an indicator or strategy with the MQL language was 15-20 years ago. All of them worked with the closing of a bar in a certain period, they were not based on ticks. I am trying to refresh my knowledge as quickly as possible.

As for code sharing, since I developed the strategy for someone else, I cannot reveal his personal ideas here, but I can of course share code snippets related to the question I asked.

You said, "Be reasonable!" Since English is not my native language, I perceived it as an insult when I first read it, but I want to think that your intentions are not bad and I perceive it that way.

 
Fernando Carreiro #:

Even at "zero" spread, that is a total waste of your money and destined to ruin your account. Learn the maths of Trading ...

Either go long (buy) or go short (sell) depending on the situation. Doing both on the same symbol, is the same as going nowhere except to pay twice the trading fees (spread, commission, etc.) and getting nothing else out of it.

PS! Your "ideal" strategy is simply not feasible in practice, because there are all manner of broker fees, be they the spread, commission, or others. 


Thank you very much for sharing your valuable information. I will take it into consideration, but this strategy is just a strategy that I wrote for someone else and that will help me refresh my MQL language knowledge. If we end up with something that has no value and no practical feasibility, I had to start somewhere. As soon as possible, I will split the three separate questions I asked in this post into three separate posts with code snippets.
 
aytacasan #: Since the multiple questions I asked here are related, I wanted to share them in the same post. However, if it is against the written or unwritten rules of this forum, I will do my best not to repeat it again. If you want, I can open a separate post for each question.

There are no rules against it at all. It is just that the forum is more for simpler Q&A that can be handled in a simple fashion. Having a very complex multipoint query will be difficult to address and answer satisfactorily.

It is best to narrow things down to specific points that you are having difficult with, and address them progressively as you resolve them.

aytacasan #: I cannot agree with my lack of knowledge about basic trading concepts. Since the post would already be long enough, I avoided giving some details. What I meant by mid price was (ask + bid / 2). Yes, I know that this price has no value. I also know that no transaction takes place in the market at this price. However, I am currently trying to develop a rough version of the strategy that will only work on tester and demo accounts. Of course, this will not provide the desired solution on a real account.

The Strategy Tester simulates the real-world environment of ask/bid prices. So, even if you use (ask + bid / 2) for calculations, positions will still respect the normal spread rules. And even if you set the spread to zero, you still have commission to content with. You may initially just want to use it in the Strategy Tester, but the whole point of creating a strategy, is because you want to have it work on a real-world live account at some point.

aytacasan #: As for code sharing, since I developed the strategy for someone else, I cannot reveal his personal ideas here, but I can of course share code snippets related to the question I asked.

Countless traders have searched for the "holy-grail" by using same-symbol hedging, martingale and grid solutions. They all fail eventually. They are not feasible in the real world.

If your "customer" still wants to pursue this irrespective of the advice against it, then it is his and your prerogative. So, I will not say any more about it.

aytacasan #: ou said, "Be reasonable!" Since English is not my native language, I perceived it as an insult when I first read it, but I want to think that your intentions are not bad and I perceive it that way.

By "reasonable", I mean for you to break your query down into smaller parts and address each one step-by-step as you progress with your own research and experimentation as you code it.

For each point, you should try to research and implement it yourself, and then you come here when you have reached a wall or need some clarification, showing your code, your efforts, your testing results with log output of the debugging process. Then once solved, you move on to the next phase.

 
Fernando Carreiro #:

There are no rules against it at all. It is just that the forum is more for simpler Q&A that can be handled in a simple fashion. Having a very complex multipoint query will be difficult to address and answer satisfactorily.

It is best to narrow things down to specific points that you are having difficult with, and address them progressively as you resolve them.

The Strategy Tester simulates the real-world environment of ask/bid prices. So, even if you use (ask + bid / 2) for calculations, positions will still respect the normal spread rules. And even if you set the spread to zero, you still have commission to content with. You may initially just want to use it in the Strategy Tester, but the whole point of creating a strategy, is because you want to have it work on a real-world live account at some point.

Countless traders have searched for the "holy-grail" by using same-symbol hedging, martingale and grid solutions. They all fail eventually. They are not feasible in the real world.

If your "customer" still wants to pursue this irrespective of the advice against it, then it is his and your prerogative. So, I will not say any more about it.

By "reasonable", I mean for you to break your query down into smaller parts and address each one step-by-step as you progress with your own research and experimentation as you code it.

For each point, you should try to research and implement it yourself, and then you come here when you have reached a wall or need some clarification, showing your code, your efforts, your testing results with log output of the debugging process. Then once solved, you move on to the next phase.

Thank you for your ideas, opinions and advice. I appreciate your efforts to keep the forum organized.