Needed help coding a trailing takeprofit

 

Hi

My wish is that I am able to accurately set a take-profit to breakeven when there can be any number of regular market orders and activated limit orders.

But this was not going very good on freelancer as this was guessed to be some kind of averaging system that actually made the target prices alot bigger.

So, my goal is to find out if this would be possible and by chance somebody knows how to do it - <Deleted>

But thanks anyway for your time, any suggestions or comments.

 
Brian Lillard:

Hi

My wish is that I am able to accurately set a take-profit to breakeven when there can be any number of regular market orders and activated limit orders.

But this was not going very good on freelancer as this was guessed to be some kind of averaging system that actually made the target prices alot bigger.

So, my goal is to find out if this would be possible and by chance somebody knows how to do it - I would be interested to hire for a freelancer project.

But thanks anyway for your time, any suggestions or comments.

I guess you mean StopLoss to break even right ? (when your open trade is in profit, move SL to ~Open Price)
I think by "regular market orders and activated limit orders" you mean positions. (using right names helps you (re)write your job requests much better/understandable)

 

Example: only activated buy limit orders, expiry in end of bar by only a couple seconds, close @ TP for breakeven.

Stoploss? Correct me if I'm wrong, because, I think takeprofit and stoploss are no different; and work the same.

 
Brian Lillard:

Example: only activated limit orders; expiry in end of bar by only a couple seconds.

Stoploss? Correct me if I'm wrong, because: I think takeprofit and stoploss are no different and work the same.

yes, based on profit/loss viewpoint, SL and TP have similarities. it's possible for an SL to land in profit, and for a TP to close position in loss.

anyway, Are you trying to close a few open positions together when the overall profit is at breakeven level (or any other profit you want) ?
if so, and based on your image, that actually is an averaging procedure. you increase volume of long positions in downtrend, until the market reverses to an uptrend.
often, averaging is not profitable/safe/done right for most traders.
 

Say for example, if there were a mix of buy and sell orders, worst case scenario, some orders were activated limit orders and others market orders: suppose you just add up commission/swap/any other fees and current profits & losses and incremented them by points in a loop to ultimately place the exit/target price to close when the price was at a breakeven point. For as far as a formula I haven't a clue what would have the kind of accuracy counting point by point -- and I think it may be that the breakeven will be overshot to a profit versus a loss, anyways.

Code2219 or probably 2319:

if so, and based on your image, that actually is an averaging procedure. you increase volume of long positions in downtrend, until the market reverses to an uptrend.
often, averaging is not profitable/safe/done right for most traders.

I wrote a code that logs the breakeven price when it occurred, and it closes all orders when the target was within the target plus a slippage point value of the exit/target price, but this is not good enough for me - as possibly having many orders to fill takes time and costs money which is why I am opting for a broker side exit.

 
Brian Lillard:

An activated limit order has the spread assumed by the opposite market price as opposed to regular market orders and market orders are placed after spread points but unlike the example.

If there were a mix of buy and sell orders, worst case scenario, and some were activated limit orders and others market orders then suppose you just added up things up by points to basically place the exit/target price to the side of price that mattered. For as far as a formula I haven't a clue that would have the kind of accuracy counting point by point.

sorry I'm not sure if I've understood you (actually i'm sure I haven't :) ), but these are things that help you get that answer :

you can calculate profit/loss of an order, (a hypothetical order), for any Open and Close price, any volume. OrderCalcPrifit()...
you can also get the current impact of any price move, 1point or bigger: SymbolInfoDouble(SYMBOL_TRADE_TICK_VALUE)...

using the first function, you can calculate the point value at  another price (not current market price)

using these methods, you can estimate, exactly what profit/loss any of your orders will have at a certain price
so you'll be able to find he price, that will lead to breakeven, and set your pending order on that price
 

Brian Lillard: My wish is that I am able to accurately set a take-profit to breakeven when there can be any number of regular market orders and activated limit orders.

But this was not going very good on freelancer as this was guessed to be some kind of averaging system that actually made the target prices alot bigger.

So, my goal is to find out if this would be possible and by chance somebody knows how to do it - I would be interested to hire for a freelancer project.

