-
Notifications
You must be signed in to change notification settings - Fork 0
/
StopLossCandleStrategy.cs
59 lines (50 loc) · 1.71 KB
/
StopLossCandleStrategy.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using StockSharp.Algo;
using StockSharp.Algo.Candles;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Indicators.Trend;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Logging;
namespace SampleSMA
{
public class StopLossCandleStrategy : Strategy
{
public CandleSeries CandleSeries { get; private set; }
public MyTrade Trade { get; set; }
public Unit StopLossUnit { get; set; }
public StopLossCandleStrategy(CandleSeries series, MyTrade trade, Unit stopLossUnit)
{
this.CandleSeries = series;
this.Trade = trade;
this.StopLossUnit = stopLossUnit;
}
protected override void OnStarted()
{
this.AddInfoLog("StopLossCandleStrategy strategy {0} has been started.", this.Name);
this
.CandleSeries
.WhenCandlesFinished()
.Do(ProcessCandle)
.Apply(this);
base.OnStarted();
}
protected override void OnStopped()
{
this.AddInfoLog("StopLossCandleStrategy strategy {0} has been stopped.", this.Name);
base.OnStopped();
}
protected void ProcessCandle(Candle candle)
{
if (Trade.GetPnL() <= -StopLossUnit)
{
OrderDirections direction = this.Trade.Order.Direction.Invert();
decimal price = Security.LastTrade.Price;
Order order = this.CreateOrder(direction, price, this.Trade.Order.Volume);
RegisterOrder(order);
}
}
}
}