enzoampil / fastquant

fastquant — Backtest and optimize your ML trading strategies with only 3 lines of code!
MIT License
1.52k stars 239 forks source link

How to give flags or limit our buy and sell signal for the backtesting?? #403

Closed YASHGUPTA2611 closed 2 years ago

mikeejazmines commented 2 years ago

Please give more details on the issue/ask

R-LP commented 2 years ago

Hi, similar question here I guess: let's say i have buy/sell signals; but at every trade i want to simulate stop loss/stop profit; how can I take this into consideration in the backtesting?

Example: 2021/1/1 buy signal; i want to put stop loss at -1% and stop profit at +3% 2021/1/2 : market drop -2% so I want stop loss at -1% executed (even though no sell signal) etc

Thanks!

mikeejazmines commented 2 years ago

@RogerMichel

there is already stop loss and take profit (https://github.com/enzoampil/fastquant/blob/4683879bc51bac24fed2ff8bc52d1a528a9f2203/python/fastquant/strategies/base.py#L52-L54)

You simply need to state the number when you create the strategy https://github.com/enzoampil/fastquant/blob/4683879bc51bac24fed2ff8bc52d1a528a9f2203/python/fastquant/strategies/base.py#L386-L396

YASHGUPTA2611 commented 2 years ago

Let's say i have initial_cash = 100000 and according to my strategy there are 3 buy signals in the starting and i wand to limit my buy signals to only one. So the below approach is right? or do we have any alternative? Capture11 Capture12

mikeejazmines commented 2 years ago

this will not work.

def buy_signal you are defining a function which should be its own line. I highly suggest going over the backtrader quickstart guide https://www.backtrader.com/docu/quickstart/quickstart/

what fastquant does is handle the main backtrader so all you need is to create the buy_signal and sell_signal. no need to add count. it will automatically handle the signals and buy using the first signal. if there is another buy before a sell, it wont do anything

YASHGUPTA2611 commented 2 years ago

Below is my strategy that i implemented. 1 2 In the starting my strategy takes the trade and buy 2 times then sell only one. I only want to trade one time and will give buy prop and sell prop. How will i do it??

mikeejazmines commented 2 years ago

This is a case of slippage and this really happens even in real life scenarios. You can try changing the slippage to 0

YASHGUPTA2611 commented 2 years ago

Okay understood @mikeejazmines . Thanks for the reply.