Cypress Texas/USA
Posts: 10 since Mar 2021
Thanks Given: 0
Thanks Received: 1
|
I want to write a simple entry strategy for Multicharts.Net. Just calculate the current bar's height and determine whether it is a bullish bar. If the bar is bullish and closed above 60% of the height, place a stop order one tick above the high of the current bar. The risk per futures contract will be the height of the current bar plus 2 ticks. The input value is the total risk tolerated per trade in dollar value and the risk tolerated per futures contract in dollar value. If the risk is larger than the risk tolerated per futures contact, there is no trade. If the risk calculated according to the bar height plus two ticks is less than the risk tolerance, the number of contracts for the order is the total risk tolerance divided by the risk per contract and rounded down.
Below is the code I wrote. Yet after I applied the code to ES futures symbol, the order was never triggered. I used Output.WriteLine to debug and I found something weird. I use CurrentPosition.Value to know the current open position and the order is submitted only if there is no open position. Yet I found CurrentPosition.Value is 4 most of the times even if there is nothing open. The CurrentPosition.Value is 0 only when the time is 8:33 pm US central standard time on Jan 27, 2021. The chart I use is a 2000 tick chart. If I use StrategyInfo.MarketPosition to detect the market open position, I get the value exactly the same as CurrentPosition.Value. Please advise what's gone wrong.
using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using ATCenterProxy.interop;
namespace PowerLanguage.Strategy
{
[IOGMode(IOGMode.Disabled)]
public class MackBuyStop_Open : SignalObject
{
// Create the inputs
[Input]
public double Max_Risk { get; set; }
[Input]
public double PerContract_Risk { get; set; }
public MackBuyStop_Open(object _ctx) : base(_ctx)
{
// Give the inputs a default value
Max_Risk = 950;
PerContract_Risk = 300;
}
private IOrderPriced enterLong;
private double TickValue, PointValue, BarRisk;
protected override void Create()
{
// Create the orders
enterLong = OrderCreator.Stop(new
SOrderParameters(Contracts.UserSpecified, EOrderAction.Buy));
}
protected override void StartCalc()
{
TickValue = Bars.Info.MinMove / Bars.Info.PriceScale ;
PointValue = Bars.Info.BigPointValue ;
Output.Clear();
}
protected override void CalcBar()
{
BarRisk = ( Bars.High[0] - Bars.Low[0] + 2 * TickValue ) * PointValue ;
if ((CurrentPosition.Value == 0) &&
( Bars.Close[0] - Bars.Open[0] > 0 ) &&
( (Bars.Close[0] - Bars.Low[0] ) / (Bars.High[0] - Bars.Low[0]) >= 0.6 ) &&
( BarRisk <= PerContract_Risk )) //((double)(Bars.High - Bars.Low) + 2 * TickValue)
{
Output.WriteLine(Bars.Time[0].ToString("d-M HH:mm:ss")+" Market Position = {0}\n", CurrentPosition.Value);
if (BarRisk <= PerContract_Risk)
{
enterLong.Send(Bars.High[0] + TickValue, (int)(Max_Risk/BarRisk));
}
}
}
}
}
|