For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Executing Sell and Buy orders of an asset



  • I have been banging my head around the forum and the internet but could not figure out, how to place sell and buy order in one asset. The code I have just makes a buy trade based on a certain condition and then sells the bought asset off after meeting a different condition. I am trying to instantiate two different trades (long and short) which will be closed on meeting specific conditions.
    Code:

        self.log('Close: %.5f' % self.dataclose[0])
    
        if self.order:
            return
    
        if not self.position:
            if self.rsi[0] >= 70:
                self.log('SELL Created: %.5f' % self.dataclose[0])
                self.order = self.sell()
    
        else:
            if self.rsi[0] <= 30:
                self.log('BUY Created: %.5f' % self.dataclose[0])
                self.order = self.buy()

  • administrators

    It may be to early in the morning, but I fail to understand your problem.

    You want to buy when a certain condition is met and buy when another is met. The code apparently does that.

    May be the key is here

    @robin-dhillon said in Executing Sell and Buy orders of an asset:

    I am trying to instantiate two different trades (long and short) which will be closed on meeting specific conditions.

    But it seems your code already does that.

    You probably want to elaborate a bit more.



  • It was infact early in the morning, but to elaborate, as of now my code only buys the asset i.e it only goes long and closes the position when a certain condition is met. I want to know is how can I initiate a long order and a short order within the code. So go long when RSI below 30 but short it when its is above 70.


  • administrators

    If you buy and sell the same quantity you won't obviously go short.

    You can either:

    • Double the size of the existing position and buy/sell that amount (you obviously need a small exception when the initial position is not existent)
    • Use first self.close() and then buy/sell the exact amount you want.

    And you of course:

    • Need to check if the position is positive (long)/negative (short) rather than simply test for position existence.

  • administrators

    As an alternative you can try the FixedReverser sizer, which does everything automatically for you. See:



  • @backtrader I tried executing the trade with what you mentioned above, but I got the best execution using the bracket orders with a risk method controlling the sizes.
    Really appreciate the timely response, Thank you.