ok24601 / etoro-api

etoro trading REST API
ISC License
101 stars 47 forks source link

Error with shorting (sell) positions #20

Open WarlaxZ opened 3 years ago

WarlaxZ commented 3 years ago

Whenever I try to create a short position it fails

Unfortunately I don't have the error now as the market is closed, however will try and get it on monday. I just want to confirm that the library supports shorts, as it looks as if it does.

Will confirm that buys went through completely fine, it was just an issue with 'sells' (and I don't mean closing an open position, mean a shot position).

Let me know if there is anything else I can provide you with

ok24601 commented 3 years ago

Hi,

For the sell positions is required to specify SL and TP values, have you done that? You can verify it with Bitcoin its market is open even on weekends.

Best regards Oleg


От: WarlaxZ notifications@github.com Отправлено: 9 января 2021 г. 0:33 Кому: ok24601/etoro-api etoro-api@noreply.github.com Копия: Subscribed subscribed@noreply.github.com Тема: [ok24601/etoro-api] Error with shorting (sell) positions (#20)

Whenever I try to create a short position it fails

Unfortunately I don't have the error now as the market is closed, however will try and get it on monday. I just want to confirm that the library supports shorts, as it looks as if it does.

Will confirm that buys went through completely fine, it was just an issue with 'sells' (and I don't mean closing an open position, mean a shot position).

Let me know if there is anything else I can provide you with

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/ok24601/etoro-api/issues/20, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ADHNNVTCBJD6KPORGZSDBBDSY6B3LANCNFSM4V3BPZXA.

WarlaxZ commented 3 years ago

originally I had (but it was part of a larger program so couldnt directly see the results) - however nice shout on the btc - will test locally now. wonder if my issue may have been due to some positions requiring larger SL/TP - but will investigate. thank you for the response!

WarlaxZ commented 3 years ago

ah can't test - now that u cant buy cfd and u have to buy the underlying asset, u cant short (button not available in etoro UI) - so currently see this error (although this is correct that it errors)

{ "timestamp": "2021-01-09T19:28:11.872+0000", "status": 500, "error": "Internal Server Error", "message": "Code: 747, details: null", "path": "/etoro-api/positions/open" }

Will wait until market opens monday and provide more details :)

WarlaxZ commented 3 years ago

Ok just confirmed, tried to short TUI.L

I see this error:

{
  "timestamp": "2021-01-11T10:20:55.744+0000",
  "status": 500,
  "error": "Internal Server Error",
  "message": "Code: 796, details: null",
  "path": "/etoro-api/positions/open"
}

And the stack trace in the logs is:

{"Id":"e2101552-b280-4ec8-a779-5a83d6bcf0d5","Durable":false,"Type":"Trading.Failure.Position.Open","Content":"{\"CID\":19298152,\"ErrorMessageCode\":796,\"RequestToken\":\"110952bf-df6d-425d-b538-034b34b0b3c0\"}","Aggregated":false}

2021-01-11 10:32:45.423 ERROR 40 --- [nio-8088-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/etoro-api] threw exception [Request processing failed; nested exception is ok.work.etoroapi.transactions.TransactionError: Code: 796, details: null] with root cause

ok.work.etoroapi.transactions.TransactionError: Code: 796, details: null

at ok.work.etoroapi.transactions.TransactionPool.getFromPool(TransactionPool.kt:35) ~[classes!/:na]

at ok.work.etoroapi.client.EtoroHttpClient.openPosition(EtoroHttpClient.kt:126) ~[classes!/:na]

at ok.work.etoroapi.controller.PositionsController.openPosition(PositionsController.kt:27) ~[classes!/:na]

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]

at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]

at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888) ~[spring-webmvc-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.30.jar!/:9.0.30]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1598) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]

at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.30.jar!/:9.0.30]

at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]

And for full confirmation, watchlist looks like this:

[
  {
    "id": "2163",
    "name": "tui.l",
    "fullName": "TUI AG",
    "buy": 356.12,
    "sell": 354.58,
    "marketOpen": true,
    "askDiscounted": 355.8,
    "bidDiscounted": 354.9
  }
]

And position being sent was:

{
  "name": "tui.l",
  "type": "SELL",
  "amount": 123,
  "leverage": 1,
  "takeProfitRate": 100.12,
  "stopLossRate": 500.03,
  "tsl": true
}

(Confirmed those TP/SL values were valid from the etoro front end)

ok24601 commented 3 years ago

hi, thanks fore pointing this out, i'll look how to fix it.

Oleg


