Con.open_trade setting a stoploss and target

Hi,

I’ve tried to open a trade using the following code:

con.open_trade(symbol=‘EUR/USD’, is_buy=True, is_in_pips=True,amount=‘100’,
time_in_force=‘GTC’,order_type=‘AtMarket’, stop=1.11370)

I’m getting the following error:


ServerError Traceback (most recent call last)
in ()
1 con.open_trade(symbol=‘EUR/USD’, is_buy=True, is_in_pips=True,amount=‘100’,
----> 2 time_in_force=‘GTC’,order_type=‘AtMarket’, stop=1.11370)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\fxcmpy\fxcmpy.py in open_trade(self, symbol, is_buy, amount, time_in_force, order_type, rate, is_in_pips, limit, at_market, stop, trailing_step, account_id)
1130
1131 data = self.handle_request(method=‘trading/open_trade’,
-> 1132 params=params, protocol=‘post’)
1133 if ‘data’ in data and ‘orderId’ in data[‘data’]:
1134 order_id = int(data[‘data’][‘orderId’])

~\AppData\Local\Continuum\anaconda3\lib\site-packages\fxcmpy\fxcmpy.py in handle_request(self, method, params, protocol)
2525
2526 raise ServerError(‘FXCM Server reports an error: %s.’
-> 2527 % data[‘response’][‘error’])
2528 else:
2529 self.logger.error(‘FXCM Server reports an unknown error: %s.’

ServerError: FXCM Server reports an error: {“code”:18,“message”:“The Stop distance must be “,“parameters”:[”<”,0]}.

referencing the REST API documentation, the variable type says “number”. so I’m not sure why it doesn’t work. I’m making sure the SL is below the entry level.

When I remove the SL and run the code like that, it works so it definitely is an issue with the stop.

also, I didn’t see a parameter to enter a target, any ideas on how to enter this?

Hi @Jens, to enter your stop as a rate as you have above, set is_in_pips to False. If is_in_pips=True, it is expecting you to enter your stop in terms of pips, rather than as a rate, which is why you got the error.
Then, just add your limit to the end. For example:

order = con.open_trade(symbol='EUR/USD', is_buy=True,
                       stop=1.11370, is_in_pips=False,
                       amount='100', time_in_force='GTC',
                       order_type='AtMarket', limit=1.13370)

of course, that makes a lot of sense… stupid mistake, thanks

what would the difference between the parameter “stop” and “rate” here?

Ah my mistake there, you would use “stop” not “rate”. Should be:

order = con.open_trade(symbol='EUR/USD', is_buy=True,
stop=1.11370,  is_in_pips=False ,
amount='100', time_in_force='GTC',
order_type='AtMarket', limit=1.13370)
1 Like

I’d like to share you the following code I’ve created to automatically take trades at 12 am.

import datetime
import time

def enter_order(pair, SL, T, market_close, buy=True):

    marketclose = 0
    now = datetime.datetime.now()

    while now.hour != market_close:
        time.sleep(1)

    con.subscribe_market_data(symbol)
    con.get_prices(pair)['Ask'].tail(1)

    con.open_trade(symbol=pair, is_buy=buy, is_in_pips=False,amount='100',
                   time_in_force='GTC',order_type='AtMarket', stop=SL, limit=T)

enter_order('EUR/USD', 1.11370, 1.13370, 0)

I was wondering if there is a way to run this code without having to have my computer running? I’m planning to run automated strategies eventually and would like to be able to do so without having to run my pc. Do you have any suggestions there? I think in one of the webinars I’ve seen, it was suggested to use something like AWS.

1 Like

Hi Jens, this looks great.

There are a few options to consider if you are looking to have your algo system running continuously. FXCM offers two virtual private server options; FXcolo and Beeks Financial. The VPS vendors also have really low latency as you can see in this recent performance by FXcolo: http://www.fxcolo.com/Performance.html.

AWS EC2 has a variety of offerings based on CPUs and memory. You can find the on demand pricing based on your region here: https://aws.amazon.com/ec2/pricing/on-demand/. Another option is Google Compute Engine (similar to AWS’s offering). Kris Longmore wrote a helpful article on running an algo on GCE: https://robotwealth.com/run-trading-algorithms-google-cloud-platform-6-easy-steps/.

3 Likes

Hi @QN_Liza ,

Please advice how to set trailing lost with below code:

myorder = 1
mylimit = 2
mystoploss = -5

order = con2.open_trade(‘EUR/USD’, is_buy=False,
amount=myorder, time_in_force=‘GTC’,
order_type=“AtMarket”, is_in_pips=True,
limit=mylimit,stop=mystoploss, trailing_step=2)

When I had executed this there was no trailing lost in the order.

Please advice if this is correct way to set trailing _step.

Hi @adrian, are you trying to do a fixed stop or a dynamic stop? (The difference between the two is explained here.)

If creating a market order, only a dynamic trailing stop can be used. The value must be .1 (which means the stop will trail by .1 pips when the trade moves .1 pips in your favor). So to enter a market order with a dynamic trailing stop use:
order = con.open_trade(‘EUR/USD’, is_buy=False,
amount=myorder, time_in_force=‘GTC’,
order_type=‘AtMarket’, is_in_pips=True,
limit=mylimit, stop=mystoploss, trailing_step=.1)

If changing an open position, you can change the stop to a fixed trailing stop using the following code:
con.change_trade_stop_limit(66326675, is_stop=True, rate=-20, trailing_step=10)

If creating an entry order, you can use a dynamic trailing stop or a fixed trailing stop. For a fixed trailing stop, acceptable inputs are multiples of 10. Example:
order = con.create_entry_order(symbol=‘USD/JPY’, is_buy=True,
amount=1, limit=100,
is_in_pips = True,
time_in_force=‘GTD’, expiration=‘2108-10-01’,
rate=113.53, stop=-200, trailing_stop_step=10)

*note, for entry orders, use trailing_stop_step. For market orders, use trailing_step.

1 Like

Hi @QN_Liza,

I executed this line: order =
con.open_trade(‘EUR/USD’, is_buy=False, amount=1,
time_in_force=‘GTC’,order_type=‘AtMarket’,
is_in_pips=True, limit=5, stop=-5, trailing_step=.1)

Please advice how to check if the trailing_step is working. Because I don’t see number in Stop Move column in Open Positions tab nor in the order detail (alt + l).

image

@adrian executing the same code on my demo I see it has set a trailing stop, which I can see by callingcon.get_open_positions(). This shows a stop move column, which for any value other than 0.0 indicates you have set a trailing stop:

If you’re using TS to confirm your orders, you can check the open positions tab which also has stop move:

@QN_Liza ,

Thanks for prompt reply.

I didn’t get same outout put like you have. I run it twice. What could be the reason?

@adrian you may be using an out-of-date version of fxcmpy. Please upgrade with pip: pip install fxcmpy -upgrade. It should be version 1.1.34

Hi Liza,

I am trying to run a code in a REST client interface following your guidelines. Here is how it looks like: {
“account_id”: “1134724”,
“symbol”: “EUR/USD”,
“is_buy”: true,
“is_in_pips”: false,
“stop”: 1.13,
“trailing_step”: 0,
“trailing_stop_step”: 0,
“limit”: 1.17,
“is_buy2”: false,
“stop2”: 1.18,
“trailing_step2”: 0,
“trailing_stop_step2”: 0,
“amount”: 500,
“order_type”: “AtMarket”,
“time_in_force”: “GTC”
}

The thing is a got the same error message: “error”: “{“code”:18,“message”:“The Stop distance must be “,“parameters”:[”<”,0]}”

Can you advise me what shall I correct?

Hello,

I’am haveing the same issue however i have my stop set to pips.

def place_Sell_Order():
order = con.open_trade(symbol=symbol, is_buy=False,
is_in_pips=True, stop=6,
amount=trade_Amount(), time_in_force=‘GTC’,
order_type=‘AtMarket’, limit=10)
I tried it without the stop and it work fine but i keep getting the error:
{“code”:18,“message”:“The Stop distance must be “,“parameters”:[”<”,0]}"},“data”:{}}
Can you please advise on how to make a stop work in pips.

Thank you.