This is the code I am having difficulty with:
// Calculates new position size based on the account balance
// ContractsPerPosition = Convert.ToInt32(Math.Floor(Account.Get(AccountItem .CashValue, Currency.UsDollar) / MinBalancePerContract));
// ContractsPerPosition = (int) (Math.Floor(Account.Get(AccountItem.BuyingPower, Currency.UsDollar) / MinBalancePerContract));
ContractsPerPosition = Convert.ToInt32(Math.Floor(Account.Get(AccountItem .BuyingPower, Currency.UsDollar) / MinBalancePerContract));
As you can see I have already tried several approaches. Nothing appears to work.
Any help will be greatly appreciated.
Also, if you know of any book, Youtube video or tutorial for NinjaScript 8 beginners, I will appreciate that too. I am a seasoned MQL4 programmer but I know nothing about NinjaScript 8. I found a couple of books on NinjaScript 7 but I know how dramatic the changes from 7 to 8 are so I don't want to learn what is already obsolete.
This is the program I am trying to modify:
#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 BarColorTraderv02 : Strategy
{
private int ContractsPerPosition;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"On the open close we trade the bar color. This one has money management.";
Name = "BarColorTraderv02";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 600;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 2;
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;
Target = 12;
StopLoss = 15;
MinBalancePerContract = 2000;
TrailingSL = 10;
ContractsPerPosition = 1;
}
else if (State == State.Configure)
{
SetStopLoss("", CalculationMode.Ticks, StopLoss, false);
SetProfitTarget("", CalculationMode.Ticks, Target);
SetTrailStop(@"TrailingSL. " + Convert.ToString(CurrentBars[0]), CalculationMode.Ticks, TrailingSL, false);
}
}
protected override void OnBarUpdate()
{
// Calculates new position size based on account balance
// ContractsPerPosition = Convert.ToInt32(Math.Floor(Account.Get(AccountItem .CashValue, Currency.UsDollar) / MinBalancePerContract));
// ContractsPerPosition = (int) (Math.Floor(Account.Get(AccountItem.BuyingPower, Currency.UsDollar) / MinBalancePerContract));
ContractsPerPosition = Convert.ToInt32(Math.Floor(Account.Get(AccountItem .BuyingPower, Currency.UsDollar) / MinBalancePerContract));
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if ((Close[0] >= Open[0])
&& (Close[1] < Open[1]))
{
EnterLong(Convert.ToInt32(ContractsPerPosition), @"LongPos. " + Convert.ToString(CurrentBars[0]));
Draw.ArrowUp(this, @"ArrowUP. " + Convert.ToString(CurrentBars[0]), false, 0, (Low[0] + (-4 * TickSize)) , Brushes.Lime);
}
// Set 2
if ((Close[0] < Open[0])
&& (Close[1] >= Open[1]))
{
EnterShort(Convert.ToInt32(ContractsPerPosition), @"ShortPos. " + Convert.ToString(CurrentBars[0]));
Draw.ArrowDown(this, @"ArrowDown. " + Convert.ToString(CurrentBars[0]), false, 0, (High[0] + (4 * TickSize)) , Brushes.Red);
}
}
#region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Target", Order=1, GroupName="Parameters")]
public int Target
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="StopLoss", Order=2, GroupName="Parameters")]
public int StopLoss
{ get; set; }
[NinjaScriptProperty]
[Range(500, double.MaxValue)]
[Display(Name="MinBalancePerContract", Description="Minimum balance required to open 1 contract", Order=3, GroupName="Parameters")]
public double MinBalancePerContract
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="TrailingSL", Description="Distance from price to Trailing Stop Loss", Order=4, GroupName="Parameters")]
public int TrailingSL
{ get; set; }
#endregion
}
}
Comment