От: WarlaxZ notifications@github.com Отправлено: 11 января 2021 г. 12:28 Кому: ok24601/etoro-api etoro-api@noreply.github.com Копия: ok24601 oleh_22_08@outlook.com; Comment comment@noreply.github.com Тема: Re: [ok24601/etoro-api] Error with shorting (sell) positions (#20)

Also tried with:

"takeProfitRate": 500.00, "stopLossRate": 320.00,

Just to really test the water, but still failed

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/ok24601/etoro-api/issues/20#issuecomment-757859840, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ADHNNVSAAESYMMD3ZXTVKCTSZLHGHANCNFSM4V3BPZXA.

WarlaxZ commented 3 years ago

Hero, thank you so much for this library!

WarlaxZ commented 3 years ago

I've done a quick look to see what the actual packet should look like when sent to etoro, hopefully this help!:

https://www.etoro.com/sapi/trade-demo/positions?client_request_id=XXXXX

{
    "CID":XXXXX,
    "InstrumentID":2163,
    "IsBuy":false,
    "Leverage":1,
    "StopLossRate":374.62,
    "TakeProfitRate":279.31,
    "IsTslEnabled":true,
    "View_UnitMargin":4.82908408,
    "View_MaxPositionUnits":1440,
    "View_Units":1041.61,
    "View_openByUnits":false,
    "IsDiscounted":false,
    "ViewRateContext": {
        "ClientViewRate":358.48,
        "ClientViewRateID":XXXX,
        "ClientRateForCalc":359.7,
        "ClientRateForCalcID":XXXX
    },
    "Amount":5030,
    "View_Source_Application":"apps-components-autocomplete-autocomplete.view-trade-button",
    "View_Source_Location":"/portfolio",
    "View_CurrentTradingMode":"REGULAR"
}

buy position for reference:

https://www.etoro.com/sapi/trade-demo/positions?client_request_id=XXXX'

{
    "CID":XXXX,
    "InstrumentID":2163,
    "IsBuy":true,
    "Leverage":1,
    "StopLossRate":351.95,
    "TakeProfitRate":1158.88,
    "IsTslEnabled":false,
    "View_UnitMargin":4.8496525,
    "View_MaxPositionUnits":1440,
    "View_Units":20.62,
    "View_openByUnits":false,
    "IsDiscounted":true,
    "ViewRateContext":{
        "ClientViewRate":359.9,
        "ClientViewRateID":XXXX,
        "ClientRateForCalc":360,
        "ClientRateForCalcID":XXXX
    },
    "Amount":100,
    "View_Source_Application":"apps-components-autocomplete-autocomplete.view-trade-button",
    "View_Source_Location":"/portfolio",
    "View_CurrentTradingMode":"REGULAR"
}

difference I can spot:

"IsBuy":false,
"IsDiscounted":false,

Looking at your code nothing jumps out at me, but I don't have an IDE installed to breakpoint it to see the actual error, although IsBuy looks like its set correctly

If theres anything else I can help with, please let me know, I'm desperate to get this working and this is literally the final piece for me, so I'm more than happy to help where I can

ok24601 commented 3 years ago

ok, thanks for this overview it's helpfull, it can be it's because price for SELL is discounted, in theory my code should use discounted price in this case but may be smthng went wrong


От: WarlaxZ notifications@github.com Отправлено: 11 января 2021 г. 14:40 Кому: ok24601/etoro-api etoro-api@noreply.github.com Копия: ok24601 oleh_22_08@outlook.com; Comment comment@noreply.github.com Тема: Re: [ok24601/etoro-api] Error with shorting (sell) positions (#20)

I've done a quick look to see what the actual packet should look like when sent to etoro, hopefully this help!:

https://www.etoro.com/sapi/trade-demo/positions?client_request_id=XXXXX

{ "CID":XXXXX, "InstrumentID":2163, "IsBuy":false, "Leverage":1, "StopLossRate":374.62, "TakeProfitRate":279.31, "IsTslEnabled":true, "View_UnitMargin":4.82908408, "View_MaxPositionUnits":1440, "View_Units":1041.61, "View_openByUnits":false, "IsDiscounted":false, "ViewRateContext": { "ClientViewRate":358.48, "ClientViewRateID":XXXX, "ClientRateForCalc":359.7, "ClientRateForCalcID":XXXX }, "Amount":5030, "View_Source_Application":"apps-components-autocomplete-autocomplete.view-trade-button", "View_Source_Location":"/portfolio", "View_CurrentTradingMode":"REGULAR" }

buy position for reference:

https://www.etoro.com/sapi/trade-demo/positions?client_request_id=XXXX'

{ "CID":XXXX, "InstrumentID":2163, "IsBuy":true, "Leverage":1, "StopLossRate":351.95, "TakeProfitRate":1158.88, "IsTslEnabled":false, "View_UnitMargin":4.8496525, "View_MaxPositionUnits":1440, "View_Units":20.62, "View_openByUnits":false, "IsDiscounted":true, "ViewRateContext":{ "ClientViewRate":359.9, "ClientViewRateID":XXXX, "ClientRateForCalc":360, "ClientRateForCalcID":XXXX }, "Amount":100, "View_Source_Application":"apps-components-autocomplete-autocomplete.view-trade-button", "View_Source_Location":"/portfolio", "View_CurrentTradingMode":"REGULAR" }

difference I can spot:

"IsBuy":false, "IsDiscounted":false,

Looking at your code nothing jumps out at me, but I don't have an IDE installed to breakpoint it to see the actual error, although IsBuy looks like its set correctly

If theres anything else I can help with, please let me know, I'm desperate to get this working and this is literally the final piece for me, so I'm more than happy to help where I can

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/ok24601/etoro-api/issues/20#issuecomment-757926533, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ADHNNVQSIFXOUXVRHCFBTXDSZLWT7ANCNFSM4V3BPZXA.

WarlaxZ commented 3 years ago

maybe worth adding an override param perhaps.

from what I can see in your code it calls:

assetInfo.getBoolean("AllowDiscountedRates") which is just fixed no matter the direciton. I think its weird that the same asset should or shouldnt have a discount depending on the direction. to be honest I'm really not even sure what the discount is and how u get it. I did also notice i missed off the trailing stop when i manually opened the buy, so no idea if a trailing stop would affect the discount.

might be worth changing it to !type && assetInfo.getBoolean("AllowDiscountedRates")

WarlaxZ commented 3 years ago

sorry i mean type not !type - as discount is only allowed on buys

(type = position.type.equals(PositionType.BUY))

WarlaxZ commented 3 years ago

nope re-ran is isdiscounted set to false and that doesnt seem to have done the magic trick :/

WarlaxZ commented 3 years ago

So I tried replaying an actual post an I dont get an error, however the position didnt open (presumably because the ID for calc might have been a dupe, not entirely sure. Also one thing I noticed as that the caps get lost in you post body, ie your looks like this:

{
    "amount": 50,
    "instrumentID": "2163",
    "isBuy": false,
    "isDiscounted": false,
    "isTslEnabled": false,
    "leverage": 1,
    "stopLossRate": 355.98,
    "takeProfitRate": 178.81,
    "view_MaxPositionUnits": 1440,
    "view_openByUnits": false,
    "view_Units": 0.01,
    "viewRateContext": {
        "clientViewRate": 356.3
    }
}

so vs a real packet we're missing:

   "View_CurrentTradingMode": "REGULAR",
    "View_Source_Application": "apps-components-autocomplete-autocomplete.view-trade-button",
    "View_Source_Location": "/markets/tui.l",
    "View_UnitMargin": 0.87582864,
    "View_Units": 114.18, <--- this is always fixed at 0.01 in your code (probably not a problem)
    "ViewRateContext": {
        "ClientRateForCalc": 65.04,
        "ClientRateForCalcID": XXXXX,
        "ClientViewRateID": XXXX
    }
WarlaxZ commented 3 years ago

u had any joy with this? not being able to set any breakpoints to investigate properly was killing me yesterday, that and I've not used kotlin before :P let me know if theres anything I can help you with to get this up and running though

ok24601 commented 3 years ago

hi, I din't get a chance to take a look on that, had a lot of work this week. Will probably do it on weekends.


От: WarlaxZ notifications@github.com Отправлено: 12 января 2021 г. 13:07 Кому: ok24601/etoro-api etoro-api@noreply.github.com Копия: ok24601 oleh_22_08@outlook.com; Comment comment@noreply.github.com Тема: Re: [ok24601/etoro-api] Error with shorting (sell) positions (#20)

u had any joy with this? not being able to set any breakpoints to investigate properly was killing me yesterday, that and I've not used kotlin before :P let me know if theres anything I can help you with to get this up and running though

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/ok24601/etoro-api/issues/20#issuecomment-758583575, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ADHNNVUZHDYZEE5RJO7TDBDSZQUQ3ANCNFSM4V3BPZXA.

WarlaxZ commented 3 years ago

ok - once again thank you so much for the library, and do let me know if there is anything you need from me to assist

WarlaxZ commented 3 years ago

any luck?

ok24601 commented 3 years ago

nope couldnt test it because market was closed on weekends, will try to fix it on the week

WarlaxZ commented 3 years ago

ah lol didnt think about that either, stupid markets :P as always let me know if theres anything i can do to assist

username77 commented 3 years ago

By me is not able to open trades on LOGN.ZU. Go errors on these too: ASC.L, SMSN.L, ABBN.ZU

Seem that don't like to open trades on assets with extension .L, .ZU :)

Is possible to use uppercase or lowercase in all assets because I see cryptos, forex are in uppercase?

WarlaxZ commented 3 years ago

i'm fairly certain i get the same issue with american stocks, so no '.blah' extention (for example "bb") - but the casing thing certainly is annoying, might be worth a .lower()/upper() (or language equivalent) on all variables just to harmonize them, as symbols are case insensitive

WarlaxZ commented 3 years ago

how u getting on?

WarlaxZ commented 3 years ago

bump