When I run the below code in Strategy Analyzer (as a strategy) the breakeven doesn't appear to work correctly. (full code file attached for reference)
I have downloaded and looked at other breakeven code examples and they do not appear to work correctly either. I have played with the run settings (order fill resolution etc.), perhaps I am missing something here?
The issue is that the breakeven occurs too early. Refer attached image - the first green trade moves to breakeven when its only 2.6 pips in positive direction (it should have been about 6 pips). I have tried playing wiht the *TickSize*10 code.
If this code has TickSize*10 it seems to be too large.... is there a scaling issue here? <BreakevenPrice = (Position.AveragePrice - (BreakevenPIPS * TickSize * 1));>
Also - can anyone help me print bar timestamps for output and debugging purposes?!?!
I am using NT8 (8.0.28.0 64-bit) multi-broker. The instrument being used is forex pair.
[HASHTAG="t3322"]region[/HASHTAG] 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 RSIcrossTESTshortsX2 : Strategy
{
private RSI RSI1;
private ParabolicSAR ParabolicSAR1;
private int TriggerSTATE;
private double CurrentAskPrice;
private double BreakevenPrice;
private double EntryPrice;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "RSIcrossTESTshortsX2";
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 = 0;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
RSIperiod = 13;
RSIsmooth = 4;
RSIextremeHIGH = 70;
RSIextremeLOW = 64;
StopPIPS = 4;
BreakevenPIPS = 6;
ProfitPIPS = 6;
SUPERTRENDperiod = 14;
SUPERTRENDsmooth = 14;
TradeSize = 2000;
SMAperiod = 14;
BreakEvenBool = false;
TriggerSTATE = 0;
TrailingStopValue = 5;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
RSI1 = RSI(Close, Convert.ToInt32(RSIperiod), Convert.ToInt32(RSIsmooth));
RSI1.Plots[0].Brush = Brushes.DodgerBlue;
RSI1.Plots[1].Brush = Brushes.Goldenrod;
AddChartIndicator(RSI1);
ParabolicSAR1 = ParabolicSAR(Close, 0.02, 0.2, 0.02);
ParabolicSAR1.Plots[0].Brush = Brushes.Goldenrod;
//AddChartIndicator(ParabolicSAR1);
}
}
protected override void OnBarUpdate()
{
CurrentAskPrice = GetCurrentBid(0);
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 2)
return;
// Set 1 - ENTRY
if ((Position.MarketPosition == MarketPosition.Flat)
&& ())//ENTRY CRITERIA HERE );
{
EnterShort(Convert.ToInt32(TradeSize), @"EntryShort");
//SetStopLoss(CalculationMode.Price, Position.AveragePrice+(StopPIPS*TickSize*10));
ExitShortStopMarket(Convert.ToInt32(Position.Quant ity), (Position.AveragePrice + (StopPIPS * TickSize * 10)) , @"EntryShortStop", @"EntryShort");
TriggerSTATE = 1;
}
// Set 2 - DEFINE VARIABLES
if ((TriggerSTATE == 1)
&& (Position.MarketPosition == MarketPosition.Short))
{
BreakevenPrice = (Position.AveragePrice - (BreakevenPIPS * TickSize * 1));
EntryPrice = Position.AveragePrice;
}
// Set 3 - TRAILING STOP
if ((TriggerSTATE == 1)
&& (Position.MarketPosition == MarketPosition.Short)
&& (CurrentAskPrice < BreakevenPrice))
{
//SetStopLoss(CalculationMode.Price, EntryPrice);
ExitShortStopMarket(Convert.ToInt32(Position.Quant ity), EntryPrice, @"EntryShortStop", @"EntryShort");
//Print(Cbi.Connection.PlaybackConnection.Now.ToStri ng("hh:mm:ss"));
Print(EntryPrice + " Entry Price");
Print(BreakevenPrice + " BreakevenPrice");
Print(Position.AveragePrice + " Position.AveragePrice");
Print(string.Format("High: {0} | Low: {1}", High[0], Low[0]));
//Print(Low[0], " Low");
//Print(string.Format("{0}"), EntryShortStop);
}

Comment