<commit_before>
import json
import numpy as np
np.set_printoptions(threshold=np.inf)
import random

from constants import TRADING_FEE
from constants import SHOW_TRADES

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt


class Trading(object):

    def __init__(self, data_store=None, sequence_length=500, features_length=32, testing=False, show_trades=None ):
     
        self.data_store = data_store
        self.training_days = data_store.get_number_days()
        self.sequence_length = sequence_length
        self.features_length = features_length
        # Will be broadcasted to the other Modules. Change if changing actions below...
        self.testing = testing
        self.iday = -1 # for testing

        self.show_trades = SHOW_TRADES if show_trades is None else show_trades
        self.trade_history = []

        #print("TRADING: Testing is" ,self.testing)
        #for i in range (self.data_store.get_number_days()):
        #    print("Day ",i,", len: ", self.data_store.get_day_length(i))

    def reset(self):

        # randomize days
        if not self.testing: 
            self.iday = np.random.randint(0, self.training_days)
        else:
            self.iday += 1 # iterate over all idays
            if self.iday >= self.data_store.get_number_days():
                self.iday = 0

        self.day_length = self.data_store.get_day_length(self.iday)

        self.current_index = int(self.sequence_length)

        # Current position
        self.position = 0.
        self.initrate = 0.
        print ("IDAY:" , self.iday, self.current_index)
   
        self.current_rate_bid_norm, self.current_rate_bid, self.current_rate_ask_norm, self.current_rate_ask = self.data_store.get_bid_ask(self.iday, self.current_index)

        # Trade statistics
        self.daily_pnl = 0.
        self.last_pnl = 0.
        self.daily_trades = 0
        self.daily_long_trades = 0
        self.daily_short_trades = 0
        self.daily_wins = 0.
        self.trade_history = []

    def create_plot(self, testday):
        self.iday = testday
        self.day_length = self.data_store.get_day_length(self.iday)

        # plot of the rates
        bid = np.zeros(self.day_length)
   
        for i in range(self.day_length):
            rate_bid_norm, rate_bid, rate_ask_norm, rate_ask = self.data_store.get_bid_ask(self.iday, i)
            bid[i] = rate_bid
            #print("I",i,", Bid: ",rate_bid, " norm", rate_bid_norm, rate_ask_norm, rate_ask )

        fig = plt.figure()
        plt.plot(bid, lw=1)
        plt.xlabel('ticks')
        plt.ylabel('Bid')
        plt.title('Bid Rate for day '+str(testday) + " : " + str(self.data_store.get_day(self.iday)))
        plt.savefig("Rate_"+str(testday)+".png")
        plt.close(fig)

        plot_x = []
        plot_y = []
        total_pnl = 0.

        # and create a plot of the trades
        for position, pnl in self.trade_history:
            plot_x.append(position)
            total_pnl += pnl
            plot_y.append(total_pnl)

        #print(plot_x)
        #print(plot_y)

        fig = plt.figure()
        plt.plot(plot_x, plot_y,'.')

        plt.xlabel('ticks')
        plt.ylabel('Realized PnL')
        plt.title('Realized PnL for day '+str(testday) + " : " + str(self.data_store.get_day(self.iday)))
        plt.savefig("PnL_"+str(testday)+".png")
        plt.close(fig)


    def get_reward(self, action):
        """ #reward, terminal, self._screen = get_reward(action) # Screen is 84 x 8 i
        This is the version without sliding
        added a term in get_reward that adds winning trades twice to the reward - 
           once each tick it lasts (here winning and losing trades are treated equally), 
           and when closing a winning trade, the winning amount is credited again. 
           So reward != pnl. See daily_pnl instead.
        """
        reward = 0.
        fee = 0.

        debug = False
     
        #store the last rates...
        last_rate_bid = self.current_rate_bid
        last_rate_ask = self.current_rate_ask
        terminal = False
        self.new_trade = False
        close_trade = False
 
        OFFSET = 0
        last_position = self.position

        #print("day length ", self.day_length, self.iday,  self.current_index )
        if self.current_index >= self.day_length - 2 - OFFSET:
            if abs(self.position) > 0.1:
                close_trade = True
            terminal = True
            self.position = 0 
        else:
            if action == 0: # STAY/GO_SHORT
                if self.position >- 0.1:
                    if self.position > 0.1:
                        close_trade = True
                    if debug:
                        print("Going SHORT: ",index, self.current_rate_bid)

                    self.position = -1 # SHORT TRADES, otherwise 0
                    if True: # SHORT TRADES, otherwise False
                        self.initrate = self.current_rate_bid # SELL at the BID
                        self.new_trade = True
                        fee = - TRADING_FEE * abs(self.position)
                        last_rate_ask = self.initrate # for correct & simple position calculation
                        self.daily_trades += 1
                        self.daily_short_trades += 1

            if action == 1: # STAY/GO_LONG 
                #if self.position == 0:
                if self.position < 0.1:
                    if self.position < -0.1:
                        close_trade = True
                    if debug:
                        print("Going LONG: ",index, self.current_rate_bid)
                    self.initrate = self.current_rate_ask # BUY at the ASK
                    self.position = 1 # only 1 contract LONG
                    self.new_trade = True
                    fee = - TRADING_FEE * abs(self.position)
                    last_rate_bid = self.initrate # for correct & simple position calculation
                    self.daily_trades += 1
                    self.daily_long_trades += 1

            if action == 2: # STAY/GO_FLAT
                if abs(self.position) == 1: ## make useless
                    if debug:
                        print("Going FLAT: ",index, self.current_rate_bid)
                    self.position = 0 
                    close_trade = True

        if close_trade and self.show_trades:
            print("CLOSE: ", self.current_index, last_position, self.initrate, self.last_pnl)
            self.trade_history.append((self.current_index, self.last_pnl))

        # move to the next time step...
        self.current_index += 1

        # and get the rates...
        self.current_rate_bid_norm, self.current_rate_bid, self.current_rate_ask_norm, self.current_rate_ask = self.data_store.get_bid_ask(self.iday, self.current_index)

        # Calculate value
        # LONG:  CURRENT_BID - INITIAL_ASK
        # SHORT: CURRENT_ASK - INITIAL_BID
        value = 0.
        if self.position > 0.1:
            value = self.position * (self.current_rate_bid - last_rate_bid)
        elif self.position < -0.1:
            value = self.position * (self.current_rate_ask - last_rate_ask) 

        # New trade is opened
        if self.new_trade:
            if self.show_trades:
                print("OPEN:  ", self.current_index-1, self.position, self.initrate)
            # enforce a minimum holding period (OFFSET)
            self.current_index += OFFSET 

        # Trade is concluded, do the bookkeeping<commit_msg>Cosmetics, prettify OPEN/CLOSE show_trades output<commit_after>
