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

# How to write algorithm for both long and short selling?

• my code works correctly only for long positions, and in this algorithm I added a code of short position, which I wrote manually, but the code does not work correctly
In the documentation I read about the signal, but did not understand how to make a profit, and stop loss separately for long and short positions
For example,
For short positions
I have 100\$
SELL EXECUTED, Price: 40.42
OPERATION PROFIT, GROSS 3.15, NET 3.15
here I stayed in + (cash=100+3.15), and had to be in - (cash=100-3.15)

• my code works correctly only for long positions, and in this algorithm I added a code of short position, which I wrote manually, but the code does not work correctly
In the documentation I read about the signal, but did not understand how to make a profit, and stop loss separately for long and short positions
For example, I have 1000 in stock. In Long I buy a share for 100, then I sell for 102.
profit = 2, total 1002
for this code works well
For short positions,
For shorts
I have 100\$
SELL EXECUTED, Price: 40.42
OPERATION PROFIT, GROSS 3.15, NET 3.15
here I stayed in + (cash=100+3.15), and had to be in - (cash=100-3.15)

• @sasha

If you want to share your failure in coding and docs understanding, than It is enough to have a single post about it. I am sorry for this, but try harder and you will succeed.

If you want to ask the question and get an answer, than you may want to ask the question and maybe even to post your code.

• @ab_trader Thanks for the advice. I just wanted to know how to write the code for the short and long positions.

• I just wanted to know how to write the code for the short and long positions.

``````if not self.position:
# open long
if <long entry signal>:
# open short
elif <short entry signal>:
self.sell()

else:
# close long or short
if <long exit signal> or <short exit signal>:
self.close()
``````

});