104 lines
3.6 KiB
C#
104 lines
3.6 KiB
C#
#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 RandomBot : Strategy
|
|
{
|
|
private Random RNG = new Random();
|
|
|
|
private ATR atr;
|
|
|
|
protected override void OnStateChange()
|
|
{
|
|
if (State == State.SetDefaults)
|
|
{
|
|
Description = @"";
|
|
Name = "Random Bot";
|
|
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 = 5;
|
|
AtrPeriod = 14;
|
|
AtrMultiplier = 2;
|
|
}
|
|
else if (State == State.DataLoaded)
|
|
{
|
|
RNG = new Random();
|
|
atr = ATR(AtrPeriod);
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
bool isLongTrade = (RNG.Next(2) == 0); // 50% chance to go long or short.
|
|
if (isLongTrade)
|
|
{
|
|
EnterLong();
|
|
SetStopLoss(CalculationMode.Ticks, stopLoss / TickSize);
|
|
SetProfitTarget(CalculationMode.Ticks, profitTarget / TickSize);
|
|
}
|
|
else
|
|
{
|
|
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", Order = 1)]
|
|
public int PercentageChanceToEnterTrade { get; set; }
|
|
|
|
[Range(1, int.MaxValue), NinjaScriptProperty]
|
|
[Display(Name = "Profit / Stop ATR Period", GroupName = "Random Bot", Order = 2)]
|
|
public int AtrPeriod { get; set; }
|
|
|
|
[Range(0.1, double.MaxValue), NinjaScriptProperty]
|
|
[Display(Name = "Profit / Stop ATR Multiplier", GroupName = "Random Bot", Order = 3)]
|
|
public double AtrMultiplier { get; set; }
|
|
}
|
|
}
|