import json
import numpy as np
np.set_printoptions(threshold=np.inf)
import random

from constants import TRADING_FEE
from constants import SHOW_TRADES

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt


class Trading(object):

    def __init__(self, data_store=None, sequence_length=500, features_length=32, testing=False, show_trades=None):
     
        self.data_store = data_store
        self.training_days = data_store.get_number_days()
        self.sequence_length = sequence_length
        self.features_length = features_length
        # Will be broadcasted to the other Modules. Change if changing actions below...
        self.testing = testing
        self.iday = -1 # for testing

        self.show_trades = SHOW_TRADES if show_trades is None else show_trades
        self.trade_history = []

        #print("TRADING: Testing is" ,self.testing)
        #for i in range (self.data_store.get_number_days()):
        #    print("Day ",i,", len: ", self.data_store.get_day_length(i))

    def reset(self):

        # randomize days
        if not self.testing: 
            self.iday = np.random.randint(0, self.training_days)
        else:
            self.iday += 1 # iterate over all idays
            if self.iday >= self.data_store.get_number_days():
                self.iday = 0

        self.day_length = self.data_store.get_day_length(self.iday)

        self.current_index = int(self.sequence_length)

        # Current position
        self.position = 0.
        self.initrate = 0.
        print ("IDAY:" , self.iday, self.current_index)
   
        self.current_rate_bid_norm, self.current_rate_bid, self.current_rate_ask_norm, self.current_rate_ask = self.data_store.get_bid_ask(self.iday, self.current_index)

        # Trade statistics
        self.daily_pnl = 0.
        self.last_pnl = 0.
        self.daily_trades = 0
        self.daily_long_trades = 0
        self.daily_short_trades = 0
        self.daily_wins = 0.
        self.trade_history = []

    def create_plot(self, testday):
        self.iday = testday
        self.day_length = self.data_store.get_day_length(self.iday)

        # plot of the rates
        bid = np.zeros(self.day_length)
   
        for i in range(self.day_length):
            rate_bid_norm, rate_bid, rate_ask_norm, rate_ask = self.data_store.get_bid_ask(self.iday, i)
            bid[i] = rate_bid
            #print("I",i,", Bid: ",rate_bid, " norm", rate_bid_norm, rate_ask_norm, rate_ask )

        fig = plt.figure()
        plt.plot(bid, lw=1)
        plt.xlabel('ticks')
        plt.ylabel('Bid')
        plt.title('Bid Rate for day '+str(testday) + " : " + str(self.data_store.get_day(self.iday)))
        plt.savefig("Rate_"+str(testday)+".png")
        plt.close(fig)

        plot_x = []
        plot_y = []
        total_pnl = 0.

        # and create a plot of the trades
        for position, pnl in self.trade_history:
            plot_x.append(position)
            total_pnl += pnl
            plot_y.append(total_pnl)

        #print(plot_x)
        #print(plot_y)

        fig = plt.figure()
        plt.plot(plot_x, plot_y,'.')

        plt.xlabel('ticks')
        plt.ylabel('Realized PnL')
        plt.title('Realized PnL for day '+str(testday) + " : " + str(self.data_store.get_day(self.iday)))
        plt.savefig("PnL_"+str(testday)+".png")
        plt.close(fig)


    def get_reward(self, action):
        """ #reward, terminal, self._screen = get_reward(action) # Screen is 84 x 8 i
        This is the version without sliding
        added a term in get_reward that adds winning trades twice to the reward - 
           once each tick it lasts (here winning and losing trades are treated equally), 
           and when closing a winning trade, the winning amount is credited again. 
           So reward != pnl. See daily_pnl instead.
        """
        reward = 0.
        fee = 0.

        debug = False
     
        #store the last rates...
        last_rate_bid = self.current_rate_bid
        last_rate_ask = self.current_rate_ask
        terminal = False
        self.new_trade = False
        close_trade = False
 
        OFFSET = 0
        last_position = self.position

        #print("day length ", self.day_length, self.iday,  self.current_index )
        if self.current_index >= self.day_length - 2 - OFFSET:
            if abs(self.position) > 0.1:
                close_trade = True
            terminal = True
            self.position = 0 
        else:
            if action == 0: # STAY/GO_SHORT
                if self.position >- 0.1:
                    if self.position > 0.1:
                        close_trade = True
                    if debug:
                        print("Going SHORT: ", index, self.current_rate_bid)

                    self.position = -1 # SHORT TRADES, otherwise 0
                    if True: # SHORT TRADES, otherwise False
                        self.initrate = self.current_rate_bid # SELL at the BID
                        self.new_trade = True
                        fee = - TRADING_FEE * abs(self.position)
                        last_rate_ask = self.initrate # for correct & simple position calculation
                        self.daily_trades += 1
                        self.daily_short_trades += 1

            if action == 1: # STAY/GO_LONG 
                #if self.position == 0:
                if self.position < 0.1:
                    if self.position < -0.1:
                        close_trade = True
                    if debug:
                        print("Going LONG: ", index, self.current_rate_bid)
                    self.initrate = self.current_rate_ask # BUY at the ASK
                    self.position = 1 # only 1 contract LONG
                    self.new_trade = True
                    fee = - TRADING_FEE * abs(self.position)
                    last_rate_bid = self.initrate # for correct & simple position calculation
                    self.daily_trades += 1
                    self.daily_long_trades += 1

            if action == 2: # STAY/GO_FLAT
                if abs(self.position) == 1: ## make useless
                    if debug:
                        print("Going FLAT: ",index, self.current_rate_bid)
                    self.position = 0 
                    close_trade = True

        if close_trade and self.show_trades:
            print("CLOSE: {:6} {:+2} {} {}".format(self.current_index, last_position, self.initrate, self.last_pnl))
            self.trade_history.append((self.current_index, self.last_pnl))

        # move to the next time step...
        self.current_index += 1

        # and get the rates...
        self.current_rate_bid_norm, self.current_rate_bid, self.current_rate_ask_norm, self.current_rate_ask = self.data_store.get_bid_ask(self.iday, self.current_index)

        # Calculate value
        # LONG:  CURRENT_BID - INITIAL_ASK
        # SHORT: CURRENT_ASK - INITIAL_BID
        value = 0.
        if self.position > 0.1:
            value = self.position * (self.current_rate_bid - last_rate_bid)
        elif self.position < -0.1:
            value = self.position * (self.current_rate_ask - last_rate_ask) 

        # New trade is opened
        if self.new_trade:
            if self.show_trades:
                print("OPEN:  {:6} {:+2} {}".format(self.current_index-1, self.position, self.initrate))
            # enforce a minimum holding period (OFFSET)
            self.current_index += OFFSET 

        # Trade is concluded, do the bookkeeping
{'fraction_non_alphanumeric': 0.0930294195600318, 'fraction_numerical': 0.007951232441028359, 'mean_word_length': 1.813758389261745, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4401167', 'n_tokens_mistral': 4745, 'n_tokens_neox': 4412, 'n_words': 1067}