import pandas as pd
from moonshot import Moonshot
from moonshot.commission import SpotFXCommission
class FxBizday(Moonshot):
CODE = "fx-bizday"
DB = "fiber-1h"
DB_FIELDS = ["Close"]
COMMISSION_CLASS = SpotFXCommission
SLIPPAGE_BPS = 0.1
SIDS = "FXEURUSD"
BENCHMARK = "FXEURUSD"
SELL_EUR_START = "03:00:00"
SELL_EUR_END = "11:00:00"
BUY_EUR_START = "11:00:00"
BUY_EUR_END = "16:00:00"
def prices_to_signals(self, prices: pd.DataFrame):
closes = prices.loc["Close"]
times = closes.index.get_level_values("Time")
times = closes.apply(lambda x: times)
sell_eur = (times >= self.SELL_EUR_START) & (times < self.SELL_EUR_END)
buy_eur = (times >= self.BUY_EUR_START) & (times < self.BUY_EUR_END)
ones = pd.DataFrame(1, index=closes.index, columns=closes.columns)
minus_ones = pd.DataFrame(-1, index=closes.index, columns=closes.columns)
signals = minus_ones.where(sell_eur, ones.where(buy_eur, 0))
are_weekdays = closes.index.get_level_values("Date").day_name().isin([
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday"
])
are_weekdays = signals.apply(lambda x: are_weekdays)
signals = signals.where(are_weekdays, 0)
return signals
def signals_to_target_weights(self, signals: pd.DataFrame, prices: pd.DataFrame):
weights = signals.copy()
return weights
def target_weights_to_positions(self, weights: pd.DataFrame, prices: pd.DataFrame):
positions = weights.copy()
return positions
def positions_to_gross_returns(self, positions: pd.DataFrame, prices: pd.DataFrame):
closes = prices.loc["Close"]
gross_returns = closes.pct_change() * positions.shift()
return gross_returns
def order_stubs_to_orders(self, orders: pd.DataFrame, prices: pd.DataFrame):
orders["Exchange"] = "IDEALPRO"
orders["OrderType"] = "MKT"
orders["Tif"] = "DAY"
return orders