I'm using a 1minute bar chart and trying to accomplish the following:
A. Evaluate OPEN tick of 1minute bar for buy or sell condition
i. Look back at the previous tick (closing tick of previous 1min bar) and compare to a variable to determine if buy or sell condition is TRUE
ii. If buy or sell condition = TRUE, then set buy or sell price to the calculatedprice variable
iii. Set "trade" variable to trigger a buy or sell in the trade execution routine.
B. Evaluate value of "trade" variable to determine if a buy or sell should be executed
i.Execute a buy or sell limit at buyprice or sellprice
I'm using the (barstatus(1)=0) code is so that the buyprice or sellprice, and the determination of whether to buy or sell, is set/made only once within the 1min bar and the condition will hold until next 1min bar open tick. This should prevent the constant submitting and cancelling of my limit order until executed. The problem is that even though the trade price is set and the trigger variable is set to buy or sell on the open tick of the 1min bar, the trade trigger variable still resets to zero on the next ticks outside of this "barstatus" routine. I'm not sure why this is happening since this part of the code should only be executed once in each 1min bar.
It seems like there is a simple explanation for this. Can anyone help with this? Example code follows:
Code: Select all
[IntrabarOrderGeneration = True];
Variable:
pricevar (0),
calculatedprice (0),
buyprice (0),
sellprice (0),
trade (0);
if (barstatus(1) = 0) then begin // evaluate opening tick of 1min bar
If close[1] > pricevar[1] then begin //compare closing tick of previous bar to variable
buyprice=calculateprice; //set buyprice variable to calculated price variable
print("O bar # is", maxbarsback+currentbar," ",close[1]," ",buyprice); //for debugging
trade = 1; //set a trade trigger variable for a buy
end else
If close[1] < pricevar[1] then begin //compare closing tick of previous bar to variable
sellprice=calculatedprice; //set sellprice variable to calculated price variable
print("O bar # is", maxbarsback+currentbar," ",close[1]," ",sellprice); //for debugging
trade = -1; //set a trade trigger variable for a sell
end else
If close[1] = calculatedprice[1] then begin //set a trade trigger variable to neutral - no trade
trade=0;
end;
end;
//Following code is to execute a buy,sell, or no trade based on value of "trade" variable.
//The price for the limit order has been calculated in the previous code and the trade trigger
//variable has been set in previous code. My assumption here is that neither variable should
//change until the opening tick of next 1min bar, however when I look at the output window
//the "trade" variable is 1 or -1 on the first tick after 1min bar opening tick, and for all other ticks
//it goes to zero/0. See following code.
if trade=1 then begin //buy next tick if trade variable=1
buy ("B1")next bar at buyprice limit;
print(time,"trade=",trade," ",buyprice); //for debugging
end else
if trade = -1 then begin //sell next tick if trade variable=-1
sellshort ("S1") next bar at sellprice limit;
print(time,"trade=",trade," ",sellprice); //for debugging
end;