So, in my strategy I have long and short entries, I tried to implement solution which was provided above, but no luck.
1. I attached screenshot of the trade in Trades section for backtesting
2. I attached screenshot of Print function in Output
3. Here is my code for tackling this task
What I don't understand:
1. Why in trades close time for the trade is 20.01.2022 0:56:20 but in Print output I see 2022-01-19 19:45:01
2. Output stops at 19.01.2022 20:10:00, just again why?
Thank you in advance.
// If not flat and our unrealized PnL has gone above $100, set pnlAbove to true
if (Position.MarketPosition != MarketPosition.Flat && Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]) < 0)
{
// double PnL = Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]);
// Print("Time PnL: " + Time[0].ToString () + " " + PnL);
pnlDown = true;
}
// now check if long, if pnlAbove is true, and if the unrealized PnL is less than $100. if so, close the position
if (Position.MarketPosition == MarketPosition.Long && pnlDown == true && Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]) < 0)
{
// put your exit long method here, such as ExitLong()
double PnL = Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]);
ExitLong();
Print("The trade profit is " + PnL);
Print ("Closing Long " + Time[0].ToString ());
pnlDown = false; // setting pnlAbove back to false so it is ready to be checked for the next position
}
// else if the position is short, pnlAbove is true, and unrealized PnL is less than $100, close the position
else if (Position.MarketPosition == MarketPosition.Short && pnlDown == true && Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]) < 0)
{
// put your exit short method here, such as ExitShort()
double PnL = Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]);
ExitShort();
Print("The trade profit is " + PnL);
Print ("Closing Short " + Time[0].ToString ());
pnlDown = false; // setting pnlAbove back to false so it is ready to be checked for the next position
}
Entering position:
orderId='NT-00003-11294' account='Backtest' name='RegShort' orderState=Submitted instrument='ES 12-22' orderAction=SellShort orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-01-19 19:45:01' gtd='2099-12-01' statementDate='2022-12-30'
orderId='NT-00003-11294' account='Backtest' name='RegShort' orderState=Accepted instrument='ES 12-22' orderAction=SellShort orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-01-19 19:45:01' gtd='2099-12-01' statementDate='2022-12-30'
orderId='NT-00003-11294' account='Backtest' name='RegShort' orderState=Working instrument='ES 12-22' orderAction=SellShort orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-01-19 19:45:01' gtd='2099-12-01' statementDate='2022-12-30'
orderId='NT-00003-11294' account='Backtest' name='RegShort' orderState=Filled instrument='ES 12-22' orderAction=SellShort orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=1 averageFillPrice=4578.125 onBehalfOf='' id=-1 time='2022-01-19 19:45:01' gtd='2099-12-01' statementDate='2022-12-30'
orderId='NT-00004-11294' account='Backtest' name='RegShortStop' orderState=Submitted instrument='ES 12-22' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4614.75 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-01-19 19:45:01' gtd='2099-12-01' statementDate='2022-12-30'
orderId='NT-00004-11294' account='Backtest' name='RegShortStop' orderState=Accepted instrument='ES 12-22' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4614.75 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-01-19 19:45:01' gtd='2099-12-01' statementDate='2022-12-30'
orderId='NT-00004-11294' account='Backtest' name='RegShortStop' orderState=Working instrument='ES 12-22' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=4614.75 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-01-19 19:45:01' gtd='2099-12-01' statementDate='2022-12-30'
orderId='NT-00005-11294' account='Backtest' name='RegShortTP' orderState=Submitted instrument='ES 12-22' orderAction=BuyToCover orderType='Limit' limitPrice=4538.5 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-01-19 19:45:01' gtd='2099-12-01' statementDate='2022-12-30'
orderId='NT-00005-11294' account='Backtest' name='RegShortTP' orderState=Accepted instrument='ES 12-22' orderAction=BuyToCover orderType='Limit' limitPrice=4538.5 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-01-19 19:45:01' gtd='2099-12-01' statementDate='2022-12-30'
orderId='NT-00005-11294' account='Backtest' name='RegShortTP' orderState=Working instrument='ES 12-22' orderAction=BuyToCover orderType='Limit' limitPrice=4538.5 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-01-19 19:45:01' gtd='2099-12-01' statementDate='2022-12-30'

Comment