def CubicCurve(positions, t, screen, color, curve_list, green, blue, quad_curve, quad_curve1, quad_curve2): P0_x = pow((1 - t), 3) * positions[0].x P0_y = pow((1 - t), 3) * positions[0].y P1_x = 3 * pow((1-t), 2) * t * positions[1].x P1_y = 3 * pow((1-t), 2) * t * positions[1].y P2_x = 3 * (1-t) * pow(t, 2) * positions[2].x P2_y = 3 * (1-t) * pow(t, 2) * positions[2].y P3_x = pow(t, 3) * positions[3].x P3_y = pow(t, 3) * positions[3].y curve = (P0_x + P1_x + P2_x + P3_x, P0_y + P1_y + P2_y + P3_y) pygame.draw.line(screen, (0, 0, 0), (positions[0].x, positions[0].y), (positions[1].x, positions[1].y), 1) pygame.draw.line(screen, (0, 0, 0), (positions[1].x, positions[1].y), (positions[2].x, positions[2].y), 1) pygame.draw.line(screen, (0, 0, 0), (positions[2].x, positions[2].y), (positions[3].x, positions[3].y), 1) first_line = [positions[0], positions[1]] second_line = [positions[1], positions[2]] third_line = [positions[2], positions[3]] fourth_line = [positions[0], positions[1], positions[2]] fifth_line = [positions[1], positions[2], positions[3]] sixth_line = [positions[0], positions[2], positions[3]] a = LinearCurve(first_line, t, screen, green, False) b = LinearCurve(second_line, t, screen, green, False) c = LinearCurve(third_line, t, screen, green, False) pygame.draw.line(screen, green, a, b, 2) pygame.draw.line(screen, green, b, c, 2) QuadraticCurve(fourth_line, t, screen, (100, 100, 0), quad_curve, green) QuadraticCurve(fifth_line, t, screen, (100, 100, 0), quad_curve1, green) QuadraticCurve(sixth_line, t, screen, (100, 100, 0), quad_curve2, green, False) position_1 = Position(a[0], a[1]) position_2 = Position(b[0], b[1]) position_3 = Position(c[0], c[1]) line1 = [position_1, position_2] line2 = [position_2, position_3] start = LinearCurve(line1, t, screen, blue, False) end = LinearCurve(line2, t, screen, blue, False) pygame.draw.line(screen, blue, start, end, 2) pygame.draw.circle(screen, color, (int(curve[0]), int(curve[1])), 8) curve_list.append((int(curve[0]), int(curve[1])))
def _from_positions_row_to_position(row): """ Convert a row into a Position object """ quantity = float(row.Quantity.replace(',', '')) this_position = Position(Code=row.Symbol.replace('+', ''), Position=quantity) return this_position
def performAction(self, newPosition, maxRow, maxColumn): tempPos = PositionManager(self.position.x, self.position.y) position = Position.factory(newPosition) tempPos = position.update(tempPos) if tempPos.validatePosition(maxRow, maxColumn): return tempPos else: return False
def get_position(self, code=None): if code is None: code = self.primary_code position = self.positions.get(code) if position is None: # print('2>>>', id(self)) self.positions[code] = Position(code=code, strategy=self) return self.positions[code]
def umatched_as_positions(self): """ Return a PositionList object containing the unmatched trades """ result=PositionList() for code in list(self.keys()): position=self[code].unmatched.final_position() result.append(Position(Code=code, Position=position)) return result
class StrategyNeural: def __init__(self): self.position = Position() self.instrument = Instrument() self.neural_network = NeuralNetwork() def execute(self): if not self.position.has_open_position(): params = self.builder_params() candles = self.instrument.get_candles(params) open, high, low, close, volume = self.candles2array(candles) operation = self.neural_network.predict(open, high, low, close, volume) self.execute_trade(operation) def builder_params(self): return {'instrument': 'EUR_USD', 'granularity': 'M5', 'count': 170} def candles2array(self, candles): open = [] high = [] low = [] close = [] volume = [] for candle in candles: mid = candle.mid open.append(mid.o) high.append(mid.h) low.append(mid.l) close.append(mid.c) volume.append(candle.volume) return numpy.array(open, dtype=float), \ numpy.array(high, dtype=float), \ numpy.array(low, dtype=float), \ numpy.array(close, dtype=float), \ numpy.array(volume, dtype=float) def execute_trade(self, operation): params = {} params['instrument'] = 'EUR_USD' params['take_profit'] = 0.002 params['stop_loss'] = 0.002 if operation == 1: Buy().execute(params) elif operation == 2: Sell().execute(params)
class Player: view = Position.factory(Positioning.TOP) def __init__(self, xStart, yStart, id): self.position = PositionManager(xStart, yStart) self.id = id def performAction(self, newPosition, maxRow, maxColumn): tempPos = PositionManager(self.position.x, self.position.y) position = Position.factory(newPosition) tempPos = position.update(tempPos) if tempPos.validatePosition(maxRow, maxColumn): return tempPos else: return False
def __init__(self): self.position = Position() self.instrument = Instrument() self.neural_network = NeuralNetwork()
fps = 60 font = pygame.font.Font('freesansbold.ttf', 32) #colors white = (235, 235, 235) black = (20, 20, 20) red = (242, 2, 2) green = (2, 242, 102) blue = (2, 146, 242) purple = (205, 163, 255) #parameters t = 0 speed = 0.002 linear_positions = [Position(100, 800, "P0"), Position(300, 200, "P1")] Quadratic_positions = [ Position(660, 800, "P0"), Position(880, 450, "P1"), Position(720, 200, "P2") ] cubic_positions = [ Position(1050, 800, "P0"), Position(1280, 200, "P1"), Position(1420, 800, "P2"), Position(1800, 200, "P3") ] quadratic_curve = [] cubic_curve = [] curve1 = []