kieran-mackle / AutoTrader

A Python-based development platform for automated trading systems - from backtesting to optimisation to livetrading.

Home Page:https://kieran-mackle.github.io/AutoTrader/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dydx Connectivity

jfitz001 opened this issue · comments

Hi there,
I am looking to live trade with dydx, but when I am attempting to connect and run a basic algorithm, I am getting errors.

The first one is: AutoData is not defined. I fixed this by importing the class in the dydx/broker.py file.

The next one is related to the data feed. The system exits right after the prompt "Please specify a data feed" shows up.

Any help would be greatly appreciated!
Jamie
Capture

Hi Jamie,

Thanks for raising this, I've fixed the AutoData import bug. You can upgrade by either re-pulling the repo or pip install --upgrade autotrader.

As per your other question, you need to tell AutoTrader where to find data to provide your strategy. If you have not set anything custom up for this, I imagine you would just like to pull price data directly from dYdX. In this case, you must specify feed="dydx" in AutoTrader.configure. This will create an instance of AutoData which connects to dYdX's public data API and pulls the latest price data each time your strategy is called for new trading signals.

I have tested your run file above locally and everything seems to be running fine, but please let me know if you still have issues. On another note, if you are using the example macd strategy, please note that it was written with periodic update mode, not continuous mode as in your example (which is now default - no need to specify it).

Let me know if you have any more questions!

Hi Kieran,
Thanks for the quick response! Really like this package you built so I appreciate the help. Looks like it's working for me too, so thanks again.

One follow up question...have you tested the dydx functionality live?

My code for the example macd is different, where I am just entering a trade if its greater/less than a 2 period moving average. I was lazy and didn't change the name of it.

I've been letting it run for a while and no trades are showing up.

Appreciate the help,
Jamie

Yes, though I was running a different sort of strategy. From your run script, you have it set to update once per day. Maybe that is a typo? I wouldn't expect you to see many trades on that frequency, depending on the size of your watchlist.

If you want to do some manual testing, you can create the dydx broker instance and trade manually through autotrader. See the example below for reference (untested, but should give you a starting point):

from autotrader import AutoTrader, Order

at = AutoTrader()
at.configure(broker="dydx")
broker = at.run()

order = Order(
    instrument="ETH-USD",
    direction=1,
    size=0.01,
)
broker.place_order(order)

Make sure to have the browser open while you test, as this will open a new position you might want to close after you have finished testing!

Thanks for sending an example as well. I modified it to be at the 1 minute level.

Any advice in dealing with this decimal error?
image

@jfitz001

Sorry about that - another bug on my end. All fixed now though, if you update again to v0.7.7 it should hopefully work as expected.

Let me know!

Yep it's working! If you need help working with dydx for stop losses/take profit, I can look into it!

Thanks so much,
Jamie

I think the reason it wasn't working for me is because the size was not specified. Is there a way to do a proportion of the total account balance in the order function you sent earlier?

@kieran-mackle Also noticed when i deployed the live trader that the trade keeps executing. Is there a method to check if there is an open position prior to trading?

@jfitz001

You can query your current account balance/NAV to calculate trade sizes. For example, something like:

nav = broker.get_NAV()
size = fraction * nav / last_price

As per your second question, that would be expected behaviour, if for example price stays above the 2 period moving average each update. If you only want to enter when this condition is first met, you can either create a signal from the crossover event (you might find this indicator useful), or you can also check your current position, as you suggested. To do the latter, use something like:

position = broker.get_positions(instrument)

The returned object will be a dictionary of the form {instrument: Position}, where instrument is a string, and Position is a Position object. You can then check for a position using len(position) > 0, if there is no open position, an empty dict {} will be returned.

I admit the docs are a bit lacking here, so I recommend trying these commands manually to get a feel for them before deploying them in the strategy. For example:

from autotrader import AutoTrader, Order

at = AutoTrader()
at.configure(broker="dydx")
broker = at.run()

# Get all positions
positions = broker.get_positions()

# Get position in single instrument
ETH_pos = broker.get_positions("ETH-USD")

Hope that helps.

Yes that was very helpful. The next step is figuring out how to use the NAV/position sizing capabilities within the .add_strategy.

Something like this:
image

I recommend having a look over the docs, particularly how to gain broker access from your strategy and the strategy configuration. Hopefully that will make things clear, but the code below is a good starter. Note that you can simply append any orders created to the orders list.

def __init__(self, instrument, broker, **kwargs):
    self.instrument = instrument
    self.broker = broker

def generate_signal(self, data):
    orders = []
    self.generate_features(data)
    
    # Fetch current position in self.instrument
    current_position = self.broker.get_positions(self.instrument)

    if not current_position:
        # Not position held

        # Check entry strategy logic
    
    else:
        # A position is held

        # Check exit logic
    
    return orders