-2

hi so i have tweeked an already existing strategy, that uses v2 pine for swing trades, it looks profitable, but i am unable to connect it to 3commas using 1 alert to long or close long, i tried to make an escape charechter to make the strategy need only 1 alert to open or close trade automaticly, i mean its pointless having to create new alert manually to open or close, it becomes half a bot lol

strategy.entry("long", strategy.long, when=buy1, comment="{/n/"message_type/": /"bot/",/n/"bot_id/": 123456,/n/"email_token/": /"123456/",/n/"delay_seconds/": 0/n}")

strategy.close("long", when=sell2, comment="{/n/"action/": /"close_at_market_price/"n/,/"message_type/": /"bot/"/n,/"bot_id/": 123456,/n/"email_token/": /"123456/",n//"delay_seconds/": 0/n}")

if someone can explain where my mistake is i'd be greatfull, thanks in advance

original command for t.v alert : Open Long: { "message_type": "bot", "bot_id": 123456, "email_token": "123456", "delay_seconds": 0 }

For closing postion: { "action": "close_at_market_price", "message_type": "bot", "bot_id": 123456, "email_token": "123456", "delay_seconds": 0 }

1 Answers1

0

The best way to escape is use a single quote (') as the 3commas keys have double quotes. If we use the singles first this tells tradingview that we intend the doubles to be there and to note start/begin a new string variable. Also in regards to where we place this string in the strategy command, I find alert_message a better place, rather than comment=. Anything we place in the comment argument gets placed on the screen, so the keys can become quite messy. I like to keep comments short like "Long" for example. If we want to send the alert message to the webhook we would select 3commas in the alert menu and then include this placeholder only to send our messages when a trade is executed:

{{strategy.order.alert_message}}

This will autofill with your string for a buy or a sell accordingly at the time they occur. Here is an example of your strings in your strategy calls. Note I removed them and placed them as their own variables to clean it up a bit and shorten the length of the calls.

Long        =  '    {  "message_type": "bot",  "bot_id": 4635591,  "email_token": "25byourtefcodeuufyd2-43314-ab98-bjorg224",  "delay_seconds": 1}  ' //start long deal
 
ExitLong    =  '    {  "message_type": "bot",  "bot_id": 4635591,  "email_token": "25byourtefcodeuufyd2-43314-ab98-bjorg224",  "delay_seconds": 0,  "action": "close_at_market_price"}  ' // close long deal market 
 
 
strategy.entry("long", strategy.long, when=buy1, comment="Long", alert_message= Long)

strategy.close("long", when=sell2, comment="Close", alert_message= ExitLong)

Notice the single quotes on either side. This way we can remove all the \ and n etc.

Cheers and best of luck in your trading and coding

Bjorgum
  • 2,054
  • 2
  • 4
  • 11
  • thanks alot for your help, i think i have done something wrong, i tried the way i had orignally and now your way i am getting same error idk if this is due to the strategy using v2 pine but its giving me this: line 119: Cannot call `strategy.close` with arguments (literal__string, fun_call__operator and, string); available overloads: strategy.close(literal__string, series__bool) => void – CryptoLawyer1 Oct 03 '21 at 06:32
  • It’s likely. V2 is buggy and has repainting issues anyway. Pine has come a long way since then and it’s likely best to convert to v4. – Bjorgum Oct 03 '21 at 13:41
  • alert_message was not available in v2 so you could try still using the comment. Single quotes as depicted in the answer will solve your string issue, given there are no other errors in the code. Good luck – Bjorgum Oct 03 '21 at 13:48