I thought this would be simple but the "next bar at market" order means that LastEntryPrice has to be trapped outside of the begin/end loop where the entry order statement is. I've tried 4-5 different approaches but keep running into a wall. I've resorted to capturing the close price when the 1st or 2nd loop is executed, but the actual entryprice occurs on the next line in the data stream: actual 1st entryprice = 1579.75 vs. 1580.00 (see Output). Close but not 100% accurate.
There must be an easy way to do this buy I'm not seeing it. Any thoughts would be greatly appreciated.
Here is the code stripped down to the basics:
Code: Select all
{******************************************************************************
Name : $Intraday_Entry_Test_v2.0
Description : Testing intrabar orders on a daily chart
Last Modified Date : 07/23/2013
Application : MultiCharts execution strategy
******************************************************************************}
[IntrabarOrderGeneration = true];
[LegacyColorValue = true];
//*****************************************************************************
// inputs and variable initialization section
//*****************************************************************************
Input:
Long_Entry_1_Contracts (1),
Long_Entry_2_Contracts (2),
Long_Entry_3_Contracts (3),
Long_Entry_1_Level (10),
Long_Entry_2_Level (4),
Long_Entry_3_Level (2),
Long_Exit_Level (90);
Variables:
myIndicator (0),
Intrabarpersist LastEntryPrice (0),
Intrabarpersist myMP (0);
//*****************************************************************************
// calculations section
//*****************************************************************************
myIndicator = SwingIndicator(close,14);
//*****************************************************************************
// entry execution section
//*****************************************************************************
// enter long
if marketposition <= 0 and myIndicator < Long_Entry_1_Level then begin
buy ("LE_1") Long_Entry_1_Contracts shares next bar at market;
myMP = 1;
LastEntryPrice = close; // approximation of the actual entry price
end;
if myMP = 1 and myIndicator < Long_Entry_2_Level then begin
buy ("LE_2") Long_Entry_2_Contracts shares next bar at market;
myMP = 2;
LastEntryPrice = close;
end;
if myMP = 2 and myIndicator < Long_Entry_3_Level then begin
buy ("LE_3") Long_Entry_3_Contracts shares next bar at market;
myMP = 3;
end;
//*****************************************************************************
// exit execution section
//*****************************************************************************
if myMP > 0 and myIndicator >= Long_Exit_Level then begin // exit long
myMP = 0;
sell ("LX") currentcontracts shares next bar at market;
end;