-
Notifications
You must be signed in to change notification settings - Fork 0
/
mftm.cs
174 lines (159 loc) · 5.95 KB
/
mftm.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#region Using declarations
using System;
using System.ComponentModel.DataAnnotations;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class MFTM : Strategy
{
private RSI rsi;
private ChaikinMoneyFlow cmf;
private TrendMagic tm;
private int lastTrend;
private bool initialPositionEntered;
private bool positionClosed;
private int trendChangeBarCount;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"MFTM.";
Name = "MFTM";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
positionClosed = false;
TakeProfitTicks = 20;
StopLossTicks = 20;
UseMFIRSI = true;
}
else if (State == State.Configure)
{
rsi = RSI(14, 14);
cmf = ChaikinMoneyFlow(14);
tm = TrendMagic(14, 20, 1.0, false);
AddChartIndicator(tm);
}
else if (State == State.DataLoaded)
{
lastTrend = 0; // Initialize lastTrend
initialPositionEntered = false; // Initialize initialPositionEntered
trendChangeBarCount = 0;
}
}
protected override void OnMarketData(MarketDataEventArgs e)
{
// Check if it is the first tick of the strategy
if (BarsInProgress == 0 && !initialPositionEntered)
{
int firstTrend = (int) tm.TrendOutput[0];
if (firstTrend == 1) {
EnterLong();
} else if (firstTrend == -1) {
EnterShort();
}
initialPositionEntered = true;
}
}
protected override void OnBarUpdate()
{
if (CurrentBars[0] < 1) return;
int currentTrend = (int) tm.TrendOutput[0];
Print("CurrentTrend: " + currentTrend);
Print("LastTrend: " + lastTrend);
Print("PositionClosed: " + positionClosed);
Print("MarketPosition: " + Position.MarketPosition);
Print("InitialPositionEntered: " + initialPositionEntered);
// If the trend changes, reset the count and place the stop loss.
if (currentTrend != lastTrend)
{
trendChangeBarCount = 0;
if (initialPositionEntered) // If a position has been entered
{
SetStopLoss(CalculationMode.Ticks, StopLossTicks);
}
}
else // If the trend is the same, increment the count.
{
trendChangeBarCount++;
}
if (Position.MarketPosition == MarketPosition.Flat && positionClosed == false)
{
if (currentTrend == 1)
{
EnterLong();
// Set profit target
SetProfitTarget(CalculationMode.Ticks, TakeProfitTicks);
}
else if (currentTrend == -1)
{
EnterShort();
// Set profit target
SetProfitTarget(CalculationMode.Ticks, TakeProfitTicks);
}
}
else if (initialPositionEntered) // If a position has been entered
{
// If the trend has been the same for two bars and position is long, enter another long position.
if (trendChangeBarCount >= 2 && Position.MarketPosition == MarketPosition.Long && currentTrend == 1 && positionClosed == true)
{
EnterLong();
// Set profit target
SetProfitTarget(CalculationMode.Ticks, TakeProfitTicks);
positionClosed = false;
}
// If the trend has been the same for two bars and position is short, enter another short position.
else if (trendChangeBarCount >= 2 && Position.MarketPosition == MarketPosition.Short && currentTrend == -1 && positionClosed == true)
{
EnterShort();
// Set profit target
SetProfitTarget(CalculationMode.Ticks, TakeProfitTicks);
positionClosed = false;
}
}
// If the position is flat, set positionClosed to true.
if (Position.MarketPosition == MarketPosition.Flat)
{
positionClosed = true;
}
// Display the current trend on the chart
string trend = currentTrend == 1 ? "Bullish" : "Bearish";
Draw.TextFixed(this, "TrendDisplay", "Current trend: " + trend, TextPosition.TopRight);
// Store the current trend for the next bar.
lastTrend = currentTrend;
}
#region Properties
[NinjaScriptProperty]
[Display(Name="TakeProfitTicks", Order=1, GroupName="Parameters")]
public int TakeProfitTicks { get; set; }
[NinjaScriptProperty]
[Display(Name="StopLossTicks", Order=2, GroupName="Parameters")]
public int StopLossTicks { get; set; }
[NinjaScriptProperty]
[Display(Name="UseMFIRSI", Order=3, GroupName="Parameters")]
public bool UseMFIRSI { get; set; }
#endregion
}
}