forked from himanjim/TradingScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBNScalperBackTesting.py
More file actions
86 lines (58 loc) · 2.52 KB
/
BNScalperBackTesting.py
File metadata and controls
86 lines (58 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from datetime import datetime
import logging
import os
import math
from TradingScripts.BNScalperUtils import *
import TradingScripts.Utils as util
from TradingScripts.GenericStaticPrinter import print_statistics
kite = util.intialize_kite_api()
results_excel_location = 'D:/BackTest/Bn_back_test.xlsx'
candles = {}
order_placed = False
target = None
stop_loss = None
trailed = False
trades = [['ENTRY TIME', 'EXIT TIME', 'SUCCESS', 'P/L']]
trade = []
DATE = 'date'
def get_candle_key(tick):
hr = tick[DATE].hour
min = tick[DATE].minute
return int((hr * 60) + (int(min / 3) * 3))
def add_candle(hist_candle):
candles[get_candle_key(hist_candle)] = {OPEN : hist_candle[OPEN], LOW : hist_candle[LOW], HIGH : hist_candle[HIGH], CLOSE : hist_candle[CLOSE]}
def check_and_place_order(hist_candle):
global candles, order_placed, target, stop_loss, trade
candle_key = get_candle_key(hist_candle)
if (candle_key - 3) in candles and (candle_key - 9) in candles:
prev_candle = candles[(candle_key - 3)]
old_candle = candles[(candle_key - 9)]
if prev_candle[LOW] < old_candle[LOW] and hist_candle[HIGH] > prev_candle[OPEN] and prev_candle[CLOSE] > prev_candle[OPEN] and (hist_candle[HIGH] - prev_candle[LOW]) > MIN_RISE:
target = prev_candle[LOW] + MIN_RISE + TARGET
stop_loss = prev_candle[LOW] + MIN_RISE - STOP_LOSS
trade.append(hist_candle[DATE].strftime("%b %d %Y %H:%M:%S"))
print('Order placed:' + str(hist_candle))
order_placed = True
hist_candles = kite.historical_data(260105, datetime(2020, 10, 19, 0, 0, 0), datetime(2021, 1, 25, 0, 0, 0), '3minute')
for hist_candle in hist_candles:
add_candle(hist_candle)
if order_placed is False:
check_and_place_order(hist_candle)
if order_placed:
if hist_candle[LOW] <=stop_loss:
print('!!!Failed:' + str(hist_candle))
trade.extend([hist_candle[DATE].strftime("%b %d %Y %H:%M:%S"), 0, [-500, 0][trailed]])
trades.append(trade)
order_placed = False
trailed = False
trade = []
elif hist_candle[HIGH] >= target:
print('!!!SUCCESS:' + str(hist_candle))
trade.extend([hist_candle[DATE].strftime("%b %d %Y %H:%M:%S"), 1, 1000])
trades.append(trade)
order_placed = False
trailed = False
trade = []
# elif hist_candle[HIGH] >= (target - TRAIL):
# trailed = True
print_statistics(trades, results_excel_location)