-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrategy_base.py
More file actions
82 lines (67 loc) · 2.32 KB
/
Copy pathstrategy_base.py
File metadata and controls
82 lines (67 loc) · 2.32 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
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional
from dataclasses import dataclass
from datetime import datetime
@dataclass
class TickData:
symbol: str
last_price: float
volume: int
open_interest: int
datetime: datetime
@dataclass
class OrderData:
symbol: str
order_id: str
direction: str
offset: str
price: float
volume: int
status: str
class CtaTemplate(ABC):
"""
Base class for CTA (Commodity Trading Advisor) strategies.
Designed for Trading Brains Studio internal execution engine.
"""
author: str = "Trading Brains Studio"
def __init__(self, cta_engine: Any, strategy_name: str, setting: Dict):
self.cta_engine = cta_engine
self.strategy_name = strategy_name
self.setting = setting
self.trading = False
self.pos = 0 # Current position
@abstractmethod
def on_init(self):
"""Callback when strategy is initialized."""
pass
@abstractmethod
def on_start(self):
"""Callback when strategy is started."""
pass
@abstractmethod
def on_stop(self):
"""Callback when strategy is stopped."""
pass
@abstractmethod
def on_tick(self, tick: TickData):
"""Callback for new tick data."""
pass
@abstractmethod
def on_bar(self, bar: Any):
"""Callback for new K-line bar data."""
pass
def buy(self, price: float, volume: int, stop: bool = False):
"""Send buy order (Open Long)."""
return self.cta_engine.send_order(self, "BUY", "OPEN", price, volume, stop)
def sell(self, price: float, volume: int, stop: bool = False):
"""Send sell order (Close Long)."""
return self.cta_engine.send_order(self, "SELL", "CLOSE", price, volume, stop)
def short(self, price: float, volume: int, stop: bool = False):
"""Send short order (Open Short)."""
return self.cta_engine.send_order(self, "SELL", "OPEN", price, volume, stop)
def cover(self, price: float, volume: int, stop: bool = False):
"""Send cover order (Close Short)."""
return self.cta_engine.send_order(self, "BUY", "CLOSE", price, volume, stop)
def write_log(self, msg: str):
"""Write log message."""
print(f"[{datetime.now()}] {self.strategy_name}: {msg}")