-
int OnInit() { ⋮ CheckOpenPosition(…
Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
Once you set those variables to true, they will forever be true. if(sar(shift)<Bid) BUYCondition = true; if(sar(shift)>Bid) SELLCondition = true;
Fix and simplify BUYCondition = sar(shift)<Bid; SELLCondition = !BUYCondition;
Hi, I already applying what you suggest. I change the code from OnInit to OnTick and adding newBar func to easily debugging,
void OnTick() { bool BUYCondition = sar()<Bid; bool SELLCondition = !BUYCondition; static datetime preTime=0,curTime; bool newBar=false; curTime=Time[0]; if(preTime!=curTime) { preTime=curTime; newBar=true; } if(newBar && BUYCondition) Alert("BUY true"); if(newBar && SELLCondition) Alert("SELL true"); }
the problem when only return SELL is true is no more but according to the Alert from strategy tester the result is somewhat lagging by a bar. When the chart on 2020.01.03 12:30 should be BUY the Alert still recognize it with SELL (lagging one bar, image attach). Do you know why this is happened?
Hi everyone, I had a test with SAR indicator. Currently I want to test about SAR position on the chart. This is my code:
The problem is every position of SAR indicator it always return SELL.
First I thought that because of CheckOpenPosition function that I wrote but when I'm not using that function the problem still occur.
Second I thought about the tick data that I used so instead using data from the tick data that I download so I tried to use data outside of that downloaded data and still the result is the same.
if anyone knows why this problem occur please kindly help me.
double InpSARStep=0.0002; // Step double InpSARMaximum=0.2; // Maximum double SarsDeger1=iSAR(NULL,0,InpSARStep,InpSARMaximum,1); double SarsDeger2=iSAR(NULL,0,InpSARStep,InpSARMaximum,2); double SarsFark=((SarsDeger1-SarsDeger2))/Point; if(SarsDeger1>SarsDeger2 && SarsFark> 80) Alert("BUY"); if(SarsDeger1<SarsDeger2 && SarsFark> 80) Alert("SELL");
if(SarsDeger1>SarsDeger2
Print out your variables and you will find out why.
double InpSARStep=0.0002; // Step double InpSARMaximum=0.2; // Maximum double SarsDeger1=iSAR(NULL,0,InpSARStep,InpSARMaximum,1); double SarsDeger2=iSAR(NULL,0,InpSARStep,InpSARMaximum,2); double SarsFark=((SarsDeger1-SarsDeger2))/Point; if(SarsDeger1>SarsDeger2 && SarsFark> 80) Alert("BUY"); if(SarsDeger1<SarsDeger2 && SarsFark> 80) Alert("SELL");
Sorry I don't really understand what you want to tell me about this code. My problem lies in the alert result of the SAR indi position. The problem of the SAR indi position is already resolved but still there's a problem where the result is lagging with 1 bar.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi everyone, I had a test with SAR indicator. Currently I want to test about SAR position on the chart. This is my code:
The problem is every position of SAR indicator it always return SELL.
First I thought that because of CheckOpenPosition function that I wrote but when I'm not using that function the problem still occur.
Second I thought about the tick data that I used so instead using data from the tick data that I download so I tried to use data outside of that downloaded data and still the result is the same.
if anyone knows why this problem occur please kindly help me.