Skip to content

Latest commit

 

History

History
202 lines (88 loc) · 4.21 KB

trading-strategy-for-exiting-trades.md

File metadata and controls

202 lines (88 loc) · 4.21 KB

Trading Strategy for Exiting trades | Start Chat

Monitor the current time, exit position 3 minutes before every hour then re-enter the funding rate at a slightly better price

Prompt

The funding rate on dydx is every hour, how would I build something that gets me out of the position 3 minutes before the hour and gets me back in a little lower if long, and a little higher if short. Create code no explanations .

Welcome Message

Here is a Python solution that makes use of the dydxpy library to interact with the dYdX API. It gets you out of the position 3 minutes before the hour and gets you back in a little lower if long, and a little higher if short.

import dydxpy

import time

from datetime import datetime, timedelta



public_key = 'your_public_key'

private_key = 'your_private_key'



dydx = dydxpy.Dydx(private_key=private_key, public_key=public_key)



def get_out_of_position():

    positions = dydx.get_my_positions()

    for position in positions:

        if position['size'] > 0:

            dydx.place_order(

                market='ETH-USD',

                side=dydxpy.SIDE_SELL,

                amount=position['size'],

                price=position['price'] - 0.01,

                fillOrKill=False,

                postOnly=True

            )

        else:

            dydx.place_order(

                market='ETH-USD',

                side=dydxpy.SIDE_BUY,

                amount=-position['size'],

                price=position['price'] + 0.01,

                fillOrKill=False,

                postOnly=True

            )



def funding_rate_strategy():

    while True:

        now = datetime.now()

        minutes_to_hour = 60 - now.minute

        if minutes_to_hour == 3:

            get_out_of_position()

            time.sleep(180)

        time.sleep(60)



funding_rate_strategy()

Please replace 'your_public_key' and 'your_private_key' with your actual public and private keys. Also note that error handling and edge case handling are not included in this example, you should add them as per your needs. This code assumes that you are trading the 'ETH-USD' market, you may change it according to your requirements. This code is a simple demonstration and does not guarantee any financial outcomes.

Conversation

ASSISTANT: Here is a Python solution that makes use of the dydxpy library to interact with the dYdX API. It gets you out of the position 3 minutes before the hour and gets you back in a little lower if long, and a little higher if short.

import dydxpy

import time

from datetime import datetime, timedelta



public_key = 'your_public_key'

private_key = 'your_private_key'



dydx = dydxpy.Dydx(private_key=private_key, public_key=public_key)



def get_out_of_position():

    positions = dydx.get_my_positions()

    for position in positions:

        if position['size'] > 0:

            dydx.place_order(

                market='ETH-USD',

                side=dydxpy.SIDE_SELL,

                amount=position['size'],

                price=position['price'] - 0.01,

                fillOrKill=False,

                postOnly=True

            )

        else:

            dydx.place_order(

                market='ETH-USD',

                side=dydxpy.SIDE_BUY,

                amount=-position['size'],

                price=position['price'] + 0.01,

                fillOrKill=False,

                postOnly=True

            )



def funding_rate_strategy():

    while True:

        now = datetime.now()

        minutes_to_hour = 60 - now.minute

        if minutes_to_hour == 3:

            get_out_of_position()

            time.sleep(180)

        time.sleep(60)



funding_rate_strategy()

Please replace 'your_public_key' and 'your_private_key' with your actual public and private keys. Also note that error handling and edge case handling are not included in this example, you should add them as per your needs. This code assumes that you are trading the 'ETH-USD' market, you may change it according to your requirements. This code is a simple demonstration and does not guarantee any financial outcomes.