-1

I want to implement spread so I calculate the ask and bid price already

class TestStrategy(Strategy):

    def next(self):
        current_price = self.data.Close[-1]
        current_spread = self.data.Spread[-1]
        current_signal = self.data.Signal[-1]


      # Calculate bid and ask prices using the spread in points
        bid_price = current_price - (current_spread * 0.001 / 2)
        ask_price = current_price + (current_spread * 0.001 / 2)

        if current_signal == 1:
            self.buy()

        if current_signal == -1:
            self.sell()

how can I implement the ask and bid here the only arguments in self.buy and self.sell is

def sell(self, *, size=.9999, limit=None, stop=None, sl=None, tp=None)
def buy(self, *, size=.9999, limit=None, stop=None, sl=None, tp=None)

1 Answer 1

0

To implement the ask and bid prices when executing self.buy() and self.sell() in your strategy, you can utilize the limit argument. The limit argument specifies the price at which you want to execute your order. Since you've already calculated the ask_price and bid_price using the spread, you can pass these prices as limits when calling self.buy() and self.sell().

class TestStrategy(Strategy):

    def next(self):
        current_price = self.data.Close[-1]
        current_spread = self.data.Spread[-1]
        current_signal = self.data.Signal[-1]

        # Calculate bid and ask prices using the spread in points
        bid_price = current_price - (current_spread * 0.001 / 2)
        ask_price = current_price + (current_spread * 0.001 / 2)

        # If signal is 1, place a buy order at the ask price
        if current_signal == 1:
            self.buy(limit=ask_price)

        # If signal is -1, place a sell order at the bid price
        if current_signal == -1:
            self.sell(limit=bid_price)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.