Power Hour Money 戦略
この戦略は選択されたニューヨークセッション中に取引し、主要な時間軸すべてが一致したときにポジションを建てます。 月足、週足、日足、時間足のローソク足が始値より高く引けたときにロングポジションを建てます。 すべてが低く引けたときにショートポジションを建てます。 オプションのトレーリングストップが利益を保護し、16:45にポジションを決済することができます。
詳細
- エントリー: すべての時間軸が緑のときロング、すべて赤のときショート。
- セッションフィルター: NY 9:30-11:30、拡張 8:00-16:00 またはすべてのセッション。
- トレーリングストップ: ロングとショート両側のパーセントベース。
- デイエンド: 16:45にすべてのポジションをオプションで決済。
計算と執行
C# と Python は同じ因果モデルを使います。基本足が確定するたびに、その終値を形成中の月足・週足・日足・時間足の現在判明している終値として、各始値と厳密に比較します。色の不一致や同値ではポジションを維持します。将来の上位足の確定終値は参照しません。
月、月曜日始まりの週、日は夏時間を含むニューヨークの暦に従います。秋の重複する時間を区別するため、時間足は UTC で識別します。期間の切り替わりを観測した後、最初の利用可能な基本足の始値を新期間の始値とします。履歴開始前から進行中の期間は次の境界まで不明とし、最初に観測した価格で欠けた始値を代用しません。エントリーには四つの始値が必要です。
標準の CandleType は5分です。SessionStart=09:30 と SessionEnd=11:30 はニューヨークの市場時刻で、開始を含み終了を含みません。拡張時間は 08:00–16:00 に設定し、全時間は IsAllSessions=true にします。IsAllSessions、IsTrailingEnabled、IsEndOfDayCloseEnabled の標準値は false、LongTrailingPercent=2、ShortTrailingPercent=2、EndOfDayTime=16:45 です。
色が一致すれば、ポジションなしでは Volume、反対ポジションでは Volume + abs(Position) で完全反転します。未完了注文中は追加注文を禁止し、待機時間はありません。任意のトレーリングは実際の約定価格から開始します。ロングは現在の bid が 観測した最高 bid * (1 - LongTrailingPercent / 100)、ショートは現在の ask が 観測した最低 ask * (1 + ShortTrailingPercent / 100) に達すると決済します。約定価格も有利な極値を更新します。両割合は厳密に0より大きく100より小さく設定します。反転で極値をリセットします。
任意の日次決済は締切時刻以後の最初の市場時刻イベントで全ポジションの成行決済を送り、ニューヨークの午前0時まで新規エントリーを禁止します。気配値と1分の市場時刻タイマーが、セッションや足の準備とは独立に確認します。これらの決済には市場イベントが必要で、価格のギャップ時に正確な約定価格を保証しません。
namespace StockSharp.Samples.Strategies;
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
/// <summary>
/// Trades causal developing month, week, day and hour colors in New York sessions,
/// with optional executable-quote trailing and market-time end-of-day closing.
/// </summary>
public class PowerHourMoneyStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<TimeSpan> _sessionStart;
private readonly StrategyParam<TimeSpan> _sessionEnd;
private readonly StrategyParam<bool> _isAllSessions;
private readonly StrategyParam<bool> _isEndOfDayCloseEnabled;
private readonly StrategyParam<TimeSpan> _endOfDayTime;
private readonly StrategyParam<bool> _isTrailingEnabled;
private readonly StrategyParam<decimal> _longTrailingPercent;
private readonly StrategyParam<decimal> _shortTrailingPercent;
private DateOnly? _month;
private DateOnly? _week;
private DateOnly? _day;
private DateTime? _hour;
private decimal? _monthOpen;
private decimal? _weekOpen;
private decimal? _dayOpen;
private decimal? _hourOpen;
private Order _pendingOrder;
private Sides? _heldSide;
private decimal _extreme;
/// <summary>Initializes a 09:30–11:30 New York window, disabled optional exits and five-minute signals.</summary>
public PowerHourMoneyStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Finished candles supplying causal calendar opens and closes", "General");
_sessionStart = Param(nameof(SessionStart), TimeSpan.FromMinutes(570))
.SetRange(TimeSpan.Zero, TimeSpan.FromDays(1) - TimeSpan.FromTicks(1))
.SetDisplay("Session Start", "Inclusive New York wall-clock time", "Sessions");
_sessionEnd = Param(nameof(SessionEnd), TimeSpan.FromMinutes(690))
.SetRange(TimeSpan.FromTicks(1), TimeSpan.FromDays(1))
.SetDisplay("Session End", "Exclusive New York wall-clock time", "Sessions");
_isAllSessions = Param(nameof(IsAllSessions), false)
.SetDisplay("All Sessions", "Allow entries throughout the day", "Sessions");
_isEndOfDayCloseEnabled = Param(nameof(IsEndOfDayCloseEnabled), false)
.SetDisplay("End of Day Close", "Close at the cutoff and block further entries until New York midnight", "Protection");
_endOfDayTime = Param(nameof(EndOfDayTime), TimeSpan.FromMinutes(1005))
.SetRange(TimeSpan.Zero, TimeSpan.FromDays(1) - TimeSpan.FromTicks(1))
.SetDisplay("End of Day Time", "New York wall-clock cutoff", "Protection");
_isTrailingEnabled = Param(nameof(IsTrailingEnabled), false)
.SetDisplay("Trailing Enabled", "Protect both sides on real executable Level1 quotes", "Protection");
_longTrailingPercent = Param(nameof(LongTrailingPercent), 2m).SetGreaterThanZero()
.SetDisplay("Long Trailing Percent", "Percent below the highest observed bid since entry", "Protection");
_shortTrailingPercent = Param(nameof(ShortTrailingPercent), 2m).SetGreaterThanZero()
.SetDisplay("Short Trailing Percent", "Percent above the lowest observed ask since entry", "Protection");
OrderRegistering += order => _pendingOrder = order;
OwnTradeReceived += ObserveFill;
}
/// <summary>Base candle type; five minutes by default.</summary>
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
/// <summary>Inclusive start of the New York entry window.</summary>
public TimeSpan SessionStart { get => _sessionStart.Value; set => _sessionStart.Value = value; }
/// <summary>Exclusive end of the New York entry window.</summary>
public TimeSpan SessionEnd { get => _sessionEnd.Value; set => _sessionEnd.Value = value; }
/// <summary>Allow all session times; an enabled daily cutoff still blocks late entries.</summary>
public bool IsAllSessions { get => _isAllSessions.Value; set => _isAllSessions.Value = value; }
/// <summary>Close positions and block new entries at the New York cutoff.</summary>
public bool IsEndOfDayCloseEnabled { get => _isEndOfDayCloseEnabled.Value; set => _isEndOfDayCloseEnabled.Value = value; }
/// <summary>Daily cutoff in New York wall-clock time.</summary>
public TimeSpan EndOfDayTime { get => _endOfDayTime.Value; set => _endOfDayTime.Value = value; }
/// <summary>Enable executable-quote percent trailing for both sides.</summary>
public bool IsTrailingEnabled { get => _isTrailingEnabled.Value; set => _isTrailingEnabled.Value = value; }
/// <summary>Long trailing distance in percent, strictly between zero and one hundred.</summary>
public decimal LongTrailingPercent { get => _longTrailingPercent.Value; set => _longTrailingPercent.Value = value; }
/// <summary>Short trailing distance in percent, strictly between zero and one hundred.</summary>
public decimal ShortTrailingPercent { get => _shortTrailingPercent.Value; set => _shortTrailingPercent.Value = value; }
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType), (Security, DataType.Level1)];
private void ResetState()
{
_month = _week = _day = null;
_hour = null;
_monthOpen = _weekOpen = _dayOpen = _hourOpen = null;
_pendingOrder = null;
_heldSide = null;
_extreme = 0m;
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
ResetState();
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
if (SessionStart < TimeSpan.Zero || SessionStart >= SessionEnd || SessionEnd > TimeSpan.FromDays(1)
|| EndOfDayTime < TimeSpan.Zero || EndOfDayTime >= TimeSpan.FromDays(1)
|| LongTrailingPercent <= 0m || LongTrailingPercent >= 100m
|| ShortTrailingPercent <= 0m || ShortTrailingPercent >= 100m)
throw new ArgumentException("Sessions must be ordered within a day and trailing percentages within (0, 100).");
ResetState();
foreach (var field in new[] { Level1Fields.BestBidPrice, Level1Fields.BestAskPrice })
{
var quotes = new Subscription(DataType.Level1, Security);
quotes.MarketData.BuildField = field;
SubscribeLevel1(quotes).Bind(ObserveQuote).Start();
}
if (IsEndOfDayCloseEnabled)
StartTimer(TimeSpan.FromMinutes(1), () => TryEndOfDay());
var subscription = SubscribeCandles(CandleType);
subscription.Bind(ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private bool IsPending => _pendingOrder is not null && _pendingOrder.State is not (OrderStates.Done or OrderStates.Failed);
private void Submit(Sides side, decimal volume, string comment)
=> RegisterOrder(new Order { Security = Security, Portfolio = Portfolio, Type = OrderTypes.Market, Side = side, Volume = volume, Comment = comment });
private bool TryEndOfDay()
{
if (!IsEndOfDayCloseEnabled || TimeZoneInfo.ConvertTimeFromUtc(CurrentTime, TimeHelper.Est).TimeOfDay < EndOfDayTime)
return false;
if (Position != 0m && !IsPending && IsFormedAndOnlineAndAllowTrading())
Submit(Position > 0m ? Sides.Sell : Sides.Buy, Math.Abs(Position), "Power Hour end-of-day close");
return true;
}
private void ObserveFill(Subscription subscription, MyTrade trade)
{
Sides? side = Position > 0m ? Sides.Buy : Position < 0m ? Sides.Sell : null;
if (side != _heldSide)
_extreme = side is null ? 0m : trade.Trade.Price;
else if (side is Sides current)
_extreme = current == Sides.Buy ? Math.Max(_extreme, trade.Trade.Price) : Math.Min(_extreme, trade.Trade.Price);
_heldSide = side;
}
private void ObserveQuote(Level1ChangeMessage quote)
{
if (TryEndOfDay() || _heldSide is not Sides side || Position == 0m)
return;
var field = side == Sides.Buy ? Level1Fields.BestBidPrice : Level1Fields.BestAskPrice;
if (quote.TryGetDecimal(field) is not decimal price || price <= 0m)
return;
_extreme = side == Sides.Buy ? Math.Max(_extreme, price) : Math.Min(_extreme, price);
var stop = side == Sides.Buy ? _extreme * (1m - LongTrailingPercent / 100m) : _extreme * (1m + ShortTrailingPercent / 100m);
if (IsTrailingEnabled && !IsPending && IsFormedAndOnlineAndAllowTrading() && (side == Sides.Buy ? price <= stop : price >= stop))
Submit(side == Sides.Buy ? Sides.Sell : Sides.Buy, Math.Abs(Position), "Power Hour trailing close");
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
var local = TimeZoneInfo.ConvertTimeFromUtc(candle.OpenTime, TimeHelper.Est);
var day = DateOnly.FromDateTime(local);
var month = new DateOnly(local.Year, local.Month, 1);
var week = day.AddDays(-(((int)local.DayOfWeek + 6) % 7));
var hour = new DateTime(candle.OpenTime.Year, candle.OpenTime.Month, candle.OpenTime.Day, candle.OpenTime.Hour, 0, 0, DateTimeKind.Utc);
var midnight = local.TimeOfDay == TimeSpan.Zero;
if (_month != month)
{
_monthOpen = _month is not null || local.Day == 1 && midnight ? candle.OpenPrice : null;
_month = month;
}
if (_week != week)
{
_weekOpen = _week is not null || local.DayOfWeek == DayOfWeek.Monday && midnight ? candle.OpenPrice : null;
_week = week;
}
if (_day != day)
{
_dayOpen = _day is not null || midnight ? candle.OpenPrice : null;
_day = day;
}
if (_hour != hour)
{
_hourOpen = _hour is not null || candle.OpenTime == hour ? candle.OpenPrice : null;
_hour = hour;
}
if (TryEndOfDay() || IsPending || !IsFormedAndOnlineAndAllowTrading()
|| _monthOpen is not decimal monthly || _weekOpen is not decimal weekly
|| _dayOpen is not decimal daily || _hourOpen is not decimal hourly)
return;
var now = TimeZoneInfo.ConvertTimeFromUtc(CurrentTime, TimeHelper.Est).TimeOfDay;
if (!IsAllSessions && (now < SessionStart || now >= SessionEnd))
return;
var close = candle.ClosePrice;
if (close > monthly && close > weekly && close > daily && close > hourly && Position <= 0m)
Submit(Sides.Buy, Volume + Math.Abs(Position), "Power Hour consensus entry");
else if (close < monthly && close < weekly && close < daily && close < hourly && Position >= 0m)
Submit(Sides.Sell, Volume + Math.Abs(Position), "Power Hour consensus entry");
}
}
import clr
clr.AddReference("Ecng.Common")
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.BusinessEntities")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Strategies")
from System import DateOnly, DateTime, DateTimeKind, DayOfWeek, Decimal, TimeSpan, TimeZoneInfo, ArgumentException
from Ecng.Common import TimeHelper
from StockSharp.Messages import DataType, CandleStates, OrderStates, OrderTypes, Sides, Level1Fields, ITickTradeMessage
from StockSharp.BusinessEntities import Order, Subscription
from StockSharp.Algo.Strategies import Strategy
class power_hour_money_strategy(Strategy):
def __init__(self):
super(power_hour_money_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Finished candles supplying causal calendar opens and closes", "General")
self._session_start = self.Param("SessionStart", TimeSpan.FromMinutes(570)) \
.SetRange(TimeSpan.Zero, TimeSpan.FromDays(1) - TimeSpan.FromTicks(1)) \
.SetDisplay("Session Start", "Inclusive New York wall-clock time", "Sessions")
self._session_end = self.Param("SessionEnd", TimeSpan.FromMinutes(690)) \
.SetRange(TimeSpan.FromTicks(1), TimeSpan.FromDays(1)) \
.SetDisplay("Session End", "Exclusive New York wall-clock time", "Sessions")
self._is_all_sessions = self.Param("IsAllSessions", False) \
.SetDisplay("All Sessions", "Allow entries throughout the day", "Sessions")
self._is_end_of_day_close_enabled = self.Param("IsEndOfDayCloseEnabled", False) \
.SetDisplay("End of Day Close", "Close at the cutoff and block further entries until New York midnight", "Protection")
self._end_of_day_time = self.Param("EndOfDayTime", TimeSpan.FromMinutes(1005)) \
.SetRange(TimeSpan.Zero, TimeSpan.FromDays(1) - TimeSpan.FromTicks(1)) \
.SetDisplay("End of Day Time", "New York wall-clock cutoff", "Protection")
self._is_trailing_enabled = self.Param("IsTrailingEnabled", False) \
.SetDisplay("Trailing Enabled", "Protect both sides on real executable Level1 quotes", "Protection")
self._long_trailing_percent = self.Param("LongTrailingPercent", Decimal.Parse("2")).SetGreaterThanZero() \
.SetDisplay("Long Trailing Percent", "Percent below the highest observed bid since entry", "Protection")
self._short_trailing_percent = self.Param("ShortTrailingPercent", Decimal.Parse("2")).SetGreaterThanZero() \
.SetDisplay("Short Trailing Percent", "Percent above the lowest observed ask since entry", "Protection")
self._reset_state()
self.OrderRegistering += self._observe_order
self.OwnTradeReceived += self._observe_fill
@property
def candle_type(self):
return self._candle_type.Value
@candle_type.setter
def candle_type(self, value):
self._candle_type.Value = value
def _reset_state(self):
self._month = self._week = self._day = self._hour = None
self._month_open = self._week_open = self._day_open = self._hour_open = None
self._pending_order = None
self._held_side = None
self._extreme = Decimal.Zero
def _observe_order(self, order):
self._pending_order = order
def OnReseted(self):
super(power_hour_money_strategy, self).OnReseted()
self._reset_state()
def OnStarted2(self, time):
super(power_hour_money_strategy, self).OnStarted2(time)
if self._session_start.Value < TimeSpan.Zero or self._session_start.Value >= self._session_end.Value \
or self._session_end.Value > TimeSpan.FromDays(1) or self._end_of_day_time.Value < TimeSpan.Zero \
or self._end_of_day_time.Value >= TimeSpan.FromDays(1) \
or self._long_trailing_percent.Value <= 0 or self._long_trailing_percent.Value >= 100 \
or self._short_trailing_percent.Value <= 0 or self._short_trailing_percent.Value >= 100:
raise ArgumentException("Sessions must be ordered within a day and trailing percentages within (0, 100).")
self._reset_state()
for field in (Level1Fields.BestBidPrice, Level1Fields.BestAskPrice):
quotes = Subscription(DataType.Level1, self.Security)
quotes.MarketData.BuildField = field
self.SubscribeLevel1(quotes).Bind(self._observe_quote).Start()
if self._is_end_of_day_close_enabled.Value:
self.StartTimer(TimeSpan.FromMinutes(1), self._try_end_of_day)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self.OnProcess).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def _is_pending(self):
order = self._pending_order
return order is not None and order.State != OrderStates.Done and order.State != OrderStates.Failed
def _submit(self, side, volume, comment):
order = Order()
order.Security = self.Security
order.Portfolio = self.Portfolio
order.Type = OrderTypes.Market
order.Side = side
order.Volume = volume
order.Comment = comment
self.RegisterOrder(order)
def _try_end_of_day(self):
if not self._is_end_of_day_close_enabled.Value \
or TimeZoneInfo.ConvertTimeFromUtc(self.CurrentTime, TimeHelper.Est).TimeOfDay < self._end_of_day_time.Value:
return False
if self.Position != 0 and not self._is_pending() and self.IsFormedAndOnlineAndAllowTrading():
self._submit(Sides.Sell if self.Position > 0 else Sides.Buy, abs(self.Position), "Power Hour end-of-day close")
return True
def _observe_fill(self, subscription, trade):
# Reflection reads the explicit CLR interface member for every tick-message implementation.
price = clr.GetClrType(ITickTradeMessage).GetProperty("Price").GetValue(trade.Trade)
side = Sides.Buy if self.Position > 0 else Sides.Sell if self.Position < 0 else None
if side != self._held_side:
self._extreme = Decimal.Zero if side is None else price
elif side is not None:
self._extreme = max(self._extreme, price) if side == Sides.Buy else min(self._extreme, price)
self._held_side = side
def _observe_quote(self, quote):
if self._try_end_of_day() or self._held_side is None or self.Position == 0:
return
side = self._held_side
field = Level1Fields.BestBidPrice if side == Sides.Buy else Level1Fields.BestAskPrice
if not quote.Changes.ContainsKey(field):
return
price = quote.Changes[field]
if price <= 0:
return
self._extreme = max(self._extreme, price) if side == Sides.Buy else min(self._extreme, price)
percent = self._long_trailing_percent.Value if side == Sides.Buy else self._short_trailing_percent.Value
stop = self._extreme * (Decimal(1) - percent / Decimal(100)) if side == Sides.Buy \
else self._extreme * (Decimal(1) + percent / Decimal(100))
triggered = price <= stop if side == Sides.Buy else price >= stop
if self._is_trailing_enabled.Value and not self._is_pending() and self.IsFormedAndOnlineAndAllowTrading() and triggered:
self._submit(Sides.Sell if side == Sides.Buy else Sides.Buy, abs(self.Position), "Power Hour trailing close")
def OnProcess(self, candle):
if candle.State != CandleStates.Finished:
return
local = TimeZoneInfo.ConvertTimeFromUtc(candle.OpenTime, TimeHelper.Est)
day = DateOnly.FromDateTime(local)
month = DateOnly(local.Year, local.Month, 1)
week = day.AddDays(-((int(local.DayOfWeek) + 6) % 7))
hour = DateTime(candle.OpenTime.Year, candle.OpenTime.Month, candle.OpenTime.Day, candle.OpenTime.Hour, 0, 0, DateTimeKind.Utc)
midnight = local.TimeOfDay == TimeSpan.Zero
if self._month != month:
self._month_open = candle.OpenPrice if self._month is not None or local.Day == 1 and midnight else None
self._month = month
if self._week != week:
self._week_open = candle.OpenPrice if self._week is not None or local.DayOfWeek == DayOfWeek.Monday and midnight else None
self._week = week
if self._day != day:
self._day_open = candle.OpenPrice if self._day is not None or midnight else None
self._day = day
if self._hour != hour:
self._hour_open = candle.OpenPrice if self._hour is not None or candle.OpenTime == hour else None
self._hour = hour
if self._try_end_of_day() or self._is_pending() or not self.IsFormedAndOnlineAndAllowTrading() \
or any(value is None for value in (self._month_open, self._week_open, self._day_open, self._hour_open)):
return
now = TimeZoneInfo.ConvertTimeFromUtc(self.CurrentTime, TimeHelper.Est).TimeOfDay
if not self._is_all_sessions.Value and (now < self._session_start.Value or now >= self._session_end.Value):
return
close = candle.ClosePrice
opens = (self._month_open, self._week_open, self._day_open, self._hour_open)
if all(close > value for value in opens) and self.Position <= 0:
self._submit(Sides.Buy, self.Volume + abs(self.Position), "Power Hour consensus entry")
elif all(close < value for value in opens) and self.Position >= 0:
self._submit(Sides.Sell, self.Volume + abs(self.Position), "Power Hour consensus entry")
def CreateClone(self):
return power_hour_money_strategy()