-1

I'm quite a python newbie and I would kindly need your help. I'm trying to test a trading strategy very similar to what has been discussed here:

Python trading logic

however in my case: once a buy or sell signal is triggered (Gregorio's answer is perfect) the trade has a 2% stop loss and 2% take profit treshold compared to the buy or sell price. hence the strategy would give a positive or negative pnl depending on what will be reached first. is there any way to implement it without using a loop? Reason why I would prefer to avoid a loop is because I have 500,000 rows just for one stock and it would take a really long time. Many thanks

Andrea
  • 113
  • 1
  • 4
  • 10
  • We need some code so we can help you take a loop and do something with it. Which would mean putting up some of the data as well. Also reading those 500,000 rows means generally using a loop even if you are a seasoned pro. – Back2Basics Jan 17 '18 at 00:44

1 Answers1

0

Thank you @Back2Basics. Strategy and code is literally like in the link I provided above.

source, you could use this:

#make the necessary imports
import pandas as pd
from pandas_datareader import data, wb
import numpy as np
import matplotlib.pyplot as plt
import quandl
%matplotlib inline
df = quandl.get("XXX", authtoken="YYY",start_date="2015-01-01")

The strategy will buy when current price is above lower bollinger band and previous price is below lower bollinger band. At that point, the take_profit price will be: buy_price*(1+5%) and stop loss price = buy_price*(1-5%).

However, if the loop is the only solution, I guess I have to give up.

Kasia Gogolek
  • 3,374
  • 4
  • 33
  • 50
Andrea
  • 113
  • 1
  • 4
  • 10