Page 1 of 1

How to automate partial close based on percentage profit target

Posted: Fri Aug 19, 2022 2:29 pm
by shayefx
Hi All,

I trade Forex using TradingView with Forex.com as my broker.

I've been searching the internet and even wrote to TradingView support but I can't seem to get a straight answer on what I wish to do:

For example, say I have 2000 units (2 micro lots) and I want to automatically close 1000 units (1 micro lot) at 40% of take profit target and then close the next 1000 units at 80% take profit target.

Is this possible to do in principle via Pine script in TradingView? I have never heard of Pine until I started Googling what I am trying to accomplish above

Thanks

-PS I'm versed in SQL/Python/VBA

I've found something here that looks promising:
https://www.tradingcode.net/tradingview ... -function/

This looks like something that could work. Either I could use it if price hits a certain alert or certain percentage profit target then sell x quantity divided by 2 for example.
I'm I way off or on the right track?

Code: Select all

// When prices drop below the 20-bar lowest low, submit a sell order
if low < ta.lowest(low, 20)[1]
	strategy.order("Sell (Market)", strategy.short, qty=20)

Re: How to automate partial close based on percentage profit target

Posted: Sun Aug 21, 2022 9:25 pm
by Deep-Wave
Hi shayefx,
what you describe is definitely possible, I utilise partial TP's myself.
(I think you have a flaw in your logic though, if your position is 2 micro lots and you close one at 40% TP and the second at 80% TP, then you are flat and the original TP target is useless :cool: )

Check out this link, I hope you find this useful.
https://kodify.net/tradingview/orders/p ... ge-profit/
Best of luck with your coding,
cheers, :up:
Deep

Re: How to automate partial close based on percentage profit target

Posted: Mon Aug 22, 2022 1:46 am
by shayefx
Thanks for this Deep!

I've scanned through the code and I think I can make it work :-). I'll report back on how I get on with it.

In the mean time I'm not sure what you mean by "flaw in your logic" and "you are flat".

Based on what I want to achieve in which all gurus recommend to take some money off the table my manual process is currently as follows:

Set an alert at 40% TP and when that get's hit I sell 1000 units with 1000 remaining and simultaneously move my stop loss to breakeven. If the trade carries on in my favour I sell the remaining 1000 units at 80% TP. If not (and it reverses and hits my stop loss) I don't mind I took some profits.

The reason I'm interested in automating this is that when price hits my alert at 40% and I'm not there to execute the partial TP and then it reverses against me I end up missing out on the opportunity to bag some profits.

This is as opposed to waiting to hit 100% of my TP which is far less likely than hitting 40% of my TP

Re: How to automate partial close based on percentage profit target

Posted: Mon Aug 22, 2022 12:25 pm
by Deep-Wave
Hi there,
being flat means that you don't have an open position in the market. There are basically 3 possible scenarios, being long (you hold a buy position) being short (you hold a sell position) being flat (you hold no position).

By flaw I meant that if your entire position is 2 micro lots and you close one micro lot at 40% of your original take profit and the second micro lot at 80% of your original take profit then you don't have any position left that you could close at the original take profit. That means that the original take profit calculation is futile. You would never reach that level anyway because you scale out of your trades beforehand.
I hope this makes things clearer.

Cheers, :cool:

Re: How to automate partial close based on percentage profit target

Posted: Mon Aug 22, 2022 2:57 pm
by shayefx
Ah Ok! Gotcha!!

I'll get cracking with playing with the strategy.order() function :-)

Thanks

Re: How to automate partial close based on percentage profit target

Posted: Sun Sep 25, 2022 3:10 pm
by Scofield85
Hello;
I need your help please,
I want to link my BOT DCA 3commas with my strategy on trading view, for this my strategy includes a trailing stop and a take profit executet with the strategy.exit command, however on the alerts available on 3 commas I cannot manage to differntier between position closing message and exit position for taking profit or stop loss