the 1st goal is this:
When the EMA(12) Crosses above the SMA(27) : if the close of the bar during the crossing of MA's is above the SMA(27) EnterLong.
When the EMA(12) Crosses below the SMA(27) : if the close of the bar during the crossing of MA's is below the SMA(27) EnterShort.
the 2nd goal is this:
If I am long a position and the SMA(27) is closer in ticks to my buy price than my 48 tick stop loss, I want my stop to be either
A) if price hits the current SMA(27) price it will stop me out for less of a loss than my 48 tick stop loss.
or
B) if price hits the SMA(27) price at the time of purchase it will stop me out again for less of a loss than the 48 tick stop loss
Shorting will be the same just reversed
Edit 2************************************************* ********** with newly edited code
I have edited the code and worked out a lot of the problems. my only problems now are that my long trades aren't working. they buy and then sell flat on the same candle.
im also looking for a way to change the colors of the text when it plots the text for each order execution on the chart. i have the draw.line working for entries but i dont know how to get them for exits. these would also be unnecessary if i could change the default plot text for each order fill and add a "+" or a "-" but theyre not changeable in the data Series.
EDITED CODE********************************************** ************************************************
region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
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
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class EMACodeTester : Strategy
{
private EMA EMA1;
private SMA SMA1;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "EMACodeTester";
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;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
FastEMA = 12;
SlowSMA = 27;
ProfitTrigger = 48;
StopLoss = 24;
MarketStart = 063000;
MarketEnd = 130000;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
EMA1 = EMA(FastEMA);
SMA1 = SMA(SlowSMA);
SetProfitTarget(@"EMACodeTester", CalculationMode.Ticks, ProfitTrigger);
SetStopLoss(@"EMACodeTester", CalculationMode.Ticks, StopLoss, true);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 5)
return;
double SMAPrice = SMA1[0];
double EntryPrice = Position.AveragePrice;
double SMALongDistance = (EntryPrice - SMAPrice) / 4;
double SMAShortDistance = (SMAPrice - EntryPrice) / 4;
double PositionSize = Position.Quantity;
bool mar****pen = ToTime(Time[0]) >= MarketStart && ToTime(Time[0]) <= MarketEnd;
bool ActiveLong = Position.MarketPosition == MarketPosition.Long;
bool ActiveShort = Position.MarketPosition == MarketPosition.Short;
bool PriceAboveSMA = Close[0] >= SMAPrice;
bool PriceBelowSMA = Close[0] <= SMAPrice;
if(mar****pen)
{
// Long********************************************** ***********************************************
if (CrossAbove(EMA1, SMA1, 1) && !ActiveLong && !ActiveShort && PriceAboveSMA)
{
EnterLong(Convert.ToInt32(DefaultQuantity), @"EMACodeTester");
}
if (ActiveLong)
{
if(BarsSinceEntryExecution() == 0)
{
Draw.Text(this, "EntryPrice" + CurrentBar, "+" + PositionSize + "@" + EntryPrice , 0, EntryPrice + 33, Brushes.Cyan);
}
if (SMALongDistance < StopLoss)
{
SetStopLoss(@"EMACodeTester", CalculationMode.Price, SMAPrice, true);
}
}
// Short********************************************* ************************************************
if (CrossBelow(EMA1, SMA1, 1) && !ActiveLong && !ActiveShort && PriceBelowSMA)
{
EnterShort(Convert.ToInt32(DefaultQuantity), @"EMACodeTester");
}
if (ActiveShort)
{
if(BarsSinceEntryExecution() == 1)
{
Draw.Text(this, "EntryPrice" + CurrentBar, "-" + PositionSize + "@" + EntryPrice , 0, EntryPrice + 33, Brushes.Magenta);
}
if (SMAShortDistance < StopLoss)
{
SetStopLoss(@"EMACodeTester", CalculationMode.Price, SMAPrice, true);
}
}
}
if(!mar****pen)
{
ExitLong();
ExitShort();
}
}
region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="FastEMA", Description="EMA", Order=1, GroupName="Parameters")]
public int FastEMA
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="SlowSMA", Description="SMA", Order=2, GroupName="Parameters")]
public int SlowSMA
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="ProfitTrigger", Description="Profit", Order=1, GroupName="Parameters")]
public int ProfitTrigger
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="StopLoss", Description="Stop Loss", Order=1, GroupName="Parameters")]
public int StopLoss
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="MarketStart", Description="Trading Hours Start", Order=1, GroupName="Parameters")]
public int MarketStart
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="MarketEnd", Description="Trading Hours End", Order=1, GroupName="Parameters")]
public int MarketEnd
{ get; set; }
#endregion
}
}

Comment