カラー戦略
設定された色の知覚輝度に基づいて取引する戦略。 色が明るい(輝度 > 0.5)場合は買い、それ以外は売りを行います。
詳細
- エントリー条件:
- ロング:
Color luminance > 0.5 - ショート:
Color luminance <= 0.5
- ロング:
- ロング/ショート: 両方
- エグジット条件: 逆シグナル
- ストップ: いいえ
- デフォルト値:
ColorHex= "#f23645"CandleType= TimeSpan.FromMinutes(1).TimeFrame()
- フィルター:
- カテゴリ: その他
- 方向: 両方
- インジケーター: なし
- ストップ: いいえ
- 複雑さ: 基本
- 時間軸: 任意
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 低
using System;
using System.Collections.Generic;
using System.Globalization;
using Ecng.Common;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Color strategy.
/// The perceived luminance of ColorHex decides the side on every finished candle: a light color (luminance above 0.5) holds a long
/// position and a dark one holds a short position, reversing when the color changes.
/// </summary>
public class ColorStrategy : Strategy
{
private readonly StrategyParam<string> _colorHex;
private readonly StrategyParam<DataType> _candleType;
/// <summary>
/// Color in #RRGGBB form.
/// </summary>
public string ColorHex
{
get => _colorHex.Value;
set => _colorHex.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public ColorStrategy()
{
_colorHex = Param(nameof(ColorHex), "#f23645")
.SetDisplay("Color", "Color in #RRGGBB form", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (GetLuminance(ColorHex) is not decimal luminance)
return;
if (luminance > 0.5m)
{
if (Position <= 0)
BuyMarket(Volume + Math.Abs(Position));
}
else if (Position >= 0)
{
SellMarket(Volume + Math.Abs(Position));
}
}
private static decimal? GetLuminance(string hex)
{
var text = (hex ?? string.Empty).Trim().TrimStart('#');
if (text.Length != 6 || !int.TryParse(text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var rgb))
return null;
var r = (rgb >> 16) & 0xFF;
var g = (rgb >> 8) & 0xFF;
var b = rgb & 0xFF;
// Perceived brightness weights of the RGB channels.
return (0.299m * r + 0.587m * g + 0.114m * b) / 255m;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Strategies import Strategy
class color_strategy(Strategy):
"""
Color strategy.
The perceived luminance of ColorHex decides the side on every finished candle: a light color (luminance above 0.5) holds a long
position and a dark one holds a short position, reversing when the color changes.
"""
def __init__(self):
super(color_strategy, self).__init__()
self._color_hex = self.Param("ColorHex", "#f23645").SetDisplay("Color", "Color in #RRGGBB form", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(1))).SetDisplay("Candle Type", "Type of candles to use", "General")
@property
def candle_type(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(color_strategy, self).OnStarted2(time)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def _process_candle(self, candle):
if candle.State != CandleStates.Finished:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
luminance = self._get_luminance(self._color_hex.Value)
if luminance is None:
return
if luminance > 0.5:
if self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
elif self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
@staticmethod
def _get_luminance(hex_value):
text = (hex_value or "").strip().lstrip("#")
if len(text) != 6:
return None
try:
rgb = int(text, 16)
except ValueError:
return None
r = (rgb >> 16) & 0xFF
g = (rgb >> 8) & 0xFF
b = rgb & 0xFF
# Perceived brightness weights of the RGB channels.
return (0.299 * r + 0.587 * g + 0.114 * b) / 255.0
def CreateClone(self):
return color_strategy()