But thanks anyway for your time, any suggestions or comments.

In order to calculate the Net result of all the orders in the basket and then calculate where the break-even price should be you can use the following formulas (that I posted in another forum thread) ...

Also, check your PM (Private Messages) for more details.

Forum on trading, automated trading systems and testing trading strategies

Any great idea about HEDGING positions welcome here

Fernando Carreiro, 2018.09.17 21:17

For those of you that want to learn to see the "light" and not fall into the trap of "hedging" and Grid strategies, here is the basic math:

How to Calculate the Net Resulting Equivalent Order:


Learn to do your research properly! Do not be blinded by false promises. Do the math!

 
Fernando Carreiro:

In order to calculate the Net result of all the orders in the basket and then calculate where the break-even price should be you can use the following formulas (that I posted in another forum thread) ...

Also, check your PM (Private Messages) for more details.

The OP talked about buys AND sells orders, it this case your formula is incorrect. Mixing buys and sells complicate things and you need to take into account the spread.

The OP also mentioned the swap and commission (but this is independent of above point).

EDIT : Looking at the formula more closely after Fernando below comment shows that the formula is right. It could just be more simple.

 
Brian Lillard:

Say for example, if there were a mix of buy and sell orders, worst case scenario, some orders were activated limit orders and others market orders: suppose you just add up commission/swap/any other fees and current profits & losses and incremented them by points in a loop to ultimately place the exit/target price to close when the price was at a breakeven point. For as far as a formula I haven't a clue what would have the kind of accuracy counting point by point -- and I think it may be that the breakeven will be overshot to a profit versus a loss, anyways.

I wrote a code that logs the breakeven price when it occurred, and it closes all orders when the target was within the target plus a slippage point value of the exit/target price, but this is not good enough for me - as possibly having many orders to fill takes time and costs money which is why I am opting for a broker side exit.

If you want precision (exact breakeven) and broker side exit, it's almost impossible if you mix buys and sells orders. As it will depends of the spread and the spread can change on each tick, in theory it' s possible to change SL/TP for several orders on each tick, but in practice it will not work. You will need some compromise.

A remark about you TP at breakeven, that doesn't make much sense, the stoploss should be used in your case, unless I misunderstood something in your requirements.

 
Alain Verleyen:

The OP talked about buys AND sells orders, it this case your formula is incorrect. Mixing buys and sells complicate things and you need to take into account the spread.

The OP also mentioned the swap and commission (but this is independent of above point).

The formula can be used for that case as well and compensate for the spread as well, you just have to know how to apply it. Obviously I did not describe all of the steps for the formula and just provided the general formula.

I had to put in the effort and hard work of researching it, so obviously I am not about to spoon feed the users with all the details. I just point the users in the right direction and let them put in some effort.

If they are not willing to put in the effort and research it further, then they know how to contact me!

Commissions can also be included in the same formula, by converting the cost into a pip-lot equivalent, as I described on another thread: https://www.mql5.com/en/forum/166621#comment_3995363

Swaps however, cannot be directly calculated via the formula because it depends on the duration of each order, but that can be approximated using another formula, which I have not included in my post.

Question about Tester Commisions and Swaps
Question about Tester Commisions and Swaps
  • 2017.01.12
  • www.mql5.com
Another proof why an EA has most of times completely different results on live...
 
Fernando Carreiro:

The formula can be used for that case as well and compensate for the spread as well, you just have to know how to apply it. Obviously I did not describe all of the steps for the formula and just provided the general formula.

I had to put in the effort and hard work of researching it, so obviously I am not about to spoon feed the users with all the details. I just point the users in the right direction and let them put in some effort.

If they are not willing to put in the effort and research it further, then they know how to contact me!

Commissions can also be included in the same formula, by converting the cost into a pip-lot equivalent, as I described on another thread: https://www.mql5.com/en/forum/166621#comment_3995363

Swaps however, cannot be directly calculated via the formula because it depends on the duration of each order, but that can be approximated using another formula, which I have not included in my post.

You are right, your formula is correct, I was misled by your notation as I have the spread appearing directly in mine (I edited my previous post).

Of course I will not provide the formula neither, I totally agree with you on this point.

Reason: