Add strategy that randomly places trades based on the current RSI value
This commit is contained in:
parent
e07bc36071
commit
8e923b7a41
116
strategies/RandomBotRSI.cs
Normal file
116
strategies/RandomBotRSI.cs
Normal file
@ -0,0 +1,116 @@
|
||||
#region Using declarations
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using NinjaTrader.Cbi;
|
||||
using NinjaTrader.NinjaScript.Indicators;
|
||||
#endregion
|
||||
|
||||
//This namespace holds Strategies in this folder and is required. Do not change it.
|
||||
namespace NinjaTrader.NinjaScript.Strategies
|
||||
{
|
||||
public class RandomBotRSI : Strategy
|
||||
{
|
||||
private Random RNG = new Random();
|
||||
|
||||
private ATR atr;
|
||||
private RSI rsi;
|
||||
|
||||
protected override void OnStateChange()
|
||||
{
|
||||
if (State == State.SetDefaults)
|
||||
{
|
||||
Description = @"Randomly places trades based on the current RSI value";
|
||||
Name = "Random Bot (RSI)";
|
||||
Calculate = Calculate.OnBarClose;
|
||||
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;
|
||||
IsInstantiatedOnEachOptimizationIteration = true;
|
||||
|
||||
PercentageChanceToEnterTrade = 25;
|
||||
RSIPeriod = 14;
|
||||
RSISmoothing = 3;
|
||||
ATRPeriod = 14;
|
||||
ATRMultiplier = 2;
|
||||
}
|
||||
else if (State == State.DataLoaded)
|
||||
{
|
||||
RNG = new Random();
|
||||
|
||||
atr = ATR(ATRPeriod);
|
||||
rsi = RSI(RSIPeriod, RSISmoothing);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnBarUpdate()
|
||||
{
|
||||
if (CurrentBar < BarsRequiredToTrade)
|
||||
return;
|
||||
|
||||
if (Position.MarketPosition != MarketPosition.Flat)
|
||||
return;
|
||||
|
||||
if (ShouldEnterTrade())
|
||||
{
|
||||
double atrValue = atr[0];
|
||||
double stopLoss = ATRMultiplier * atrValue;
|
||||
double profitTarget = ATRMultiplier * atrValue;
|
||||
|
||||
if (rsi[0] > 80.0)
|
||||
{
|
||||
EnterLong();
|
||||
SetStopLoss(CalculationMode.Ticks, stopLoss / TickSize);
|
||||
SetProfitTarget(CalculationMode.Ticks, profitTarget / TickSize);
|
||||
}
|
||||
else if (rsi[0] < 20.0)
|
||||
{
|
||||
EnterShort();
|
||||
SetStopLoss(CalculationMode.Ticks, stopLoss / TickSize);
|
||||
SetProfitTarget(CalculationMode.Ticks, profitTarget / TickSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool ShouldEnterTrade()
|
||||
{
|
||||
return RNG.Next(100) < PercentageChanceToEnterTrade;
|
||||
}
|
||||
|
||||
public override string DisplayName
|
||||
{
|
||||
get { return Name; }
|
||||
}
|
||||
|
||||
[Range(1, 100), NinjaScriptProperty]
|
||||
[Display(Name = "Percentage Chance to Enter Trade", GroupName = "Random Bot (RSI)", Order = 1)]
|
||||
public int PercentageChanceToEnterTrade { get; set; }
|
||||
|
||||
[Range(1, int.MaxValue), NinjaScriptProperty]
|
||||
[Display(Name = "RSI Period", GroupName = "Random Bot (RSI)", Order = 2)]
|
||||
public int RSIPeriod { get; set; }
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Range(1, int.MaxValue)]
|
||||
[Display(Name = "RSI Smoothing", GroupName = "Random Bot (RSI)", Order = 3)]
|
||||
public int RSISmoothing { get; set; }
|
||||
|
||||
[Range(1, int.MaxValue), NinjaScriptProperty]
|
||||
[Display(Name = "ATR Period", GroupName = "Random Bot (RSI)", Order = 4)]
|
||||
public int ATRPeriod { get; set; }
|
||||
|
||||
[Range(0.1, double.MaxValue), NinjaScriptProperty]
|
||||
[Display(Name = "ATR Multiplier", GroupName = "Random Bot (RSI)", Order = 5)]
|
||||
public double ATRMultiplier { get; set; }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user